blob: d6c915d82b4a599655b5d6c58e7b69c635d72997 [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) {
Chris Mason771ed682008-11-06 22:02:51 -0500487 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100488 * inline extent creation worked or returned error,
489 * we don't need to create any more async work items.
490 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500491 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400492 extent_clear_unlock_delalloc(inode, start, end, NULL,
493 EXTENT_DIRTY |
494 EXTENT_DELALLOC,
495 PAGE_UNLOCK |
496 PAGE_CLEAR_DIRTY |
497 PAGE_SET_WRITEBACK |
498 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000499 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400500 goto free_pages_out;
501 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000502 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400503 }
504
505 if (will_compress) {
506 /*
507 * we aren't doing an inline extent round the compressed size
508 * up to a block size boundary so the allocator does sane
509 * things
510 */
Qu Wenruofda28322013-02-26 08:10:22 +0000511 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400512
513 /*
514 * one last check to make sure the compression is really a
515 * win, compare the page count read with the blocks on disk
516 */
Qu Wenruofda28322013-02-26 08:10:22 +0000517 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400518 if (total_compressed >= total_in) {
519 will_compress = 0;
520 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400521 num_bytes = total_in;
522 }
523 }
524 if (!will_compress && pages) {
525 /*
526 * the compression code ran but failed to make things smaller,
527 * free any pages it allocated and our page pointer array
528 */
529 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400530 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400531 page_cache_release(pages[i]);
532 }
533 kfree(pages);
534 pages = NULL;
535 total_compressed = 0;
536 nr_pages_ret = 0;
537
538 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500539 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
540 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500541 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500542 }
Chris Masonc8b97812008-10-29 14:49:59 -0400543 }
Chris Mason771ed682008-11-06 22:02:51 -0500544 if (will_compress) {
545 *num_added += 1;
546
547 /* the async work queues will take care of doing actual
548 * allocation on disk for these compressed pages,
549 * and will submit them to the elevator.
550 */
551 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800552 total_compressed, pages, nr_pages_ret,
553 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500554
Yan, Zheng24ae6362010-12-06 07:02:36 +0000555 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500556 start += num_bytes;
557 pages = NULL;
558 cond_resched();
559 goto again;
560 }
561 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500562cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500563 /*
564 * No compression, but we still need to write the pages in
565 * the file we've been given so far. redirty the locked
566 * page if it corresponds to our extent and set things up
567 * for the async work queue to run cow_file_range to do
568 * the normal delalloc dance
569 */
570 if (page_offset(locked_page) >= start &&
571 page_offset(locked_page) <= end) {
572 __set_page_dirty_nobuffers(locked_page);
573 /* unlocked later on in the async handlers */
574 }
Chris Mason4adaa612013-03-26 13:07:00 -0400575 if (redirty)
576 extent_range_redirty_for_io(inode, start, end);
Li Zefan261507a02010-12-17 14:21:50 +0800577 add_async_extent(async_cow, start, end - start + 1,
578 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500579 *num_added += 1;
580 }
581
582out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100583 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500584
585free_pages_out:
586 for (i = 0; i < nr_pages_ret; i++) {
587 WARN_ON(pages[i]->mapping);
588 page_cache_release(pages[i]);
589 }
Chris Masond3977122009-01-05 21:25:51 -0500590 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500591
592 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100593
594cleanup_and_out:
Josef Bacikc2790a22013-07-29 11:20:47 -0400595 extent_clear_unlock_delalloc(inode, start, end, NULL,
596 EXTENT_DIRTY | EXTENT_DELALLOC,
597 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
598 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100599 if (!trans || IS_ERR(trans))
600 btrfs_error(root->fs_info, ret, "Failed to join transaction");
601 else
602 btrfs_abort_transaction(trans, root, ret);
603 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500604}
605
606/*
607 * phase two of compressed writeback. This is the ordered portion
608 * of the code, which only gets called in the order the work was
609 * queued. We walk all the async extents created by compress_file_range
610 * and send them down to the disk.
611 */
612static noinline int submit_compressed_extents(struct inode *inode,
613 struct async_cow *async_cow)
614{
615 struct async_extent *async_extent;
616 u64 alloc_hint = 0;
617 struct btrfs_trans_handle *trans;
618 struct btrfs_key ins;
619 struct extent_map *em;
620 struct btrfs_root *root = BTRFS_I(inode)->root;
621 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
622 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500623 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500624
625 if (list_empty(&async_cow->extents))
626 return 0;
627
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500628again:
Chris Masond3977122009-01-05 21:25:51 -0500629 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500630 async_extent = list_entry(async_cow->extents.next,
631 struct async_extent, list);
632 list_del(&async_extent->list);
633
634 io_tree = &BTRFS_I(inode)->io_tree;
635
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500636retry:
Chris Mason771ed682008-11-06 22:02:51 -0500637 /* did the compression code fall back to uncompressed IO? */
638 if (!async_extent->pages) {
639 int page_started = 0;
640 unsigned long nr_written = 0;
641
642 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000643 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100644 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500645
646 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500647 ret = cow_file_range(inode, async_cow->locked_page,
648 async_extent->start,
649 async_extent->start +
650 async_extent->ram_size - 1,
651 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500652
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100653 /* JDM XXX */
654
Chris Mason771ed682008-11-06 22:02:51 -0500655 /*
656 * if page_started, cow_file_range inserted an
657 * inline extent and took care of all the unlocking
658 * and IO for us. Otherwise, we need to submit
659 * all those pages down to the drive.
660 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500661 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500662 extent_write_locked_range(io_tree,
663 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500664 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500665 async_extent->ram_size - 1,
666 btrfs_get_extent,
667 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500668 else if (ret)
669 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500670 kfree(async_extent);
671 cond_resched();
672 continue;
673 }
674
675 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100676 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500677
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400678 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100679 if (IS_ERR(trans)) {
680 ret = PTR_ERR(trans);
681 } else {
682 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
683 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500684 async_extent->compressed_size,
685 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500686 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600687 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100688 btrfs_abort_transaction(trans, root, ret);
689 btrfs_end_transaction(trans, root);
690 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000691
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500692 if (ret) {
693 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500694
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500695 for (i = 0; i < async_extent->nr_pages; i++) {
696 WARN_ON(async_extent->pages[i]->mapping);
697 page_cache_release(async_extent->pages[i]);
698 }
699 kfree(async_extent->pages);
700 async_extent->nr_pages = 0;
701 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500702
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400703 if (ret == -ENOSPC) {
704 unlock_extent(io_tree, async_extent->start,
705 async_extent->start +
706 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100707 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400708 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500709 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500710 }
711
Yan, Zhengc2167752009-11-12 09:34:21 +0000712 /*
713 * here we're doing allocation and writeback of the
714 * compressed pages
715 */
716 btrfs_drop_extent_cache(inode, async_extent->start,
717 async_extent->start +
718 async_extent->ram_size - 1, 0);
719
David Sterba172ddd62011-04-21 00:48:27 +0200720 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000721 if (!em) {
722 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500723 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000724 }
Chris Mason771ed682008-11-06 22:02:51 -0500725 em->start = async_extent->start;
726 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500727 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400728 em->mod_start = em->start;
729 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500730
731 em->block_start = ins.objectid;
732 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500733 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400734 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500735 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800736 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500737 set_bit(EXTENT_FLAG_PINNED, &em->flags);
738 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400739 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500740
Chris Masond3977122009-01-05 21:25:51 -0500741 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400742 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400743 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400744 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500745 if (ret != -EEXIST) {
746 free_extent_map(em);
747 break;
748 }
749 btrfs_drop_extent_cache(inode, async_extent->start,
750 async_extent->start +
751 async_extent->ram_size - 1, 0);
752 }
753
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500754 if (ret)
755 goto out_free_reserve;
756
Li Zefan261507a02010-12-17 14:21:50 +0800757 ret = btrfs_add_ordered_extent_compress(inode,
758 async_extent->start,
759 ins.objectid,
760 async_extent->ram_size,
761 ins.offset,
762 BTRFS_ORDERED_COMPRESSED,
763 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500764 if (ret)
765 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500766
Chris Mason771ed682008-11-06 22:02:51 -0500767 /*
768 * clear dirty, set writeback and unlock the pages.
769 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400770 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400771 async_extent->start +
772 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400773 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
774 EXTENT_DIRTY, PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
775 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500776 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500777 async_extent->start,
778 async_extent->ram_size,
779 ins.objectid,
780 ins.offset, async_extent->pages,
781 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500782 alloc_hint = ins.objectid + ins.offset;
783 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500784 if (ret)
785 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500786 cond_resched();
787 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100788 ret = 0;
789out:
790 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500791out_free_reserve:
792 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100793out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400794 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500795 async_extent->start +
796 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400797 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
798 EXTENT_DIRTY, PAGE_UNLOCK |
799 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
800 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100801 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500802 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500803}
804
Josef Bacik4b46fce2010-05-23 11:00:55 -0400805static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
806 u64 num_bytes)
807{
808 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
809 struct extent_map *em;
810 u64 alloc_hint = 0;
811
812 read_lock(&em_tree->lock);
813 em = search_extent_mapping(em_tree, start, num_bytes);
814 if (em) {
815 /*
816 * if block start isn't an actual block number then find the
817 * first block in this inode and use that as a hint. If that
818 * block is also bogus then just don't worry about it.
819 */
820 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
821 free_extent_map(em);
822 em = search_extent_mapping(em_tree, 0, 0);
823 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
824 alloc_hint = em->block_start;
825 if (em)
826 free_extent_map(em);
827 } else {
828 alloc_hint = em->block_start;
829 free_extent_map(em);
830 }
831 }
832 read_unlock(&em_tree->lock);
833
834 return alloc_hint;
835}
836
Chris Mason771ed682008-11-06 22:02:51 -0500837/*
838 * when extent_io.c finds a delayed allocation range in the file,
839 * the call backs end up in this code. The basic idea is to
840 * allocate extents on disk for the range, and create ordered data structs
841 * in ram to track those extents.
842 *
843 * locked_page is the page that writepage had locked already. We use
844 * it to make sure we don't do extra locks or unlocks.
845 *
846 * *page_started is set to one if we unlock locked_page and do everything
847 * required to start IO on it. It may be clean and already done with
848 * IO when we return.
849 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000850static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
851 struct inode *inode,
852 struct btrfs_root *root,
853 struct page *locked_page,
854 u64 start, u64 end, int *page_started,
855 unsigned long *nr_written,
856 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500857{
Chris Mason771ed682008-11-06 22:02:51 -0500858 u64 alloc_hint = 0;
859 u64 num_bytes;
860 unsigned long ram_size;
861 u64 disk_num_bytes;
862 u64 cur_alloc_size;
863 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500864 struct btrfs_key ins;
865 struct extent_map *em;
866 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
867 int ret = 0;
868
Liu Bo83eea1f2012-07-10 05:28:39 -0600869 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500870
Qu Wenruofda28322013-02-26 08:10:22 +0000871 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500872 num_bytes = max(blocksize, num_bytes);
873 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500874
Chris Mason4cb53002011-05-24 15:35:30 -0400875 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400876 if (num_bytes < 64 * 1024 &&
877 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400878 btrfs_add_inode_defrag(trans, inode);
879
Chris Mason771ed682008-11-06 22:02:51 -0500880 if (start == 0) {
881 /* lets try to make an inline extent */
882 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000883 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500884 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400885 extent_clear_unlock_delalloc(inode, start, end, NULL,
886 EXTENT_LOCKED | EXTENT_DELALLOC |
887 EXTENT_DIRTY, PAGE_UNLOCK |
888 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
889 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000890
Chris Mason771ed682008-11-06 22:02:51 -0500891 *nr_written = *nr_written +
892 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
893 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500894 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100895 } else if (ret < 0) {
896 btrfs_abort_transaction(trans, root, ret);
897 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500898 }
899 }
Chris Masonc8b97812008-10-29 14:49:59 -0400900
901 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200902 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400903
Josef Bacik4b46fce2010-05-23 11:00:55 -0400904 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400905 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400906
Chris Masond3977122009-01-05 21:25:51 -0500907 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400908 unsigned long op;
909
Josef Bacik287a0ab2010-03-19 18:07:23 +0000910 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400911 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500912 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500913 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100914 if (ret < 0) {
915 btrfs_abort_transaction(trans, root, ret);
916 goto out_unlock;
917 }
Chris Masond3977122009-01-05 21:25:51 -0500918
David Sterba172ddd62011-04-21 00:48:27 +0200919 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000920 if (!em) {
921 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +0000922 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000923 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400924 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500925 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500926 ram_size = ins.offset;
927 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400928 em->mod_start = em->start;
929 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400930
Chris Masone6dcd2d2008-07-17 12:53:50 -0400931 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400932 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500933 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400934 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400935 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400936 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400937 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400938
Chris Masond3977122009-01-05 21:25:51 -0500939 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400940 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400941 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400942 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400943 if (ret != -EEXIST) {
944 free_extent_map(em);
945 break;
946 }
947 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400948 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400949 }
Liu Boace68ba2013-04-22 10:53:47 +0000950 if (ret)
951 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400952
Chris Mason98d20f62008-04-14 09:46:10 -0400953 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400954 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500955 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +0000956 if (ret)
957 goto out_reserve;
Chris Masonc8b97812008-10-29 14:49:59 -0400958
Yan Zheng17d217f2008-12-12 10:03:38 -0500959 if (root->root_key.objectid ==
960 BTRFS_DATA_RELOC_TREE_OBJECTID) {
961 ret = btrfs_reloc_clone_csums(inode, start,
962 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100963 if (ret) {
964 btrfs_abort_transaction(trans, root, ret);
Liu Boace68ba2013-04-22 10:53:47 +0000965 goto out_reserve;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500967 }
968
Chris Masond3977122009-01-05 21:25:51 -0500969 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400970 break;
Chris Masond3977122009-01-05 21:25:51 -0500971
Chris Masonc8b97812008-10-29 14:49:59 -0400972 /* we're not doing compressed IO, don't unlock the first
973 * page (which the caller expects to stay locked), don't
974 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400975 *
976 * Do set the Private2 bit so we know this page was properly
977 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400978 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400979 op = unlock ? PAGE_UNLOCK : 0;
980 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -0400981
Josef Bacikc2790a22013-07-29 11:20:47 -0400982 extent_clear_unlock_delalloc(inode, start,
983 start + ram_size - 1, locked_page,
984 EXTENT_LOCKED | EXTENT_DELALLOC,
985 op);
Chris Masonc8b97812008-10-29 14:49:59 -0400986 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500987 num_bytes -= cur_alloc_size;
988 alloc_hint = ins.objectid + ins.offset;
989 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400990 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100991out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500992 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000993
Liu Boace68ba2013-04-22 10:53:47 +0000994out_reserve:
995 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100996out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -0400997 extent_clear_unlock_delalloc(inode, start, end, locked_page,
998 EXTENT_LOCKED | EXTENT_DIRTY |
999 EXTENT_DELALLOC, PAGE_UNLOCK |
1000 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1001 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001002 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001003}
Chris Masonc8b97812008-10-29 14:49:59 -04001004
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001005static noinline int cow_file_range(struct inode *inode,
1006 struct page *locked_page,
1007 u64 start, u64 end, int *page_started,
1008 unsigned long *nr_written,
1009 int unlock)
1010{
1011 struct btrfs_trans_handle *trans;
1012 struct btrfs_root *root = BTRFS_I(inode)->root;
1013 int ret;
1014
1015 trans = btrfs_join_transaction(root);
1016 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001017 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1018 EXTENT_LOCKED | EXTENT_DELALLOC |
1019 EXTENT_DIRTY, PAGE_UNLOCK |
1020 PAGE_CLEAR_DIRTY |
1021 PAGE_SET_WRITEBACK |
1022 PAGE_END_WRITEBACK);
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001023 return PTR_ERR(trans);
1024 }
1025 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1026
1027 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1028 page_started, nr_written, unlock);
1029
1030 btrfs_end_transaction(trans, root);
1031
1032 return ret;
1033}
1034
Chris Mason771ed682008-11-06 22:02:51 -05001035/*
1036 * work queue call back to started compression on a file and pages
1037 */
1038static noinline void async_cow_start(struct btrfs_work *work)
1039{
1040 struct async_cow *async_cow;
1041 int num_added = 0;
1042 async_cow = container_of(work, struct async_cow, work);
1043
1044 compress_file_range(async_cow->inode, async_cow->locked_page,
1045 async_cow->start, async_cow->end, async_cow,
1046 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001047 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001048 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001049 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001050 }
Chris Mason771ed682008-11-06 22:02:51 -05001051}
1052
1053/*
1054 * work queue call back to submit previously compressed pages
1055 */
1056static noinline void async_cow_submit(struct btrfs_work *work)
1057{
1058 struct async_cow *async_cow;
1059 struct btrfs_root *root;
1060 unsigned long nr_pages;
1061
1062 async_cow = container_of(work, struct async_cow, work);
1063
1064 root = async_cow->root;
1065 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1066 PAGE_CACHE_SHIFT;
1067
Josef Bacik66657b32012-08-01 15:36:24 -04001068 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001069 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001070 waitqueue_active(&root->fs_info->async_submit_wait))
1071 wake_up(&root->fs_info->async_submit_wait);
1072
Chris Masond3977122009-01-05 21:25:51 -05001073 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001074 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001075}
Chris Masonc8b97812008-10-29 14:49:59 -04001076
Chris Mason771ed682008-11-06 22:02:51 -05001077static noinline void async_cow_free(struct btrfs_work *work)
1078{
1079 struct async_cow *async_cow;
1080 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001081 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001082 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001083 kfree(async_cow);
1084}
1085
1086static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1087 u64 start, u64 end, int *page_started,
1088 unsigned long *nr_written)
1089{
1090 struct async_cow *async_cow;
1091 struct btrfs_root *root = BTRFS_I(inode)->root;
1092 unsigned long nr_pages;
1093 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001094 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001095
Chris Masona3429ab2009-10-08 12:30:20 -04001096 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1097 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001098 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001099 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001100 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001101 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001102 async_cow->root = root;
1103 async_cow->locked_page = locked_page;
1104 async_cow->start = start;
1105
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001106 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001107 cur_end = end;
1108 else
1109 cur_end = min(end, start + 512 * 1024 - 1);
1110
1111 async_cow->end = cur_end;
1112 INIT_LIST_HEAD(&async_cow->extents);
1113
1114 async_cow->work.func = async_cow_start;
1115 async_cow->work.ordered_func = async_cow_submit;
1116 async_cow->work.ordered_free = async_cow_free;
1117 async_cow->work.flags = 0;
1118
Chris Mason771ed682008-11-06 22:02:51 -05001119 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1120 PAGE_CACHE_SHIFT;
1121 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1122
1123 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1124 &async_cow->work);
1125
1126 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1127 wait_event(root->fs_info->async_submit_wait,
1128 (atomic_read(&root->fs_info->async_delalloc_pages) <
1129 limit));
1130 }
1131
Chris Masond3977122009-01-05 21:25:51 -05001132 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001133 atomic_read(&root->fs_info->async_delalloc_pages)) {
1134 wait_event(root->fs_info->async_submit_wait,
1135 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1136 0));
1137 }
1138
1139 *nr_written += nr_pages;
1140 start = cur_end + 1;
1141 }
1142 *page_started = 1;
1143 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001144}
1145
Chris Masond3977122009-01-05 21:25:51 -05001146static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001147 u64 bytenr, u64 num_bytes)
1148{
1149 int ret;
1150 struct btrfs_ordered_sum *sums;
1151 LIST_HEAD(list);
1152
Yan Zheng07d400a2009-01-06 11:42:00 -05001153 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001154 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001155 if (ret == 0 && list_empty(&list))
1156 return 0;
1157
1158 while (!list_empty(&list)) {
1159 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1160 list_del(&sums->list);
1161 kfree(sums);
1162 }
1163 return 1;
1164}
1165
Chris Masond352ac62008-09-29 15:18:18 -04001166/*
1167 * when nowcow writeback call back. This checks for snapshots or COW copies
1168 * of the extents that exist in the file, and COWs the file as required.
1169 *
1170 * If no cow copies or snapshots exist, we write directly to the existing
1171 * blocks on disk
1172 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001173static noinline int run_delalloc_nocow(struct inode *inode,
1174 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001175 u64 start, u64 end, int *page_started, int force,
1176 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001177{
Chris Masonbe20aa92007-12-17 20:14:01 -05001178 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001179 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001180 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001181 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001182 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001183 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001184 u64 cow_start;
1185 u64 cur_offset;
1186 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001187 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001188 u64 disk_bytenr;
1189 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001190 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001191 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001192 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001193 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001194 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001195 int nocow;
1196 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001197 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001198 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001199
1200 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001201 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001202 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1203 EXTENT_LOCKED | EXTENT_DELALLOC |
1204 EXTENT_DIRTY, PAGE_UNLOCK |
1205 PAGE_CLEAR_DIRTY |
1206 PAGE_SET_WRITEBACK |
1207 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001208 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001209 }
Li Zefan82d59022011-04-20 10:33:24 +08001210
Liu Bo83eea1f2012-07-10 05:28:39 -06001211 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001212
1213 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001214 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001215 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001216 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001217
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001218 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001219 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1220 EXTENT_LOCKED | EXTENT_DELALLOC |
1221 EXTENT_DIRTY, PAGE_UNLOCK |
1222 PAGE_CLEAR_DIRTY |
1223 PAGE_SET_WRITEBACK |
1224 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001225 btrfs_free_path(path);
1226 return PTR_ERR(trans);
1227 }
1228
Josef Bacik74b21072011-04-13 12:02:53 -04001229 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001230
Yan Zheng80ff3852008-10-30 14:20:02 -04001231 cow_start = (u64)-1;
1232 cur_offset = start;
1233 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001234 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001235 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001236 if (ret < 0) {
1237 btrfs_abort_transaction(trans, root, ret);
1238 goto error;
1239 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1241 leaf = path->nodes[0];
1242 btrfs_item_key_to_cpu(leaf, &found_key,
1243 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001244 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001245 found_key.type == BTRFS_EXTENT_DATA_KEY)
1246 path->slots[0]--;
1247 }
1248 check_prev = 0;
1249next_slot:
1250 leaf = path->nodes[0];
1251 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1252 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001253 if (ret < 0) {
1254 btrfs_abort_transaction(trans, root, ret);
1255 goto error;
1256 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 if (ret > 0)
1258 break;
1259 leaf = path->nodes[0];
1260 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001261
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 nocow = 0;
1263 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001264 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001266
Li Zefan33345d012011-04-20 10:31:50 +08001267 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1269 found_key.offset > end)
1270 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001271
Yan Zheng80ff3852008-10-30 14:20:02 -04001272 if (found_key.offset > cur_offset) {
1273 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001274 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 goto out_check;
1276 }
Chris Masonc31f8832008-01-08 15:46:31 -05001277
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 fi = btrfs_item_ptr(leaf, path->slots[0],
1279 struct btrfs_file_extent_item);
1280 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001281
Josef Bacikcc95bef2013-04-04 14:31:27 -04001282 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001283 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1284 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001286 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 extent_end = found_key.offset +
1288 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001289 disk_num_bytes =
1290 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 if (extent_end <= start) {
1292 path->slots[0]++;
1293 goto next_slot;
1294 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001295 if (disk_bytenr == 0)
1296 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 if (btrfs_file_extent_compression(leaf, fi) ||
1298 btrfs_file_extent_encryption(leaf, fi) ||
1299 btrfs_file_extent_other_encoding(leaf, fi))
1300 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001301 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1302 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001303 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001305 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001306 found_key.offset -
1307 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001308 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001309 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001310 disk_bytenr += cur_offset - found_key.offset;
1311 num_bytes = min(end + 1, extent_end) - cur_offset;
1312 /*
1313 * force cow if csum exists in the range.
1314 * this ensure that csum for a given extent are
1315 * either valid or do not exist.
1316 */
1317 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1318 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 nocow = 1;
1320 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1321 extent_end = found_key.offset +
1322 btrfs_file_extent_inline_len(leaf, fi);
1323 extent_end = ALIGN(extent_end, root->sectorsize);
1324 } else {
1325 BUG_ON(1);
1326 }
1327out_check:
1328 if (extent_end <= start) {
1329 path->slots[0]++;
1330 goto next_slot;
1331 }
1332 if (!nocow) {
1333 if (cow_start == (u64)-1)
1334 cow_start = cur_offset;
1335 cur_offset = extent_end;
1336 if (cur_offset > end)
1337 break;
1338 path->slots[0]++;
1339 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001340 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001341
David Sterbab3b4aa72011-04-21 01:20:15 +02001342 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001344 ret = __cow_file_range(trans, inode, root, locked_page,
1345 cow_start, found_key.offset - 1,
1346 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001347 if (ret) {
1348 btrfs_abort_transaction(trans, root, ret);
1349 goto error;
1350 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001352 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001353
Yan Zhengd899e052008-10-30 14:25:28 -04001354 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1355 struct extent_map *em;
1356 struct extent_map_tree *em_tree;
1357 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001358 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001359 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001360 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001361 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001362 em->len = num_bytes;
1363 em->block_len = num_bytes;
1364 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001365 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001366 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001367 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001368 em->mod_start = em->start;
1369 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001370 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001371 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001372 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001373 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001374 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001375 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001376 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001377 if (ret != -EEXIST) {
1378 free_extent_map(em);
1379 break;
1380 }
1381 btrfs_drop_extent_cache(inode, em->start,
1382 em->start + em->len - 1, 0);
1383 }
1384 type = BTRFS_ORDERED_PREALLOC;
1385 } else {
1386 type = BTRFS_ORDERED_NOCOW;
1387 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001388
1389 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001390 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001391 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001392
Yan, Zhengefa56462010-05-16 10:49:59 -04001393 if (root->root_key.objectid ==
1394 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1395 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1396 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001397 if (ret) {
1398 btrfs_abort_transaction(trans, root, ret);
1399 goto error;
1400 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001401 }
1402
Josef Bacikc2790a22013-07-29 11:20:47 -04001403 extent_clear_unlock_delalloc(inode, cur_offset,
1404 cur_offset + num_bytes - 1,
1405 locked_page, EXTENT_LOCKED |
1406 EXTENT_DELALLOC, PAGE_UNLOCK |
1407 PAGE_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 cur_offset = extent_end;
1409 if (cur_offset > end)
1410 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001411 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001412 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001413
Josef Bacik17ca04a2012-05-31 15:58:55 -04001414 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001415 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001416 cur_offset = end;
1417 }
1418
Yan Zheng80ff3852008-10-30 14:20:02 -04001419 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001420 ret = __cow_file_range(trans, inode, root, locked_page,
1421 cow_start, end,
1422 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001423 if (ret) {
1424 btrfs_abort_transaction(trans, root, ret);
1425 goto error;
1426 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001427 }
1428
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001429error:
Miao Xiea698d0752012-09-20 01:51:59 -06001430 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001431 if (!ret)
1432 ret = err;
1433
Josef Bacik17ca04a2012-05-31 15:58:55 -04001434 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001435 extent_clear_unlock_delalloc(inode, cur_offset, end,
1436 locked_page, EXTENT_LOCKED |
1437 EXTENT_DELALLOC | EXTENT_DIRTY,
1438 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1439 PAGE_SET_WRITEBACK |
1440 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001441 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001442 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001443}
1444
Chris Masond352ac62008-09-29 15:18:18 -04001445/*
1446 * extent_io.c call back to do delayed allocation processing
1447 */
Chris Masonc8b97812008-10-29 14:49:59 -04001448static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001449 u64 start, u64 end, int *page_started,
1450 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001451{
Chris Masonbe20aa92007-12-17 20:14:01 -05001452 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001453 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001454
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001455 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001456 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001457 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001458 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001459 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001460 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001461 } else if (!btrfs_test_opt(root, COMPRESS) &&
1462 !(BTRFS_I(inode)->force_compress) &&
1463 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001464 ret = cow_file_range(inode, locked_page, start, end,
1465 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001466 } else {
1467 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1468 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001469 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001470 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001471 }
Chris Masonb888db22007-08-27 16:49:44 -04001472 return ret;
1473}
1474
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001475static void btrfs_split_extent_hook(struct inode *inode,
1476 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001477{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001478 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001479 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001480 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001481
Josef Bacik9e0baf62011-07-15 15:16:44 +00001482 spin_lock(&BTRFS_I(inode)->lock);
1483 BTRFS_I(inode)->outstanding_extents++;
1484 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001485}
1486
1487/*
1488 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1489 * extents so we can keep track of new extents that are just merged onto old
1490 * extents, such as when we are doing sequential writes, so we can properly
1491 * account for the metadata space we'll need.
1492 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001493static void btrfs_merge_extent_hook(struct inode *inode,
1494 struct extent_state *new,
1495 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001496{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001497 /* not delalloc, ignore it */
1498 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001499 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001500
Josef Bacik9e0baf62011-07-15 15:16:44 +00001501 spin_lock(&BTRFS_I(inode)->lock);
1502 BTRFS_I(inode)->outstanding_extents--;
1503 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001504}
1505
Miao Xieeb73c1b2013-05-15 07:48:22 +00001506static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1507 struct inode *inode)
1508{
1509 spin_lock(&root->delalloc_lock);
1510 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1511 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1512 &root->delalloc_inodes);
1513 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1514 &BTRFS_I(inode)->runtime_flags);
1515 root->nr_delalloc_inodes++;
1516 if (root->nr_delalloc_inodes == 1) {
1517 spin_lock(&root->fs_info->delalloc_root_lock);
1518 BUG_ON(!list_empty(&root->delalloc_root));
1519 list_add_tail(&root->delalloc_root,
1520 &root->fs_info->delalloc_roots);
1521 spin_unlock(&root->fs_info->delalloc_root_lock);
1522 }
1523 }
1524 spin_unlock(&root->delalloc_lock);
1525}
1526
1527static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1528 struct inode *inode)
1529{
1530 spin_lock(&root->delalloc_lock);
1531 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1532 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1533 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1534 &BTRFS_I(inode)->runtime_flags);
1535 root->nr_delalloc_inodes--;
1536 if (!root->nr_delalloc_inodes) {
1537 spin_lock(&root->fs_info->delalloc_root_lock);
1538 BUG_ON(list_empty(&root->delalloc_root));
1539 list_del_init(&root->delalloc_root);
1540 spin_unlock(&root->fs_info->delalloc_root_lock);
1541 }
1542 }
1543 spin_unlock(&root->delalloc_lock);
1544}
1545
Chris Masond352ac62008-09-29 15:18:18 -04001546/*
1547 * extent_io.c set_bit_hook, used to track delayed allocation
1548 * bytes in this file, and to maintain the list of inodes that
1549 * have pending delalloc work to be done.
1550 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001551static void btrfs_set_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001552 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001553{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001554
Chris Mason75eff682008-12-15 15:54:40 -05001555 /*
1556 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001557 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001558 * bit, which is only set or cleared with irqs on
1559 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001560 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001561 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001562 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001563 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001564
Josef Bacik9e0baf62011-07-15 15:16:44 +00001565 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001566 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001567 } else {
1568 spin_lock(&BTRFS_I(inode)->lock);
1569 BTRFS_I(inode)->outstanding_extents++;
1570 spin_unlock(&BTRFS_I(inode)->lock);
1571 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001572
Miao Xie963d6782013-01-29 10:10:51 +00001573 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1574 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001575 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001576 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001577 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001578 &BTRFS_I(inode)->runtime_flags))
1579 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001580 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001581 }
Chris Mason291d6732008-01-29 15:55:23 -05001582}
1583
Chris Masond352ac62008-09-29 15:18:18 -04001584/*
1585 * extent_io.c clear_bit_hook, see set_bit_hook for why
1586 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001587static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001588 struct extent_state *state,
1589 unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001590{
Chris Mason75eff682008-12-15 15:54:40 -05001591 /*
1592 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001593 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001594 * bit, which is only set or cleared with irqs on
1595 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001596 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001597 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001598 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001599 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001600
Josef Bacik9e0baf62011-07-15 15:16:44 +00001601 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001602 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001603 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1604 spin_lock(&BTRFS_I(inode)->lock);
1605 BTRFS_I(inode)->outstanding_extents--;
1606 spin_unlock(&BTRFS_I(inode)->lock);
1607 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001608
1609 if (*bits & EXTENT_DO_ACCOUNTING)
1610 btrfs_delalloc_release_metadata(inode, len);
1611
Josef Bacik0cb59c92010-07-02 12:14:14 -04001612 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001613 && do_list && !(state->state & EXTENT_NORESERVE))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001614 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001615
Miao Xie963d6782013-01-29 10:10:51 +00001616 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1617 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001618 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001619 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001620 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001621 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001622 &BTRFS_I(inode)->runtime_flags))
1623 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001624 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001625 }
Chris Mason291d6732008-01-29 15:55:23 -05001626}
1627
Chris Masond352ac62008-09-29 15:18:18 -04001628/*
1629 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1630 * we don't create bios that span stripes or chunks
1631 */
David Woodhouse64a16702009-07-15 23:29:37 +01001632int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001633 size_t size, struct bio *bio,
1634 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001635{
1636 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001637 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001638 u64 length = 0;
1639 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001640 int ret;
1641
Chris Mason771ed682008-11-06 22:02:51 -05001642 if (bio_flags & EXTENT_BIO_COMPRESSED)
1643 return 0;
1644
Chris Masonf2d8d742008-04-21 10:03:05 -04001645 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001646 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001647 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001648 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001649 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001650 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001651 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001652 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001653 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001654}
1655
Chris Masond352ac62008-09-29 15:18:18 -04001656/*
1657 * in order to insert checksums into the metadata in large chunks,
1658 * we wait until bio submission time. All the pages in the bio are
1659 * checksummed and sums are attached onto the ordered extent record.
1660 *
1661 * At IO completion time the cums attached on the ordered extent record
1662 * are inserted into the btree
1663 */
Chris Masond3977122009-01-05 21:25:51 -05001664static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1665 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001666 unsigned long bio_flags,
1667 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001668{
Chris Mason065631f2008-02-20 12:07:25 -05001669 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001670 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001671
Chris Masond20f7042008-12-08 16:58:54 -05001672 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001673 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001674 return 0;
1675}
Chris Masone0156402008-04-16 11:15:20 -04001676
Chris Mason4a69a412008-11-06 22:03:00 -05001677/*
1678 * in order to insert checksums into the metadata in large chunks,
1679 * we wait until bio submission time. All the pages in the bio are
1680 * checksummed and sums are attached onto the ordered extent record.
1681 *
1682 * At IO completion time the cums attached on the ordered extent record
1683 * are inserted into the btree
1684 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001685static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001686 int mirror_num, unsigned long bio_flags,
1687 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001688{
1689 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001690 int ret;
1691
1692 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1693 if (ret)
1694 bio_endio(bio, ret);
1695 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001696}
1697
Chris Masond352ac62008-09-29 15:18:18 -04001698/*
Chris Masoncad321a2008-12-17 14:51:42 -05001699 * extent_io.c submission hook. This does the right thing for csum calculation
1700 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001701 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001702static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001703 int mirror_num, unsigned long bio_flags,
1704 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001705{
1706 struct btrfs_root *root = BTRFS_I(inode)->root;
1707 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001708 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001709 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001710 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001711
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001712 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001713
Liu Bo83eea1f2012-07-10 05:28:39 -06001714 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001715 metadata = 2;
1716
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001717 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001718 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1719 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001720 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001721
Chris Masond20f7042008-12-08 16:58:54 -05001722 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001723 ret = btrfs_submit_compressed_read(inode, bio,
1724 mirror_num,
1725 bio_flags);
1726 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001727 } else if (!skip_sum) {
1728 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1729 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001730 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001731 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001732 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001733 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001734 /* csum items have already been cloned */
1735 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1736 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001737 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001738 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001739 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001740 bio_flags, bio_offset,
1741 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001742 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001743 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001744 } else if (!skip_sum) {
1745 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1746 if (ret)
1747 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001748 }
1749
Chris Mason0b86a832008-03-24 15:01:56 -04001750mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001751 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1752
1753out:
1754 if (ret < 0)
1755 bio_endio(bio, ret);
1756 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001757}
Chris Mason6885f302008-02-20 16:11:05 -05001758
Chris Masond352ac62008-09-29 15:18:18 -04001759/*
1760 * given a list of ordered sums record them in the inode. This happens
1761 * at IO completion time based on sums calculated at bio submission time.
1762 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001763static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001764 struct inode *inode, u64 file_offset,
1765 struct list_head *list)
1766{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001767 struct btrfs_ordered_sum *sum;
1768
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001769 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001770 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001771 btrfs_csum_file_blocks(trans,
1772 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001773 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001774 }
1775 return 0;
1776}
1777
Josef Bacik2ac55d42010-02-03 19:33:23 +00001778int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1779 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001780{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001781 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001782 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001783 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001784}
1785
Chris Masond352ac62008-09-29 15:18:18 -04001786/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001787struct btrfs_writepage_fixup {
1788 struct page *page;
1789 struct btrfs_work work;
1790};
1791
Christoph Hellwigb2950862008-12-02 09:54:17 -05001792static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001793{
1794 struct btrfs_writepage_fixup *fixup;
1795 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001796 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001797 struct page *page;
1798 struct inode *inode;
1799 u64 page_start;
1800 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001801 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001802
1803 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1804 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001805again:
Chris Mason247e7432008-07-17 12:53:51 -04001806 lock_page(page);
1807 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1808 ClearPageChecked(page);
1809 goto out_page;
1810 }
1811
1812 inode = page->mapping->host;
1813 page_start = page_offset(page);
1814 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1815
Josef Bacik2ac55d42010-02-03 19:33:23 +00001816 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001817 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001818
1819 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001820 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001821 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001822
1823 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1824 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001825 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1826 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001827 unlock_page(page);
1828 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001829 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001830 goto again;
1831 }
Chris Mason247e7432008-07-17 12:53:51 -04001832
Jeff Mahoney87826df2012-02-15 16:23:57 +01001833 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1834 if (ret) {
1835 mapping_set_error(page->mapping, ret);
1836 end_extent_writepage(page, ret, page_start, page_end);
1837 ClearPageChecked(page);
1838 goto out;
1839 }
1840
Josef Bacik2ac55d42010-02-03 19:33:23 +00001841 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001842 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001843 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001844out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001845 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1846 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001847out_page:
1848 unlock_page(page);
1849 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001850 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001851}
1852
1853/*
1854 * There are a few paths in the higher layers of the kernel that directly
1855 * set the page dirty bit without asking the filesystem if it is a
1856 * good idea. This causes problems because we want to make sure COW
1857 * properly happens and the data=ordered rules are followed.
1858 *
Chris Masonc8b97812008-10-29 14:49:59 -04001859 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001860 * hasn't been properly setup for IO. We kick off an async process
1861 * to fix it up. The async helper will wait for ordered extents, set
1862 * the delalloc bit and make it safe to write the page.
1863 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001864static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001865{
1866 struct inode *inode = page->mapping->host;
1867 struct btrfs_writepage_fixup *fixup;
1868 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001869
Chris Mason8b62b722009-09-02 16:53:46 -04001870 /* this page is properly in the ordered list */
1871 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001872 return 0;
1873
1874 if (PageChecked(page))
1875 return -EAGAIN;
1876
1877 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1878 if (!fixup)
1879 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001880
Chris Mason247e7432008-07-17 12:53:51 -04001881 SetPageChecked(page);
1882 page_cache_get(page);
1883 fixup->work.func = btrfs_writepage_fixup_worker;
1884 fixup->page = page;
1885 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001886 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001887}
1888
Yan Zhengd899e052008-10-30 14:25:28 -04001889static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1890 struct inode *inode, u64 file_pos,
1891 u64 disk_bytenr, u64 disk_num_bytes,
1892 u64 num_bytes, u64 ram_bytes,
1893 u8 compression, u8 encryption,
1894 u16 other_encoding, int extent_type)
1895{
1896 struct btrfs_root *root = BTRFS_I(inode)->root;
1897 struct btrfs_file_extent_item *fi;
1898 struct btrfs_path *path;
1899 struct extent_buffer *leaf;
1900 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001901 int ret;
1902
1903 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001904 if (!path)
1905 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001906
Chris Masonb9473432009-03-13 11:00:37 -04001907 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001908
1909 /*
1910 * we may be replacing one extent in the tree with another.
1911 * The new extent is pinned in the extent map, and we don't want
1912 * to drop it from the cache until it is completely in the btree.
1913 *
1914 * So, tell btrfs_drop_extents to leave this extent in the cache.
1915 * the caller is expected to unpin it and allow it to be merged
1916 * with the others.
1917 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001918 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001919 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001920 if (ret)
1921 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001922
Li Zefan33345d012011-04-20 10:31:50 +08001923 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001924 ins.offset = file_pos;
1925 ins.type = BTRFS_EXTENT_DATA_KEY;
1926 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001927 if (ret)
1928 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001929 leaf = path->nodes[0];
1930 fi = btrfs_item_ptr(leaf, path->slots[0],
1931 struct btrfs_file_extent_item);
1932 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1933 btrfs_set_file_extent_type(leaf, fi, extent_type);
1934 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1935 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1936 btrfs_set_file_extent_offset(leaf, fi, 0);
1937 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1938 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1939 btrfs_set_file_extent_compression(leaf, fi, compression);
1940 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1941 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001942
Yan Zhengd899e052008-10-30 14:25:28 -04001943 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001944 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001945
1946 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001947
1948 ins.objectid = disk_bytenr;
1949 ins.offset = disk_num_bytes;
1950 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001951 ret = btrfs_alloc_reserved_file_extent(trans, root,
1952 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001953 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001954out:
Yan Zhengd899e052008-10-30 14:25:28 -04001955 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001956
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001957 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001958}
1959
Liu Bo38c227d2013-01-29 03:18:40 +00001960/* snapshot-aware defrag */
1961struct sa_defrag_extent_backref {
1962 struct rb_node node;
1963 struct old_sa_defrag_extent *old;
1964 u64 root_id;
1965 u64 inum;
1966 u64 file_pos;
1967 u64 extent_offset;
1968 u64 num_bytes;
1969 u64 generation;
1970};
1971
1972struct old_sa_defrag_extent {
1973 struct list_head list;
1974 struct new_sa_defrag_extent *new;
1975
1976 u64 extent_offset;
1977 u64 bytenr;
1978 u64 offset;
1979 u64 len;
1980 int count;
1981};
1982
1983struct new_sa_defrag_extent {
1984 struct rb_root root;
1985 struct list_head head;
1986 struct btrfs_path *path;
1987 struct inode *inode;
1988 u64 file_pos;
1989 u64 len;
1990 u64 bytenr;
1991 u64 disk_len;
1992 u8 compress_type;
1993};
1994
1995static int backref_comp(struct sa_defrag_extent_backref *b1,
1996 struct sa_defrag_extent_backref *b2)
1997{
1998 if (b1->root_id < b2->root_id)
1999 return -1;
2000 else if (b1->root_id > b2->root_id)
2001 return 1;
2002
2003 if (b1->inum < b2->inum)
2004 return -1;
2005 else if (b1->inum > b2->inum)
2006 return 1;
2007
2008 if (b1->file_pos < b2->file_pos)
2009 return -1;
2010 else if (b1->file_pos > b2->file_pos)
2011 return 1;
2012
2013 /*
2014 * [------------------------------] ===> (a range of space)
2015 * |<--->| |<---->| =============> (fs/file tree A)
2016 * |<---------------------------->| ===> (fs/file tree B)
2017 *
2018 * A range of space can refer to two file extents in one tree while
2019 * refer to only one file extent in another tree.
2020 *
2021 * So we may process a disk offset more than one time(two extents in A)
2022 * and locate at the same extent(one extent in B), then insert two same
2023 * backrefs(both refer to the extent in B).
2024 */
2025 return 0;
2026}
2027
2028static void backref_insert(struct rb_root *root,
2029 struct sa_defrag_extent_backref *backref)
2030{
2031 struct rb_node **p = &root->rb_node;
2032 struct rb_node *parent = NULL;
2033 struct sa_defrag_extent_backref *entry;
2034 int ret;
2035
2036 while (*p) {
2037 parent = *p;
2038 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2039
2040 ret = backref_comp(backref, entry);
2041 if (ret < 0)
2042 p = &(*p)->rb_left;
2043 else
2044 p = &(*p)->rb_right;
2045 }
2046
2047 rb_link_node(&backref->node, parent, p);
2048 rb_insert_color(&backref->node, root);
2049}
2050
2051/*
2052 * Note the backref might has changed, and in this case we just return 0.
2053 */
2054static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2055 void *ctx)
2056{
2057 struct btrfs_file_extent_item *extent;
2058 struct btrfs_fs_info *fs_info;
2059 struct old_sa_defrag_extent *old = ctx;
2060 struct new_sa_defrag_extent *new = old->new;
2061 struct btrfs_path *path = new->path;
2062 struct btrfs_key key;
2063 struct btrfs_root *root;
2064 struct sa_defrag_extent_backref *backref;
2065 struct extent_buffer *leaf;
2066 struct inode *inode = new->inode;
2067 int slot;
2068 int ret;
2069 u64 extent_offset;
2070 u64 num_bytes;
2071
2072 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2073 inum == btrfs_ino(inode))
2074 return 0;
2075
2076 key.objectid = root_id;
2077 key.type = BTRFS_ROOT_ITEM_KEY;
2078 key.offset = (u64)-1;
2079
2080 fs_info = BTRFS_I(inode)->root->fs_info;
2081 root = btrfs_read_fs_root_no_name(fs_info, &key);
2082 if (IS_ERR(root)) {
2083 if (PTR_ERR(root) == -ENOENT)
2084 return 0;
2085 WARN_ON(1);
2086 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2087 inum, offset, root_id);
2088 return PTR_ERR(root);
2089 }
2090
2091 key.objectid = inum;
2092 key.type = BTRFS_EXTENT_DATA_KEY;
2093 if (offset > (u64)-1 << 32)
2094 key.offset = 0;
2095 else
2096 key.offset = offset;
2097
2098 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2099 if (ret < 0) {
2100 WARN_ON(1);
2101 return ret;
2102 }
Josef Bacik50f13192013-07-22 12:50:37 -04002103 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002104
2105 while (1) {
2106 cond_resched();
2107
2108 leaf = path->nodes[0];
2109 slot = path->slots[0];
2110
2111 if (slot >= btrfs_header_nritems(leaf)) {
2112 ret = btrfs_next_leaf(root, path);
2113 if (ret < 0) {
2114 goto out;
2115 } else if (ret > 0) {
2116 ret = 0;
2117 goto out;
2118 }
2119 continue;
2120 }
2121
2122 path->slots[0]++;
2123
2124 btrfs_item_key_to_cpu(leaf, &key, slot);
2125
2126 if (key.objectid > inum)
2127 goto out;
2128
2129 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2130 continue;
2131
2132 extent = btrfs_item_ptr(leaf, slot,
2133 struct btrfs_file_extent_item);
2134
2135 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2136 continue;
2137
Liu Boe68afa42013-07-01 22:13:26 +08002138 /*
2139 * 'offset' refers to the exact key.offset,
2140 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2141 * (key.offset - extent_offset).
2142 */
2143 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002144 continue;
2145
Liu Boe68afa42013-07-01 22:13:26 +08002146 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002147 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002148
Liu Bo38c227d2013-01-29 03:18:40 +00002149 if (extent_offset >= old->extent_offset + old->offset +
2150 old->len || extent_offset + num_bytes <=
2151 old->extent_offset + old->offset)
2152 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002153 break;
2154 }
2155
2156 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2157 if (!backref) {
2158 ret = -ENOENT;
2159 goto out;
2160 }
2161
2162 backref->root_id = root_id;
2163 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002164 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002165 backref->num_bytes = num_bytes;
2166 backref->extent_offset = extent_offset;
2167 backref->generation = btrfs_file_extent_generation(leaf, extent);
2168 backref->old = old;
2169 backref_insert(&new->root, backref);
2170 old->count++;
2171out:
2172 btrfs_release_path(path);
2173 WARN_ON(ret);
2174 return ret;
2175}
2176
2177static noinline bool record_extent_backrefs(struct btrfs_path *path,
2178 struct new_sa_defrag_extent *new)
2179{
2180 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2181 struct old_sa_defrag_extent *old, *tmp;
2182 int ret;
2183
2184 new->path = path;
2185
2186 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002187 ret = iterate_inodes_from_logical(old->bytenr +
2188 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002189 path, record_one_backref,
2190 old);
2191 BUG_ON(ret < 0 && ret != -ENOENT);
2192
2193 /* no backref to be processed for this extent */
2194 if (!old->count) {
2195 list_del(&old->list);
2196 kfree(old);
2197 }
2198 }
2199
2200 if (list_empty(&new->head))
2201 return false;
2202
2203 return true;
2204}
2205
2206static int relink_is_mergable(struct extent_buffer *leaf,
2207 struct btrfs_file_extent_item *fi,
2208 u64 disk_bytenr)
2209{
2210 if (btrfs_file_extent_disk_bytenr(leaf, fi) != disk_bytenr)
2211 return 0;
2212
2213 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2214 return 0;
2215
2216 if (btrfs_file_extent_compression(leaf, fi) ||
2217 btrfs_file_extent_encryption(leaf, fi) ||
2218 btrfs_file_extent_other_encoding(leaf, fi))
2219 return 0;
2220
2221 return 1;
2222}
2223
2224/*
2225 * Note the backref might has changed, and in this case we just return 0.
2226 */
2227static noinline int relink_extent_backref(struct btrfs_path *path,
2228 struct sa_defrag_extent_backref *prev,
2229 struct sa_defrag_extent_backref *backref)
2230{
2231 struct btrfs_file_extent_item *extent;
2232 struct btrfs_file_extent_item *item;
2233 struct btrfs_ordered_extent *ordered;
2234 struct btrfs_trans_handle *trans;
2235 struct btrfs_fs_info *fs_info;
2236 struct btrfs_root *root;
2237 struct btrfs_key key;
2238 struct extent_buffer *leaf;
2239 struct old_sa_defrag_extent *old = backref->old;
2240 struct new_sa_defrag_extent *new = old->new;
2241 struct inode *src_inode = new->inode;
2242 struct inode *inode;
2243 struct extent_state *cached = NULL;
2244 int ret = 0;
2245 u64 start;
2246 u64 len;
2247 u64 lock_start;
2248 u64 lock_end;
2249 bool merge = false;
2250 int index;
2251
2252 if (prev && prev->root_id == backref->root_id &&
2253 prev->inum == backref->inum &&
2254 prev->file_pos + prev->num_bytes == backref->file_pos)
2255 merge = true;
2256
2257 /* step 1: get root */
2258 key.objectid = backref->root_id;
2259 key.type = BTRFS_ROOT_ITEM_KEY;
2260 key.offset = (u64)-1;
2261
2262 fs_info = BTRFS_I(src_inode)->root->fs_info;
2263 index = srcu_read_lock(&fs_info->subvol_srcu);
2264
2265 root = btrfs_read_fs_root_no_name(fs_info, &key);
2266 if (IS_ERR(root)) {
2267 srcu_read_unlock(&fs_info->subvol_srcu, index);
2268 if (PTR_ERR(root) == -ENOENT)
2269 return 0;
2270 return PTR_ERR(root);
2271 }
Liu Bo38c227d2013-01-29 03:18:40 +00002272
2273 /* step 2: get inode */
2274 key.objectid = backref->inum;
2275 key.type = BTRFS_INODE_ITEM_KEY;
2276 key.offset = 0;
2277
2278 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2279 if (IS_ERR(inode)) {
2280 srcu_read_unlock(&fs_info->subvol_srcu, index);
2281 return 0;
2282 }
2283
2284 srcu_read_unlock(&fs_info->subvol_srcu, index);
2285
2286 /* step 3: relink backref */
2287 lock_start = backref->file_pos;
2288 lock_end = backref->file_pos + backref->num_bytes - 1;
2289 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2290 0, &cached);
2291
2292 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2293 if (ordered) {
2294 btrfs_put_ordered_extent(ordered);
2295 goto out_unlock;
2296 }
2297
2298 trans = btrfs_join_transaction(root);
2299 if (IS_ERR(trans)) {
2300 ret = PTR_ERR(trans);
2301 goto out_unlock;
2302 }
2303
2304 key.objectid = backref->inum;
2305 key.type = BTRFS_EXTENT_DATA_KEY;
2306 key.offset = backref->file_pos;
2307
2308 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2309 if (ret < 0) {
2310 goto out_free_path;
2311 } else if (ret > 0) {
2312 ret = 0;
2313 goto out_free_path;
2314 }
2315
2316 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2317 struct btrfs_file_extent_item);
2318
2319 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2320 backref->generation)
2321 goto out_free_path;
2322
2323 btrfs_release_path(path);
2324
2325 start = backref->file_pos;
2326 if (backref->extent_offset < old->extent_offset + old->offset)
2327 start += old->extent_offset + old->offset -
2328 backref->extent_offset;
2329
2330 len = min(backref->extent_offset + backref->num_bytes,
2331 old->extent_offset + old->offset + old->len);
2332 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2333
2334 ret = btrfs_drop_extents(trans, root, inode, start,
2335 start + len, 1);
2336 if (ret)
2337 goto out_free_path;
2338again:
2339 key.objectid = btrfs_ino(inode);
2340 key.type = BTRFS_EXTENT_DATA_KEY;
2341 key.offset = start;
2342
Liu Boa09a0a72013-03-11 09:20:58 +00002343 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002344 if (merge) {
2345 struct btrfs_file_extent_item *fi;
2346 u64 extent_len;
2347 struct btrfs_key found_key;
2348
2349 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2350 if (ret < 0)
2351 goto out_free_path;
2352
2353 path->slots[0]--;
2354 leaf = path->nodes[0];
2355 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2356
2357 fi = btrfs_item_ptr(leaf, path->slots[0],
2358 struct btrfs_file_extent_item);
2359 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2360
2361 if (relink_is_mergable(leaf, fi, new->bytenr) &&
2362 extent_len + found_key.offset == start) {
2363 btrfs_set_file_extent_num_bytes(leaf, fi,
2364 extent_len + len);
2365 btrfs_mark_buffer_dirty(leaf);
2366 inode_add_bytes(inode, len);
2367
2368 ret = 1;
2369 goto out_free_path;
2370 } else {
2371 merge = false;
2372 btrfs_release_path(path);
2373 goto again;
2374 }
2375 }
2376
2377 ret = btrfs_insert_empty_item(trans, root, path, &key,
2378 sizeof(*extent));
2379 if (ret) {
2380 btrfs_abort_transaction(trans, root, ret);
2381 goto out_free_path;
2382 }
2383
2384 leaf = path->nodes[0];
2385 item = btrfs_item_ptr(leaf, path->slots[0],
2386 struct btrfs_file_extent_item);
2387 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2388 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2389 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2390 btrfs_set_file_extent_num_bytes(leaf, item, len);
2391 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2392 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2393 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2394 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2395 btrfs_set_file_extent_encryption(leaf, item, 0);
2396 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2397
2398 btrfs_mark_buffer_dirty(leaf);
2399 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002400 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002401
2402 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2403 new->disk_len, 0,
2404 backref->root_id, backref->inum,
2405 new->file_pos, 0); /* start - extent_offset */
2406 if (ret) {
2407 btrfs_abort_transaction(trans, root, ret);
2408 goto out_free_path;
2409 }
2410
2411 ret = 1;
2412out_free_path:
2413 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002414 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002415 btrfs_end_transaction(trans, root);
2416out_unlock:
2417 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2418 &cached, GFP_NOFS);
2419 iput(inode);
2420 return ret;
2421}
2422
2423static void relink_file_extents(struct new_sa_defrag_extent *new)
2424{
2425 struct btrfs_path *path;
2426 struct old_sa_defrag_extent *old, *tmp;
2427 struct sa_defrag_extent_backref *backref;
2428 struct sa_defrag_extent_backref *prev = NULL;
2429 struct inode *inode;
2430 struct btrfs_root *root;
2431 struct rb_node *node;
2432 int ret;
2433
2434 inode = new->inode;
2435 root = BTRFS_I(inode)->root;
2436
2437 path = btrfs_alloc_path();
2438 if (!path)
2439 return;
2440
2441 if (!record_extent_backrefs(path, new)) {
2442 btrfs_free_path(path);
2443 goto out;
2444 }
2445 btrfs_release_path(path);
2446
2447 while (1) {
2448 node = rb_first(&new->root);
2449 if (!node)
2450 break;
2451 rb_erase(node, &new->root);
2452
2453 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2454
2455 ret = relink_extent_backref(path, prev, backref);
2456 WARN_ON(ret < 0);
2457
2458 kfree(prev);
2459
2460 if (ret == 1)
2461 prev = backref;
2462 else
2463 prev = NULL;
2464 cond_resched();
2465 }
2466 kfree(prev);
2467
2468 btrfs_free_path(path);
2469
2470 list_for_each_entry_safe(old, tmp, &new->head, list) {
2471 list_del(&old->list);
2472 kfree(old);
2473 }
2474out:
2475 atomic_dec(&root->fs_info->defrag_running);
2476 wake_up(&root->fs_info->transaction_wait);
2477
2478 kfree(new);
2479}
2480
2481static struct new_sa_defrag_extent *
2482record_old_file_extents(struct inode *inode,
2483 struct btrfs_ordered_extent *ordered)
2484{
2485 struct btrfs_root *root = BTRFS_I(inode)->root;
2486 struct btrfs_path *path;
2487 struct btrfs_key key;
2488 struct old_sa_defrag_extent *old, *tmp;
2489 struct new_sa_defrag_extent *new;
2490 int ret;
2491
2492 new = kmalloc(sizeof(*new), GFP_NOFS);
2493 if (!new)
2494 return NULL;
2495
2496 new->inode = inode;
2497 new->file_pos = ordered->file_offset;
2498 new->len = ordered->len;
2499 new->bytenr = ordered->start;
2500 new->disk_len = ordered->disk_len;
2501 new->compress_type = ordered->compress_type;
2502 new->root = RB_ROOT;
2503 INIT_LIST_HEAD(&new->head);
2504
2505 path = btrfs_alloc_path();
2506 if (!path)
2507 goto out_kfree;
2508
2509 key.objectid = btrfs_ino(inode);
2510 key.type = BTRFS_EXTENT_DATA_KEY;
2511 key.offset = new->file_pos;
2512
2513 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2514 if (ret < 0)
2515 goto out_free_path;
2516 if (ret > 0 && path->slots[0] > 0)
2517 path->slots[0]--;
2518
2519 /* find out all the old extents for the file range */
2520 while (1) {
2521 struct btrfs_file_extent_item *extent;
2522 struct extent_buffer *l;
2523 int slot;
2524 u64 num_bytes;
2525 u64 offset;
2526 u64 end;
2527 u64 disk_bytenr;
2528 u64 extent_offset;
2529
2530 l = path->nodes[0];
2531 slot = path->slots[0];
2532
2533 if (slot >= btrfs_header_nritems(l)) {
2534 ret = btrfs_next_leaf(root, path);
2535 if (ret < 0)
2536 goto out_free_list;
2537 else if (ret > 0)
2538 break;
2539 continue;
2540 }
2541
2542 btrfs_item_key_to_cpu(l, &key, slot);
2543
2544 if (key.objectid != btrfs_ino(inode))
2545 break;
2546 if (key.type != BTRFS_EXTENT_DATA_KEY)
2547 break;
2548 if (key.offset >= new->file_pos + new->len)
2549 break;
2550
2551 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2552
2553 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2554 if (key.offset + num_bytes < new->file_pos)
2555 goto next;
2556
2557 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2558 if (!disk_bytenr)
2559 goto next;
2560
2561 extent_offset = btrfs_file_extent_offset(l, extent);
2562
2563 old = kmalloc(sizeof(*old), GFP_NOFS);
2564 if (!old)
2565 goto out_free_list;
2566
2567 offset = max(new->file_pos, key.offset);
2568 end = min(new->file_pos + new->len, key.offset + num_bytes);
2569
2570 old->bytenr = disk_bytenr;
2571 old->extent_offset = extent_offset;
2572 old->offset = offset - key.offset;
2573 old->len = end - offset;
2574 old->new = new;
2575 old->count = 0;
2576 list_add_tail(&old->list, &new->head);
2577next:
2578 path->slots[0]++;
2579 cond_resched();
2580 }
2581
2582 btrfs_free_path(path);
2583 atomic_inc(&root->fs_info->defrag_running);
2584
2585 return new;
2586
2587out_free_list:
2588 list_for_each_entry_safe(old, tmp, &new->head, list) {
2589 list_del(&old->list);
2590 kfree(old);
2591 }
2592out_free_path:
2593 btrfs_free_path(path);
2594out_kfree:
2595 kfree(new);
2596 return NULL;
2597}
2598
Chris Mason5d13a982009-03-13 11:41:46 -04002599/*
2600 * helper function for btrfs_finish_ordered_io, this
2601 * just reads in some of the csum leaves to prime them into ram
2602 * before we start the transaction. It limits the amount of btree
2603 * reads required while inside the transaction.
2604 */
Chris Masond352ac62008-09-29 15:18:18 -04002605/* as ordered data IO finishes, this gets called so we can finish
2606 * an ordered extent if the range of bytes in the file it covers are
2607 * fully written.
2608 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002609static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002610{
Josef Bacik5fd02042012-05-02 14:00:54 -04002611 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002612 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002613 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002614 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002615 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002616 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002617 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002618 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08002619 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002620
Liu Bo83eea1f2012-07-10 05:28:39 -06002621 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002622
Josef Bacik5fd02042012-05-02 14:00:54 -04002623 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2624 ret = -EIO;
2625 goto out;
2626 }
2627
Yan, Zhengc2167752009-11-12 09:34:21 +00002628 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002629 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05002630 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2631 if (nolock)
2632 trans = btrfs_join_transaction_nolock(root);
2633 else
2634 trans = btrfs_join_transaction(root);
2635 if (IS_ERR(trans)) {
2636 ret = PTR_ERR(trans);
2637 trans = NULL;
2638 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002639 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002640 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2641 ret = btrfs_update_inode_fallback(trans, root, inode);
2642 if (ret) /* -ENOMEM or corruption */
2643 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002644 goto out;
2645 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002646
Josef Bacik2ac55d42010-02-03 19:33:23 +00002647 lock_extent_bits(io_tree, ordered_extent->file_offset,
2648 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002649 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002650
Liu Bo38c227d2013-01-29 03:18:40 +00002651 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2652 ordered_extent->file_offset + ordered_extent->len - 1,
2653 EXTENT_DEFRAG, 1, cached_state);
2654 if (ret) {
2655 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2656 if (last_snapshot >= BTRFS_I(inode)->generation)
2657 /* the inode is shared */
2658 new = record_old_file_extents(inode, ordered_extent);
2659
2660 clear_extent_bit(io_tree, ordered_extent->file_offset,
2661 ordered_extent->file_offset + ordered_extent->len - 1,
2662 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2663 }
2664
Josef Bacik0cb59c92010-07-02 12:14:14 -04002665 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002666 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002667 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002668 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002669 if (IS_ERR(trans)) {
2670 ret = PTR_ERR(trans);
2671 trans = NULL;
2672 goto out_unlock;
2673 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002674 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002675
Chris Masonc8b97812008-10-29 14:49:59 -04002676 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002677 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002678 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002679 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002680 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002681 ordered_extent->file_offset,
2682 ordered_extent->file_offset +
2683 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002684 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002685 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002686 ret = insert_reserved_file_extent(trans, inode,
2687 ordered_extent->file_offset,
2688 ordered_extent->start,
2689 ordered_extent->disk_len,
2690 ordered_extent->len,
2691 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002692 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002693 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002694 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002695 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2696 ordered_extent->file_offset, ordered_extent->len,
2697 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002698 if (ret < 0) {
2699 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002700 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002701 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002702
Chris Masone6dcd2d2008-07-17 12:53:50 -04002703 add_pending_csums(trans, inode, ordered_extent->file_offset,
2704 &ordered_extent->list);
2705
Josef Bacik6c760c02012-11-09 10:53:21 -05002706 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2707 ret = btrfs_update_inode_fallback(trans, root, inode);
2708 if (ret) { /* -ENOMEM or corruption */
2709 btrfs_abort_transaction(trans, root, ret);
2710 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002711 }
2712 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002713out_unlock:
2714 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2715 ordered_extent->file_offset +
2716 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002717out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002718 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002719 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002720 if (trans)
2721 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002722
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002723 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002724 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2725 ordered_extent->file_offset +
2726 ordered_extent->len - 1, NULL, GFP_NOFS);
2727
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002728 /*
2729 * If the ordered extent had an IOERR or something else went
2730 * wrong we need to return the space for this ordered extent
2731 * back to the allocator.
2732 */
2733 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2734 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2735 btrfs_free_reserved_extent(root, ordered_extent->start,
2736 ordered_extent->disk_len);
2737 }
2738
2739
Josef Bacik5fd02042012-05-02 14:00:54 -04002740 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002741 * This needs to be done to make sure anybody waiting knows we are done
2742 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002743 */
2744 btrfs_remove_ordered_extent(inode, ordered_extent);
2745
Liu Bo38c227d2013-01-29 03:18:40 +00002746 /* for snapshot-aware defrag */
2747 if (new)
2748 relink_file_extents(new);
2749
Chris Masone6dcd2d2008-07-17 12:53:50 -04002750 /* once for us */
2751 btrfs_put_ordered_extent(ordered_extent);
2752 /* once for the tree */
2753 btrfs_put_ordered_extent(ordered_extent);
2754
Josef Bacik5fd02042012-05-02 14:00:54 -04002755 return ret;
2756}
2757
2758static void finish_ordered_fn(struct btrfs_work *work)
2759{
2760 struct btrfs_ordered_extent *ordered_extent;
2761 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2762 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002763}
2764
Christoph Hellwigb2950862008-12-02 09:54:17 -05002765static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002766 struct extent_state *state, int uptodate)
2767{
Josef Bacik5fd02042012-05-02 14:00:54 -04002768 struct inode *inode = page->mapping->host;
2769 struct btrfs_root *root = BTRFS_I(inode)->root;
2770 struct btrfs_ordered_extent *ordered_extent = NULL;
2771 struct btrfs_workers *workers;
2772
liubo1abe9b82011-03-24 11:18:59 +00002773 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2774
Chris Mason8b62b722009-09-02 16:53:46 -04002775 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002776 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2777 end - start + 1, uptodate))
2778 return 0;
2779
2780 ordered_extent->work.func = finish_ordered_fn;
2781 ordered_extent->work.flags = 0;
2782
Liu Bo83eea1f2012-07-10 05:28:39 -06002783 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002784 workers = &root->fs_info->endio_freespace_worker;
2785 else
2786 workers = &root->fs_info->endio_write_workers;
2787 btrfs_queue_worker(workers, &ordered_extent->work);
2788
2789 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002790}
2791
Chris Masond352ac62008-09-29 15:18:18 -04002792/*
Chris Masond352ac62008-09-29 15:18:18 -04002793 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002794 * if there's a match, we allow the bio to finish. If not, the code in
2795 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002796 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002797static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2798 u64 phy_offset, struct page *page,
2799 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002800{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002801 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002802 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002803 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002804 char *kaddr;
Chris Masonff79f812007-10-15 16:22:25 -04002805 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiefacc8a222013-07-25 19:22:34 +08002806 u32 csum_expected;
Chris Masonff79f812007-10-15 16:22:25 -04002807 u32 csum = ~(u32)0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002808 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2809 DEFAULT_RATELIMIT_BURST);
Chris Masond1310b22008-01-24 16:13:08 -05002810
Chris Masond20f7042008-12-08 16:58:54 -05002811 if (PageChecked(page)) {
2812 ClearPageChecked(page);
2813 goto good;
2814 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002815
2816 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002817 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002818
Yan Zheng17d217f2008-12-12 10:03:38 -05002819 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002820 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002821 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2822 GFP_NOFS);
2823 return 0;
2824 }
2825
Miao Xiefacc8a222013-07-25 19:22:34 +08002826 phy_offset >>= inode->i_sb->s_blocksize_bits;
2827 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
Chris Masond3977122009-01-05 21:25:51 -05002828
Miao Xiefacc8a222013-07-25 19:22:34 +08002829 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00002830 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
Chris Masonff79f812007-10-15 16:22:25 -04002831 btrfs_csum_final(csum, (char *)&csum);
Miao Xiefacc8a222013-07-25 19:22:34 +08002832 if (csum != csum_expected)
Chris Mason07157aa2007-08-30 08:50:51 -04002833 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002834
Cong Wang7ac687d2011-11-25 23:14:28 +08002835 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002836good:
Chris Mason07157aa2007-08-30 08:50:51 -04002837 return 0;
2838
2839zeroit:
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002840 if (__ratelimit(&_rs))
Miao Xiefacc8a222013-07-25 19:22:34 +08002841 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002842 (unsigned long long)btrfs_ino(page->mapping->host),
Miao Xiefacc8a222013-07-25 19:22:34 +08002843 (unsigned long long)start, csum, csum_expected);
Chris Masondb945352007-10-15 16:15:53 -04002844 memset(kaddr + offset, 1, end - start + 1);
2845 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002846 kunmap_atomic(kaddr);
Miao Xiefacc8a222013-07-25 19:22:34 +08002847 if (csum_expected == 0)
Chris Mason3b951512008-04-17 11:29:12 -04002848 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002849 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002850}
Chris Masonb888db22007-08-27 16:49:44 -04002851
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002852struct delayed_iput {
2853 struct list_head list;
2854 struct inode *inode;
2855};
2856
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002857/* JDM: If this is fs-wide, why can't we add a pointer to
2858 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002859void btrfs_add_delayed_iput(struct inode *inode)
2860{
2861 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2862 struct delayed_iput *delayed;
2863
2864 if (atomic_add_unless(&inode->i_count, -1, 1))
2865 return;
2866
2867 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2868 delayed->inode = inode;
2869
2870 spin_lock(&fs_info->delayed_iput_lock);
2871 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2872 spin_unlock(&fs_info->delayed_iput_lock);
2873}
2874
2875void btrfs_run_delayed_iputs(struct btrfs_root *root)
2876{
2877 LIST_HEAD(list);
2878 struct btrfs_fs_info *fs_info = root->fs_info;
2879 struct delayed_iput *delayed;
2880 int empty;
2881
2882 spin_lock(&fs_info->delayed_iput_lock);
2883 empty = list_empty(&fs_info->delayed_iputs);
2884 spin_unlock(&fs_info->delayed_iput_lock);
2885 if (empty)
2886 return;
2887
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002888 spin_lock(&fs_info->delayed_iput_lock);
2889 list_splice_init(&fs_info->delayed_iputs, &list);
2890 spin_unlock(&fs_info->delayed_iput_lock);
2891
2892 while (!list_empty(&list)) {
2893 delayed = list_entry(list.next, struct delayed_iput, list);
2894 list_del(&delayed->list);
2895 iput(delayed->inode);
2896 kfree(delayed);
2897 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002898}
2899
Yan, Zhengd68fc572010-05-16 10:49:58 -04002900/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002901 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002902 * files in the subvolume, it removes orphan item and frees block_rsv
2903 * structure.
2904 */
2905void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2906 struct btrfs_root *root)
2907{
Josef Bacik90290e12011-12-02 15:44:12 -05002908 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002909 int ret;
2910
Josef Bacik8a35d952012-05-23 14:26:42 -04002911 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002912 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2913 return;
2914
Josef Bacik90290e12011-12-02 15:44:12 -05002915 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002916 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002917 spin_unlock(&root->orphan_lock);
2918 return;
2919 }
2920
2921 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2922 spin_unlock(&root->orphan_lock);
2923 return;
2924 }
2925
2926 block_rsv = root->orphan_block_rsv;
2927 root->orphan_block_rsv = NULL;
2928 spin_unlock(&root->orphan_lock);
2929
Yan, Zhengd68fc572010-05-16 10:49:58 -04002930 if (root->orphan_item_inserted &&
2931 btrfs_root_refs(&root->root_item) > 0) {
2932 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2933 root->root_key.objectid);
2934 BUG_ON(ret);
2935 root->orphan_item_inserted = 0;
2936 }
2937
Josef Bacik90290e12011-12-02 15:44:12 -05002938 if (block_rsv) {
2939 WARN_ON(block_rsv->size > 0);
2940 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002941 }
2942}
2943
2944/*
Josef Bacik7b128762008-07-24 12:17:14 -04002945 * This creates an orphan entry for the given inode in case something goes
2946 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002947 *
2948 * NOTE: caller of this function should reserve 5 units of metadata for
2949 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002950 */
2951int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2952{
2953 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002954 struct btrfs_block_rsv *block_rsv = NULL;
2955 int reserve = 0;
2956 int insert = 0;
2957 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002958
Yan, Zhengd68fc572010-05-16 10:49:58 -04002959 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002960 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002961 if (!block_rsv)
2962 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002963 }
2964
Yan, Zhengd68fc572010-05-16 10:49:58 -04002965 spin_lock(&root->orphan_lock);
2966 if (!root->orphan_block_rsv) {
2967 root->orphan_block_rsv = block_rsv;
2968 } else if (block_rsv) {
2969 btrfs_free_block_rsv(root, block_rsv);
2970 block_rsv = NULL;
2971 }
Josef Bacik7b128762008-07-24 12:17:14 -04002972
Josef Bacik8a35d952012-05-23 14:26:42 -04002973 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2974 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002975#if 0
2976 /*
2977 * For proper ENOSPC handling, we should do orphan
2978 * cleanup when mounting. But this introduces backward
2979 * compatibility issue.
2980 */
2981 if (!xchg(&root->orphan_item_inserted, 1))
2982 insert = 2;
2983 else
2984 insert = 1;
2985#endif
2986 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002987 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002988 }
Josef Bacik7b128762008-07-24 12:17:14 -04002989
Josef Bacik72ac3c02012-05-23 14:13:11 -04002990 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2991 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002992 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002993 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002994
Yan, Zhengd68fc572010-05-16 10:49:58 -04002995 /* grab metadata reservation from transaction handle */
2996 if (reserve) {
2997 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002998 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002999 }
3000
3001 /* insert an orphan item to track this unlinked/truncated file */
3002 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003003 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003004 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003005 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3006 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003007 btrfs_abort_transaction(trans, root, ret);
3008 return ret;
3009 }
3010 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003011 }
3012
3013 /* insert an orphan item to track subvolume contains orphan files */
3014 if (insert >= 2) {
3015 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3016 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003017 if (ret && ret != -EEXIST) {
3018 btrfs_abort_transaction(trans, root, ret);
3019 return ret;
3020 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003021 }
3022 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003023}
3024
3025/*
3026 * We have done the truncate/delete so we can go ahead and remove the orphan
3027 * item for this particular inode.
3028 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003029static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3030 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003031{
3032 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003033 int delete_item = 0;
3034 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003035 int ret = 0;
3036
Yan, Zhengd68fc572010-05-16 10:49:58 -04003037 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003038 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3039 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003040 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003041
Josef Bacik72ac3c02012-05-23 14:13:11 -04003042 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3043 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003044 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003045 spin_unlock(&root->orphan_lock);
3046
3047 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08003048 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04003050 }
3051
Josef Bacik8a35d952012-05-23 14:26:42 -04003052 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003053 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04003054 atomic_dec(&root->orphan_inodes);
3055 }
Josef Bacik7b128762008-07-24 12:17:14 -04003056
Yan, Zhengd68fc572010-05-16 10:49:58 -04003057 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003058}
3059
3060/*
3061 * this cleans up any orphans that may be left on the list from the last use
3062 * of this root.
3063 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003064int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003065{
3066 struct btrfs_path *path;
3067 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003068 struct btrfs_key key, found_key;
3069 struct btrfs_trans_handle *trans;
3070 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003071 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003072 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3073
Yan, Zhengd68fc572010-05-16 10:49:58 -04003074 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003075 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003076
3077 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003078 if (!path) {
3079 ret = -ENOMEM;
3080 goto out;
3081 }
Josef Bacik7b128762008-07-24 12:17:14 -04003082 path->reada = -1;
3083
3084 key.objectid = BTRFS_ORPHAN_OBJECTID;
3085 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3086 key.offset = (u64)-1;
3087
Josef Bacik7b128762008-07-24 12:17:14 -04003088 while (1) {
3089 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003090 if (ret < 0)
3091 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003092
3093 /*
3094 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003095 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003096 * find the key and see if we have stuff that matches
3097 */
3098 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003099 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003100 if (path->slots[0] == 0)
3101 break;
3102 path->slots[0]--;
3103 }
3104
3105 /* pull out the item */
3106 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003107 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3108
3109 /* make sure the item matches what we want */
3110 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3111 break;
3112 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3113 break;
3114
3115 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003116 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003117
3118 /*
3119 * this is where we are basically btrfs_lookup, without the
3120 * crossing root thing. we store the inode number in the
3121 * offset of the orphan item.
3122 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003123
3124 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003125 btrfs_err(root->fs_info,
3126 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003127 ret = -EINVAL;
3128 goto out;
3129 }
3130
3131 last_objectid = found_key.offset;
3132
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003133 found_key.objectid = found_key.offset;
3134 found_key.type = BTRFS_INODE_ITEM_KEY;
3135 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003136 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04003137 ret = PTR_RET(inode);
3138 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003139 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003140
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003141 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3142 struct btrfs_root *dead_root;
3143 struct btrfs_fs_info *fs_info = root->fs_info;
3144 int is_dead_root = 0;
3145
3146 /*
3147 * this is an orphan in the tree root. Currently these
3148 * could come from 2 sources:
3149 * a) a snapshot deletion in progress
3150 * b) a free space cache inode
3151 * We need to distinguish those two, as the snapshot
3152 * orphan must not get deleted.
3153 * find_dead_roots already ran before us, so if this
3154 * is a snapshot deletion, we should find the root
3155 * in the dead_roots list
3156 */
3157 spin_lock(&fs_info->trans_lock);
3158 list_for_each_entry(dead_root, &fs_info->dead_roots,
3159 root_list) {
3160 if (dead_root->root_key.objectid ==
3161 found_key.objectid) {
3162 is_dead_root = 1;
3163 break;
3164 }
3165 }
3166 spin_unlock(&fs_info->trans_lock);
3167 if (is_dead_root) {
3168 /* prevent this orphan from being found again */
3169 key.offset = found_key.objectid - 1;
3170 continue;
3171 }
3172 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003173 /*
3174 * Inode is already gone but the orphan item is still there,
3175 * kill the orphan item.
3176 */
3177 if (ret == -ESTALE) {
3178 trans = btrfs_start_transaction(root, 1);
3179 if (IS_ERR(trans)) {
3180 ret = PTR_ERR(trans);
3181 goto out;
3182 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003183 btrfs_debug(root->fs_info, "auto deleting %Lu",
3184 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003185 ret = btrfs_del_orphan_item(trans, root,
3186 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003187 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04003188 btrfs_end_transaction(trans, root);
3189 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003190 }
Josef Bacik7b128762008-07-24 12:17:14 -04003191
Josef Bacik7b128762008-07-24 12:17:14 -04003192 /*
3193 * add this inode to the orphan list so btrfs_orphan_del does
3194 * the proper thing when we hit it
3195 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003196 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3197 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003198 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003199
Josef Bacik7b128762008-07-24 12:17:14 -04003200 /* if we have links, this was a truncate, lets do that */
3201 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05003202 if (!S_ISREG(inode->i_mode)) {
3203 WARN_ON(1);
3204 iput(inode);
3205 continue;
3206 }
Josef Bacik7b128762008-07-24 12:17:14 -04003207 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003208
3209 /* 1 for the orphan item deletion. */
3210 trans = btrfs_start_transaction(root, 1);
3211 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003212 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003213 ret = PTR_ERR(trans);
3214 goto out;
3215 }
3216 ret = btrfs_orphan_add(trans, inode);
3217 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003218 if (ret) {
3219 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003220 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003221 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003222
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003223 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003224 if (ret)
3225 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003226 } else {
3227 nr_unlink++;
3228 }
3229
3230 /* this will do delete_inode and everything for us */
3231 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003232 if (ret)
3233 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003234 }
Miao Xie3254c872011-11-10 20:45:05 -05003235 /* release the path since we're done with it */
3236 btrfs_release_path(path);
3237
Yan, Zhengd68fc572010-05-16 10:49:58 -04003238 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3239
3240 if (root->orphan_block_rsv)
3241 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3242 (u64)-1);
3243
3244 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003245 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003246 if (!IS_ERR(trans))
3247 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003248 }
Josef Bacik7b128762008-07-24 12:17:14 -04003249
3250 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003251 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003252 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003253 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003254
3255out:
3256 if (ret)
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003257 btrfs_crit(root->fs_info,
3258 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003259 btrfs_free_path(path);
3260 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003261}
3262
Chris Masond352ac62008-09-29 15:18:18 -04003263/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003264 * very simple check to peek ahead in the leaf looking for xattrs. If we
3265 * don't find any xattrs, we know there can't be any acls.
3266 *
3267 * slot is the slot the inode is in, objectid is the objectid of the inode
3268 */
3269static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3270 int slot, u64 objectid)
3271{
3272 u32 nritems = btrfs_header_nritems(leaf);
3273 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003274 static u64 xattr_access = 0;
3275 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003276 int scanned = 0;
3277
Josef Bacikf23b5a52013-06-19 10:16:26 -04003278 if (!xattr_access) {
3279 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3280 strlen(POSIX_ACL_XATTR_ACCESS));
3281 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3282 strlen(POSIX_ACL_XATTR_DEFAULT));
3283 }
3284
Chris Mason46a53cc2009-04-27 11:47:50 -04003285 slot++;
3286 while (slot < nritems) {
3287 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3288
3289 /* we found a different objectid, there must not be acls */
3290 if (found_key.objectid != objectid)
3291 return 0;
3292
3293 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003294 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3295 if (found_key.offset == xattr_access ||
3296 found_key.offset == xattr_default)
3297 return 1;
3298 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003299
3300 /*
3301 * we found a key greater than an xattr key, there can't
3302 * be any acls later on
3303 */
3304 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3305 return 0;
3306
3307 slot++;
3308 scanned++;
3309
3310 /*
3311 * it goes inode, inode backrefs, xattrs, extents,
3312 * so if there are a ton of hard links to an inode there can
3313 * be a lot of backrefs. Don't waste time searching too hard,
3314 * this is just an optimization
3315 */
3316 if (scanned >= 8)
3317 break;
3318 }
3319 /* we hit the end of the leaf before we found an xattr or
3320 * something larger than an xattr. We have to assume the inode
3321 * has acls
3322 */
3323 return 1;
3324}
3325
3326/*
Chris Masond352ac62008-09-29 15:18:18 -04003327 * read an inode from the btree into the in-memory inode
3328 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003329static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003330{
3331 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003332 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003333 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04003334 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04003335 struct btrfs_root *root = BTRFS_I(inode)->root;
3336 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04003337 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003338 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003339 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003340 bool filled = false;
3341
3342 ret = btrfs_fill_inode(inode, &rdev);
3343 if (!ret)
3344 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003345
3346 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003347 if (!path)
3348 goto make_bad;
3349
Josef Bacikd90c7322011-05-17 09:50:54 -04003350 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003351 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003352
Chris Mason39279cc2007-06-12 06:35:45 -04003353 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003354 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003355 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003356
Chris Mason5f39d392007-10-15 16:14:19 -04003357 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003358
3359 if (filled)
3360 goto cache_acl;
3361
Chris Mason5f39d392007-10-15 16:14:19 -04003362 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3363 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003364 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003365 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003366 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3367 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003368 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003369
3370 tspec = btrfs_inode_atime(inode_item);
3371 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3372 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3373
3374 tspec = btrfs_inode_mtime(inode_item);
3375 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3376 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3377
3378 tspec = btrfs_inode_ctime(inode_item);
3379 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3380 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3381
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003382 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003383 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003384 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3385
3386 /*
3387 * If we were modified in the current generation and evicted from memory
3388 * and then re-read we need to do a full sync since we don't have any
3389 * idea about which extents were modified before we were evicted from
3390 * cache.
3391 */
3392 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3393 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3394 &BTRFS_I(inode)->runtime_flags);
3395
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003396 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003397 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003398 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003399 rdev = btrfs_inode_rdev(leaf, inode_item);
3400
Josef Bacikaec74772008-07-24 12:12:38 -04003401 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003402 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00003403cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003404 /*
3405 * try to precache a NULL acl entry for files that don't have
3406 * any xattrs or acls
3407 */
Li Zefan33345d012011-04-20 10:31:50 +08003408 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3409 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04003410 if (!maybe_acls)
3411 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003412
Chris Mason39279cc2007-06-12 06:35:45 -04003413 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003414
Chris Mason39279cc2007-06-12 06:35:45 -04003415 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003416 case S_IFREG:
3417 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003418 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05003419 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003420 inode->i_fop = &btrfs_file_operations;
3421 inode->i_op = &btrfs_file_inode_operations;
3422 break;
3423 case S_IFDIR:
3424 inode->i_fop = &btrfs_dir_file_operations;
3425 if (root == root->fs_info->tree_root)
3426 inode->i_op = &btrfs_dir_ro_inode_operations;
3427 else
3428 inode->i_op = &btrfs_dir_inode_operations;
3429 break;
3430 case S_IFLNK:
3431 inode->i_op = &btrfs_symlink_inode_operations;
3432 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003433 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003434 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003435 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003436 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003437 init_special_inode(inode, inode->i_mode, rdev);
3438 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003439 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003440
3441 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003442 return;
3443
3444make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003445 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003446 make_bad_inode(inode);
3447}
3448
Chris Masond352ac62008-09-29 15:18:18 -04003449/*
3450 * given a leaf and an inode, copy the inode fields into the leaf
3451 */
Chris Masone02119d2008-09-05 16:13:11 -04003452static void fill_inode_item(struct btrfs_trans_handle *trans,
3453 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003454 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003455 struct inode *inode)
3456{
Liu Bo51fab692012-12-27 09:01:21 +00003457 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003458
Liu Bo51fab692012-12-27 09:01:21 +00003459 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003460
Liu Bo51fab692012-12-27 09:01:21 +00003461 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3462 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3463 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3464 &token);
3465 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3466 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003467
Liu Bo51fab692012-12-27 09:01:21 +00003468 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3469 inode->i_atime.tv_sec, &token);
3470 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3471 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003472
Liu Bo51fab692012-12-27 09:01:21 +00003473 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3474 inode->i_mtime.tv_sec, &token);
3475 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3476 inode->i_mtime.tv_nsec, &token);
3477
3478 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3479 inode->i_ctime.tv_sec, &token);
3480 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3481 inode->i_ctime.tv_nsec, &token);
3482
3483 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3484 &token);
3485 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3486 &token);
3487 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3488 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3489 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3490 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3491 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003492}
3493
Chris Masond352ac62008-09-29 15:18:18 -04003494/*
3495 * copy everything in the in-memory inode into the btree.
3496 */
Chris Mason21151332011-11-10 20:39:08 -05003497static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003498 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003499{
3500 struct btrfs_inode_item *inode_item;
3501 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003502 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003503 int ret;
3504
3505 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003506 if (!path)
3507 return -ENOMEM;
3508
Chris Masonb9473432009-03-13 11:00:37 -04003509 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003510 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3511 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003512 if (ret) {
3513 if (ret > 0)
3514 ret = -ENOENT;
3515 goto failed;
3516 }
3517
Chris Masonb4ce94d2009-02-04 09:25:08 -05003518 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003519 leaf = path->nodes[0];
3520 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003521 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003522
Chris Masone02119d2008-09-05 16:13:11 -04003523 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003524 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003525 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003526 ret = 0;
3527failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003528 btrfs_free_path(path);
3529 return ret;
3530}
3531
Chris Masond352ac62008-09-29 15:18:18 -04003532/*
Chris Mason21151332011-11-10 20:39:08 -05003533 * copy everything in the in-memory inode into the btree.
3534 */
3535noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3536 struct btrfs_root *root, struct inode *inode)
3537{
3538 int ret;
3539
3540 /*
3541 * If the inode is a free space inode, we can deadlock during commit
3542 * if we put it into the delayed code.
3543 *
3544 * The data relocation inode should also be directly updated
3545 * without delay
3546 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003547 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05003548 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003549 btrfs_update_root_times(trans, root);
3550
Chris Mason21151332011-11-10 20:39:08 -05003551 ret = btrfs_delayed_update_inode(trans, root, inode);
3552 if (!ret)
3553 btrfs_set_inode_last_trans(trans, inode);
3554 return ret;
3555 }
3556
3557 return btrfs_update_inode_item(trans, root, inode);
3558}
3559
Josef Bacikbe6aef62012-10-22 15:43:12 -04003560noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3561 struct btrfs_root *root,
3562 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003563{
3564 int ret;
3565
3566 ret = btrfs_update_inode(trans, root, inode);
3567 if (ret == -ENOSPC)
3568 return btrfs_update_inode_item(trans, root, inode);
3569 return ret;
3570}
3571
3572/*
Chris Masond352ac62008-09-29 15:18:18 -04003573 * unlink helper that gets used here in inode.c and in the tree logging
3574 * recovery code. It remove a link in a directory with a given name, and
3575 * also drops the back refs in the inode to the directory
3576 */
Al Viro92986792011-03-04 17:14:37 +00003577static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3578 struct btrfs_root *root,
3579 struct inode *dir, struct inode *inode,
3580 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003581{
3582 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003583 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003584 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003585 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003586 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003587 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003588 u64 ino = btrfs_ino(inode);
3589 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003590
3591 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003592 if (!path) {
3593 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003594 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003595 }
3596
Chris Masonb9473432009-03-13 11:00:37 -04003597 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003598 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003599 name, name_len, -1);
3600 if (IS_ERR(di)) {
3601 ret = PTR_ERR(di);
3602 goto err;
3603 }
3604 if (!di) {
3605 ret = -ENOENT;
3606 goto err;
3607 }
Chris Mason5f39d392007-10-15 16:14:19 -04003608 leaf = path->nodes[0];
3609 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003610 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003611 if (ret)
3612 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003613 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003614
Li Zefan33345d012011-04-20 10:31:50 +08003615 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3616 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003617 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003618 btrfs_info(root->fs_info,
3619 "failed to delete reference to %.*s, inode %llu parent %llu",
3620 name_len, name,
3621 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003622 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003623 goto err;
3624 }
3625
Miao Xie16cdcec2011-04-22 18:12:22 +08003626 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003627 if (ret) {
3628 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003629 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003630 }
Chris Mason39279cc2007-06-12 06:35:45 -04003631
Chris Masone02119d2008-09-05 16:13:11 -04003632 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08003633 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003634 if (ret != 0 && ret != -ENOENT) {
3635 btrfs_abort_transaction(trans, root, ret);
3636 goto err;
3637 }
Chris Masone02119d2008-09-05 16:13:11 -04003638
3639 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3640 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04003641 if (ret == -ENOENT)
3642 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003643 else if (ret)
3644 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003645err:
3646 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003647 if (ret)
3648 goto out;
3649
3650 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003651 inode_inc_iversion(inode);
3652 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04003653 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06003654 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003655out:
Chris Mason39279cc2007-06-12 06:35:45 -04003656 return ret;
3657}
3658
Al Viro92986792011-03-04 17:14:37 +00003659int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3660 struct btrfs_root *root,
3661 struct inode *dir, struct inode *inode,
3662 const char *name, int name_len)
3663{
3664 int ret;
3665 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3666 if (!ret) {
3667 btrfs_drop_nlink(inode);
3668 ret = btrfs_update_inode(trans, root, inode);
3669 }
3670 return ret;
3671}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003672
3673/*
3674 * helper to start transaction for unlink and rmdir.
3675 *
Josef Bacikd52be812013-05-29 14:54:47 -04003676 * unlink and rmdir are special in btrfs, they do not always free space, so
3677 * if we cannot make our reservations the normal way try and see if there is
3678 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3679 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003680 */
Josef Bacikd52be812013-05-29 14:54:47 -04003681static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003682{
3683 struct btrfs_trans_handle *trans;
3684 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003685 int ret;
3686
Josef Bacike70bea52011-10-11 14:18:24 -04003687 /*
3688 * 1 for the possible orphan item
3689 * 1 for the dir item
3690 * 1 for the dir index
3691 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003692 * 1 for the inode
3693 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04003694 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003695 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3696 return trans;
3697
Josef Bacikd52be812013-05-29 14:54:47 -04003698 if (PTR_ERR(trans) == -ENOSPC) {
3699 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003700
Josef Bacikd52be812013-05-29 14:54:47 -04003701 trans = btrfs_start_transaction(root, 0);
3702 if (IS_ERR(trans))
3703 return trans;
3704 ret = btrfs_cond_migrate_bytes(root->fs_info,
3705 &root->fs_info->trans_block_rsv,
3706 num_bytes, 5);
3707 if (ret) {
3708 btrfs_end_transaction(trans, root);
3709 return ERR_PTR(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003710 }
Josef Bacik5a77d762011-11-01 14:32:23 -04003711 trans->block_rsv = &root->fs_info->trans_block_rsv;
Josef Bacikd52be812013-05-29 14:54:47 -04003712 trans->bytes_reserved = num_bytes;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003713 }
Josef Bacikd52be812013-05-29 14:54:47 -04003714 return trans;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003715}
3716
Chris Mason39279cc2007-06-12 06:35:45 -04003717static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3718{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003719 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003720 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003721 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003722 int ret;
3723
Josef Bacikd52be812013-05-29 14:54:47 -04003724 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003725 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003726 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003727
Chris Mason12fcfd22009-03-24 10:24:20 -04003728 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3729
Chris Masone02119d2008-09-05 16:13:11 -04003730 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3731 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003732 if (ret)
3733 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003734
Yan, Zhenga22285a2010-05-16 10:48:46 -04003735 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003736 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003737 if (ret)
3738 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003739 }
Josef Bacik7b128762008-07-24 12:17:14 -04003740
Tsutomu Itohb5324022011-07-19 07:27:20 +00003741out:
Josef Bacikd52be812013-05-29 14:54:47 -04003742 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003743 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003744 return ret;
3745}
3746
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003747int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3748 struct btrfs_root *root,
3749 struct inode *dir, u64 objectid,
3750 const char *name, int name_len)
3751{
3752 struct btrfs_path *path;
3753 struct extent_buffer *leaf;
3754 struct btrfs_dir_item *di;
3755 struct btrfs_key key;
3756 u64 index;
3757 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003758 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003759
3760 path = btrfs_alloc_path();
3761 if (!path)
3762 return -ENOMEM;
3763
Li Zefan33345d012011-04-20 10:31:50 +08003764 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003765 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003766 if (IS_ERR_OR_NULL(di)) {
3767 if (!di)
3768 ret = -ENOENT;
3769 else
3770 ret = PTR_ERR(di);
3771 goto out;
3772 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003773
3774 leaf = path->nodes[0];
3775 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3776 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3777 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003778 if (ret) {
3779 btrfs_abort_transaction(trans, root, ret);
3780 goto out;
3781 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003782 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003783
3784 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3785 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003786 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003787 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003788 if (ret != -ENOENT) {
3789 btrfs_abort_transaction(trans, root, ret);
3790 goto out;
3791 }
Li Zefan33345d012011-04-20 10:31:50 +08003792 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003793 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003794 if (IS_ERR_OR_NULL(di)) {
3795 if (!di)
3796 ret = -ENOENT;
3797 else
3798 ret = PTR_ERR(di);
3799 btrfs_abort_transaction(trans, root, ret);
3800 goto out;
3801 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003802
3803 leaf = path->nodes[0];
3804 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003805 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003806 index = key.offset;
3807 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003808 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003809
Miao Xie16cdcec2011-04-22 18:12:22 +08003810 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003811 if (ret) {
3812 btrfs_abort_transaction(trans, root, ret);
3813 goto out;
3814 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003815
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003816 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003817 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003818 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003819 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003820 if (ret)
3821 btrfs_abort_transaction(trans, root, ret);
3822out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003823 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003824 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003825}
3826
Chris Mason39279cc2007-06-12 06:35:45 -04003827static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3828{
3829 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003830 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003831 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003832 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003833
David Sterbab3ae2442012-09-13 16:04:34 -06003834 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003835 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003836 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3837 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003838
Josef Bacikd52be812013-05-29 14:54:47 -04003839 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003840 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003841 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003842
Li Zefan33345d012011-04-20 10:31:50 +08003843 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003844 err = btrfs_unlink_subvol(trans, root, dir,
3845 BTRFS_I(inode)->location.objectid,
3846 dentry->d_name.name,
3847 dentry->d_name.len);
3848 goto out;
3849 }
3850
Josef Bacik7b128762008-07-24 12:17:14 -04003851 err = btrfs_orphan_add(trans, inode);
3852 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003853 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003854
Chris Mason39279cc2007-06-12 06:35:45 -04003855 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003856 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3857 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003858 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003859 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003860out:
Josef Bacikd52be812013-05-29 14:54:47 -04003861 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003862 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003863
Chris Mason39279cc2007-06-12 06:35:45 -04003864 return err;
3865}
3866
Chris Mason323ac952008-10-01 19:05:46 -04003867/*
Chris Mason39279cc2007-06-12 06:35:45 -04003868 * this can truncate away extent items, csum items and directory items.
3869 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003870 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003871 *
3872 * csum items that cross the new i_size are truncated to the new size
3873 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003874 *
3875 * min_type is the minimum key type to truncate down to. If set to 0, this
3876 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003877 */
Yan, Zheng80825102009-11-12 09:35:36 +00003878int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3879 struct btrfs_root *root,
3880 struct inode *inode,
3881 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003882{
Chris Mason39279cc2007-06-12 06:35:45 -04003883 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003884 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003885 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003886 struct btrfs_key key;
3887 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003888 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003889 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003890 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003891 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003892 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003893 int found_extent;
3894 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003895 int pending_del_nr = 0;
3896 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003897 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003898 int ret;
3899 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003900 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003901
3902 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003903
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003904 path = btrfs_alloc_path();
3905 if (!path)
3906 return -ENOMEM;
3907 path->reada = -1;
3908
Josef Bacik5dc562c2012-08-17 13:14:17 -04003909 /*
3910 * We want to drop from the next block forward in case this new size is
3911 * not block aligned since we will be keeping the last block of the
3912 * extent just the way it is.
3913 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003914 if (root->ref_cows || root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00003915 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3916 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003917
Miao Xie16cdcec2011-04-22 18:12:22 +08003918 /*
3919 * This function is also used to drop the items in the log tree before
3920 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3921 * it is used to drop the loged items. So we shouldn't kill the delayed
3922 * items.
3923 */
3924 if (min_type == 0 && root == BTRFS_I(inode)->root)
3925 btrfs_kill_delayed_inode_items(inode);
3926
Li Zefan33345d012011-04-20 10:31:50 +08003927 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003928 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003929 key.type = (u8)-1;
3930
Chris Mason85e21ba2008-01-29 15:11:36 -05003931search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003932 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003933 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003934 if (ret < 0) {
3935 err = ret;
3936 goto out;
3937 }
Chris Masond3977122009-01-05 21:25:51 -05003938
Chris Mason85e21ba2008-01-29 15:11:36 -05003939 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003940 /* there are no items in the tree for us to truncate, we're
3941 * done
3942 */
Yan, Zheng80825102009-11-12 09:35:36 +00003943 if (path->slots[0] == 0)
3944 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003945 path->slots[0]--;
3946 }
3947
Chris Masond3977122009-01-05 21:25:51 -05003948 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003949 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003950 leaf = path->nodes[0];
3951 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3952 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003953
Li Zefan33345d012011-04-20 10:31:50 +08003954 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003955 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003956
Chris Mason85e21ba2008-01-29 15:11:36 -05003957 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003958 break;
3959
Chris Mason5f39d392007-10-15 16:14:19 -04003960 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003961 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003962 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003963 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003964 extent_type = btrfs_file_extent_type(leaf, fi);
3965 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003966 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003967 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003968 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003969 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003970 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003971 }
Yan008630c2007-11-07 13:31:09 -05003972 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003973 }
Yan, Zheng80825102009-11-12 09:35:36 +00003974 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003975 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003976 } else {
3977 if (item_end < new_size)
3978 break;
3979 if (found_key.offset >= new_size)
3980 del_item = 1;
3981 else
3982 del_item = 0;
3983 }
Chris Mason39279cc2007-06-12 06:35:45 -04003984 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003985 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003986 if (found_type != BTRFS_EXTENT_DATA_KEY)
3987 goto delete;
3988
3989 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003990 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003991 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003992 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003993 u64 orig_num_bytes =
3994 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00003995 extent_num_bytes = ALIGN(new_size -
3996 found_key.offset,
3997 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04003998 btrfs_set_file_extent_num_bytes(leaf, fi,
3999 extent_num_bytes);
4000 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004001 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04004002 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004003 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004004 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004005 } else {
Chris Masondb945352007-10-15 16:15:53 -04004006 extent_num_bytes =
4007 btrfs_file_extent_disk_num_bytes(leaf,
4008 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004009 extent_offset = found_key.offset -
4010 btrfs_file_extent_offset(leaf, fi);
4011
Chris Mason39279cc2007-06-12 06:35:45 -04004012 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004013 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004014 if (extent_start != 0) {
4015 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04004016 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004017 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004018 }
4019 }
Chris Mason90692182008-02-08 13:49:28 -05004020 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004021 /*
4022 * we can't truncate inline items that have had
4023 * special encodings
4024 */
4025 if (!del_item &&
4026 btrfs_file_extent_compression(leaf, fi) == 0 &&
4027 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4028 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004029 u32 size = new_size - found_key.offset;
4030
4031 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004032 inode_sub_bytes(inode, item_end + 1 -
4033 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04004034 }
4035 size =
4036 btrfs_file_extent_calc_inline_size(size);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004037 btrfs_truncate_item(root, path, size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04004038 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004039 inode_sub_bytes(inode, item_end + 1 -
4040 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05004041 }
Chris Mason39279cc2007-06-12 06:35:45 -04004042 }
Chris Mason179e29e2007-11-01 11:28:41 -04004043delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004044 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004045 if (!pending_del_nr) {
4046 /* no pending yet, add ourselves */
4047 pending_del_slot = path->slots[0];
4048 pending_del_nr = 1;
4049 } else if (pending_del_nr &&
4050 path->slots[0] + 1 == pending_del_slot) {
4051 /* hop on the pending chunk */
4052 pending_del_nr++;
4053 pending_del_slot = path->slots[0];
4054 } else {
Chris Masond3977122009-01-05 21:25:51 -05004055 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004056 }
Chris Mason39279cc2007-06-12 06:35:45 -04004057 } else {
4058 break;
4059 }
Josef Bacik0af3d002010-06-21 14:48:16 -04004060 if (found_extent && (root->ref_cows ||
4061 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004062 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004063 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004064 extent_num_bytes, 0,
4065 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02004066 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004067 BUG_ON(ret);
4068 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004069
Yan, Zheng80825102009-11-12 09:35:36 +00004070 if (found_type == BTRFS_INODE_ITEM_KEY)
4071 break;
4072
4073 if (path->slots[0] == 0 ||
4074 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00004075 if (pending_del_nr) {
4076 ret = btrfs_del_items(trans, root, path,
4077 pending_del_slot,
4078 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004079 if (ret) {
4080 btrfs_abort_transaction(trans,
4081 root, ret);
4082 goto error;
4083 }
Yan, Zheng80825102009-11-12 09:35:36 +00004084 pending_del_nr = 0;
4085 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004086 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05004087 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004088 } else {
4089 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004090 }
Chris Mason39279cc2007-06-12 06:35:45 -04004091 }
Yan, Zheng80825102009-11-12 09:35:36 +00004092out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004093 if (pending_del_nr) {
4094 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4095 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004096 if (ret)
4097 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004098 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004099error:
Chris Mason39279cc2007-06-12 06:35:45 -04004100 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00004101 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004102}
4103
Chris Masona52d9a82007-08-27 16:49:44 -04004104/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04004105 * btrfs_truncate_page - read, zero a chunk and write a page
4106 * @inode - inode that we're zeroing
4107 * @from - the offset to start zeroing
4108 * @len - the length to zero, 0 to zero the entire range respective to the
4109 * offset
4110 * @front - zero up to the offset instead of from the offset on
4111 *
4112 * This will find the page for the "from" offset and cow the page and zero the
4113 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004114 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04004115int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4116 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004117{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004118 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004119 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004120 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4121 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004122 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004123 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004124 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04004125 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4126 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4127 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004128 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004129 int ret = 0;
4130 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004131 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004132
Josef Bacik2aaa6652012-08-29 14:27:18 -04004133 if ((offset & (blocksize - 1)) == 0 &&
4134 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004135 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004136 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004137 if (ret)
4138 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004139
Chris Mason211c17f2008-05-15 09:13:45 -04004140again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004141 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004142 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004143 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00004144 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004145 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004146 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004147
4148 page_start = page_offset(page);
4149 page_end = page_start + PAGE_CACHE_SIZE - 1;
4150
Chris Masona52d9a82007-08-27 16:49:44 -04004151 if (!PageUptodate(page)) {
4152 ret = btrfs_readpage(NULL, page);
4153 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004154 if (page->mapping != mapping) {
4155 unlock_page(page);
4156 page_cache_release(page);
4157 goto again;
4158 }
Chris Masona52d9a82007-08-27 16:49:44 -04004159 if (!PageUptodate(page)) {
4160 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004161 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004162 }
4163 }
Chris Mason211c17f2008-05-15 09:13:45 -04004164 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004165
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004166 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004167 set_page_extent_mapped(page);
4168
4169 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4170 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004171 unlock_extent_cached(io_tree, page_start, page_end,
4172 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004173 unlock_page(page);
4174 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004175 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004176 btrfs_put_ordered_extent(ordered);
4177 goto again;
4178 }
4179
Josef Bacik2ac55d42010-02-03 19:33:23 +00004180 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004181 EXTENT_DIRTY | EXTENT_DELALLOC |
4182 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004183 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004184
Josef Bacik2ac55d42010-02-03 19:33:23 +00004185 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4186 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004187 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00004188 unlock_extent_cached(io_tree, page_start, page_end,
4189 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004190 goto out_unlock;
4191 }
4192
Chris Masone6dcd2d2008-07-17 12:53:50 -04004193 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004194 if (!len)
4195 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004196 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004197 if (front)
4198 memset(kaddr, 0, offset);
4199 else
4200 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004201 flush_dcache_page(page);
4202 kunmap(page);
4203 }
Chris Mason247e7432008-07-17 12:53:51 -04004204 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004205 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004206 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4207 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004208
Chris Mason89642222008-07-24 09:41:53 -04004209out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004210 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004211 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04004212 unlock_page(page);
4213 page_cache_release(page);
4214out:
4215 return ret;
4216}
4217
Josef Bacik695a0d02011-03-04 15:46:53 -05004218/*
4219 * This function puts in dummy file extents for the area we're creating a hole
4220 * for. So if we are truncating this file to a larger size we need to insert
4221 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4222 * the range between oldsize and size
4223 */
Josef Bacika41ad392011-01-31 15:30:16 -05004224int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004225{
4226 struct btrfs_trans_handle *trans;
4227 struct btrfs_root *root = BTRFS_I(inode)->root;
4228 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004229 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004230 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004231 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004232 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4233 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004234 u64 last_byte;
4235 u64 cur_offset;
4236 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004237 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004238
Josef Bacika71754f2013-06-17 17:14:39 -04004239 /*
4240 * If our size started in the middle of a page we need to zero out the
4241 * rest of the page before we expand the i_size, otherwise we could
4242 * expose stale data.
4243 */
4244 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4245 if (err)
4246 return err;
4247
Yan Zheng9036c102008-10-30 14:19:41 -04004248 if (size <= hole_start)
4249 return 0;
4250
Yan Zheng9036c102008-10-30 14:19:41 -04004251 while (1) {
4252 struct btrfs_ordered_extent *ordered;
4253 btrfs_wait_ordered_range(inode, hole_start,
4254 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004255 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004256 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004257 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4258 if (!ordered)
4259 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004260 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4261 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004262 btrfs_put_ordered_extent(ordered);
4263 }
4264
Yan Zheng9036c102008-10-30 14:19:41 -04004265 cur_offset = hole_start;
4266 while (1) {
4267 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4268 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004269 if (IS_ERR(em)) {
4270 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004271 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004272 break;
4273 }
Yan Zheng9036c102008-10-30 14:19:41 -04004274 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004275 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004276 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004277 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004278 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004279
Miao Xie36423202011-12-14 20:12:02 -05004280 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004281 if (IS_ERR(trans)) {
4282 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05004283 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004284 }
Yan, Zheng80825102009-11-12 09:35:36 +00004285
Josef Bacik5dc562c2012-08-17 13:14:17 -04004286 err = btrfs_drop_extents(trans, root, inode,
4287 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04004288 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04004289 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004290 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004291 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004292 break;
Miao Xie5b397372011-09-11 10:52:24 -04004293 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004294
Yan Zheng9036c102008-10-30 14:19:41 -04004295 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08004296 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04004297 0, hole_size, 0, hole_size,
4298 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04004299 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004300 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04004301 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05004302 break;
Miao Xie5b397372011-09-11 10:52:24 -04004303 }
Yan, Zheng80825102009-11-12 09:35:36 +00004304
Josef Bacik5dc562c2012-08-17 13:14:17 -04004305 btrfs_drop_extent_cache(inode, cur_offset,
4306 cur_offset + hole_size - 1, 0);
4307 hole_em = alloc_extent_map();
4308 if (!hole_em) {
4309 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4310 &BTRFS_I(inode)->runtime_flags);
4311 goto next;
4312 }
4313 hole_em->start = cur_offset;
4314 hole_em->len = hole_size;
4315 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004316
Josef Bacik5dc562c2012-08-17 13:14:17 -04004317 hole_em->block_start = EXTENT_MAP_HOLE;
4318 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004319 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004320 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004321 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4322 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4323 hole_em->generation = trans->transid;
4324
4325 while (1) {
4326 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004327 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004328 write_unlock(&em_tree->lock);
4329 if (err != -EEXIST)
4330 break;
4331 btrfs_drop_extent_cache(inode, cur_offset,
4332 cur_offset +
4333 hole_size - 1, 0);
4334 }
4335 free_extent_map(hole_em);
4336next:
Miao Xie36423202011-12-14 20:12:02 -05004337 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004338 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04004339 }
4340 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004341 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004342 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004343 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004344 break;
4345 }
4346
Yan, Zhenga22285a2010-05-16 10:48:46 -04004347 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004348 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4349 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004350 return err;
4351}
4352
Eric Sandeen3972f262013-01-12 02:57:22 +00004353static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004354{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004355 struct btrfs_root *root = BTRFS_I(inode)->root;
4356 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004357 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004358 loff_t newsize = attr->ia_size;
4359 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004360 int ret;
4361
Eric Sandeen3972f262013-01-12 02:57:22 +00004362 /*
4363 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4364 * special case where we need to update the times despite not having
4365 * these flags set. For all other operations the VFS set these flags
4366 * explicitly if it wants a timestamp update.
4367 */
4368 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4369 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4370
Josef Bacika41ad392011-01-31 15:30:16 -05004371 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05004372 truncate_pagecache(inode, oldsize, newsize);
4373 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004374 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00004375 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004376
Miao Xief4a2f4c2011-12-14 20:12:01 -05004377 trans = btrfs_start_transaction(root, 1);
4378 if (IS_ERR(trans))
4379 return PTR_ERR(trans);
4380
4381 i_size_write(inode, newsize);
4382 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4383 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004384 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004385 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004386
Josef Bacika41ad392011-01-31 15:30:16 -05004387 /*
4388 * We're truncating a file that used to have good data down to
4389 * zero. Make sure it gets into the ordered flush list so that
4390 * any new writes get down to disk quickly.
4391 */
4392 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004393 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4394 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004395
Josef Bacikf3fe8202013-01-07 17:03:21 -05004396 /*
4397 * 1 for the orphan item we're going to add
4398 * 1 for the orphan item deletion.
4399 */
4400 trans = btrfs_start_transaction(root, 2);
4401 if (IS_ERR(trans))
4402 return PTR_ERR(trans);
4403
4404 /*
4405 * We need to do this in case we fail at _any_ point during the
4406 * actual truncate. Once we do the truncate_setsize we could
4407 * invalidate pages which forces any outstanding ordered io to
4408 * be instantly completed which will give us extents that need
4409 * to be truncated. If we fail to get an orphan inode down we
4410 * could have left over extents that were never meant to live,
4411 * so we need to garuntee from this point on that everything
4412 * will be consistent.
4413 */
4414 ret = btrfs_orphan_add(trans, inode);
4415 btrfs_end_transaction(trans, root);
4416 if (ret)
4417 return ret;
4418
Josef Bacika41ad392011-01-31 15:30:16 -05004419 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4420 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004421
4422 /* Disable nonlocked read DIO to avoid the end less truncate */
4423 btrfs_inode_block_unlocked_dio(inode);
4424 inode_dio_wait(inode);
4425 btrfs_inode_resume_unlocked_dio(inode);
4426
Josef Bacika41ad392011-01-31 15:30:16 -05004427 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05004428 if (ret && inode->i_nlink)
4429 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004430 }
4431
Josef Bacika41ad392011-01-31 15:30:16 -05004432 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004433}
4434
Chris Mason39279cc2007-06-12 06:35:45 -04004435static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4436{
4437 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08004438 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004439 int err;
4440
Li Zefanb83cc962010-12-20 16:04:08 +08004441 if (btrfs_root_readonly(root))
4442 return -EROFS;
4443
Chris Mason39279cc2007-06-12 06:35:45 -04004444 err = inode_change_ok(inode, attr);
4445 if (err)
4446 return err;
4447
Chris Mason5a3f23d2009-03-31 13:27:11 -04004448 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004449 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004450 if (err)
4451 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004452 }
Yan Zheng9036c102008-10-30 14:19:41 -04004453
Christoph Hellwig10257742010-06-04 11:30:02 +02004454 if (attr->ia_valid) {
4455 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004456 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004457 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004458
Josef Bacik22c44fe2011-11-30 10:45:38 -05004459 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02004460 err = btrfs_acl_chmod(inode);
4461 }
4462
Chris Mason39279cc2007-06-12 06:35:45 -04004463 return err;
4464}
Chris Mason61295eb2008-01-14 16:24:38 -05004465
Al Virobd555972010-06-07 11:35:40 -04004466void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004467{
4468 struct btrfs_trans_handle *trans;
4469 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04004470 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04004471 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004472 int ret;
4473
liubo1abe9b82011-03-24 11:18:59 +00004474 trace_btrfs_inode_evict(inode);
4475
Chris Mason39279cc2007-06-12 06:35:45 -04004476 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04004477 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06004478 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04004479 goto no_delete;
4480
Chris Mason39279cc2007-06-12 06:35:45 -04004481 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04004482 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004483 goto no_delete;
4484 }
Al Virobd555972010-06-07 11:35:40 -04004485 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04004486 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04004487
Yan, Zhengc71bf092009-11-12 09:34:40 +00004488 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06004489 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04004490 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00004491 goto no_delete;
4492 }
4493
Yan, Zheng76dda932009-09-21 16:00:26 -04004494 if (inode->i_nlink > 0) {
4495 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4496 goto no_delete;
4497 }
4498
Miao Xie0e8c36a2012-12-19 06:59:51 +00004499 ret = btrfs_commit_inode_delayed_inode(inode);
4500 if (ret) {
4501 btrfs_orphan_del(NULL, inode);
4502 goto no_delete;
4503 }
4504
Miao Xie66d8f3d2012-09-06 04:02:28 -06004505 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04004506 if (!rsv) {
4507 btrfs_orphan_del(NULL, inode);
4508 goto no_delete;
4509 }
Josef Bacik4a338542011-08-29 11:01:31 -04004510 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04004511 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04004512 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04004513
Chris Masondbe674a2008-07-17 12:54:05 -04004514 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004515
Josef Bacik4289a662011-08-05 13:22:24 -04004516 /*
Miao Xie8407aa42012-09-07 01:43:32 -06004517 * This is a bit simpler than btrfs_truncate since we've already
4518 * reserved our space for our orphan item in the unlink, so we just
4519 * need to reserve some slack space in case we add bytes and update
4520 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04004521 */
Yan, Zheng80825102009-11-12 09:35:36 +00004522 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00004523 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4524 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00004525
Josef Bacik726c35f2011-09-26 15:46:06 -04004526 /*
4527 * Try and steal from the global reserve since we will
4528 * likely not use this space anyway, we want to try as
4529 * hard as possible to get this to work.
4530 */
4531 if (ret)
4532 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4533
Yan, Zhengd68fc572010-05-16 10:49:58 -04004534 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004535 btrfs_warn(root->fs_info,
4536 "Could not get space for a delete, will truncate on mount %d",
4537 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004538 btrfs_orphan_del(NULL, inode);
4539 btrfs_free_block_rsv(root, rsv);
4540 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004541 }
4542
Miao Xie0e8c36a2012-12-19 06:59:51 +00004543 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004544 if (IS_ERR(trans)) {
4545 btrfs_orphan_del(NULL, inode);
4546 btrfs_free_block_rsv(root, rsv);
4547 goto no_delete;
4548 }
4549
4550 trans->block_rsv = rsv;
4551
Yan, Zhengd68fc572010-05-16 10:49:58 -04004552 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004553 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004554 break;
4555
Miao Xie8407aa42012-09-07 01:43:32 -06004556 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004557 btrfs_end_transaction(trans, root);
4558 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004559 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004560 }
4561
Josef Bacik4289a662011-08-05 13:22:24 -04004562 btrfs_free_block_rsv(root, rsv);
4563
Yan, Zheng80825102009-11-12 09:35:36 +00004564 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004565 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004566 ret = btrfs_orphan_del(trans, inode);
4567 BUG_ON(ret);
4568 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004569
Josef Bacik4289a662011-08-05 13:22:24 -04004570 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004571 if (!(root == root->fs_info->tree_root ||
4572 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004573 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004574
Chris Mason54aa1f42007-06-22 14:16:25 -04004575 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004576 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004577no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00004578 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02004579 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004580 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004581}
4582
4583/*
4584 * this returns the key found in the dir entry in the location pointer.
4585 * If no dir entries were found, location->objectid is 0.
4586 */
4587static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4588 struct btrfs_key *location)
4589{
4590 const char *name = dentry->d_name.name;
4591 int namelen = dentry->d_name.len;
4592 struct btrfs_dir_item *di;
4593 struct btrfs_path *path;
4594 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004595 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004596
4597 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004598 if (!path)
4599 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004600
Li Zefan33345d012011-04-20 10:31:50 +08004601 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004602 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004603 if (IS_ERR(di))
4604 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004605
David Sterbac7040052011-04-19 18:00:01 +02004606 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004607 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004608
Chris Mason5f39d392007-10-15 16:14:19 -04004609 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004610out:
Chris Mason39279cc2007-06-12 06:35:45 -04004611 btrfs_free_path(path);
4612 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004613out_err:
4614 location->objectid = 0;
4615 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004616}
4617
4618/*
4619 * when we hit a tree root in a directory, the btrfs part of the inode
4620 * needs to be changed to reflect the root directory of the tree root. This
4621 * is kind of like crossing a mount point.
4622 */
4623static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004624 struct inode *dir,
4625 struct dentry *dentry,
4626 struct btrfs_key *location,
4627 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004628{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004629 struct btrfs_path *path;
4630 struct btrfs_root *new_root;
4631 struct btrfs_root_ref *ref;
4632 struct extent_buffer *leaf;
4633 int ret;
4634 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004635
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004636 path = btrfs_alloc_path();
4637 if (!path) {
4638 err = -ENOMEM;
4639 goto out;
4640 }
Chris Mason39279cc2007-06-12 06:35:45 -04004641
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004642 err = -ENOENT;
4643 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4644 BTRFS_I(dir)->root->root_key.objectid,
4645 location->objectid);
4646 if (ret) {
4647 if (ret < 0)
4648 err = ret;
4649 goto out;
4650 }
Chris Mason39279cc2007-06-12 06:35:45 -04004651
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004652 leaf = path->nodes[0];
4653 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004654 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004655 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4656 goto out;
4657
4658 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4659 (unsigned long)(ref + 1),
4660 dentry->d_name.len);
4661 if (ret)
4662 goto out;
4663
David Sterbab3b4aa72011-04-21 01:20:15 +02004664 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004665
4666 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4667 if (IS_ERR(new_root)) {
4668 err = PTR_ERR(new_root);
4669 goto out;
4670 }
4671
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004672 *sub_root = new_root;
4673 location->objectid = btrfs_root_dirid(&new_root->root_item);
4674 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004675 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004676 err = 0;
4677out:
4678 btrfs_free_path(path);
4679 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004680}
4681
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004682static void inode_tree_add(struct inode *inode)
4683{
4684 struct btrfs_root *root = BTRFS_I(inode)->root;
4685 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004686 struct rb_node **p;
4687 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004688 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004689
Al Viro1d3382cb2010-10-23 15:19:20 -04004690 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004691 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00004692again:
4693 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004694 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00004695 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004696 while (*p) {
4697 parent = *p;
4698 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4699
Li Zefan33345d012011-04-20 10:31:50 +08004700 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004701 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004702 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004703 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004704 else {
4705 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004706 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004707 rb_erase(parent, &root->inode_tree);
4708 RB_CLEAR_NODE(parent);
4709 spin_unlock(&root->inode_lock);
4710 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004711 }
4712 }
4713 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4714 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4715 spin_unlock(&root->inode_lock);
4716}
4717
4718static void inode_tree_del(struct inode *inode)
4719{
4720 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004721 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004722
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004723 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004724 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004725 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004726 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004727 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004728 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004729 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004730
Josef Bacik0af3d002010-06-21 14:48:16 -04004731 /*
4732 * Free space cache has inodes in the tree root, but the tree root has a
4733 * root_refs of 0, so this could end up dropping the tree root as a
4734 * snapshot, so we need the extra !root->fs_info->tree_root check to
4735 * make sure we don't drop it.
4736 */
4737 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4738 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004739 synchronize_srcu(&root->fs_info->subvol_srcu);
4740 spin_lock(&root->inode_lock);
4741 empty = RB_EMPTY_ROOT(&root->inode_tree);
4742 spin_unlock(&root->inode_lock);
4743 if (empty)
4744 btrfs_add_dead_root(root);
4745 }
4746}
4747
Jeff Mahoney143bede2012-03-01 14:56:26 +01004748void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004749{
4750 struct rb_node *node;
4751 struct rb_node *prev;
4752 struct btrfs_inode *entry;
4753 struct inode *inode;
4754 u64 objectid = 0;
4755
4756 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4757
4758 spin_lock(&root->inode_lock);
4759again:
4760 node = root->inode_tree.rb_node;
4761 prev = NULL;
4762 while (node) {
4763 prev = node;
4764 entry = rb_entry(node, struct btrfs_inode, rb_node);
4765
Li Zefan33345d012011-04-20 10:31:50 +08004766 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004767 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004768 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004769 node = node->rb_right;
4770 else
4771 break;
4772 }
4773 if (!node) {
4774 while (prev) {
4775 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004776 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004777 node = prev;
4778 break;
4779 }
4780 prev = rb_next(prev);
4781 }
4782 }
4783 while (node) {
4784 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004785 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004786 inode = igrab(&entry->vfs_inode);
4787 if (inode) {
4788 spin_unlock(&root->inode_lock);
4789 if (atomic_read(&inode->i_count) > 1)
4790 d_prune_aliases(inode);
4791 /*
Al Viro45321ac2010-06-07 13:43:19 -04004792 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004793 * the inode cache when its usage count
4794 * hits zero.
4795 */
4796 iput(inode);
4797 cond_resched();
4798 spin_lock(&root->inode_lock);
4799 goto again;
4800 }
4801
4802 if (cond_resched_lock(&root->inode_lock))
4803 goto again;
4804
4805 node = rb_next(node);
4806 }
4807 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004808}
4809
Chris Masone02119d2008-09-05 16:13:11 -04004810static int btrfs_init_locked_inode(struct inode *inode, void *p)
4811{
4812 struct btrfs_iget_args *args = p;
4813 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004814 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004815 return 0;
4816}
4817
4818static int btrfs_find_actor(struct inode *inode, void *opaque)
4819{
4820 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004821 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004822 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004823}
4824
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004825static struct inode *btrfs_iget_locked(struct super_block *s,
4826 u64 objectid,
4827 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004828{
4829 struct inode *inode;
4830 struct btrfs_iget_args args;
4831 args.ino = objectid;
4832 args.root = root;
4833
4834 inode = iget5_locked(s, objectid, btrfs_find_actor,
4835 btrfs_init_locked_inode,
4836 (void *)&args);
4837 return inode;
4838}
4839
Balaji Rao1a54ef82008-07-21 02:01:04 +05304840/* Get an inode object given its location and corresponding root.
4841 * Returns in *is_new if the inode was read from disk
4842 */
4843struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004844 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304845{
4846 struct inode *inode;
4847
4848 inode = btrfs_iget_locked(s, location->objectid, root);
4849 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004850 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304851
4852 if (inode->i_state & I_NEW) {
4853 BTRFS_I(inode)->root = root;
4854 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4855 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004856 if (!is_bad_inode(inode)) {
4857 inode_tree_add(inode);
4858 unlock_new_inode(inode);
4859 if (new)
4860 *new = 1;
4861 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004862 unlock_new_inode(inode);
4863 iput(inode);
4864 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004865 }
4866 }
4867
Balaji Rao1a54ef82008-07-21 02:01:04 +05304868 return inode;
4869}
4870
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004871static struct inode *new_simple_dir(struct super_block *s,
4872 struct btrfs_key *key,
4873 struct btrfs_root *root)
4874{
4875 struct inode *inode = new_inode(s);
4876
4877 if (!inode)
4878 return ERR_PTR(-ENOMEM);
4879
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004880 BTRFS_I(inode)->root = root;
4881 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004882 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004883
4884 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004885 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004886 inode->i_fop = &simple_dir_operations;
4887 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4888 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4889
4890 return inode;
4891}
4892
Chris Mason3de45862008-11-17 21:02:50 -05004893struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004894{
Chris Masond3977122009-01-05 21:25:51 -05004895 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004896 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004897 struct btrfs_root *sub_root = root;
4898 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004899 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004900 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004901
4902 if (dentry->d_name.len > BTRFS_NAME_LEN)
4903 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004904
Jeff Layton39e3c952012-11-28 11:30:53 -05004905 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004906 if (ret < 0)
4907 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004908
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004909 if (location.objectid == 0)
4910 return NULL;
4911
4912 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004913 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004914 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004915 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004916
4917 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4918
Yan, Zheng76dda932009-09-21 16:00:26 -04004919 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004920 ret = fixup_tree_root_location(root, dir, dentry,
4921 &location, &sub_root);
4922 if (ret < 0) {
4923 if (ret != -ENOENT)
4924 inode = ERR_PTR(ret);
4925 else
4926 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4927 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004928 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004929 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004930 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4931
Julia Lawall34d19ba2011-01-24 19:55:19 +00004932 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004933 down_read(&root->fs_info->cleanup_work_sem);
4934 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004935 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004936 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04004937 if (ret) {
4938 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004939 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04004940 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00004941 }
4942
Chris Mason3de45862008-11-17 21:02:50 -05004943 return inode;
4944}
4945
Nick Pigginfe15ce42011-01-07 17:49:23 +11004946static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004947{
4948 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004949 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004950
Li Zefan848cce02012-02-21 17:04:28 +08004951 if (!inode && !IS_ROOT(dentry))
4952 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004953
Li Zefan848cce02012-02-21 17:04:28 +08004954 if (inode) {
4955 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004956 if (btrfs_root_refs(&root->root_item) == 0)
4957 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004958
4959 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4960 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004961 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004962 return 0;
4963}
4964
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004965static void btrfs_dentry_release(struct dentry *dentry)
4966{
4967 if (dentry->d_fsdata)
4968 kfree(dentry->d_fsdata);
4969}
4970
Chris Mason3de45862008-11-17 21:02:50 -05004971static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004972 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004973{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004974 struct dentry *ret;
4975
4976 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004977 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004978}
4979
Miao Xie16cdcec2011-04-22 18:12:22 +08004980unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004981 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4982};
4983
Al Viro9cdda8d2013-05-22 16:48:09 -04004984static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04004985{
Al Viro9cdda8d2013-05-22 16:48:09 -04004986 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04004987 struct btrfs_root *root = BTRFS_I(inode)->root;
4988 struct btrfs_item *item;
4989 struct btrfs_dir_item *di;
4990 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004991 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004992 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004993 struct list_head ins_list;
4994 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004995 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004996 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004997 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004998 unsigned char d_type;
4999 int over = 0;
5000 u32 di_cur;
5001 u32 di_total;
5002 u32 di_len;
5003 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005004 char tmp_name[32];
5005 char *name_ptr;
5006 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005007 int is_curr = 0; /* ctx->pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04005008
5009 /* FIXME, use a real flag for deciding about the key type */
5010 if (root->fs_info->tree_root == root)
5011 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005012
Al Viro9cdda8d2013-05-22 16:48:09 -04005013 if (!dir_emit_dots(file, ctx))
5014 return 0;
5015
David Woodhouse49593bf2008-08-17 17:08:36 +01005016 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005017 if (!path)
5018 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005019
Josef Bacik026fd312011-05-13 10:32:11 -04005020 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005021
Miao Xie16cdcec2011-04-22 18:12:22 +08005022 if (key_type == BTRFS_DIR_INDEX_KEY) {
5023 INIT_LIST_HEAD(&ins_list);
5024 INIT_LIST_HEAD(&del_list);
5025 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5026 }
5027
Chris Mason39279cc2007-06-12 06:35:45 -04005028 btrfs_set_key_type(&key, key_type);
Al Viro9cdda8d2013-05-22 16:48:09 -04005029 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005030 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005031
Chris Mason39279cc2007-06-12 06:35:45 -04005032 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5033 if (ret < 0)
5034 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005035
5036 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005037 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005038 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005039 if (slot >= btrfs_header_nritems(leaf)) {
5040 ret = btrfs_next_leaf(root, path);
5041 if (ret < 0)
5042 goto err;
5043 else if (ret > 0)
5044 break;
5045 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005046 }
Chris Mason3de45862008-11-17 21:02:50 -05005047
Chris Mason5f39d392007-10-15 16:14:19 -04005048 item = btrfs_item_nr(leaf, slot);
5049 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5050
5051 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005052 break;
Chris Mason5f39d392007-10-15 16:14:19 -04005053 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005054 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005055 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005056 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005057 if (key_type == BTRFS_DIR_INDEX_KEY &&
5058 btrfs_should_delete_dir_index(&del_list,
5059 found_key.offset))
5060 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005061
Al Viro9cdda8d2013-05-22 16:48:09 -04005062 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005063 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005064
Chris Mason39279cc2007-06-12 06:35:45 -04005065 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5066 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005067 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005068
5069 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005070 struct btrfs_key location;
5071
Josef Bacik22a94d42011-03-16 16:47:17 -04005072 if (verify_dir_item(root, leaf, di))
5073 break;
5074
Chris Mason5f39d392007-10-15 16:14:19 -04005075 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005076 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005077 name_ptr = tmp_name;
5078 } else {
5079 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01005080 if (!name_ptr) {
5081 ret = -ENOMEM;
5082 goto err;
5083 }
Chris Mason5f39d392007-10-15 16:14:19 -04005084 }
5085 read_extent_buffer(leaf, name_ptr,
5086 (unsigned long)(di + 1), name_len);
5087
5088 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5089 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005090
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005091
Chris Mason3de45862008-11-17 21:02:50 -05005092 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005093 * skip it.
5094 *
5095 * In contrast to old kernels, we insert the snapshot's
5096 * dir item and dir index after it has been created, so
5097 * we won't find a reference to our own snapshot. We
5098 * still keep the following code for backward
5099 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005100 */
5101 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5102 location.objectid == root->root_key.objectid) {
5103 over = 0;
5104 goto skip;
5105 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005106 over = !dir_emit(ctx, name_ptr, name_len,
5107 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005108
Chris Mason3de45862008-11-17 21:02:50 -05005109skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005110 if (name_ptr != tmp_name)
5111 kfree(name_ptr);
5112
Chris Mason39279cc2007-06-12 06:35:45 -04005113 if (over)
5114 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05005115 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005116 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005117 di_cur += di_len;
5118 di = (struct btrfs_dir_item *)((char *)di + di_len);
5119 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005120next:
5121 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005122 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005123
Miao Xie16cdcec2011-04-22 18:12:22 +08005124 if (key_type == BTRFS_DIR_INDEX_KEY) {
5125 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005126 ctx->pos++;
5127 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005128 if (ret)
5129 goto nopos;
5130 }
5131
David Woodhouse49593bf2008-08-17 17:08:36 +01005132 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005133 ctx->pos++;
5134
5135 /*
5136 * Stop new entries from being returned after we return the last
5137 * entry.
5138 *
5139 * New directory entries are assigned a strictly increasing
5140 * offset. This means that new entries created during readdir
5141 * are *guaranteed* to be seen in the future by that readdir.
5142 * This has broken buggy programs which operate on names as
5143 * they're returned by readdir. Until we re-use freed offsets
5144 * we have this hack to stop new entries from being returned
5145 * under the assumption that they'll never reach this huge
5146 * offset.
5147 *
5148 * This is being careful not to overflow 32bit loff_t unless the
5149 * last entry requires it because doing so has broken 32bit apps
5150 * in the past.
5151 */
5152 if (key_type == BTRFS_DIR_INDEX_KEY) {
5153 if (ctx->pos >= INT_MAX)
5154 ctx->pos = LLONG_MAX;
5155 else
5156 ctx->pos = INT_MAX;
5157 }
Chris Mason39279cc2007-06-12 06:35:45 -04005158nopos:
5159 ret = 0;
5160err:
Miao Xie16cdcec2011-04-22 18:12:22 +08005161 if (key_type == BTRFS_DIR_INDEX_KEY)
5162 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005163 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005164 return ret;
5165}
5166
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005167int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005168{
5169 struct btrfs_root *root = BTRFS_I(inode)->root;
5170 struct btrfs_trans_handle *trans;
5171 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005172 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005173
Josef Bacik72ac3c02012-05-23 14:13:11 -04005174 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005175 return 0;
5176
Liu Bo83eea1f2012-07-10 05:28:39 -06005177 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005178 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005179
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005180 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005181 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005182 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005183 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005184 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005185 if (IS_ERR(trans))
5186 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005187 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005188 }
5189 return ret;
5190}
5191
5192/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005193 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005194 * inode changes. But, it is most likely to find the inode in cache.
5195 * FIXME, needs more benchmarking...there are no reasons other than performance
5196 * to keep or drop this code.
5197 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005198static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005199{
5200 struct btrfs_root *root = BTRFS_I(inode)->root;
5201 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005202 int ret;
5203
Josef Bacik72ac3c02012-05-23 14:13:11 -04005204 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005205 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005206
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005207 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005208 if (IS_ERR(trans))
5209 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005210
5211 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005212 if (ret && ret == -ENOSPC) {
5213 /* whoops, lets try again with the full transaction */
5214 btrfs_end_transaction(trans, root);
5215 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005216 if (IS_ERR(trans))
5217 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005218
Chris Mason94b60442010-05-26 11:02:00 -04005219 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005220 }
Chris Mason39279cc2007-06-12 06:35:45 -04005221 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005222 if (BTRFS_I(inode)->delayed_node)
5223 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005224
5225 return ret;
5226}
5227
5228/*
5229 * This is a copy of file_update_time. We need this so we can return error on
5230 * ENOSPC for updating the inode in the case of file write and mmap writes.
5231 */
Josef Bacike41f9412012-03-26 09:46:47 -04005232static int btrfs_update_time(struct inode *inode, struct timespec *now,
5233 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005234{
Alexander Block2bc5565282012-06-15 09:49:33 +02005235 struct btrfs_root *root = BTRFS_I(inode)->root;
5236
5237 if (btrfs_root_readonly(root))
5238 return -EROFS;
5239
Josef Bacike41f9412012-03-26 09:46:47 -04005240 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005241 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04005242 if (flags & S_CTIME)
5243 inode->i_ctime = *now;
5244 if (flags & S_MTIME)
5245 inode->i_mtime = *now;
5246 if (flags & S_ATIME)
5247 inode->i_atime = *now;
5248 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005249}
5250
Chris Masond352ac62008-09-29 15:18:18 -04005251/*
5252 * find the highest existing sequence number in a directory
5253 * and then set the in-memory index_cnt variable to reflect
5254 * free sequence numbers
5255 */
Josef Bacikaec74772008-07-24 12:12:38 -04005256static int btrfs_set_inode_index_count(struct inode *inode)
5257{
5258 struct btrfs_root *root = BTRFS_I(inode)->root;
5259 struct btrfs_key key, found_key;
5260 struct btrfs_path *path;
5261 struct extent_buffer *leaf;
5262 int ret;
5263
Li Zefan33345d012011-04-20 10:31:50 +08005264 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005265 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5266 key.offset = (u64)-1;
5267
5268 path = btrfs_alloc_path();
5269 if (!path)
5270 return -ENOMEM;
5271
5272 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5273 if (ret < 0)
5274 goto out;
5275 /* FIXME: we should be able to handle this */
5276 if (ret == 0)
5277 goto out;
5278 ret = 0;
5279
5280 /*
5281 * MAGIC NUMBER EXPLANATION:
5282 * since we search a directory based on f_pos we have to start at 2
5283 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5284 * else has to start at 2
5285 */
5286 if (path->slots[0] == 0) {
5287 BTRFS_I(inode)->index_cnt = 2;
5288 goto out;
5289 }
5290
5291 path->slots[0]--;
5292
5293 leaf = path->nodes[0];
5294 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5295
Li Zefan33345d012011-04-20 10:31:50 +08005296 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04005297 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5298 BTRFS_I(inode)->index_cnt = 2;
5299 goto out;
5300 }
5301
5302 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5303out:
5304 btrfs_free_path(path);
5305 return ret;
5306}
5307
Chris Masond352ac62008-09-29 15:18:18 -04005308/*
5309 * helper to find a free sequence number in a given directory. This current
5310 * code is very simple, later versions will do smarter things in the btree
5311 */
Chris Mason3de45862008-11-17 21:02:50 -05005312int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005313{
5314 int ret = 0;
5315
5316 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08005317 ret = btrfs_inode_delayed_dir_index_count(dir);
5318 if (ret) {
5319 ret = btrfs_set_inode_index_count(dir);
5320 if (ret)
5321 return ret;
5322 }
Josef Bacikaec74772008-07-24 12:12:38 -04005323 }
5324
Chris Mason00e4e6b2008-08-05 11:18:09 -04005325 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04005326 BTRFS_I(dir)->index_cnt++;
5327
5328 return ret;
5329}
5330
Chris Mason39279cc2007-06-12 06:35:45 -04005331static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5332 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005333 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005334 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005335 u64 ref_objectid, u64 objectid,
5336 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005337{
5338 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005339 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005340 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005341 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005342 struct btrfs_inode_ref *ref;
5343 struct btrfs_key key[2];
5344 u32 sizes[2];
5345 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005346 int ret;
5347 int owner;
5348
Chris Mason5f39d392007-10-15 16:14:19 -04005349 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005350 if (!path)
5351 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005352
Chris Mason39279cc2007-06-12 06:35:45 -04005353 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005354 if (!inode) {
5355 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005356 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005357 }
Chris Mason39279cc2007-06-12 06:35:45 -04005358
Li Zefan581bb052011-04-20 10:06:11 +08005359 /*
5360 * we have to initialize this early, so we can reclaim the inode
5361 * number if we fail afterwards in this function.
5362 */
5363 inode->i_ino = objectid;
5364
Josef Bacikaec74772008-07-24 12:12:38 -04005365 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00005366 trace_btrfs_inode_request(dir);
5367
Chris Mason3de45862008-11-17 21:02:50 -05005368 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04005369 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005370 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005371 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005372 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005373 }
Josef Bacikaec74772008-07-24 12:12:38 -04005374 }
5375 /*
5376 * index_cnt is ignored for everything but a dir,
5377 * btrfs_get_inode_index_count has an explanation for the magic
5378 * number
5379 */
5380 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04005381 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005382 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005383 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005384
Josef Bacik5dc562c2012-08-17 13:14:17 -04005385 /*
5386 * We could have gotten an inode number from somebody who was fsynced
5387 * and then removed in this same transaction, so let's just set full
5388 * sync since it will be a full sync anyway and this will blow away the
5389 * old info in the log.
5390 */
5391 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5392
Al Viro569254b2011-07-24 17:08:40 -04005393 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04005394 owner = 0;
5395 else
5396 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005397
5398 key[0].objectid = objectid;
5399 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5400 key[0].offset = 0;
5401
Mark Fashehf1863732012-08-08 11:32:27 -07005402 /*
5403 * Start new inodes with an inode_ref. This is slightly more
5404 * efficient for small numbers of hard links since they will
5405 * be packed into one item. Extended refs will kick in if we
5406 * add more hard links than can fit in the ref item.
5407 */
Chris Mason9c583092008-01-29 15:15:18 -05005408 key[1].objectid = objectid;
5409 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5410 key[1].offset = ref_objectid;
5411
5412 sizes[0] = sizeof(struct btrfs_inode_item);
5413 sizes[1] = name_len + sizeof(*ref);
5414
Chris Masonb9473432009-03-13 11:00:37 -04005415 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05005416 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5417 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04005418 goto fail;
5419
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005420 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005421 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005422 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04005423 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5424 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06005425 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5426 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005427 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005428
5429 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5430 struct btrfs_inode_ref);
5431 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005432 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05005433 ptr = (unsigned long)(ref + 1);
5434 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5435
Chris Mason5f39d392007-10-15 16:14:19 -04005436 btrfs_mark_buffer_dirty(path->nodes[0]);
5437 btrfs_free_path(path);
5438
Chris Mason39279cc2007-06-12 06:35:45 -04005439 location = &BTRFS_I(inode)->location;
5440 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005441 location->offset = 0;
5442 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5443
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005444 btrfs_inherit_iflags(inode, dir);
5445
Al Viro569254b2011-07-24 17:08:40 -04005446 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04005447 if (btrfs_test_opt(root, NODATASUM))
5448 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06005449 if (btrfs_test_opt(root, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005450 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5451 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005452 }
5453
Chris Mason39279cc2007-06-12 06:35:45 -04005454 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005455 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005456
5457 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005458 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005459
Alexander Block8ea05e32012-07-25 17:35:53 +02005460 btrfs_update_root_times(trans, root);
5461
Chris Mason39279cc2007-06-12 06:35:45 -04005462 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005463fail:
Josef Bacikaec74772008-07-24 12:12:38 -04005464 if (dir)
5465 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005466 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005467 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005468 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005469}
5470
5471static inline u8 btrfs_inode_type(struct inode *inode)
5472{
5473 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5474}
5475
Chris Masond352ac62008-09-29 15:18:18 -04005476/*
5477 * utility function to add 'inode' into 'parent_inode' with
5478 * a give name and a given sequence number.
5479 * if 'add_backref' is true, also insert a backref from the
5480 * inode to the parent directory.
5481 */
Chris Masone02119d2008-09-05 16:13:11 -04005482int btrfs_add_link(struct btrfs_trans_handle *trans,
5483 struct inode *parent_inode, struct inode *inode,
5484 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005485{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005486 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005487 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04005488 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08005489 u64 ino = btrfs_ino(inode);
5490 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005491
Li Zefan33345d012011-04-20 10:31:50 +08005492 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005493 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5494 } else {
Li Zefan33345d012011-04-20 10:31:50 +08005495 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005496 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5497 key.offset = 0;
5498 }
Chris Mason39279cc2007-06-12 06:35:45 -04005499
Li Zefan33345d012011-04-20 10:31:50 +08005500 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005501 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5502 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08005503 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005504 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08005505 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5506 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005507 }
5508
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005509 /* Nothing to clean up yet */
5510 if (ret)
5511 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005512
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005513 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5514 parent_inode, &key,
5515 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05005516 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005517 goto fail_dir_item;
5518 else if (ret) {
5519 btrfs_abort_transaction(trans, root, ret);
5520 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005521 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005522
5523 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5524 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005525 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005526 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5527 ret = btrfs_update_inode(trans, root, parent_inode);
5528 if (ret)
5529 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005530 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005531
5532fail_dir_item:
5533 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5534 u64 local_index;
5535 int err;
5536 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5537 key.objectid, root->root_key.objectid,
5538 parent_ino, &local_index, name, name_len);
5539
5540 } else if (add_backref) {
5541 u64 local_index;
5542 int err;
5543
5544 err = btrfs_del_inode_ref(trans, root, name, name_len,
5545 ino, parent_ino, &local_index);
5546 }
5547 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005548}
5549
5550static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005551 struct inode *dir, struct dentry *dentry,
5552 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005553{
Josef Bacika1b075d2010-11-19 20:36:11 +00005554 int err = btrfs_add_link(trans, dir, inode,
5555 dentry->d_name.name, dentry->d_name.len,
5556 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005557 if (err > 0)
5558 err = -EEXIST;
5559 return err;
5560}
5561
Josef Bacik618e21d2007-07-11 10:18:17 -04005562static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005563 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005564{
5565 struct btrfs_trans_handle *trans;
5566 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005567 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005568 int err;
5569 int drop_inode = 0;
5570 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005571 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005572
5573 if (!new_valid_dev(rdev))
5574 return -EINVAL;
5575
Josef Bacik9ed74f22009-09-11 16:12:44 -04005576 /*
5577 * 2 for inode item and ref
5578 * 2 for dir items
5579 * 1 for xattr if selinux is on
5580 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005581 trans = btrfs_start_transaction(root, 5);
5582 if (IS_ERR(trans))
5583 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005584
Li Zefan581bb052011-04-20 10:06:11 +08005585 err = btrfs_find_free_ino(root, &objectid);
5586 if (err)
5587 goto out_unlock;
5588
Josef Bacikaec74772008-07-24 12:12:38 -04005589 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005590 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005591 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005592 if (IS_ERR(inode)) {
5593 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005594 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005595 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005596
Eric Paris2a7dba32011-02-01 11:05:39 -05005597 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005598 if (err) {
5599 drop_inode = 1;
5600 goto out_unlock;
5601 }
5602
Casey Schauflerad19db72011-12-15 10:09:07 -05005603 /*
5604 * If the active LSM wants to access the inode during
5605 * d_instantiate it needs these. Smack checks to see
5606 * if the filesystem supports xattrs by looking at the
5607 * ops vector.
5608 */
5609
5610 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005611 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005612 if (err)
5613 drop_inode = 1;
5614 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005615 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005616 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005617 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005618 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005619out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005620 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005621 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005622 if (drop_inode) {
5623 inode_dec_link_count(inode);
5624 iput(inode);
5625 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005626 return err;
5627}
5628
Chris Mason39279cc2007-06-12 06:35:45 -04005629static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005630 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005631{
5632 struct btrfs_trans_handle *trans;
5633 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005634 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005635 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005636 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005637 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005638 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005639
Josef Bacik9ed74f22009-09-11 16:12:44 -04005640 /*
5641 * 2 for inode item and ref
5642 * 2 for dir items
5643 * 1 for xattr if selinux is on
5644 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005645 trans = btrfs_start_transaction(root, 5);
5646 if (IS_ERR(trans))
5647 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005648
Li Zefan581bb052011-04-20 10:06:11 +08005649 err = btrfs_find_free_ino(root, &objectid);
5650 if (err)
5651 goto out_unlock;
5652
Josef Bacikaec74772008-07-24 12:12:38 -04005653 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005654 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005655 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005656 if (IS_ERR(inode)) {
5657 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005658 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005659 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005660 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005661
Eric Paris2a7dba32011-02-01 11:05:39 -05005662 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005663 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005664 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005665
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005666 err = btrfs_update_inode(trans, root, inode);
5667 if (err)
5668 goto out_unlock;
5669
Casey Schauflerad19db72011-12-15 10:09:07 -05005670 /*
5671 * If the active LSM wants to access the inode during
5672 * d_instantiate it needs these. Smack checks to see
5673 * if the filesystem supports xattrs by looking at the
5674 * ops vector.
5675 */
5676 inode->i_fop = &btrfs_file_operations;
5677 inode->i_op = &btrfs_file_inode_operations;
5678
Josef Bacika1b075d2010-11-19 20:36:11 +00005679 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005680 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005681 goto out_unlock;
5682
5683 inode->i_mapping->a_ops = &btrfs_aops;
5684 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5685 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5686 d_instantiate(dentry, inode);
5687
Chris Mason39279cc2007-06-12 06:35:45 -04005688out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005689 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005690 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005691 inode_dec_link_count(inode);
5692 iput(inode);
5693 }
Liu Bob53d3f52012-11-14 14:34:34 +00005694 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005695 return err;
5696}
5697
5698static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5699 struct dentry *dentry)
5700{
5701 struct btrfs_trans_handle *trans;
5702 struct btrfs_root *root = BTRFS_I(dir)->root;
5703 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005704 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005705 int err;
5706 int drop_inode = 0;
5707
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005708 /* do not allow sys_link's with other subvols of the same device */
5709 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005710 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005711
Mark Fashehf1863732012-08-08 11:32:27 -07005712 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005713 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005714
Chris Mason3de45862008-11-17 21:02:50 -05005715 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005716 if (err)
5717 goto fail;
5718
Yan, Zhenga22285a2010-05-16 10:48:46 -04005719 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005720 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005721 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005722 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005723 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005724 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005725 if (IS_ERR(trans)) {
5726 err = PTR_ERR(trans);
5727 goto fail;
5728 }
Chris Mason5f39d392007-10-15 16:14:19 -04005729
Miao Xie31534952011-04-13 13:19:21 +08005730 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005731 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005732 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005733 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005734 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005735
Josef Bacika1b075d2010-11-19 20:36:11 +00005736 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005737
Yan, Zhenga5719522009-09-24 09:17:31 -04005738 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005739 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005740 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005741 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005742 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005743 if (err)
5744 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005745 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005746 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005747 }
Chris Mason39279cc2007-06-12 06:35:45 -04005748
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005749 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005750fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005751 if (drop_inode) {
5752 inode_dec_link_count(inode);
5753 iput(inode);
5754 }
Liu Bob53d3f52012-11-14 14:34:34 +00005755 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005756 return err;
5757}
5758
Al Viro18bb1db2011-07-26 01:41:39 -04005759static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005760{
Chris Masonb9d86662008-05-02 16:13:49 -04005761 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005762 struct btrfs_trans_handle *trans;
5763 struct btrfs_root *root = BTRFS_I(dir)->root;
5764 int err = 0;
5765 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005766 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005767 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005768
Josef Bacik9ed74f22009-09-11 16:12:44 -04005769 /*
5770 * 2 items for inode and ref
5771 * 2 items for dir items
5772 * 1 for xattr if selinux is on
5773 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005774 trans = btrfs_start_transaction(root, 5);
5775 if (IS_ERR(trans))
5776 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005777
Li Zefan581bb052011-04-20 10:06:11 +08005778 err = btrfs_find_free_ino(root, &objectid);
5779 if (err)
5780 goto out_fail;
5781
Josef Bacikaec74772008-07-24 12:12:38 -04005782 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005783 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005784 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005785 if (IS_ERR(inode)) {
5786 err = PTR_ERR(inode);
5787 goto out_fail;
5788 }
Chris Mason5f39d392007-10-15 16:14:19 -04005789
Chris Mason39279cc2007-06-12 06:35:45 -04005790 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005791
Eric Paris2a7dba32011-02-01 11:05:39 -05005792 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005793 if (err)
5794 goto out_fail;
5795
Chris Mason39279cc2007-06-12 06:35:45 -04005796 inode->i_op = &btrfs_dir_inode_operations;
5797 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005798
Chris Masondbe674a2008-07-17 12:54:05 -04005799 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005800 err = btrfs_update_inode(trans, root, inode);
5801 if (err)
5802 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005803
Josef Bacika1b075d2010-11-19 20:36:11 +00005804 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5805 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005806 if (err)
5807 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005808
Chris Mason39279cc2007-06-12 06:35:45 -04005809 d_instantiate(dentry, inode);
5810 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005811
5812out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005813 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005814 if (drop_on_err)
5815 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005816 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005817 return err;
5818}
5819
Chris Masond352ac62008-09-29 15:18:18 -04005820/* helper for btfs_get_extent. Given an existing extent in the tree,
5821 * and an extent that you want to insert, deal with overlap and insert
5822 * the new extent into the tree.
5823 */
Chris Mason3b951512008-04-17 11:29:12 -04005824static int merge_extent_mapping(struct extent_map_tree *em_tree,
5825 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005826 struct extent_map *em,
5827 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005828{
5829 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005830
Chris Masone6dcd2d2008-07-17 12:53:50 -04005831 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5832 start_diff = map_start - em->start;
5833 em->start = map_start;
5834 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005835 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5836 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005837 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005838 em->block_len -= start_diff;
5839 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005840 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04005841}
5842
Chris Masonc8b97812008-10-29 14:49:59 -04005843static noinline int uncompress_inline(struct btrfs_path *path,
5844 struct inode *inode, struct page *page,
5845 size_t pg_offset, u64 extent_offset,
5846 struct btrfs_file_extent_item *item)
5847{
5848 int ret;
5849 struct extent_buffer *leaf = path->nodes[0];
5850 char *tmp;
5851 size_t max_size;
5852 unsigned long inline_size;
5853 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005854 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005855
5856 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005857 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005858 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5859 inline_size = btrfs_file_extent_inline_item_len(leaf,
5860 btrfs_item_nr(leaf, path->slots[0]));
5861 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005862 if (!tmp)
5863 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005864 ptr = btrfs_file_extent_inline_start(item);
5865
5866 read_extent_buffer(leaf, tmp, ptr, inline_size);
5867
Chris Mason5b050f02008-11-11 09:34:41 -05005868 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005869 ret = btrfs_decompress(compress_type, tmp, page,
5870 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005871 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005872 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005873 unsigned long copy_size = min_t(u64,
5874 PAGE_CACHE_SIZE - pg_offset,
5875 max_size - extent_offset);
5876 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005877 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005878 }
5879 kfree(tmp);
5880 return 0;
5881}
5882
Chris Masond352ac62008-09-29 15:18:18 -04005883/*
5884 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005885 * the ugly parts come from merging extents from the disk with the in-ram
5886 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005887 * where the in-ram extents might be locked pending data=ordered completion.
5888 *
5889 * This also copies inline extents directly into the page.
5890 */
Chris Masond3977122009-01-05 21:25:51 -05005891
Chris Masona52d9a82007-08-27 16:49:44 -04005892struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005893 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005894 int create)
5895{
5896 int ret;
5897 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005898 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005899 u64 extent_start = 0;
5900 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005901 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005902 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005903 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005904 struct btrfs_root *root = BTRFS_I(inode)->root;
5905 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005906 struct extent_buffer *leaf;
5907 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005908 struct extent_map *em = NULL;
5909 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005910 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005911 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005912 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005913
Chris Masona52d9a82007-08-27 16:49:44 -04005914again:
Chris Mason890871b2009-09-02 16:24:52 -04005915 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005916 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005917 if (em)
5918 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005919 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005920
Chris Masona52d9a82007-08-27 16:49:44 -04005921 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005922 if (em->start > start || em->start + em->len <= start)
5923 free_extent_map(em);
5924 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005925 free_extent_map(em);
5926 else
5927 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005928 }
David Sterba172ddd62011-04-21 00:48:27 +02005929 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005930 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005931 err = -ENOMEM;
5932 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005933 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005934 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005935 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005936 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005937 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005938 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005939
5940 if (!path) {
5941 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005942 if (!path) {
5943 err = -ENOMEM;
5944 goto out;
5945 }
5946 /*
5947 * Chances are we'll be called again, so go ahead and do
5948 * readahead
5949 */
5950 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005951 }
5952
Chris Mason179e29e2007-11-01 11:28:41 -04005953 ret = btrfs_lookup_file_extent(trans, root, path,
5954 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005955 if (ret < 0) {
5956 err = ret;
5957 goto out;
5958 }
5959
5960 if (ret != 0) {
5961 if (path->slots[0] == 0)
5962 goto not_found;
5963 path->slots[0]--;
5964 }
5965
Chris Mason5f39d392007-10-15 16:14:19 -04005966 leaf = path->nodes[0];
5967 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005968 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005969 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005970 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5971 found_type = btrfs_key_type(&found_key);
5972 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005973 found_type != BTRFS_EXTENT_DATA_KEY) {
5974 goto not_found;
5975 }
5976
Chris Mason5f39d392007-10-15 16:14:19 -04005977 found_type = btrfs_file_extent_type(leaf, item);
5978 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005979 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005980 if (found_type == BTRFS_FILE_EXTENT_REG ||
5981 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005982 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005983 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005984 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5985 size_t size;
5986 size = btrfs_file_extent_inline_len(leaf, item);
Qu Wenruofda28322013-02-26 08:10:22 +00005987 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04005988 }
5989
5990 if (start >= extent_end) {
5991 path->slots[0]++;
5992 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5993 ret = btrfs_next_leaf(root, path);
5994 if (ret < 0) {
5995 err = ret;
5996 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005997 }
Yan Zheng9036c102008-10-30 14:19:41 -04005998 if (ret > 0)
5999 goto not_found;
6000 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006001 }
Yan Zheng9036c102008-10-30 14:19:41 -04006002 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6003 if (found_key.objectid != objectid ||
6004 found_key.type != BTRFS_EXTENT_DATA_KEY)
6005 goto not_found;
6006 if (start + len <= found_key.offset)
6007 goto not_found;
6008 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006009 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006010 em->len = found_key.offset - start;
6011 goto not_found_em;
6012 }
6013
Josef Bacikcc95bef2013-04-04 14:31:27 -04006014 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04006015 if (found_type == BTRFS_FILE_EXTENT_REG ||
6016 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04006017 em->start = extent_start;
6018 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05006019 em->orig_start = extent_start -
6020 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05006021 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6022 item);
Chris Masondb945352007-10-15 16:15:53 -04006023 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6024 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04006025 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006026 goto insert;
6027 }
Li Zefan261507a02010-12-17 14:21:50 +08006028 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006029 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006030 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006031 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05006032 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04006033 } else {
6034 bytenr += btrfs_file_extent_offset(leaf, item);
6035 em->block_start = bytenr;
6036 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04006037 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6038 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04006039 }
Chris Masona52d9a82007-08-27 16:49:44 -04006040 goto insert;
6041 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006042 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006043 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006044 size_t size;
6045 size_t extent_offset;
6046 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006047
Chris Masona52d9a82007-08-27 16:49:44 -04006048 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04006049 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04006050 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04006051 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04006052 goto out;
6053 }
6054
Yan Zheng9036c102008-10-30 14:19:41 -04006055 size = btrfs_file_extent_inline_len(leaf, item);
6056 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05006057 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04006058 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006059 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006060 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006061 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006062 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08006063 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04006064 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08006065 em->compress_type = compress_type;
6066 }
Yan689f9342007-10-29 11:41:07 -04006067 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006068 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006069 if (btrfs_file_extent_compression(leaf, item) !=
6070 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04006071 ret = uncompress_inline(path, inode, page,
6072 pg_offset,
6073 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006074 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04006075 } else {
6076 map = kmap(page);
6077 read_extent_buffer(leaf, map + pg_offset, ptr,
6078 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04006079 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6080 memset(map + pg_offset + copy_size, 0,
6081 PAGE_CACHE_SIZE - pg_offset -
6082 copy_size);
6083 }
Chris Masonc8b97812008-10-29 14:49:59 -04006084 kunmap(page);
6085 }
Chris Mason179e29e2007-11-01 11:28:41 -04006086 flush_dcache_page(page);
6087 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006088 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006089 if (!trans) {
6090 kunmap(page);
6091 free_extent_map(em);
6092 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006093
David Sterbab3b4aa72011-04-21 01:20:15 +02006094 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006095 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006096
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006097 if (IS_ERR(trans))
6098 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006099 goto again;
6100 }
Chris Masonc8b97812008-10-29 14:49:59 -04006101 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006102 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006103 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006104 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006105 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006106 }
Chris Masond1310b22008-01-24 16:13:08 -05006107 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006108 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006109 goto insert;
6110 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00006111 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04006112 }
6113not_found:
6114 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006115 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006116 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006117not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006118 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006119 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006120insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006121 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006122 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006123 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6124 (unsigned long long)em->start,
6125 (unsigned long long)em->len,
6126 (unsigned long long)start,
6127 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04006128 err = -EIO;
6129 goto out;
6130 }
Chris Masond1310b22008-01-24 16:13:08 -05006131
6132 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006133 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006134 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006135 /* it is possible that someone inserted the extent into the tree
6136 * while we had the lock dropped. It is also possible that
6137 * an overlapping map exists in the tree
6138 */
Chris Masona52d9a82007-08-27 16:49:44 -04006139 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006140 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006141
6142 ret = 0;
6143
Chris Mason3b951512008-04-17 11:29:12 -04006144 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04006145 if (existing && (existing->start > start ||
6146 existing->start + existing->len <= start)) {
6147 free_extent_map(existing);
6148 existing = NULL;
6149 }
Chris Mason3b951512008-04-17 11:29:12 -04006150 if (!existing) {
6151 existing = lookup_extent_mapping(em_tree, em->start,
6152 em->len);
6153 if (existing) {
6154 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006155 em, start,
6156 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04006157 free_extent_map(existing);
6158 if (err) {
6159 free_extent_map(em);
6160 em = NULL;
6161 }
6162 } else {
6163 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04006164 free_extent_map(em);
6165 em = NULL;
6166 }
6167 } else {
6168 free_extent_map(em);
6169 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006170 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006171 }
Chris Masona52d9a82007-08-27 16:49:44 -04006172 }
Chris Mason890871b2009-09-02 16:24:52 -04006173 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006174out:
liubo1abe9b82011-03-24 11:18:59 +00006175
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06006176 if (em)
6177 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00006178
Chris Masonf4219502008-07-22 11:18:09 -04006179 if (path)
6180 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04006181 if (trans) {
6182 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05006183 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04006184 err = ret;
6185 }
Chris Masona52d9a82007-08-27 16:49:44 -04006186 if (err) {
6187 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006188 return ERR_PTR(err);
6189 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006190 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006191 return em;
6192}
6193
Chris Masonec29ed52011-02-23 16:23:20 -05006194struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6195 size_t pg_offset, u64 start, u64 len,
6196 int create)
6197{
6198 struct extent_map *em;
6199 struct extent_map *hole_em = NULL;
6200 u64 range_start = start;
6201 u64 end;
6202 u64 found;
6203 u64 found_end;
6204 int err = 0;
6205
6206 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6207 if (IS_ERR(em))
6208 return em;
6209 if (em) {
6210 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00006211 * if our em maps to
6212 * - a hole or
6213 * - a pre-alloc extent,
6214 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05006215 */
Liu Bof9e4fb52013-01-07 10:10:12 +00006216 if (em->block_start != EXTENT_MAP_HOLE &&
6217 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05006218 return em;
6219 else
6220 hole_em = em;
6221 }
6222
6223 /* check to see if we've wrapped (len == -1 or similar) */
6224 end = start + len;
6225 if (end < start)
6226 end = (u64)-1;
6227 else
6228 end -= 1;
6229
6230 em = NULL;
6231
6232 /* ok, we didn't find anything, lets look for delalloc */
6233 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6234 end, len, EXTENT_DELALLOC, 1);
6235 found_end = range_start + found;
6236 if (found_end < range_start)
6237 found_end = (u64)-1;
6238
6239 /*
6240 * we didn't find anything useful, return
6241 * the original results from get_extent()
6242 */
6243 if (range_start > end || found_end <= start) {
6244 em = hole_em;
6245 hole_em = NULL;
6246 goto out;
6247 }
6248
6249 /* adjust the range_start to make sure it doesn't
6250 * go backwards from the start they passed in
6251 */
6252 range_start = max(start,range_start);
6253 found = found_end - range_start;
6254
6255 if (found > 0) {
6256 u64 hole_start = start;
6257 u64 hole_len = len;
6258
David Sterba172ddd62011-04-21 00:48:27 +02006259 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006260 if (!em) {
6261 err = -ENOMEM;
6262 goto out;
6263 }
6264 /*
6265 * when btrfs_get_extent can't find anything it
6266 * returns one huge hole
6267 *
6268 * make sure what it found really fits our range, and
6269 * adjust to make sure it is based on the start from
6270 * the caller
6271 */
6272 if (hole_em) {
6273 u64 calc_end = extent_map_end(hole_em);
6274
6275 if (calc_end <= start || (hole_em->start > end)) {
6276 free_extent_map(hole_em);
6277 hole_em = NULL;
6278 } else {
6279 hole_start = max(hole_em->start, start);
6280 hole_len = calc_end - hole_start;
6281 }
6282 }
6283 em->bdev = NULL;
6284 if (hole_em && range_start > hole_start) {
6285 /* our hole starts before our delalloc, so we
6286 * have to return just the parts of the hole
6287 * that go until the delalloc starts
6288 */
6289 em->len = min(hole_len,
6290 range_start - hole_start);
6291 em->start = hole_start;
6292 em->orig_start = hole_start;
6293 /*
6294 * don't adjust block start at all,
6295 * it is fixed at EXTENT_MAP_HOLE
6296 */
6297 em->block_start = hole_em->block_start;
6298 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006299 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6300 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006301 } else {
6302 em->start = range_start;
6303 em->len = found;
6304 em->orig_start = range_start;
6305 em->block_start = EXTENT_MAP_DELALLOC;
6306 em->block_len = found;
6307 }
6308 } else if (hole_em) {
6309 return hole_em;
6310 }
6311out:
6312
6313 free_extent_map(hole_em);
6314 if (err) {
6315 free_extent_map(em);
6316 return ERR_PTR(err);
6317 }
6318 return em;
6319}
6320
Josef Bacik4b46fce2010-05-23 11:00:55 -04006321static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6322 u64 start, u64 len)
6323{
6324 struct btrfs_root *root = BTRFS_I(inode)->root;
6325 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04006326 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006327 struct btrfs_key ins;
6328 u64 alloc_hint;
6329 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006330
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006331 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006332 if (IS_ERR(trans))
6333 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006334
6335 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6336
6337 alloc_hint = get_extent_allocation_hint(inode, start, len);
6338 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006339 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006340 if (ret) {
6341 em = ERR_PTR(ret);
6342 goto out;
6343 }
6344
Josef Bacik70c8a912012-10-11 16:54:30 -04006345 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006346 ins.offset, ins.offset, ins.offset, 0);
Josef Bacik70c8a912012-10-11 16:54:30 -04006347 if (IS_ERR(em))
6348 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006349
6350 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6351 ins.offset, ins.offset, 0);
6352 if (ret) {
6353 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6354 em = ERR_PTR(ret);
6355 }
6356out:
6357 btrfs_end_transaction(trans, root);
6358 return em;
6359}
6360
Chris Mason46bfbb52010-05-26 11:04:10 -04006361/*
6362 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6363 * block must be cow'd
6364 */
Josef Bacik7ee9e442013-06-21 16:37:03 -04006365noinline int can_nocow_extent(struct btrfs_trans_handle *trans,
6366 struct inode *inode, u64 offset, u64 *len,
6367 u64 *orig_start, u64 *orig_block_len,
6368 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006369{
6370 struct btrfs_path *path;
6371 int ret;
6372 struct extent_buffer *leaf;
6373 struct btrfs_root *root = BTRFS_I(inode)->root;
6374 struct btrfs_file_extent_item *fi;
6375 struct btrfs_key key;
6376 u64 disk_bytenr;
6377 u64 backref_offset;
6378 u64 extent_end;
6379 u64 num_bytes;
6380 int slot;
6381 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006382 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Chris Mason46bfbb52010-05-26 11:04:10 -04006383 path = btrfs_alloc_path();
6384 if (!path)
6385 return -ENOMEM;
6386
Li Zefan33345d012011-04-20 10:31:50 +08006387 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006388 offset, 0);
6389 if (ret < 0)
6390 goto out;
6391
6392 slot = path->slots[0];
6393 if (ret == 1) {
6394 if (slot == 0) {
6395 /* can't find the item, must cow */
6396 ret = 0;
6397 goto out;
6398 }
6399 slot--;
6400 }
6401 ret = 0;
6402 leaf = path->nodes[0];
6403 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08006404 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006405 key.type != BTRFS_EXTENT_DATA_KEY) {
6406 /* not our file or wrong item type, must cow */
6407 goto out;
6408 }
6409
6410 if (key.offset > offset) {
6411 /* Wrong offset, must cow */
6412 goto out;
6413 }
6414
6415 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6416 found_type = btrfs_file_extent_type(leaf, fi);
6417 if (found_type != BTRFS_FILE_EXTENT_REG &&
6418 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6419 /* not a regular extent, must cow */
6420 goto out;
6421 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006422
6423 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6424 goto out;
6425
Chris Mason46bfbb52010-05-26 11:04:10 -04006426 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006427 if (disk_bytenr == 0)
6428 goto out;
6429
6430 if (btrfs_file_extent_compression(leaf, fi) ||
6431 btrfs_file_extent_encryption(leaf, fi) ||
6432 btrfs_file_extent_other_encoding(leaf, fi))
6433 goto out;
6434
Chris Mason46bfbb52010-05-26 11:04:10 -04006435 backref_offset = btrfs_file_extent_offset(leaf, fi);
6436
Josef Bacik7ee9e442013-06-21 16:37:03 -04006437 if (orig_start) {
6438 *orig_start = key.offset - backref_offset;
6439 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6440 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6441 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006442
Chris Mason46bfbb52010-05-26 11:04:10 -04006443 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason46bfbb52010-05-26 11:04:10 -04006444
6445 if (btrfs_extent_readonly(root, disk_bytenr))
6446 goto out;
6447
6448 /*
6449 * look for other files referencing this extent, if we
6450 * find any we must cow
6451 */
Li Zefan33345d012011-04-20 10:31:50 +08006452 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04006453 key.offset - backref_offset, disk_bytenr))
6454 goto out;
6455
6456 /*
6457 * adjust disk_bytenr and num_bytes to cover just the bytes
6458 * in this extent we are about to write. If there
6459 * are any csums in that range we have to cow in order
6460 * to keep the csums correct
6461 */
6462 disk_bytenr += backref_offset;
6463 disk_bytenr += offset - key.offset;
Josef Bacikeb384b52013-04-24 16:32:55 -04006464 num_bytes = min(offset + *len, extent_end) - offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04006465 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6466 goto out;
6467 /*
6468 * all of the above have passed, it is safe to overwrite this extent
6469 * without cow
6470 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006471 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006472 ret = 1;
6473out:
6474 btrfs_free_path(path);
6475 return ret;
6476}
6477
Josef Bacikeb838e72012-07-31 16:28:48 -04006478static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6479 struct extent_state **cached_state, int writing)
6480{
6481 struct btrfs_ordered_extent *ordered;
6482 int ret = 0;
6483
6484 while (1) {
6485 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6486 0, cached_state);
6487 /*
6488 * We're concerned with the entire range that we're going to be
6489 * doing DIO to, so we need to make sure theres no ordered
6490 * extents in this range.
6491 */
6492 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6493 lockend - lockstart + 1);
6494
6495 /*
6496 * We need to make sure there are no buffered pages in this
6497 * range either, we could have raced between the invalidate in
6498 * generic_file_direct_write and locking the extent. The
6499 * invalidate needs to happen so that reads after a write do not
6500 * get stale data.
6501 */
6502 if (!ordered && (!writing ||
6503 !test_range_bit(&BTRFS_I(inode)->io_tree,
6504 lockstart, lockend, EXTENT_UPTODATE, 0,
6505 *cached_state)))
6506 break;
6507
6508 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6509 cached_state, GFP_NOFS);
6510
6511 if (ordered) {
6512 btrfs_start_ordered_extent(inode, ordered, 1);
6513 btrfs_put_ordered_extent(ordered);
6514 } else {
6515 /* Screw you mmap */
6516 ret = filemap_write_and_wait_range(inode->i_mapping,
6517 lockstart,
6518 lockend);
6519 if (ret)
6520 break;
6521
6522 /*
6523 * If we found a page that couldn't be invalidated just
6524 * fall back to buffered.
6525 */
6526 ret = invalidate_inode_pages2_range(inode->i_mapping,
6527 lockstart >> PAGE_CACHE_SHIFT,
6528 lockend >> PAGE_CACHE_SHIFT);
6529 if (ret)
6530 break;
6531 }
6532
6533 cond_resched();
6534 }
6535
6536 return ret;
6537}
6538
Josef Bacik69ffb542012-09-11 15:40:07 -04006539static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6540 u64 len, u64 orig_start,
6541 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006542 u64 orig_block_len, u64 ram_bytes,
6543 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04006544{
6545 struct extent_map_tree *em_tree;
6546 struct extent_map *em;
6547 struct btrfs_root *root = BTRFS_I(inode)->root;
6548 int ret;
6549
6550 em_tree = &BTRFS_I(inode)->extent_tree;
6551 em = alloc_extent_map();
6552 if (!em)
6553 return ERR_PTR(-ENOMEM);
6554
6555 em->start = start;
6556 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006557 em->mod_start = start;
6558 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006559 em->len = len;
6560 em->block_len = block_len;
6561 em->block_start = block_start;
6562 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006563 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04006564 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04006565 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006566 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6567 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006568 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006569
6570 do {
6571 btrfs_drop_extent_cache(inode, em->start,
6572 em->start + em->len - 1, 0);
6573 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006574 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04006575 write_unlock(&em_tree->lock);
6576 } while (ret == -EEXIST);
6577
6578 if (ret) {
6579 free_extent_map(em);
6580 return ERR_PTR(ret);
6581 }
6582
6583 return em;
6584}
6585
6586
Josef Bacik4b46fce2010-05-23 11:00:55 -04006587static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6588 struct buffer_head *bh_result, int create)
6589{
6590 struct extent_map *em;
6591 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006592 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006593 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006594 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006595 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006596 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006597 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006598 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006599
Miao Xie172a5042013-02-21 02:48:22 -07006600 if (create)
Josef Bacikeb838e72012-07-31 16:28:48 -04006601 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07006602 else
Josef Bacikc3298612012-08-03 16:49:19 -04006603 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006604
Josef Bacikc3298612012-08-03 16:49:19 -04006605 lockstart = start;
6606 lockend = start + len - 1;
6607
Josef Bacikeb838e72012-07-31 16:28:48 -04006608 /*
6609 * If this errors out it's because we couldn't invalidate pagecache for
6610 * this range and we need to fallback to buffered.
6611 */
6612 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6613 return -ENOTBLK;
6614
Josef Bacik4b46fce2010-05-23 11:00:55 -04006615 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006616 if (IS_ERR(em)) {
6617 ret = PTR_ERR(em);
6618 goto unlock_err;
6619 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006620
6621 /*
6622 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6623 * io. INLINE is special, and we could probably kludge it in here, but
6624 * it's still buffered so for safety lets just fall back to the generic
6625 * buffered path.
6626 *
6627 * For COMPRESSED we _have_ to read the entire extent in so we can
6628 * decompress it, so there will be buffering required no matter what we
6629 * do, so go ahead and fallback to buffered.
6630 *
6631 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6632 * to buffered IO. Don't blame me, this is the price we pay for using
6633 * the generic code.
6634 */
6635 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6636 em->block_start == EXTENT_MAP_INLINE) {
6637 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006638 ret = -ENOTBLK;
6639 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006640 }
6641
6642 /* Just a good old fashioned hole, return */
6643 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6644 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6645 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006646 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006647 }
6648
6649 /*
6650 * We don't allocate a new extent in the following cases
6651 *
6652 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6653 * existing extent.
6654 * 2) The extent is marked as PREALLOC. We're good to go here and can
6655 * just use the extent.
6656 *
6657 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006658 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006659 len = min(len, em->len - (start - em->start));
6660 lockstart = start + len;
6661 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006662 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006663
6664 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6665 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6666 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006667 int type;
6668 int ret;
Josef Bacikeb384b52013-04-24 16:32:55 -04006669 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006670
6671 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6672 type = BTRFS_ORDERED_PREALLOC;
6673 else
6674 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006675 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006676 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006677
6678 /*
6679 * we're not going to log anything, but we do need
6680 * to make sure the current transaction stays open
6681 * while we look for nocow cross refs
6682 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006683 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006684 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006685 goto must_cow;
6686
Josef Bacik7ee9e442013-06-21 16:37:03 -04006687 if (can_nocow_extent(trans, inode, start, &len, &orig_start,
6688 &orig_block_len, &ram_bytes) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006689 if (type == BTRFS_ORDERED_PREALLOC) {
6690 free_extent_map(em);
6691 em = create_pinned_em(inode, start, len,
6692 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006693 block_start, len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04006694 orig_block_len,
6695 ram_bytes, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006696 if (IS_ERR(em)) {
6697 btrfs_end_transaction(trans, root);
6698 goto unlock_err;
6699 }
6700 }
6701
Chris Mason46bfbb52010-05-26 11:04:10 -04006702 ret = btrfs_add_ordered_extent_dio(inode, start,
6703 block_start, len, len, type);
6704 btrfs_end_transaction(trans, root);
6705 if (ret) {
6706 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006707 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006708 }
6709 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006710 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006711 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006712 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006713must_cow:
6714 /*
6715 * this will cow the extent, reset the len in case we changed
6716 * it above
6717 */
6718 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006719 free_extent_map(em);
6720 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006721 if (IS_ERR(em)) {
6722 ret = PTR_ERR(em);
6723 goto unlock_err;
6724 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006725 len = min(len, em->len - (start - em->start));
6726unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006727 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6728 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006729 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006730 bh_result->b_bdev = em->bdev;
6731 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006732 if (create) {
6733 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6734 set_buffer_new(bh_result);
6735
6736 /*
6737 * Need to update the i_size under the extent lock so buffered
6738 * readers will get the updated i_size when we unlock.
6739 */
6740 if (start + len > i_size_read(inode))
6741 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006742
Miao Xie172a5042013-02-21 02:48:22 -07006743 spin_lock(&BTRFS_I(inode)->lock);
6744 BTRFS_I(inode)->outstanding_extents++;
6745 spin_unlock(&BTRFS_I(inode)->lock);
6746
Miao Xie09348562013-02-07 10:12:07 +00006747 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6748 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6749 &cached_state, GFP_NOFS);
6750 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006751 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006752
Josef Bacikeb838e72012-07-31 16:28:48 -04006753 /*
6754 * In the case of write we need to clear and unlock the entire range,
6755 * in the case of read we need to unlock only the end area that we
6756 * aren't using if there is any left over space.
6757 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006758 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006759 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6760 lockend, unlock_bits, 1, 0,
6761 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006762 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006763 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006764 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006765
Josef Bacik4b46fce2010-05-23 11:00:55 -04006766 free_extent_map(em);
6767
6768 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006769
6770unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006771 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6772 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6773 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006774}
6775
Josef Bacik4b46fce2010-05-23 11:00:55 -04006776static void btrfs_endio_direct_read(struct bio *bio, int err)
6777{
Miao Xiee65e1532010-11-22 03:04:43 +00006778 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006779 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6780 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006781 struct inode *inode = dip->inode;
6782 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9be33952013-05-17 18:30:14 -04006783 struct bio *dio_bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08006784 u32 *csums = (u32 *)dip->csum;
6785 int index = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006786 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006787
6788 start = dip->logical_offset;
6789 do {
6790 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6791 struct page *page = bvec->bv_page;
6792 char *kaddr;
6793 u32 csum = ~(u32)0;
6794 unsigned long flags;
6795
6796 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006797 kaddr = kmap_atomic(page);
Liu Bob0496682013-03-14 14:57:45 +00006798 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006799 csum, bvec->bv_len);
6800 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006801 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006802 local_irq_restore(flags);
6803
6804 flush_dcache_page(bvec->bv_page);
Miao Xiefacc8a222013-07-25 19:22:34 +08006805 if (csum != csums[index]) {
6806 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
6807 (unsigned long long)btrfs_ino(inode),
6808 (unsigned long long)start,
6809 csum, csums[index]);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006810 err = -EIO;
6811 }
6812 }
6813
6814 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006815 bvec++;
Miao Xiefacc8a222013-07-25 19:22:34 +08006816 index++;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006817 } while (bvec <= bvec_end);
6818
6819 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006820 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04006821 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006822
Josef Bacik4b46fce2010-05-23 11:00:55 -04006823 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006824
6825 /* If we had a csum failure make sure to clear the uptodate flag */
6826 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006827 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6828 dio_end_io(dio_bio, err);
6829 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006830}
6831
6832static void btrfs_endio_direct_write(struct bio *bio, int err)
6833{
6834 struct btrfs_dio_private *dip = bio->bi_private;
6835 struct inode *inode = dip->inode;
6836 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006837 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006838 u64 ordered_offset = dip->logical_offset;
6839 u64 ordered_bytes = dip->bytes;
Chris Mason9be33952013-05-17 18:30:14 -04006840 struct bio *dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006841 int ret;
6842
6843 if (err)
6844 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006845again:
6846 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6847 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006848 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006849 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006850 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006851
Josef Bacik5fd02042012-05-02 14:00:54 -04006852 ordered->work.func = finish_ordered_fn;
6853 ordered->work.flags = 0;
6854 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6855 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006856out_test:
6857 /*
6858 * our bio might span multiple ordered extents. If we haven't
6859 * completed the accounting for the whole dio, go back and try again
6860 */
6861 if (ordered_offset < dip->logical_offset + dip->bytes) {
6862 ordered_bytes = dip->logical_offset + dip->bytes -
6863 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006864 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006865 goto again;
6866 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006867out_done:
Chris Mason9be33952013-05-17 18:30:14 -04006868 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006869
Josef Bacik4b46fce2010-05-23 11:00:55 -04006870 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006871
6872 /* If we had an error make sure to clear the uptodate flag */
6873 if (err)
Chris Mason9be33952013-05-17 18:30:14 -04006874 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6875 dio_end_io(dio_bio, err);
6876 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006877}
6878
Chris Masoneaf25d92010-05-25 09:48:28 -04006879static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6880 struct bio *bio, int mirror_num,
6881 unsigned long bio_flags, u64 offset)
6882{
6883 int ret;
6884 struct btrfs_root *root = BTRFS_I(inode)->root;
6885 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006886 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006887 return 0;
6888}
6889
Miao Xiee65e1532010-11-22 03:04:43 +00006890static void btrfs_end_dio_bio(struct bio *bio, int err)
6891{
6892 struct btrfs_dio_private *dip = bio->bi_private;
6893
6894 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006895 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006896 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006897 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006898 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006899 dip->errors = 1;
6900
6901 /*
6902 * before atomic variable goto zero, we must make sure
6903 * dip->errors is perceived to be set.
6904 */
6905 smp_mb__before_atomic_dec();
6906 }
6907
6908 /* if there are more bios still pending for this dio, just exit */
6909 if (!atomic_dec_and_test(&dip->pending_bios))
6910 goto out;
6911
Chris Mason9be33952013-05-17 18:30:14 -04006912 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00006913 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04006914 } else {
6915 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
Miao Xiee65e1532010-11-22 03:04:43 +00006916 bio_endio(dip->orig_bio, 0);
6917 }
6918out:
6919 bio_put(bio);
6920}
6921
6922static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6923 u64 first_sector, gfp_t gfp_flags)
6924{
6925 int nr_vecs = bio_get_nr_vecs(bdev);
6926 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6927}
6928
6929static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6930 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006931 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006932{
Miao Xiefacc8a222013-07-25 19:22:34 +08006933 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00006934 int write = rw & REQ_WRITE;
6935 struct btrfs_root *root = BTRFS_I(inode)->root;
6936 int ret;
6937
Josef Bacikb812ce22012-11-16 13:56:32 -05006938 if (async_submit)
6939 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6940
Miao Xiee65e1532010-11-22 03:04:43 +00006941 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006942
6943 if (!write) {
6944 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6945 if (ret)
6946 goto err;
6947 }
Miao Xiee65e1532010-11-22 03:04:43 +00006948
Josef Bacik1ae39932011-04-06 14:41:34 -04006949 if (skip_sum)
6950 goto map;
6951
6952 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006953 ret = btrfs_wq_submit_bio(root->fs_info,
6954 inode, rw, bio, 0, 0,
6955 file_offset,
6956 __btrfs_submit_bio_start_direct_io,
6957 __btrfs_submit_bio_done);
6958 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006959 } else if (write) {
6960 /*
6961 * If we aren't doing async submit, calculate the csum of the
6962 * bio now.
6963 */
6964 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6965 if (ret)
6966 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006967 } else if (!skip_sum) {
Miao Xiefacc8a222013-07-25 19:22:34 +08006968 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
6969 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006970 if (ret)
6971 goto err;
6972 }
Miao Xiee65e1532010-11-22 03:04:43 +00006973
Josef Bacik1ae39932011-04-06 14:41:34 -04006974map:
6975 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006976err:
6977 bio_put(bio);
6978 return ret;
6979}
6980
6981static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6982 int skip_sum)
6983{
6984 struct inode *inode = dip->inode;
6985 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006986 struct bio *bio;
6987 struct bio *orig_bio = dip->orig_bio;
6988 struct bio_vec *bvec = orig_bio->bi_io_vec;
6989 u64 start_sector = orig_bio->bi_sector;
6990 u64 file_offset = dip->logical_offset;
6991 u64 submit_len = 0;
6992 u64 map_length;
6993 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006994 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006995 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006996
Miao Xiee65e1532010-11-22 03:04:43 +00006997 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05006998 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006999 &map_length, NULL, 0);
7000 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04007001 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007002 return -EIO;
7003 }
Miao Xiefacc8a222013-07-25 19:22:34 +08007004
Josef Bacik02f57c72011-04-06 14:25:44 -04007005 if (map_length >= orig_bio->bi_size) {
7006 bio = orig_bio;
7007 goto submit;
7008 }
7009
David Woodhouse53b381b2013-01-29 18:40:14 -05007010 /* async crcs make it difficult to collect full stripe writes. */
7011 if (btrfs_get_alloc_profile(root, 1) &
7012 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7013 async_submit = 0;
7014 else
7015 async_submit = 1;
7016
Josef Bacik02f57c72011-04-06 14:25:44 -04007017 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7018 if (!bio)
7019 return -ENOMEM;
7020 bio->bi_private = dip;
7021 bio->bi_end_io = btrfs_end_dio_bio;
7022 atomic_inc(&dip->pending_bios);
7023
Miao Xiee65e1532010-11-22 03:04:43 +00007024 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7025 if (unlikely(map_length < submit_len + bvec->bv_len ||
7026 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7027 bvec->bv_offset) < bvec->bv_len)) {
7028 /*
7029 * inc the count before we submit the bio so
7030 * we know the end IO handler won't happen before
7031 * we inc the count. Otherwise, the dip might get freed
7032 * before we're done setting it up
7033 */
7034 atomic_inc(&dip->pending_bios);
7035 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7036 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007037 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007038 if (ret) {
7039 bio_put(bio);
7040 atomic_dec(&dip->pending_bios);
7041 goto out_err;
7042 }
7043
Miao Xiee65e1532010-11-22 03:04:43 +00007044 start_sector += submit_len >> 9;
7045 file_offset += submit_len;
7046
7047 submit_len = 0;
7048 nr_pages = 0;
7049
7050 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7051 start_sector, GFP_NOFS);
7052 if (!bio)
7053 goto out_err;
7054 bio->bi_private = dip;
7055 bio->bi_end_io = btrfs_end_dio_bio;
7056
7057 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05007058 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01007059 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00007060 &map_length, NULL, 0);
7061 if (ret) {
7062 bio_put(bio);
7063 goto out_err;
7064 }
7065 } else {
7066 submit_len += bvec->bv_len;
7067 nr_pages ++;
7068 bvec++;
7069 }
7070 }
7071
Josef Bacik02f57c72011-04-06 14:25:44 -04007072submit:
Miao Xiee65e1532010-11-22 03:04:43 +00007073 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04007074 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00007075 if (!ret)
7076 return 0;
7077
7078 bio_put(bio);
7079out_err:
7080 dip->errors = 1;
7081 /*
7082 * before atomic variable goto zero, we must
7083 * make sure dip->errors is perceived to be set.
7084 */
7085 smp_mb__before_atomic_dec();
7086 if (atomic_dec_and_test(&dip->pending_bios))
7087 bio_io_error(dip->orig_bio);
7088
7089 /* bio_end_io() will handle error, so we needn't return it */
7090 return 0;
7091}
7092
Chris Mason9be33952013-05-17 18:30:14 -04007093static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7094 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007095{
7096 struct btrfs_root *root = BTRFS_I(inode)->root;
7097 struct btrfs_dio_private *dip;
Chris Mason9be33952013-05-17 18:30:14 -04007098 struct bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007099 int skip_sum;
Miao Xiefacc8a222013-07-25 19:22:34 +08007100 int sum_len;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02007101 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007102 int ret = 0;
Miao Xiefacc8a222013-07-25 19:22:34 +08007103 u16 csum_size;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007104
7105 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7106
Chris Mason9be33952013-05-17 18:30:14 -04007107 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007108 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007109 ret = -ENOMEM;
7110 goto free_ordered;
7111 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007112
Miao Xiefacc8a222013-07-25 19:22:34 +08007113 if (!skip_sum && !write) {
7114 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7115 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7116 sum_len *= csum_size;
7117 } else {
7118 sum_len = 0;
7119 }
7120
7121 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04007122 if (!dip) {
7123 ret = -ENOMEM;
7124 goto free_io_bio;
7125 }
7126
7127 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007128 dip->inode = inode;
7129 dip->logical_offset = file_offset;
Miao Xiee6da5d2e2013-06-19 18:19:17 +08007130 dip->bytes = dio_bio->bi_size;
Chris Mason9be33952013-05-17 18:30:14 -04007131 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7132 io_bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007133 dip->errors = 0;
Chris Mason9be33952013-05-17 18:30:14 -04007134 dip->orig_bio = io_bio;
7135 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00007136 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007137
7138 if (write)
Chris Mason9be33952013-05-17 18:30:14 -04007139 io_bio->bi_end_io = btrfs_endio_direct_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007140 else
Chris Mason9be33952013-05-17 18:30:14 -04007141 io_bio->bi_end_io = btrfs_endio_direct_read;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007142
Miao Xiee65e1532010-11-22 03:04:43 +00007143 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7144 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04007145 return;
Chris Mason9be33952013-05-17 18:30:14 -04007146
7147free_io_bio:
7148 bio_put(io_bio);
7149
Josef Bacik4b46fce2010-05-23 11:00:55 -04007150free_ordered:
7151 /*
7152 * If this is a write, we need to clean up the reserved space and kill
7153 * the ordered extent.
7154 */
7155 if (write) {
7156 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05007157 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007158 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7159 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7160 btrfs_free_reserved_extent(root, ordered->start,
7161 ordered->disk_len);
7162 btrfs_put_ordered_extent(ordered);
7163 btrfs_put_ordered_extent(ordered);
7164 }
Chris Mason9be33952013-05-17 18:30:14 -04007165 bio_endio(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007166}
7167
Chris Mason5a5f79b2010-05-26 21:33:37 -04007168static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7169 const struct iovec *iov, loff_t offset,
7170 unsigned long nr_segs)
7171{
7172 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007173 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007174 size_t size;
7175 unsigned long addr;
7176 unsigned blocksize_mask = root->sectorsize - 1;
7177 ssize_t retval = -EINVAL;
7178 loff_t end = offset;
7179
7180 if (offset & blocksize_mask)
7181 goto out;
7182
7183 /* Check the memory alignment. Blocks cannot straddle pages */
7184 for (seg = 0; seg < nr_segs; seg++) {
7185 addr = (unsigned long)iov[seg].iov_base;
7186 size = iov[seg].iov_len;
7187 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00007188 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007189 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007190
7191 /* If this is a write we don't need to check anymore */
7192 if (rw & WRITE)
7193 continue;
7194
7195 /*
7196 * Check to make sure we don't have duplicate iov_base's in this
7197 * iovec, if so return EINVAL, otherwise we'll get csum errors
7198 * when reading back.
7199 */
7200 for (i = seg + 1; i < nr_segs; i++) {
7201 if (iov[seg].iov_base == iov[i].iov_base)
7202 goto out;
7203 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007204 }
7205 retval = 0;
7206out:
7207 return retval;
7208}
Josef Bacikeb838e72012-07-31 16:28:48 -04007209
Chris Mason16432982008-04-10 10:23:21 -04007210static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7211 const struct iovec *iov, loff_t offset,
7212 unsigned long nr_segs)
7213{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007214 struct file *file = iocb->ki_filp;
7215 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00007216 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007217 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007218 bool wakeup = true;
7219 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007220 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007221
Chris Mason5a5f79b2010-05-26 21:33:37 -04007222 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04007223 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007224 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007225
Miao Xie38851cc2013-02-08 07:04:11 +00007226 atomic_inc(&inode->i_dio_count);
7227 smp_mb__after_atomic_inc();
7228
Josef Bacik0e267c42013-07-02 10:38:02 -04007229 /*
7230 * The generic stuff only does filemap_write_and_wait_range, which isn't
7231 * enough if we've written compressed pages to this area, so we need to
7232 * call btrfs_wait_ordered_range to make absolutely sure that any
7233 * outstanding dirty pages are on disk.
7234 */
7235 count = iov_length(iov, nr_segs);
7236 btrfs_wait_ordered_range(inode, offset, count);
7237
Miao Xie09348562013-02-07 10:12:07 +00007238 if (rw & WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007239 /*
7240 * If the write DIO is beyond the EOF, we need update
7241 * the isize, but it is protected by i_mutex. So we can
7242 * not unlock the i_mutex at this case.
7243 */
7244 if (offset + count <= inode->i_size) {
7245 mutex_unlock(&inode->i_mutex);
7246 relock = true;
7247 }
Miao Xie09348562013-02-07 10:12:07 +00007248 ret = btrfs_delalloc_reserve_space(inode, count);
7249 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007250 goto out;
7251 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7252 &BTRFS_I(inode)->runtime_flags))) {
7253 inode_dio_done(inode);
7254 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7255 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007256 }
7257
7258 ret = __blockdev_direct_IO(rw, iocb, inode,
7259 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7260 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00007261 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00007262 if (rw & WRITE) {
7263 if (ret < 0 && ret != -EIOCBQUEUED)
7264 btrfs_delalloc_release_space(inode, count);
Miao Xie172a5042013-02-21 02:48:22 -07007265 else if (ret >= 0 && (size_t)ret < count)
Miao Xie09348562013-02-07 10:12:07 +00007266 btrfs_delalloc_release_space(inode,
7267 count - (size_t)ret);
Miao Xie172a5042013-02-21 02:48:22 -07007268 else
7269 btrfs_delalloc_release_metadata(inode, 0);
Miao Xie09348562013-02-07 10:12:07 +00007270 }
Miao Xie38851cc2013-02-08 07:04:11 +00007271out:
Miao Xie2e60a512013-02-08 07:01:08 +00007272 if (wakeup)
7273 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007274 if (relock)
7275 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00007276
7277 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007278}
7279
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007280#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7281
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007282static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7283 __u64 start, __u64 len)
7284{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007285 int ret;
7286
7287 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7288 if (ret)
7289 return ret;
7290
Chris Masonec29ed52011-02-23 16:23:20 -05007291 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007292}
7293
Chris Mason9ebefb182007-06-15 13:50:00 -04007294int btrfs_readpage(struct file *file, struct page *page)
7295{
Chris Masond1310b22008-01-24 16:13:08 -05007296 struct extent_io_tree *tree;
7297 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02007298 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007299}
Chris Mason1832a6d2007-12-21 16:27:21 -05007300
Chris Mason39279cc2007-06-12 06:35:45 -04007301static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7302{
Chris Masond1310b22008-01-24 16:13:08 -05007303 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04007304
7305
7306 if (current->flags & PF_MEMALLOC) {
7307 redirty_page_for_writepage(wbc, page);
7308 unlock_page(page);
7309 return 0;
7310 }
Chris Masond1310b22008-01-24 16:13:08 -05007311 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04007312 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7313}
Chris Mason39279cc2007-06-12 06:35:45 -04007314
Eric Sandeen48a3b632013-04-25 20:41:01 +00007315static int btrfs_writepages(struct address_space *mapping,
7316 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007317{
Chris Masond1310b22008-01-24 16:13:08 -05007318 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05007319
Chris Masond1310b22008-01-24 16:13:08 -05007320 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04007321 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7322}
7323
Chris Mason3ab2fb52007-11-08 10:59:22 -05007324static int
7325btrfs_readpages(struct file *file, struct address_space *mapping,
7326 struct list_head *pages, unsigned nr_pages)
7327{
Chris Masond1310b22008-01-24 16:13:08 -05007328 struct extent_io_tree *tree;
7329 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05007330 return extent_readpages(tree, mapping, pages, nr_pages,
7331 btrfs_get_extent);
7332}
Chris Masone6dcd2d2008-07-17 12:53:50 -04007333static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007334{
Chris Masond1310b22008-01-24 16:13:08 -05007335 struct extent_io_tree *tree;
7336 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04007337 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04007338
Chris Masond1310b22008-01-24 16:13:08 -05007339 tree = &BTRFS_I(page->mapping->host)->io_tree;
7340 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05007341 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007342 if (ret == 1) {
7343 ClearPagePrivate(page);
7344 set_page_private(page, 0);
7345 page_cache_release(page);
7346 }
7347 return ret;
7348}
Chris Mason39279cc2007-06-12 06:35:45 -04007349
Chris Masone6dcd2d2008-07-17 12:53:50 -04007350static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7351{
Chris Mason98509cf2008-09-11 15:51:43 -04007352 if (PageWriteback(page) || PageDirty(page))
7353 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05007354 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007355}
7356
Lukas Czernerd47992f2013-05-21 23:17:23 -04007357static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7358 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007359{
Josef Bacik5fd02042012-05-02 14:00:54 -04007360 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007361 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007362 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007363 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007364 u64 page_start = page_offset(page);
7365 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007366
Chris Mason8b62b722009-09-02 16:53:46 -04007367 /*
7368 * we have the page locked, so new writeback can't start,
7369 * and the dirty bit won't be cleared while we are here.
7370 *
7371 * Wait for IO on this page so that we can safely clear
7372 * the PagePrivate2 bit and do ordered accounting
7373 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007374 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04007375
Josef Bacik5fd02042012-05-02 14:00:54 -04007376 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007377 if (offset) {
7378 btrfs_releasepage(page, GFP_NOFS);
7379 return;
7380 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007381 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00007382 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04007383 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04007384 /*
7385 * IO on this page will never be started, so we need
7386 * to account for any ordered extents now
7387 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007388 clear_extent_bit(tree, page_start, page_end,
7389 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007390 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7391 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04007392 /*
7393 * whoever cleared the private bit is responsible
7394 * for the finish_ordered_io
7395 */
Josef Bacik5fd02042012-05-02 14:00:54 -04007396 if (TestClearPagePrivate2(page) &&
7397 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7398 PAGE_CACHE_SIZE, 1)) {
7399 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04007400 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007401 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007402 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007403 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007404 }
7405 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04007406 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06007407 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7408 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007409 __btrfs_releasepage(page, GFP_NOFS);
7410
Chris Mason4a096752008-07-21 10:29:44 -04007411 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04007412 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04007413 ClearPagePrivate(page);
7414 set_page_private(page, 0);
7415 page_cache_release(page);
7416 }
Chris Mason39279cc2007-06-12 06:35:45 -04007417}
7418
Chris Mason9ebefb182007-06-15 13:50:00 -04007419/*
7420 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7421 * called from a page fault handler when a page is first dirtied. Hence we must
7422 * be careful to check for EOF conditions here. We set the page up correctly
7423 * for a written page which means we get ENOSPC checking when writing into
7424 * holes and correct delalloc and unwritten extent mapping on filesystems that
7425 * support these features.
7426 *
7427 * We are not allowed to take the i_mutex here so we have to play games to
7428 * protect against truncate races as the page could now be beyond EOF. Because
7429 * vmtruncate() writes the inode size before removing pages, once we have the
7430 * page lock we can determine safely if the page is beyond EOF. If it is not
7431 * beyond EOF, then the page is guaranteed safe against truncation until we
7432 * unlock the page.
7433 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07007434int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04007435{
Nick Pigginc2ec1752009-03-31 15:23:21 -07007436 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05007437 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05007438 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007439 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7440 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007441 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007442 char *kaddr;
7443 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04007444 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05007445 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05007446 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007447 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007448 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04007449
Jan Karab2b5ef52012-06-12 16:20:45 +02007450 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007451 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007452 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04007453 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05007454 reserved = 1;
7455 }
Nick Piggin56a76f82009-03-31 15:23:23 -07007456 if (ret) {
7457 if (ret == -ENOMEM)
7458 ret = VM_FAULT_OOM;
7459 else /* -ENOSPC, -EIO, etc */
7460 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05007461 if (reserved)
7462 goto out;
7463 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07007464 }
Chris Mason1832a6d2007-12-21 16:27:21 -05007465
Nick Piggin56a76f82009-03-31 15:23:23 -07007466 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007467again:
Chris Mason9ebefb182007-06-15 13:50:00 -04007468 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04007469 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007470 page_start = page_offset(page);
7471 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04007472
Chris Mason9ebefb182007-06-15 13:50:00 -04007473 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04007474 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04007475 /* page got truncated out from underneath us */
7476 goto out_unlock;
7477 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007478 wait_on_page_writeback(page);
7479
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007480 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007481 set_page_extent_mapped(page);
7482
Chris Masoneb84ae02008-07-17 13:53:27 -04007483 /*
7484 * we can't set the delalloc bits if there are pending ordered
7485 * extents. Drop our locks and wait for them to finish
7486 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04007487 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7488 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007489 unlock_extent_cached(io_tree, page_start, page_end,
7490 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007491 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04007492 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007493 btrfs_put_ordered_extent(ordered);
7494 goto again;
7495 }
7496
Josef Bacikfbf19082009-10-01 17:10:23 -04007497 /*
7498 * XXX - page_mkwrite gets called every time the page is dirtied, even
7499 * if it was already dirty, so for space accounting reasons we need to
7500 * clear any delalloc bits for the range we are fixing to save. There
7501 * is probably a better way to do this, but for now keep consistent with
7502 * prepare_pages in the normal write path.
7503 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007504 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06007505 EXTENT_DIRTY | EXTENT_DELALLOC |
7506 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00007507 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04007508
Josef Bacik2ac55d42010-02-03 19:33:23 +00007509 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7510 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007511 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00007512 unlock_extent_cached(io_tree, page_start, page_end,
7513 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007514 ret = VM_FAULT_SIGBUS;
7515 goto out_unlock;
7516 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04007517 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04007518
7519 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04007520 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04007521 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04007522 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04007523 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04007524
Chris Masone6dcd2d2008-07-17 12:53:50 -04007525 if (zero_start != PAGE_CACHE_SIZE) {
7526 kaddr = kmap(page);
7527 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7528 flush_dcache_page(page);
7529 kunmap(page);
7530 }
Chris Mason247e7432008-07-17 12:53:51 -04007531 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007532 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04007533 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007534
Chris Mason257c62e2009-10-13 13:21:08 -04007535 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7536 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06007537 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04007538
Josef Bacik2ac55d42010-02-03 19:33:23 +00007539 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04007540
7541out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02007542 if (!ret) {
7543 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04007544 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02007545 }
Chris Mason9ebefb182007-06-15 13:50:00 -04007546 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05007547out:
Josef Bacikec39e182012-01-12 19:10:12 -05007548 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05007549out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02007550 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04007551 return ret;
7552}
7553
Josef Bacika41ad392011-01-31 15:30:16 -05007554static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007555{
7556 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007557 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04007558 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05007559 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007560 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007561 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007562 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007563
Chris Mason4a096752008-07-21 10:29:44 -04007564 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007565 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007566
Josef Bacikfcb80c22011-05-03 10:40:22 -04007567 /*
7568 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7569 * 3 things going on here
7570 *
7571 * 1) We need to reserve space for our orphan item and the space to
7572 * delete our orphan item. Lord knows we don't want to have a dangling
7573 * orphan item because we didn't reserve space to remove it.
7574 *
7575 * 2) We need to reserve space to update our inode.
7576 *
7577 * 3) We need to have something to cache all the space that is going to
7578 * be free'd up by the truncate operation, but also have some slack
7579 * space reserved in case it uses space during the truncate (thank you
7580 * very much snapshotting).
7581 *
7582 * And we need these to all be seperate. The fact is we can use alot of
7583 * space doing the truncate, and we have no earthly idea how much space
7584 * we will use, so we need the truncate reservation to be seperate so it
7585 * doesn't end up using space reserved for updating the inode or
7586 * removing the orphan item. We also need to be able to stop the
7587 * transaction and start a new one, which means we need to be able to
7588 * update the inode several times, and we have no idea of knowing how
7589 * many times that will be, so we can't just reserve 1 item for the
7590 * entirety of the opration, so that has to be done seperately as well.
7591 * Then there is the orphan item, which does indeed need to be held on
7592 * to for the whole operation, and we need nobody to touch this reserved
7593 * space except the orphan code.
7594 *
7595 * So that leaves us with
7596 *
7597 * 1) root->orphan_block_rsv - for the orphan deletion.
7598 * 2) rsv - for the truncate reservation, which we will steal from the
7599 * transaction reservation.
7600 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7601 * updating the inode.
7602 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007603 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007604 if (!rsv)
7605 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007606 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007607 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007608
Josef Bacik907cbce2011-08-08 13:46:15 -04007609 /*
Josef Bacik07127182011-08-19 10:29:59 -04007610 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007611 * 1 for updating the inode.
7612 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007613 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007614 if (IS_ERR(trans)) {
7615 err = PTR_ERR(trans);
7616 goto out;
7617 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007618
Josef Bacik907cbce2011-08-08 13:46:15 -04007619 /* Migrate the slack space for the truncate to our reserve */
7620 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7621 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007622 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007623
Chris Mason5a3f23d2009-03-31 13:27:11 -04007624 /*
7625 * setattr is responsible for setting the ordered_data_close flag,
7626 * but that is only tested during the last file release. That
7627 * could happen well after the next commit, leaving a great big
7628 * window where new writes may get lost if someone chooses to write
7629 * to this file after truncating to zero
7630 *
7631 * The inode doesn't have any dirty data here, and so if we commit
7632 * this is a noop. If someone immediately starts writing to the inode
7633 * it is very likely we'll catch some of their writes in this
7634 * transaction, and the commit will find this file on the ordered
7635 * data list with good things to send down.
7636 *
7637 * This is a best effort solution, there is still a window where
7638 * using truncate to replace the contents of the file will
7639 * end up with a zero length file after a crash.
7640 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007641 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7642 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007643 btrfs_add_ordered_operation(trans, root, inode);
7644
Josef Bacik5dc562c2012-08-17 13:14:17 -04007645 /*
7646 * So if we truncate and then write and fsync we normally would just
7647 * write the extents that changed, which is a problem if we need to
7648 * first truncate that entire inode. So set this flag so we write out
7649 * all of the extents in the inode to the sync log so we're completely
7650 * safe.
7651 */
7652 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007653 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007654
Yan, Zheng80825102009-11-12 09:35:36 +00007655 while (1) {
7656 ret = btrfs_truncate_inode_items(trans, root, inode,
7657 inode->i_size,
7658 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007659 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007660 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007661 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007662 }
Chris Mason39279cc2007-06-12 06:35:45 -04007663
Josef Bacikfcb80c22011-05-03 10:40:22 -04007664 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007665 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007666 if (ret) {
7667 err = ret;
7668 break;
7669 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007670
Yan, Zheng80825102009-11-12 09:35:36 +00007671 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007672 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007673
7674 trans = btrfs_start_transaction(root, 2);
7675 if (IS_ERR(trans)) {
7676 ret = err = PTR_ERR(trans);
7677 trans = NULL;
7678 break;
7679 }
7680
7681 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7682 rsv, min_size);
7683 BUG_ON(ret); /* shouldn't happen */
7684 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007685 }
7686
7687 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007688 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007689 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007690 if (ret)
7691 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007692 }
7693
Chris Mason917c16b2011-11-08 14:49:59 -05007694 if (trans) {
7695 trans->block_rsv = &root->fs_info->trans_block_rsv;
7696 ret = btrfs_update_inode(trans, root, inode);
7697 if (ret && !err)
7698 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007699
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007700 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007701 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007702 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007703
7704out:
7705 btrfs_free_block_rsv(root, rsv);
7706
Josef Bacik3893e332011-01-31 16:03:11 -05007707 if (ret && !err)
7708 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007709
Josef Bacik3893e332011-01-31 16:03:11 -05007710 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007711}
7712
Sven Wegener3b963622008-06-09 21:57:42 -04007713/*
Chris Masond352ac62008-09-29 15:18:18 -04007714 * create a new subvolume directory/inode (helper for the ioctl).
7715 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007716int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007717 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007718{
Chris Mason39279cc2007-06-12 06:35:45 -04007719 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007720 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007721 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007722
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007723 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7724 new_dirid, new_dirid,
7725 S_IFDIR | (~current_umask() & S_IRWXUGO),
7726 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007727 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007728 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007729 inode->i_op = &btrfs_dir_inode_operations;
7730 inode->i_fop = &btrfs_dir_file_operations;
7731
Miklos Szeredibfe86842011-10-28 14:13:29 +02007732 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007733 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007734
Yan, Zheng76dda932009-09-21 16:00:26 -04007735 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007736
Yan, Zheng76dda932009-09-21 16:00:26 -04007737 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007738 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007739}
7740
Chris Mason39279cc2007-06-12 06:35:45 -04007741struct inode *btrfs_alloc_inode(struct super_block *sb)
7742{
7743 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007744 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007745
7746 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7747 if (!ei)
7748 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007749
7750 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007751 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007752 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007753 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007754 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007755 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007756 ei->disk_i_size = 0;
7757 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007758 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007759 ei->index_cnt = (u64)-1;
7760 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007761 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007762
Josef Bacik9e0baf62011-07-15 15:16:44 +00007763 spin_lock_init(&ei->lock);
7764 ei->outstanding_extents = 0;
7765 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007766
Josef Bacik72ac3c02012-05-23 14:13:11 -04007767 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007768 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007769
Miao Xie16cdcec2011-04-22 18:12:22 +08007770 ei->delayed_node = NULL;
7771
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007772 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007773 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007774 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7775 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007776 ei->io_tree.track_uptodate = 1;
7777 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007778 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007779 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007780 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007781 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007782 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007783 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007784 RB_CLEAR_NODE(&ei->rb_node);
7785
7786 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007787}
7788
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007789static void btrfs_i_callback(struct rcu_head *head)
7790{
7791 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007792 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7793}
7794
Chris Mason39279cc2007-06-12 06:35:45 -04007795void btrfs_destroy_inode(struct inode *inode)
7796{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007797 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007798 struct btrfs_root *root = BTRFS_I(inode)->root;
7799
Al Virob3d9b7a2012-06-09 13:51:19 -04007800 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007801 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007802 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7803 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007804 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7805 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007806
Chris Mason5a3f23d2009-03-31 13:27:11 -04007807 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007808 * This can happen where we create an inode, but somebody else also
7809 * created the same inode and we need to destroy the one we already
7810 * created.
7811 */
7812 if (!root)
7813 goto free;
7814
7815 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007816 * Make sure we're properly removed from the ordered operation
7817 * lists.
7818 */
7819 smp_mb();
7820 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
Miao Xie199c2a92013-05-15 07:48:23 +00007821 spin_lock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007822 list_del_init(&BTRFS_I(inode)->ordered_operations);
Miao Xie199c2a92013-05-15 07:48:23 +00007823 spin_unlock(&root->fs_info->ordered_root_lock);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007824 }
7825
Josef Bacik8a35d952012-05-23 14:26:42 -04007826 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7827 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007828 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7829 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007830 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007831 }
Josef Bacik7b128762008-07-24 12:17:14 -04007832
Chris Masond3977122009-01-05 21:25:51 -05007833 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007834 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7835 if (!ordered)
7836 break;
7837 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007838 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7839 (unsigned long long)ordered->file_offset,
7840 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007841 btrfs_remove_ordered_extent(inode, ordered);
7842 btrfs_put_ordered_extent(ordered);
7843 btrfs_put_ordered_extent(ordered);
7844 }
7845 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007846 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007847 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007848free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007849 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007850}
7851
Al Viro45321ac2010-06-07 13:43:19 -04007852int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007853{
7854 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007855
Naohiro Aota6379ef92013-06-06 09:56:34 +00007856 if (root == NULL)
7857 return 1;
7858
Liu Bofa6ac872013-02-20 14:10:23 +00007859 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007860 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007861 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007862 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007863 else
Al Viro45321ac2010-06-07 13:43:19 -04007864 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007865}
7866
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007867static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007868{
7869 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7870
7871 inode_init_once(&ei->vfs_inode);
7872}
7873
7874void btrfs_destroy_cachep(void)
7875{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007876 /*
7877 * Make sure all delayed rcu free inodes are flushed before we
7878 * destroy cache.
7879 */
7880 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007881 if (btrfs_inode_cachep)
7882 kmem_cache_destroy(btrfs_inode_cachep);
7883 if (btrfs_trans_handle_cachep)
7884 kmem_cache_destroy(btrfs_trans_handle_cachep);
7885 if (btrfs_transaction_cachep)
7886 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007887 if (btrfs_path_cachep)
7888 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007889 if (btrfs_free_space_cachep)
7890 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007891 if (btrfs_delalloc_work_cachep)
7892 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007893}
7894
7895int btrfs_init_cachep(void)
7896{
David Sterba837e1972012-09-07 03:00:48 -06007897 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007898 sizeof(struct btrfs_inode), 0,
7899 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007900 if (!btrfs_inode_cachep)
7901 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007902
David Sterba837e1972012-09-07 03:00:48 -06007903 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007904 sizeof(struct btrfs_trans_handle), 0,
7905 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007906 if (!btrfs_trans_handle_cachep)
7907 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007908
David Sterba837e1972012-09-07 03:00:48 -06007909 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007910 sizeof(struct btrfs_transaction), 0,
7911 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007912 if (!btrfs_transaction_cachep)
7913 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007914
David Sterba837e1972012-09-07 03:00:48 -06007915 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007916 sizeof(struct btrfs_path), 0,
7917 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007918 if (!btrfs_path_cachep)
7919 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007920
David Sterba837e1972012-09-07 03:00:48 -06007921 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007922 sizeof(struct btrfs_free_space), 0,
7923 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7924 if (!btrfs_free_space_cachep)
7925 goto fail;
7926
Miao Xie8ccf6f192012-10-25 09:28:04 +00007927 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7928 sizeof(struct btrfs_delalloc_work), 0,
7929 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7930 NULL);
7931 if (!btrfs_delalloc_work_cachep)
7932 goto fail;
7933
Chris Mason39279cc2007-06-12 06:35:45 -04007934 return 0;
7935fail:
7936 btrfs_destroy_cachep();
7937 return -ENOMEM;
7938}
7939
7940static int btrfs_getattr(struct vfsmount *mnt,
7941 struct dentry *dentry, struct kstat *stat)
7942{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007943 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007944 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007945 u32 blocksize = inode->i_sb->s_blocksize;
7946
Chris Mason39279cc2007-06-12 06:35:45 -04007947 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007948 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007949 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007950
7951 spin_lock(&BTRFS_I(inode)->lock);
7952 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7953 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007954 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007955 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007956 return 0;
7957}
7958
Chris Masond3977122009-01-05 21:25:51 -05007959static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7960 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007961{
7962 struct btrfs_trans_handle *trans;
7963 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007964 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007965 struct inode *new_inode = new_dentry->d_inode;
7966 struct inode *old_inode = old_dentry->d_inode;
7967 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007968 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007969 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007970 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007971 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007972
Li Zefan33345d012011-04-20 10:31:50 +08007973 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007974 return -EPERM;
7975
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007976 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007977 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007978 return -EXDEV;
7979
Li Zefan33345d012011-04-20 10:31:50 +08007980 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7981 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007982 return -ENOTEMPTY;
7983
Chris Mason39279cc2007-06-12 06:35:45 -04007984 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007985 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007986 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007987
7988
7989 /* check for collisions, even if the name isn't there */
7990 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7991 new_dentry->d_name.name,
7992 new_dentry->d_name.len);
7993
7994 if (ret) {
7995 if (ret == -EEXIST) {
7996 /* we shouldn't get
7997 * eexist without a new_inode */
7998 if (!new_inode) {
7999 WARN_ON(1);
8000 return ret;
8001 }
8002 } else {
8003 /* maybe -EOVERFLOW */
8004 return ret;
8005 }
8006 }
8007 ret = 0;
8008
Chris Mason5a3f23d2009-03-31 13:27:11 -04008009 /*
8010 * we're using rename to replace one file with another.
8011 * and the replacement file is large. Start IO on it now so
8012 * we don't add too much work to the end of the transaction
8013 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04008014 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04008015 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8016 filemap_flush(old_inode->i_mapping);
8017
Yan, Zheng76dda932009-09-21 16:00:26 -04008018 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08008019 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008020 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008021 /*
8022 * We want to reserve the absolute worst case amount of items. So if
8023 * both inodes are subvols and we need to unlink them then that would
8024 * require 4 item modifications, but if they are both normal inodes it
8025 * would require 5 item modifications, so we'll assume their normal
8026 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8027 * should cover the worst case number of items we'll modify.
8028 */
Josef Bacik6e137ed2013-03-26 15:26:55 -04008029 trans = btrfs_start_transaction(root, 11);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008030 if (IS_ERR(trans)) {
8031 ret = PTR_ERR(trans);
8032 goto out_notrans;
8033 }
Chris Mason5f39d392007-10-15 16:14:19 -04008034
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008035 if (dest != root)
8036 btrfs_record_root_in_trans(trans, dest);
8037
Yan, Zhenga5719522009-09-24 09:17:31 -04008038 ret = btrfs_set_inode_index(new_dir, &index);
8039 if (ret)
8040 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008041
Li Zefan33345d012011-04-20 10:31:50 +08008042 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008043 /* force full log commit if subvolume involved. */
8044 root->fs_info->last_trans_log_full_commit = trans->transid;
8045 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04008046 ret = btrfs_insert_inode_ref(trans, dest,
8047 new_dentry->d_name.name,
8048 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08008049 old_ino,
8050 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04008051 if (ret)
8052 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008053 /*
8054 * this is an ugly little race, but the rename is required
8055 * to make sure that if we crash, the inode is either at the
8056 * old name or the new one. pinning the log transaction lets
8057 * us make sure we don't allow a log commit to come in after
8058 * we unlink the name but before we add the new name back in.
8059 */
8060 btrfs_pin_log_trans(root);
8061 }
Chris Mason12fcfd22009-03-24 10:24:20 -04008062 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04008063 * make sure the inode gets flushed if it is replacing
8064 * something.
8065 */
Li Zefan33345d012011-04-20 10:31:50 +08008066 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04008067 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008068
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008069 inode_inc_iversion(old_dir);
8070 inode_inc_iversion(new_dir);
8071 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008072 old_dir->i_ctime = old_dir->i_mtime = ctime;
8073 new_dir->i_ctime = new_dir->i_mtime = ctime;
8074 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04008075
Chris Mason12fcfd22009-03-24 10:24:20 -04008076 if (old_dentry->d_parent != new_dentry->d_parent)
8077 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8078
Li Zefan33345d012011-04-20 10:31:50 +08008079 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008080 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8081 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8082 old_dentry->d_name.name,
8083 old_dentry->d_name.len);
8084 } else {
Al Viro92986792011-03-04 17:14:37 +00008085 ret = __btrfs_unlink_inode(trans, root, old_dir,
8086 old_dentry->d_inode,
8087 old_dentry->d_name.name,
8088 old_dentry->d_name.len);
8089 if (!ret)
8090 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008091 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008092 if (ret) {
8093 btrfs_abort_transaction(trans, root, ret);
8094 goto out_fail;
8095 }
Chris Mason39279cc2007-06-12 06:35:45 -04008096
8097 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008098 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008099 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08008100 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008101 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8102 root_objectid = BTRFS_I(new_inode)->location.objectid;
8103 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8104 root_objectid,
8105 new_dentry->d_name.name,
8106 new_dentry->d_name.len);
8107 BUG_ON(new_inode->i_nlink == 0);
8108 } else {
8109 ret = btrfs_unlink_inode(trans, dest, new_dir,
8110 new_dentry->d_inode,
8111 new_dentry->d_name.name,
8112 new_dentry->d_name.len);
8113 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008114 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04008115 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008116 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04008117 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008118 if (ret) {
8119 btrfs_abort_transaction(trans, root, ret);
8120 goto out_fail;
8121 }
Chris Mason39279cc2007-06-12 06:35:45 -04008122 }
Josef Bacikaec74772008-07-24 12:12:38 -04008123
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008124 ret = btrfs_add_link(trans, new_dir, old_inode,
8125 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04008126 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008127 if (ret) {
8128 btrfs_abort_transaction(trans, root, ret);
8129 goto out_fail;
8130 }
Chris Mason39279cc2007-06-12 06:35:45 -04008131
Li Zefan33345d012011-04-20 10:31:50 +08008132 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04008133 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00008134 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008135 btrfs_end_log_trans(root);
8136 }
Chris Mason39279cc2007-06-12 06:35:45 -04008137out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008138 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00008139out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08008140 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04008141 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008142
Chris Mason39279cc2007-06-12 06:35:45 -04008143 return ret;
8144}
8145
Miao Xie8ccf6f192012-10-25 09:28:04 +00008146static void btrfs_run_delalloc_work(struct btrfs_work *work)
8147{
8148 struct btrfs_delalloc_work *delalloc_work;
8149
8150 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8151 work);
8152 if (delalloc_work->wait)
8153 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8154 else
8155 filemap_flush(delalloc_work->inode->i_mapping);
8156
8157 if (delalloc_work->delay_iput)
8158 btrfs_add_delayed_iput(delalloc_work->inode);
8159 else
8160 iput(delalloc_work->inode);
8161 complete(&delalloc_work->completion);
8162}
8163
8164struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8165 int wait, int delay_iput)
8166{
8167 struct btrfs_delalloc_work *work;
8168
8169 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8170 if (!work)
8171 return NULL;
8172
8173 init_completion(&work->completion);
8174 INIT_LIST_HEAD(&work->list);
8175 work->inode = inode;
8176 work->wait = wait;
8177 work->delay_iput = delay_iput;
8178 work->work.func = btrfs_run_delalloc_work;
8179
8180 return work;
8181}
8182
8183void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8184{
8185 wait_for_completion(&work->completion);
8186 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8187}
8188
Chris Masond352ac62008-09-29 15:18:18 -04008189/*
8190 * some fairly slow code that needs optimization. This walks the list
8191 * of all the inodes with pending delalloc and forces them to disk.
8192 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00008193static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04008194{
Chris Masonea8c2812008-08-04 23:17:27 -04008195 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008196 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008197 struct btrfs_delalloc_work *work, *next;
8198 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00008199 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008200 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04008201
Miao Xie8ccf6f192012-10-25 09:28:04 +00008202 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008203 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00008204
Miao Xieeb73c1b2013-05-15 07:48:22 +00008205 spin_lock(&root->delalloc_lock);
8206 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008207 while (!list_empty(&splice)) {
8208 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04008209 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008210
Miao Xieeb73c1b2013-05-15 07:48:22 +00008211 list_move_tail(&binode->delalloc_inodes,
8212 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008213 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008214 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008215 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008216 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008217 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008218 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008219
8220 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8221 if (unlikely(!work)) {
8222 ret = -ENOMEM;
8223 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008224 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008225 list_add_tail(&work->list, &works);
8226 btrfs_queue_worker(&root->fs_info->flush_workers,
8227 &work->work);
8228
Zheng Yan5b21f2e2008-09-26 10:05:38 -04008229 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00008230 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04008231 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008232 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04008233
Miao Xie1eafa6c2013-01-22 10:49:00 +00008234 list_for_each_entry_safe(work, next, &works, list) {
8235 list_del_init(&work->list);
8236 btrfs_wait_and_free_delalloc_work(work);
8237 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00008238 return 0;
8239out:
8240 list_for_each_entry_safe(work, next, &works, list) {
8241 list_del_init(&work->list);
8242 btrfs_wait_and_free_delalloc_work(work);
8243 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00008244
Miao Xieeb73c1b2013-05-15 07:48:22 +00008245 if (!list_empty_careful(&splice)) {
8246 spin_lock(&root->delalloc_lock);
8247 list_splice_tail(&splice, &root->delalloc_inodes);
8248 spin_unlock(&root->delalloc_lock);
8249 }
8250 return ret;
8251}
8252
8253int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8254{
8255 int ret;
8256
8257 if (root->fs_info->sb->s_flags & MS_RDONLY)
8258 return -EROFS;
8259
8260 ret = __start_delalloc_inodes(root, delay_iput);
8261 /*
8262 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -04008263 * we have to make sure the IO is actually started and that
8264 * ordered extents get created before we return
8265 */
8266 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05008267 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05008268 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04008269 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05008270 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8271 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04008272 }
8273 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +00008274 return ret;
8275}
8276
8277int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8278 int delay_iput)
8279{
8280 struct btrfs_root *root;
8281 struct list_head splice;
8282 int ret;
8283
8284 if (fs_info->sb->s_flags & MS_RDONLY)
8285 return -EROFS;
8286
8287 INIT_LIST_HEAD(&splice);
8288
8289 spin_lock(&fs_info->delalloc_root_lock);
8290 list_splice_init(&fs_info->delalloc_roots, &splice);
8291 while (!list_empty(&splice)) {
8292 root = list_first_entry(&splice, struct btrfs_root,
8293 delalloc_root);
8294 root = btrfs_grab_fs_root(root);
8295 BUG_ON(!root);
8296 list_move_tail(&root->delalloc_root,
8297 &fs_info->delalloc_roots);
8298 spin_unlock(&fs_info->delalloc_root_lock);
8299
8300 ret = __start_delalloc_inodes(root, delay_iput);
8301 btrfs_put_fs_root(root);
8302 if (ret)
8303 goto out;
8304
8305 spin_lock(&fs_info->delalloc_root_lock);
8306 }
8307 spin_unlock(&fs_info->delalloc_root_lock);
8308
8309 atomic_inc(&fs_info->async_submit_draining);
8310 while (atomic_read(&fs_info->nr_async_submits) ||
8311 atomic_read(&fs_info->async_delalloc_pages)) {
8312 wait_event(fs_info->async_submit_wait,
8313 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8314 atomic_read(&fs_info->async_delalloc_pages) == 0));
8315 }
8316 atomic_dec(&fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008317 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00008318out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00008319 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00008320 spin_lock(&fs_info->delalloc_root_lock);
8321 list_splice_tail(&splice, &fs_info->delalloc_roots);
8322 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00008323 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00008324 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04008325}
8326
Chris Mason39279cc2007-06-12 06:35:45 -04008327static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8328 const char *symname)
8329{
8330 struct btrfs_trans_handle *trans;
8331 struct btrfs_root *root = BTRFS_I(dir)->root;
8332 struct btrfs_path *path;
8333 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05008334 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04008335 int err;
8336 int drop_inode = 0;
8337 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008338 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04008339 int name_len;
8340 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04008341 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04008342 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04008343 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04008344
8345 name_len = strlen(symname) + 1;
8346 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8347 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05008348
Josef Bacik9ed74f22009-09-11 16:12:44 -04008349 /*
8350 * 2 items for inode item and ref
8351 * 2 items for dir items
8352 * 1 item for xattr if selinux is on
8353 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04008354 trans = btrfs_start_transaction(root, 5);
8355 if (IS_ERR(trans))
8356 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05008357
Li Zefan581bb052011-04-20 10:06:11 +08008358 err = btrfs_find_free_ino(root, &objectid);
8359 if (err)
8360 goto out_unlock;
8361
Josef Bacikaec74772008-07-24 12:12:38 -04008362 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08008363 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04008364 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008365 if (IS_ERR(inode)) {
8366 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008367 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04008368 }
Chris Mason39279cc2007-06-12 06:35:45 -04008369
Eric Paris2a7dba32011-02-01 11:05:39 -05008370 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04008371 if (err) {
8372 drop_inode = 1;
8373 goto out_unlock;
8374 }
8375
Casey Schauflerad19db72011-12-15 10:09:07 -05008376 /*
8377 * If the active LSM wants to access the inode during
8378 * d_instantiate it needs these. Smack checks to see
8379 * if the filesystem supports xattrs by looking at the
8380 * ops vector.
8381 */
8382 inode->i_fop = &btrfs_file_operations;
8383 inode->i_op = &btrfs_file_inode_operations;
8384
Josef Bacika1b075d2010-11-19 20:36:11 +00008385 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04008386 if (err)
8387 drop_inode = 1;
8388 else {
8389 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04008390 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05008391 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04008392 }
Chris Mason39279cc2007-06-12 06:35:45 -04008393 if (drop_inode)
8394 goto out_unlock;
8395
8396 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008397 if (!path) {
8398 err = -ENOMEM;
8399 drop_inode = 1;
8400 goto out_unlock;
8401 }
Li Zefan33345d012011-04-20 10:31:50 +08008402 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008403 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008404 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8405 datasize = btrfs_file_extent_calc_inline_size(name_len);
8406 err = btrfs_insert_empty_item(trans, root, path, &key,
8407 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04008408 if (err) {
8409 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00008410 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04008411 goto out_unlock;
8412 }
Chris Mason5f39d392007-10-15 16:14:19 -04008413 leaf = path->nodes[0];
8414 ei = btrfs_item_ptr(leaf, path->slots[0],
8415 struct btrfs_file_extent_item);
8416 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8417 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04008418 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04008419 btrfs_set_file_extent_encryption(leaf, ei, 0);
8420 btrfs_set_file_extent_compression(leaf, ei, 0);
8421 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8422 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8423
Chris Mason39279cc2007-06-12 06:35:45 -04008424 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04008425 write_extent_buffer(leaf, symname, ptr, name_len);
8426 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04008427 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04008428
Chris Mason39279cc2007-06-12 06:35:45 -04008429 inode->i_op = &btrfs_symlink_inode_operations;
8430 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04008431 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04008432 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04008433 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04008434 err = btrfs_update_inode(trans, root, inode);
8435 if (err)
8436 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04008437
8438out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05008439 if (!err)
8440 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05008441 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04008442 if (drop_inode) {
8443 inode_dec_link_count(inode);
8444 iput(inode);
8445 }
Liu Bob53d3f52012-11-14 14:34:34 +00008446 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04008447 return err;
8448}
Chris Mason16432982008-04-10 10:23:21 -04008449
Josef Bacik0af3d002010-06-21 14:48:16 -04008450static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8451 u64 start, u64 num_bytes, u64 min_size,
8452 loff_t actual_len, u64 *alloc_hint,
8453 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04008454{
Josef Bacik5dc562c2012-08-17 13:14:17 -04008455 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8456 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04008457 struct btrfs_root *root = BTRFS_I(inode)->root;
8458 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04008459 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00008460 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05008461 u64 cur_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04008462 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04008463 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04008464
Josef Bacik0af3d002010-06-21 14:48:16 -04008465 if (trans)
8466 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04008467 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008468 if (own_trans) {
8469 trans = btrfs_start_transaction(root, 3);
8470 if (IS_ERR(trans)) {
8471 ret = PTR_ERR(trans);
8472 break;
8473 }
Yan Zhengd899e052008-10-30 14:25:28 -04008474 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00008475
Chris Mason154ea282013-03-05 11:11:26 -05008476 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8477 cur_bytes = max(cur_bytes, min_size);
8478 ret = btrfs_reserve_extent(trans, root, cur_bytes,
Zach Brown24542bf2012-11-16 00:04:43 +00008479 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008480 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04008481 if (own_trans)
8482 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04008483 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008484 }
8485
Yan Zhengd899e052008-10-30 14:25:28 -04008486 ret = insert_reserved_file_extent(trans, inode,
8487 cur_offset, ins.objectid,
8488 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00008489 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04008490 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008491 if (ret) {
8492 btrfs_abort_transaction(trans, root, ret);
8493 if (own_trans)
8494 btrfs_end_transaction(trans, root);
8495 break;
8496 }
Chris Masona1ed8352009-09-11 12:27:37 -04008497 btrfs_drop_extent_cache(inode, cur_offset,
8498 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008499
Josef Bacik5dc562c2012-08-17 13:14:17 -04008500 em = alloc_extent_map();
8501 if (!em) {
8502 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8503 &BTRFS_I(inode)->runtime_flags);
8504 goto next;
8505 }
8506
8507 em->start = cur_offset;
8508 em->orig_start = cur_offset;
8509 em->len = ins.offset;
8510 em->block_start = ins.objectid;
8511 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05008512 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04008513 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008514 em->bdev = root->fs_info->fs_devices->latest_bdev;
8515 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8516 em->generation = trans->transid;
8517
8518 while (1) {
8519 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04008520 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04008521 write_unlock(&em_tree->lock);
8522 if (ret != -EEXIST)
8523 break;
8524 btrfs_drop_extent_cache(inode, cur_offset,
8525 cur_offset + ins.offset - 1,
8526 0);
8527 }
8528 free_extent_map(em);
8529next:
Yan Zhengd899e052008-10-30 14:25:28 -04008530 num_bytes -= ins.offset;
8531 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04008532 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00008533
Josef Bacik0c4d2d92012-04-05 15:03:02 -04008534 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04008535 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02008536 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04008537 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04008538 (actual_len > inode->i_size) &&
8539 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008540 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00008541 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00008542 else
Josef Bacik55a61d12010-11-22 18:50:32 +00008543 i_size = cur_offset;
8544 i_size_write(inode, i_size);
8545 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008546 }
8547
Yan Zhengd899e052008-10-30 14:25:28 -04008548 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008549
8550 if (ret) {
8551 btrfs_abort_transaction(trans, root, ret);
8552 if (own_trans)
8553 btrfs_end_transaction(trans, root);
8554 break;
8555 }
Yan Zhengd899e052008-10-30 14:25:28 -04008556
Josef Bacik0af3d002010-06-21 14:48:16 -04008557 if (own_trans)
8558 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00008559 }
Yan Zhengd899e052008-10-30 14:25:28 -04008560 return ret;
8561}
8562
Josef Bacik0af3d002010-06-21 14:48:16 -04008563int btrfs_prealloc_file_range(struct inode *inode, int mode,
8564 u64 start, u64 num_bytes, u64 min_size,
8565 loff_t actual_len, u64 *alloc_hint)
8566{
8567 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8568 min_size, actual_len, alloc_hint,
8569 NULL);
8570}
8571
8572int btrfs_prealloc_file_range_trans(struct inode *inode,
8573 struct btrfs_trans_handle *trans, int mode,
8574 u64 start, u64 num_bytes, u64 min_size,
8575 loff_t actual_len, u64 *alloc_hint)
8576{
8577 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8578 min_size, actual_len, alloc_hint, trans);
8579}
8580
Chris Masone6dcd2d2008-07-17 12:53:50 -04008581static int btrfs_set_page_dirty(struct page *page)
8582{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008583 return __set_page_dirty_nobuffers(page);
8584}
8585
Al Viro10556cb22011-06-20 19:28:19 -04008586static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008587{
Li Zefanb83cc962010-12-20 16:04:08 +08008588 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008589 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008590
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008591 if (mask & MAY_WRITE &&
8592 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8593 if (btrfs_root_readonly(root))
8594 return -EROFS;
8595 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8596 return -EACCES;
8597 }
Al Viro2830ba72011-06-20 19:16:29 -04008598 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008599}
Chris Mason39279cc2007-06-12 06:35:45 -04008600
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008601static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008602 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008603 .lookup = btrfs_lookup,
8604 .create = btrfs_create,
8605 .unlink = btrfs_unlink,
8606 .link = btrfs_link,
8607 .mkdir = btrfs_mkdir,
8608 .rmdir = btrfs_rmdir,
8609 .rename = btrfs_rename,
8610 .symlink = btrfs_symlink,
8611 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008612 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008613 .setxattr = btrfs_setxattr,
8614 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008615 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008616 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008617 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008618 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008619};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008620static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008621 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008622 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008623 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008624};
Yan, Zheng76dda932009-09-21 16:00:26 -04008625
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008626static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008627 .llseek = generic_file_llseek,
8628 .read = generic_read_dir,
Al Viro9cdda8d2013-05-22 16:48:09 -04008629 .iterate = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008630 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008631#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008632 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008633#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008634 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008635 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008636};
8637
Chris Masond1310b22008-01-24 16:13:08 -05008638static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008639 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008640 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008641 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008642 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008643 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008644 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008645 .set_bit_hook = btrfs_set_bit_hook,
8646 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008647 .merge_extent_hook = btrfs_merge_extent_hook,
8648 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008649};
8650
Chris Mason35054392009-01-21 13:11:13 -05008651/*
8652 * btrfs doesn't support the bmap operation because swapfiles
8653 * use bmap to make a mapping of extents in the file. They assume
8654 * these extents won't change over the life of the file and they
8655 * use the bmap result to do IO directly to the drive.
8656 *
8657 * the btrfs bmap call would return logical addresses that aren't
8658 * suitable for IO and they also will change frequently as COW
8659 * operations happen. So, swapfile + btrfs == corruption.
8660 *
8661 * For now we're avoiding this by dropping bmap.
8662 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008663static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008664 .readpage = btrfs_readpage,
8665 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008666 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008667 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008668 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008669 .invalidatepage = btrfs_invalidatepage,
8670 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008671 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008672 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008673};
8674
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008675static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008676 .readpage = btrfs_readpage,
8677 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008678 .invalidatepage = btrfs_invalidatepage,
8679 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008680};
8681
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008682static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008683 .getattr = btrfs_getattr,
8684 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008685 .setxattr = btrfs_setxattr,
8686 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008687 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008688 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008689 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008690 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008691 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008692 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008693};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008694static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008695 .getattr = btrfs_getattr,
8696 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008697 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008698 .setxattr = btrfs_setxattr,
8699 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008700 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008701 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008702 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008703 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008704};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008705static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008706 .readlink = generic_readlink,
8707 .follow_link = page_follow_link_light,
8708 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008709 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008710 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008711 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008712 .setxattr = btrfs_setxattr,
8713 .getxattr = btrfs_getxattr,
8714 .listxattr = btrfs_listxattr,
8715 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008716 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008717 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008718};
Yan, Zheng76dda932009-09-21 16:00:26 -04008719
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008720const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008721 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008722 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008723};