blob: 3bc62b181ef8302f81aeb823083554ce8a801f4b [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>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040058
59struct btrfs_iget_args {
60 u64 ino;
61 struct btrfs_root *root;
62};
63
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070064static const struct inode_operations btrfs_dir_inode_operations;
65static const struct inode_operations btrfs_symlink_inode_operations;
66static const struct inode_operations btrfs_dir_ro_inode_operations;
67static const struct inode_operations btrfs_special_inode_operations;
68static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070069static const struct address_space_operations btrfs_aops;
70static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070071static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050072static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040073
74static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000075static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_trans_handle_cachep;
77struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050079struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080
81#define S_SHIFT 12
82static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
83 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
84 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
85 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
86 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
87 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
88 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
89 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
90};
91
Eric Sandeen3972f262013-01-12 02:57:22 +000092static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050093static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040094static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050095static noinline int cow_file_range(struct inode *inode,
96 struct page *locked_page,
97 u64 start, u64 end, int *page_started,
98 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -040099static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
100 u64 len, u64 orig_start,
101 u64 block_start, u64 block_len,
102 u64 orig_block_len, int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400103
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000104static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500105 struct inode *inode, struct inode *dir,
106 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500107{
108 int err;
109
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000110 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500111 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500112 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500113 return err;
114}
115
Chris Masond352ac62008-09-29 15:18:18 -0400116/*
Chris Masonc8b97812008-10-29 14:49:59 -0400117 * this does all the hard work for inserting an inline extent into
118 * the btree. The caller should have done a btrfs_drop_extents so that
119 * no overlapping inline items exist in the btree
120 */
Chris Masond3977122009-01-05 21:25:51 -0500121static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400122 struct btrfs_root *root, struct inode *inode,
123 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000124 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400125 struct page **compressed_pages)
126{
127 struct btrfs_key key;
128 struct btrfs_path *path;
129 struct extent_buffer *leaf;
130 struct page *page = NULL;
131 char *kaddr;
132 unsigned long ptr;
133 struct btrfs_file_extent_item *ei;
134 int err = 0;
135 int ret;
136 size_t cur_size = size;
137 size_t datasize;
138 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400139
Li Zefanfe3f5662011-03-28 08:30:38 +0000140 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400141 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400142
Chris Masond3977122009-01-05 21:25:51 -0500143 path = btrfs_alloc_path();
144 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400145 return -ENOMEM;
146
Chris Masonb9473432009-03-13 11:00:37 -0400147 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400148
Li Zefan33345d012011-04-20 10:31:50 +0800149 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400150 key.offset = start;
151 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400152 datasize = btrfs_file_extent_calc_inline_size(cur_size);
153
154 inode_add_bytes(inode, size);
155 ret = btrfs_insert_empty_item(trans, root, path, &key,
156 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400157 if (ret) {
158 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 goto fail;
160 }
161 leaf = path->nodes[0];
162 ei = btrfs_item_ptr(leaf, path->slots[0],
163 struct btrfs_file_extent_item);
164 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
165 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
166 btrfs_set_file_extent_encryption(leaf, ei, 0);
167 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
168 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
169 ptr = btrfs_file_extent_inline_start(ei);
170
Li Zefan261507a02010-12-17 14:21:50 +0800171 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400172 struct page *cpage;
173 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500174 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400175 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500176 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400177 PAGE_CACHE_SIZE);
178
Cong Wang7ac687d2011-11-25 23:14:28 +0800179 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400180 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800181 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400182
183 i++;
184 ptr += cur_size;
185 compressed_size -= cur_size;
186 }
187 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800188 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400189 } else {
190 page = find_get_page(inode->i_mapping,
191 start >> PAGE_CACHE_SHIFT);
192 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800193 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400194 offset = start & (PAGE_CACHE_SIZE - 1);
195 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800196 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400197 page_cache_release(page);
198 }
199 btrfs_mark_buffer_dirty(leaf);
200 btrfs_free_path(path);
201
Yan, Zhengc2167752009-11-12 09:34:21 +0000202 /*
203 * we're an inline extent, so nobody can
204 * extend the file past i_size without locking
205 * a page we already have locked.
206 *
207 * We must do any isize and inode updates
208 * before we unlock the pages. Otherwise we
209 * could end up racing with unlink.
210 */
Chris Masonc8b97812008-10-29 14:49:59 -0400211 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100212 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000213
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100214 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400215fail:
216 btrfs_free_path(path);
217 return err;
218}
219
220
221/*
222 * conditionally insert an inline extent into the file. This
223 * does the checks required to make sure the data is small enough
224 * to fit as an inline extent.
225 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400226static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400227 struct btrfs_root *root,
228 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000229 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400230 struct page **compressed_pages)
231{
232 u64 isize = i_size_read(inode);
233 u64 actual_end = min(end + 1, isize);
234 u64 inline_len = actual_end - start;
235 u64 aligned_end = (end + root->sectorsize - 1) &
236 ~((u64)root->sectorsize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 u64 data_len = inline_len;
238 int ret;
239
240 if (compressed_size)
241 data_len = compressed_size;
242
243 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400244 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400245 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
246 (!compressed_size &&
247 (actual_end & (root->sectorsize - 1)) == 0) ||
248 end + 1 < isize ||
249 data_len > root->fs_info->max_inline) {
250 return 1;
251 }
252
Josef Bacik26714852012-08-29 12:24:27 -0400253 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100254 if (ret)
255 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400256
257 if (isize > actual_end)
258 inline_len = min_t(u64, isize, actual_end);
259 ret = insert_inline_extent(trans, root, inode, start,
260 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000261 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400262 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100263 btrfs_abort_transaction(trans, root, ret);
264 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400265 } else if (ret == -ENOSPC) {
266 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400268
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400269 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400270 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400271 return 0;
272}
273
Chris Mason771ed682008-11-06 22:02:51 -0500274struct async_extent {
275 u64 start;
276 u64 ram_size;
277 u64 compressed_size;
278 struct page **pages;
279 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800280 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500281 struct list_head list;
282};
283
284struct async_cow {
285 struct inode *inode;
286 struct btrfs_root *root;
287 struct page *locked_page;
288 u64 start;
289 u64 end;
290 struct list_head extents;
291 struct btrfs_work work;
292};
293
294static noinline int add_async_extent(struct async_cow *cow,
295 u64 start, u64 ram_size,
296 u64 compressed_size,
297 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800298 unsigned long nr_pages,
299 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500300{
301 struct async_extent *async_extent;
302
303 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100304 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500305 async_extent->start = start;
306 async_extent->ram_size = ram_size;
307 async_extent->compressed_size = compressed_size;
308 async_extent->pages = pages;
309 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800310 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500311 list_add_tail(&async_extent->list, &cow->extents);
312 return 0;
313}
314
Chris Masonc8b97812008-10-29 14:49:59 -0400315/*
Chris Mason771ed682008-11-06 22:02:51 -0500316 * we create compressed extents in two phases. The first
317 * phase compresses a range of pages that have already been
318 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400319 *
Chris Mason771ed682008-11-06 22:02:51 -0500320 * This is done inside an ordered work queue, and the compression
321 * is spread across many cpus. The actual IO submission is step
322 * two, and the ordered work queue takes care of making sure that
323 * happens in the same order things were put onto the queue by
324 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400325 *
Chris Mason771ed682008-11-06 22:02:51 -0500326 * If this code finds it can't get good compression, it puts an
327 * entry onto the work queue to write the uncompressed bytes. This
328 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300329 * are written in the same order that the flusher thread sent them
330 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400331 */
Chris Mason771ed682008-11-06 22:02:51 -0500332static noinline int compress_file_range(struct inode *inode,
333 struct page *locked_page,
334 u64 start, u64 end,
335 struct async_cow *async_cow,
336 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400337{
338 struct btrfs_root *root = BTRFS_I(inode)->root;
339 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400340 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400341 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400342 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500343 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400344 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400345 struct page **pages = NULL;
346 unsigned long nr_pages;
347 unsigned long nr_pages_ret = 0;
348 unsigned long total_compressed = 0;
349 unsigned long total_in = 0;
350 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500351 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400352 int i;
353 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800354 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400355
Liu Bo4cb13e52012-03-29 09:57:45 -0400356 /* if this is a small write inside eof, kick off a defrag */
357 if ((end - start + 1) < 16 * 1024 &&
358 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400359 btrfs_add_inode_defrag(NULL, inode);
360
Chris Mason42dc7ba2008-12-15 11:44:56 -0500361 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400362again:
363 will_compress = 0;
364 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
365 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
366
Chris Masonf03d9302009-02-04 09:31:06 -0500367 /*
368 * we don't want to send crud past the end of i_size through
369 * compression, that's just a waste of CPU time. So, if the
370 * end of the file is before the start of our current
371 * requested range of bytes, we bail out to the uncompressed
372 * cleanup code that can deal with all of this.
373 *
374 * It isn't really the fastest way to fix things, but this is a
375 * very uncommon corner.
376 */
377 if (actual_end <= start)
378 goto cleanup_and_bail_uncompressed;
379
Chris Masonc8b97812008-10-29 14:49:59 -0400380 total_compressed = actual_end - start;
381
382 /* we want to make sure that amount of ram required to uncompress
383 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500384 * of a compressed extent to 128k. This is a crucial number
385 * because it also controls how easily we can spread reads across
386 * cpus for decompression.
387 *
388 * We also want to make sure the amount of IO required to do
389 * a random read is reasonably small, so we limit the size of
390 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400391 */
392 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400393 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500394 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400395 total_in = 0;
396 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400397
Chris Mason771ed682008-11-06 22:02:51 -0500398 /*
399 * we do compression for mount -o compress and when the
400 * inode has not been flagged as nocompress. This flag can
401 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400402 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200403 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500404 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000405 (BTRFS_I(inode)->force_compress) ||
406 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400407 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400408 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800409 if (!pages) {
410 /* just bail out to the uncompressed code */
411 goto cont;
412 }
Chris Mason179e29e2007-11-01 11:28:41 -0400413
Li Zefan261507a02010-12-17 14:21:50 +0800414 if (BTRFS_I(inode)->force_compress)
415 compress_type = BTRFS_I(inode)->force_compress;
416
417 ret = btrfs_compress_pages(compress_type,
418 inode->i_mapping, start,
419 total_compressed, pages,
420 nr_pages, &nr_pages_ret,
421 &total_in,
422 &total_compressed,
423 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400424
425 if (!ret) {
426 unsigned long offset = total_compressed &
427 (PAGE_CACHE_SIZE - 1);
428 struct page *page = pages[nr_pages_ret - 1];
429 char *kaddr;
430
431 /* zero the tail end of the last page, we might be
432 * sending it down to disk
433 */
434 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800435 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400436 memset(kaddr + offset, 0,
437 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800438 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400439 }
440 will_compress = 1;
441 }
442 }
Li Zefan560f7d72011-09-08 10:22:01 +0800443cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400444 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400445 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100446 if (IS_ERR(trans)) {
447 ret = PTR_ERR(trans);
448 trans = NULL;
449 goto cleanup_and_out;
450 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400451 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500452
Chris Masonc8b97812008-10-29 14:49:59 -0400453 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500454 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400455 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500456 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400457 */
458 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000459 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400460 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500461 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400462 ret = cow_file_range_inline(trans, root, inode,
463 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000464 total_compressed,
465 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400466 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100467 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500468 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100469 * inline extent creation worked or returned error,
470 * we don't need to create any more async work items.
471 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500472 */
Chris Masonc8b97812008-10-29 14:49:59 -0400473 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400474 &BTRFS_I(inode)->io_tree,
475 start, end, NULL,
476 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400477 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400478 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000479
480 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400481 goto free_pages_out;
482 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000483 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400484 }
485
486 if (will_compress) {
487 /*
488 * we aren't doing an inline extent round the compressed size
489 * up to a block size boundary so the allocator does sane
490 * things
491 */
492 total_compressed = (total_compressed + blocksize - 1) &
493 ~(blocksize - 1);
494
495 /*
496 * one last check to make sure the compression is really a
497 * win, compare the page count read with the blocks on disk
498 */
499 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
500 ~(PAGE_CACHE_SIZE - 1);
501 if (total_compressed >= total_in) {
502 will_compress = 0;
503 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400504 num_bytes = total_in;
505 }
506 }
507 if (!will_compress && pages) {
508 /*
509 * the compression code ran but failed to make things smaller,
510 * free any pages it allocated and our page pointer array
511 */
512 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400513 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400514 page_cache_release(pages[i]);
515 }
516 kfree(pages);
517 pages = NULL;
518 total_compressed = 0;
519 nr_pages_ret = 0;
520
521 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500522 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
523 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500524 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500525 }
Chris Masonc8b97812008-10-29 14:49:59 -0400526 }
Chris Mason771ed682008-11-06 22:02:51 -0500527 if (will_compress) {
528 *num_added += 1;
529
530 /* the async work queues will take care of doing actual
531 * allocation on disk for these compressed pages,
532 * and will submit them to the elevator.
533 */
534 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800535 total_compressed, pages, nr_pages_ret,
536 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500537
Yan, Zheng24ae6362010-12-06 07:02:36 +0000538 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500539 start += num_bytes;
540 pages = NULL;
541 cond_resched();
542 goto again;
543 }
544 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500545cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500546 /*
547 * No compression, but we still need to write the pages in
548 * the file we've been given so far. redirty the locked
549 * page if it corresponds to our extent and set things up
550 * for the async work queue to run cow_file_range to do
551 * the normal delalloc dance
552 */
553 if (page_offset(locked_page) >= start &&
554 page_offset(locked_page) <= end) {
555 __set_page_dirty_nobuffers(locked_page);
556 /* unlocked later on in the async handlers */
557 }
Li Zefan261507a02010-12-17 14:21:50 +0800558 add_async_extent(async_cow, start, end - start + 1,
559 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500560 *num_added += 1;
561 }
562
563out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100564 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500565
566free_pages_out:
567 for (i = 0; i < nr_pages_ret; i++) {
568 WARN_ON(pages[i]->mapping);
569 page_cache_release(pages[i]);
570 }
Chris Masond3977122009-01-05 21:25:51 -0500571 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500572
573 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100574
575cleanup_and_out:
576 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
577 start, end, NULL,
578 EXTENT_CLEAR_UNLOCK_PAGE |
579 EXTENT_CLEAR_DIRTY |
580 EXTENT_CLEAR_DELALLOC |
581 EXTENT_SET_WRITEBACK |
582 EXTENT_END_WRITEBACK);
583 if (!trans || IS_ERR(trans))
584 btrfs_error(root->fs_info, ret, "Failed to join transaction");
585 else
586 btrfs_abort_transaction(trans, root, ret);
587 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500588}
589
590/*
591 * phase two of compressed writeback. This is the ordered portion
592 * of the code, which only gets called in the order the work was
593 * queued. We walk all the async extents created by compress_file_range
594 * and send them down to the disk.
595 */
596static noinline int submit_compressed_extents(struct inode *inode,
597 struct async_cow *async_cow)
598{
599 struct async_extent *async_extent;
600 u64 alloc_hint = 0;
601 struct btrfs_trans_handle *trans;
602 struct btrfs_key ins;
603 struct extent_map *em;
604 struct btrfs_root *root = BTRFS_I(inode)->root;
605 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
606 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500607 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500608
609 if (list_empty(&async_cow->extents))
610 return 0;
611
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500612again:
Chris Masond3977122009-01-05 21:25:51 -0500613 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500614 async_extent = list_entry(async_cow->extents.next,
615 struct async_extent, list);
616 list_del(&async_extent->list);
617
618 io_tree = &BTRFS_I(inode)->io_tree;
619
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500620retry:
Chris Mason771ed682008-11-06 22:02:51 -0500621 /* did the compression code fall back to uncompressed IO? */
622 if (!async_extent->pages) {
623 int page_started = 0;
624 unsigned long nr_written = 0;
625
626 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000627 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100628 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500629
630 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500631 ret = cow_file_range(inode, async_cow->locked_page,
632 async_extent->start,
633 async_extent->start +
634 async_extent->ram_size - 1,
635 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500636
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100637 /* JDM XXX */
638
Chris Mason771ed682008-11-06 22:02:51 -0500639 /*
640 * if page_started, cow_file_range inserted an
641 * inline extent and took care of all the unlocking
642 * and IO for us. Otherwise, we need to submit
643 * all those pages down to the drive.
644 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500645 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500646 extent_write_locked_range(io_tree,
647 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500648 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500649 async_extent->ram_size - 1,
650 btrfs_get_extent,
651 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500652 else if (ret)
653 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500654 kfree(async_extent);
655 cond_resched();
656 continue;
657 }
658
659 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100660 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500661
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400662 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100663 if (IS_ERR(trans)) {
664 ret = PTR_ERR(trans);
665 } else {
666 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
667 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500668 async_extent->compressed_size,
669 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500670 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600671 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100672 btrfs_abort_transaction(trans, root, ret);
673 btrfs_end_transaction(trans, root);
674 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000675
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500676 if (ret) {
677 int i;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500678
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500679 for (i = 0; i < async_extent->nr_pages; i++) {
680 WARN_ON(async_extent->pages[i]->mapping);
681 page_cache_release(async_extent->pages[i]);
682 }
683 kfree(async_extent->pages);
684 async_extent->nr_pages = 0;
685 async_extent->pages = NULL;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500686
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100687 if (ret == -ENOSPC)
688 goto retry;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500689 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500690 }
691
Yan, Zhengc2167752009-11-12 09:34:21 +0000692 /*
693 * here we're doing allocation and writeback of the
694 * compressed pages
695 */
696 btrfs_drop_extent_cache(inode, async_extent->start,
697 async_extent->start +
698 async_extent->ram_size - 1, 0);
699
David Sterba172ddd62011-04-21 00:48:27 +0200700 em = alloc_extent_map();
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500701 if (!em)
702 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500703 em->start = async_extent->start;
704 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500705 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400706 em->mod_start = em->start;
707 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500708
709 em->block_start = ins.objectid;
710 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500711 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500712 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800713 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500714 set_bit(EXTENT_FLAG_PINNED, &em->flags);
715 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400716 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500717
Chris Masond3977122009-01-05 21:25:51 -0500718 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400719 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500720 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400721 if (!ret)
722 list_move(&em->list,
723 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400724 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500725 if (ret != -EEXIST) {
726 free_extent_map(em);
727 break;
728 }
729 btrfs_drop_extent_cache(inode, async_extent->start,
730 async_extent->start +
731 async_extent->ram_size - 1, 0);
732 }
733
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500734 if (ret)
735 goto out_free_reserve;
736
Li Zefan261507a02010-12-17 14:21:50 +0800737 ret = btrfs_add_ordered_extent_compress(inode,
738 async_extent->start,
739 ins.objectid,
740 async_extent->ram_size,
741 ins.offset,
742 BTRFS_ORDERED_COMPRESSED,
743 async_extent->compress_type);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500744 if (ret)
745 goto out_free_reserve;
Chris Mason771ed682008-11-06 22:02:51 -0500746
Chris Mason771ed682008-11-06 22:02:51 -0500747 /*
748 * clear dirty, set writeback and unlock the pages.
749 */
750 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400751 &BTRFS_I(inode)->io_tree,
752 async_extent->start,
753 async_extent->start +
754 async_extent->ram_size - 1,
755 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
756 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400757 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400758 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500759
760 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500761 async_extent->start,
762 async_extent->ram_size,
763 ins.objectid,
764 ins.offset, async_extent->pages,
765 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500766 alloc_hint = ins.objectid + ins.offset;
767 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500768 if (ret)
769 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500770 cond_resched();
771 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100772 ret = 0;
773out:
774 return ret;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500775out_free_reserve:
776 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100777out_free:
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500778 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
779 async_extent->start,
780 async_extent->start +
781 async_extent->ram_size - 1,
782 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
783 EXTENT_CLEAR_UNLOCK |
784 EXTENT_CLEAR_DELALLOC |
785 EXTENT_CLEAR_DIRTY |
786 EXTENT_SET_WRITEBACK |
787 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100788 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500789 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500790}
791
Josef Bacik4b46fce2010-05-23 11:00:55 -0400792static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
793 u64 num_bytes)
794{
795 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
796 struct extent_map *em;
797 u64 alloc_hint = 0;
798
799 read_lock(&em_tree->lock);
800 em = search_extent_mapping(em_tree, start, num_bytes);
801 if (em) {
802 /*
803 * if block start isn't an actual block number then find the
804 * first block in this inode and use that as a hint. If that
805 * block is also bogus then just don't worry about it.
806 */
807 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
808 free_extent_map(em);
809 em = search_extent_mapping(em_tree, 0, 0);
810 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
811 alloc_hint = em->block_start;
812 if (em)
813 free_extent_map(em);
814 } else {
815 alloc_hint = em->block_start;
816 free_extent_map(em);
817 }
818 }
819 read_unlock(&em_tree->lock);
820
821 return alloc_hint;
822}
823
Chris Mason771ed682008-11-06 22:02:51 -0500824/*
825 * when extent_io.c finds a delayed allocation range in the file,
826 * the call backs end up in this code. The basic idea is to
827 * allocate extents on disk for the range, and create ordered data structs
828 * in ram to track those extents.
829 *
830 * locked_page is the page that writepage had locked already. We use
831 * it to make sure we don't do extra locks or unlocks.
832 *
833 * *page_started is set to one if we unlock locked_page and do everything
834 * required to start IO on it. It may be clean and already done with
835 * IO when we return.
836 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000837static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
838 struct inode *inode,
839 struct btrfs_root *root,
840 struct page *locked_page,
841 u64 start, u64 end, int *page_started,
842 unsigned long *nr_written,
843 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500844{
Chris Mason771ed682008-11-06 22:02:51 -0500845 u64 alloc_hint = 0;
846 u64 num_bytes;
847 unsigned long ram_size;
848 u64 disk_num_bytes;
849 u64 cur_alloc_size;
850 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500851 struct btrfs_key ins;
852 struct extent_map *em;
853 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
854 int ret = 0;
855
Liu Bo83eea1f2012-07-10 05:28:39 -0600856 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500857
Chris Mason771ed682008-11-06 22:02:51 -0500858 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
859 num_bytes = max(blocksize, num_bytes);
860 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500861
Chris Mason4cb53002011-05-24 15:35:30 -0400862 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400863 if (num_bytes < 64 * 1024 &&
864 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400865 btrfs_add_inode_defrag(trans, inode);
866
Chris Mason771ed682008-11-06 22:02:51 -0500867 if (start == 0) {
868 /* lets try to make an inline extent */
869 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000870 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500871 if (ret == 0) {
872 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400873 &BTRFS_I(inode)->io_tree,
874 start, end, NULL,
875 EXTENT_CLEAR_UNLOCK_PAGE |
876 EXTENT_CLEAR_UNLOCK |
877 EXTENT_CLEAR_DELALLOC |
878 EXTENT_CLEAR_DIRTY |
879 EXTENT_SET_WRITEBACK |
880 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000881
Chris Mason771ed682008-11-06 22:02:51 -0500882 *nr_written = *nr_written +
883 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
884 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500885 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100886 } else if (ret < 0) {
887 btrfs_abort_transaction(trans, root, ret);
888 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500889 }
890 }
Chris Masonc8b97812008-10-29 14:49:59 -0400891
892 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200893 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400894
Josef Bacik4b46fce2010-05-23 11:00:55 -0400895 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400896 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400897
Chris Masond3977122009-01-05 21:25:51 -0500898 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400899 unsigned long op;
900
Josef Bacik287a0ab2010-03-19 18:07:23 +0000901 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400902 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500903 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500904 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100905 if (ret < 0) {
906 btrfs_abort_transaction(trans, root, ret);
907 goto out_unlock;
908 }
Chris Masond3977122009-01-05 21:25:51 -0500909
David Sterba172ddd62011-04-21 00:48:27 +0200910 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100911 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400912 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500913 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500914 ram_size = ins.offset;
915 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400916 em->mod_start = em->start;
917 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400918
Chris Masone6dcd2d2008-07-17 12:53:50 -0400919 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400920 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500921 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400922 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400923 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400924 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400925
Chris Masond3977122009-01-05 21:25:51 -0500926 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400927 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400928 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400929 if (!ret)
930 list_move(&em->list,
931 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400932 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400933 if (ret != -EEXIST) {
934 free_extent_map(em);
935 break;
936 }
937 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400938 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400939 }
940
Chris Mason98d20f62008-04-14 09:46:10 -0400941 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400942 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500943 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100944 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400945
Yan Zheng17d217f2008-12-12 10:03:38 -0500946 if (root->root_key.objectid ==
947 BTRFS_DATA_RELOC_TREE_OBJECTID) {
948 ret = btrfs_reloc_clone_csums(inode, start,
949 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100950 if (ret) {
951 btrfs_abort_transaction(trans, root, ret);
952 goto out_unlock;
953 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500954 }
955
Chris Masond3977122009-01-05 21:25:51 -0500956 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400957 break;
Chris Masond3977122009-01-05 21:25:51 -0500958
Chris Masonc8b97812008-10-29 14:49:59 -0400959 /* we're not doing compressed IO, don't unlock the first
960 * page (which the caller expects to stay locked), don't
961 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400962 *
963 * Do set the Private2 bit so we know this page was properly
964 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400965 */
Chris Masona791e352009-10-08 11:27:10 -0400966 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
967 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
968 EXTENT_SET_PRIVATE2;
969
Chris Masonc8b97812008-10-29 14:49:59 -0400970 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
971 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400972 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400973 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500974 num_bytes -= cur_alloc_size;
975 alloc_hint = ins.objectid + ins.offset;
976 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400977 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100978out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500979 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000980
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100981out_unlock:
982 extent_clear_unlock_delalloc(inode,
983 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400984 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100985 EXTENT_CLEAR_UNLOCK_PAGE |
986 EXTENT_CLEAR_UNLOCK |
987 EXTENT_CLEAR_DELALLOC |
988 EXTENT_CLEAR_DIRTY |
989 EXTENT_SET_WRITEBACK |
990 EXTENT_END_WRITEBACK);
991
992 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500993}
Chris Masonc8b97812008-10-29 14:49:59 -0400994
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000995static noinline int cow_file_range(struct inode *inode,
996 struct page *locked_page,
997 u64 start, u64 end, int *page_started,
998 unsigned long *nr_written,
999 int unlock)
1000{
1001 struct btrfs_trans_handle *trans;
1002 struct btrfs_root *root = BTRFS_I(inode)->root;
1003 int ret;
1004
1005 trans = btrfs_join_transaction(root);
1006 if (IS_ERR(trans)) {
1007 extent_clear_unlock_delalloc(inode,
1008 &BTRFS_I(inode)->io_tree,
1009 start, end, locked_page,
1010 EXTENT_CLEAR_UNLOCK_PAGE |
1011 EXTENT_CLEAR_UNLOCK |
1012 EXTENT_CLEAR_DELALLOC |
1013 EXTENT_CLEAR_DIRTY |
1014 EXTENT_SET_WRITEBACK |
1015 EXTENT_END_WRITEBACK);
1016 return PTR_ERR(trans);
1017 }
1018 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1019
1020 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1021 page_started, nr_written, unlock);
1022
1023 btrfs_end_transaction(trans, root);
1024
1025 return ret;
1026}
1027
Chris Mason771ed682008-11-06 22:02:51 -05001028/*
1029 * work queue call back to started compression on a file and pages
1030 */
1031static noinline void async_cow_start(struct btrfs_work *work)
1032{
1033 struct async_cow *async_cow;
1034 int num_added = 0;
1035 async_cow = container_of(work, struct async_cow, work);
1036
1037 compress_file_range(async_cow->inode, async_cow->locked_page,
1038 async_cow->start, async_cow->end, async_cow,
1039 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001040 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001041 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001042 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001043 }
Chris Mason771ed682008-11-06 22:02:51 -05001044}
1045
1046/*
1047 * work queue call back to submit previously compressed pages
1048 */
1049static noinline void async_cow_submit(struct btrfs_work *work)
1050{
1051 struct async_cow *async_cow;
1052 struct btrfs_root *root;
1053 unsigned long nr_pages;
1054
1055 async_cow = container_of(work, struct async_cow, work);
1056
1057 root = async_cow->root;
1058 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1059 PAGE_CACHE_SHIFT;
1060
Josef Bacik66657b32012-08-01 15:36:24 -04001061 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001062 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001063 waitqueue_active(&root->fs_info->async_submit_wait))
1064 wake_up(&root->fs_info->async_submit_wait);
1065
Chris Masond3977122009-01-05 21:25:51 -05001066 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001067 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001068}
Chris Masonc8b97812008-10-29 14:49:59 -04001069
Chris Mason771ed682008-11-06 22:02:51 -05001070static noinline void async_cow_free(struct btrfs_work *work)
1071{
1072 struct async_cow *async_cow;
1073 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001074 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001075 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001076 kfree(async_cow);
1077}
1078
1079static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1080 u64 start, u64 end, int *page_started,
1081 unsigned long *nr_written)
1082{
1083 struct async_cow *async_cow;
1084 struct btrfs_root *root = BTRFS_I(inode)->root;
1085 unsigned long nr_pages;
1086 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001087 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001088
Chris Masona3429ab2009-10-08 12:30:20 -04001089 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1090 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001091 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001092 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001093 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001094 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001095 async_cow->root = root;
1096 async_cow->locked_page = locked_page;
1097 async_cow->start = start;
1098
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001099 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001100 cur_end = end;
1101 else
1102 cur_end = min(end, start + 512 * 1024 - 1);
1103
1104 async_cow->end = cur_end;
1105 INIT_LIST_HEAD(&async_cow->extents);
1106
1107 async_cow->work.func = async_cow_start;
1108 async_cow->work.ordered_func = async_cow_submit;
1109 async_cow->work.ordered_free = async_cow_free;
1110 async_cow->work.flags = 0;
1111
Chris Mason771ed682008-11-06 22:02:51 -05001112 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1113 PAGE_CACHE_SHIFT;
1114 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1115
1116 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1117 &async_cow->work);
1118
1119 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1120 wait_event(root->fs_info->async_submit_wait,
1121 (atomic_read(&root->fs_info->async_delalloc_pages) <
1122 limit));
1123 }
1124
Chris Masond3977122009-01-05 21:25:51 -05001125 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001126 atomic_read(&root->fs_info->async_delalloc_pages)) {
1127 wait_event(root->fs_info->async_submit_wait,
1128 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1129 0));
1130 }
1131
1132 *nr_written += nr_pages;
1133 start = cur_end + 1;
1134 }
1135 *page_started = 1;
1136 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001137}
1138
Chris Masond3977122009-01-05 21:25:51 -05001139static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001140 u64 bytenr, u64 num_bytes)
1141{
1142 int ret;
1143 struct btrfs_ordered_sum *sums;
1144 LIST_HEAD(list);
1145
Yan Zheng07d400a2009-01-06 11:42:00 -05001146 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001147 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001148 if (ret == 0 && list_empty(&list))
1149 return 0;
1150
1151 while (!list_empty(&list)) {
1152 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1153 list_del(&sums->list);
1154 kfree(sums);
1155 }
1156 return 1;
1157}
1158
Chris Masond352ac62008-09-29 15:18:18 -04001159/*
1160 * when nowcow writeback call back. This checks for snapshots or COW copies
1161 * of the extents that exist in the file, and COWs the file as required.
1162 *
1163 * If no cow copies or snapshots exist, we write directly to the existing
1164 * blocks on disk
1165 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001166static noinline int run_delalloc_nocow(struct inode *inode,
1167 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001168 u64 start, u64 end, int *page_started, int force,
1169 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001170{
Chris Masonbe20aa92007-12-17 20:14:01 -05001171 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001172 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001173 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001174 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001175 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001176 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001177 u64 cow_start;
1178 u64 cur_offset;
1179 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001180 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001181 u64 disk_bytenr;
1182 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001183 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001184 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001185 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001186 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001187 int nocow;
1188 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001189 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001190 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001191
1192 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001193 if (!path) {
1194 extent_clear_unlock_delalloc(inode,
1195 &BTRFS_I(inode)->io_tree,
1196 start, end, locked_page,
1197 EXTENT_CLEAR_UNLOCK_PAGE |
1198 EXTENT_CLEAR_UNLOCK |
1199 EXTENT_CLEAR_DELALLOC |
1200 EXTENT_CLEAR_DIRTY |
1201 EXTENT_SET_WRITEBACK |
1202 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001203 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001204 }
Li Zefan82d59022011-04-20 10:33:24 +08001205
Liu Bo83eea1f2012-07-10 05:28:39 -06001206 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001207
1208 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001209 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001210 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001211 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001212
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001213 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001214 extent_clear_unlock_delalloc(inode,
1215 &BTRFS_I(inode)->io_tree,
1216 start, end, locked_page,
1217 EXTENT_CLEAR_UNLOCK_PAGE |
1218 EXTENT_CLEAR_UNLOCK |
1219 EXTENT_CLEAR_DELALLOC |
1220 EXTENT_CLEAR_DIRTY |
1221 EXTENT_SET_WRITEBACK |
1222 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001223 btrfs_free_path(path);
1224 return PTR_ERR(trans);
1225 }
1226
Josef Bacik74b21072011-04-13 12:02:53 -04001227 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001228
Yan Zheng80ff3852008-10-30 14:20:02 -04001229 cow_start = (u64)-1;
1230 cur_offset = start;
1231 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001232 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001233 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001234 if (ret < 0) {
1235 btrfs_abort_transaction(trans, root, ret);
1236 goto error;
1237 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1239 leaf = path->nodes[0];
1240 btrfs_item_key_to_cpu(leaf, &found_key,
1241 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001242 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 found_key.type == BTRFS_EXTENT_DATA_KEY)
1244 path->slots[0]--;
1245 }
1246 check_prev = 0;
1247next_slot:
1248 leaf = path->nodes[0];
1249 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1250 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001251 if (ret < 0) {
1252 btrfs_abort_transaction(trans, root, ret);
1253 goto error;
1254 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001255 if (ret > 0)
1256 break;
1257 leaf = path->nodes[0];
1258 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001259
Yan Zheng80ff3852008-10-30 14:20:02 -04001260 nocow = 0;
1261 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001262 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001264
Li Zefan33345d012011-04-20 10:31:50 +08001265 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1267 found_key.offset > end)
1268 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001269
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 if (found_key.offset > cur_offset) {
1271 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001272 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001273 goto out_check;
1274 }
Chris Masonc31f8832008-01-08 15:46:31 -05001275
Yan Zheng80ff3852008-10-30 14:20:02 -04001276 fi = btrfs_item_ptr(leaf, path->slots[0],
1277 struct btrfs_file_extent_item);
1278 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001279
Yan Zhengd899e052008-10-30 14:25:28 -04001280 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1281 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001283 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001284 extent_end = found_key.offset +
1285 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001286 disk_num_bytes =
1287 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001288 if (extent_end <= start) {
1289 path->slots[0]++;
1290 goto next_slot;
1291 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001292 if (disk_bytenr == 0)
1293 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 if (btrfs_file_extent_compression(leaf, fi) ||
1295 btrfs_file_extent_encryption(leaf, fi) ||
1296 btrfs_file_extent_other_encoding(leaf, fi))
1297 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001298 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1299 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001300 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001302 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001303 found_key.offset -
1304 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001305 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001306 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001307 disk_bytenr += cur_offset - found_key.offset;
1308 num_bytes = min(end + 1, extent_end) - cur_offset;
1309 /*
1310 * force cow if csum exists in the range.
1311 * this ensure that csum for a given extent are
1312 * either valid or do not exist.
1313 */
1314 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1315 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 nocow = 1;
1317 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1318 extent_end = found_key.offset +
1319 btrfs_file_extent_inline_len(leaf, fi);
1320 extent_end = ALIGN(extent_end, root->sectorsize);
1321 } else {
1322 BUG_ON(1);
1323 }
1324out_check:
1325 if (extent_end <= start) {
1326 path->slots[0]++;
1327 goto next_slot;
1328 }
1329 if (!nocow) {
1330 if (cow_start == (u64)-1)
1331 cow_start = cur_offset;
1332 cur_offset = extent_end;
1333 if (cur_offset > end)
1334 break;
1335 path->slots[0]++;
1336 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001337 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001338
David Sterbab3b4aa72011-04-21 01:20:15 +02001339 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001340 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001341 ret = __cow_file_range(trans, inode, root, locked_page,
1342 cow_start, found_key.offset - 1,
1343 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001344 if (ret) {
1345 btrfs_abort_transaction(trans, root, ret);
1346 goto error;
1347 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001349 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001350
Yan Zhengd899e052008-10-30 14:25:28 -04001351 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1352 struct extent_map *em;
1353 struct extent_map_tree *em_tree;
1354 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001355 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001356 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001357 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001358 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001359 em->len = num_bytes;
1360 em->block_len = num_bytes;
1361 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001362 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001363 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001364 em->mod_start = em->start;
1365 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001366 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001367 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001368 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001369 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001370 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001371 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001372 if (!ret)
1373 list_move(&em->list,
1374 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001375 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001376 if (ret != -EEXIST) {
1377 free_extent_map(em);
1378 break;
1379 }
1380 btrfs_drop_extent_cache(inode, em->start,
1381 em->start + em->len - 1, 0);
1382 }
1383 type = BTRFS_ORDERED_PREALLOC;
1384 } else {
1385 type = BTRFS_ORDERED_NOCOW;
1386 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001387
1388 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001389 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001390 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001391
Yan, Zhengefa56462010-05-16 10:49:59 -04001392 if (root->root_key.objectid ==
1393 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1394 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1395 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001396 if (ret) {
1397 btrfs_abort_transaction(trans, root, ret);
1398 goto error;
1399 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001400 }
1401
Yan Zhengd899e052008-10-30 14:25:28 -04001402 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001403 cur_offset, cur_offset + num_bytes - 1,
1404 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1405 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1406 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001407 cur_offset = extent_end;
1408 if (cur_offset > end)
1409 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001410 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001411 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001412
Josef Bacik17ca04a2012-05-31 15:58:55 -04001413 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001414 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001415 cur_offset = end;
1416 }
1417
Yan Zheng80ff3852008-10-30 14:20:02 -04001418 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001419 ret = __cow_file_range(trans, inode, root, locked_page,
1420 cow_start, end,
1421 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001422 if (ret) {
1423 btrfs_abort_transaction(trans, root, ret);
1424 goto error;
1425 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001426 }
1427
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001428error:
Miao Xiea698d0752012-09-20 01:51:59 -06001429 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001430 if (!ret)
1431 ret = err;
1432
Josef Bacik17ca04a2012-05-31 15:58:55 -04001433 if (ret && cur_offset < end)
1434 extent_clear_unlock_delalloc(inode,
1435 &BTRFS_I(inode)->io_tree,
1436 cur_offset, end, locked_page,
1437 EXTENT_CLEAR_UNLOCK_PAGE |
1438 EXTENT_CLEAR_UNLOCK |
1439 EXTENT_CLEAR_DELALLOC |
1440 EXTENT_CLEAR_DIRTY |
1441 EXTENT_SET_WRITEBACK |
1442 EXTENT_END_WRITEBACK);
1443
Yan Zheng7ea394f2008-08-05 13:05:02 -04001444 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001445 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001446}
1447
Chris Masond352ac62008-09-29 15:18:18 -04001448/*
1449 * extent_io.c call back to do delayed allocation processing
1450 */
Chris Masonc8b97812008-10-29 14:49:59 -04001451static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001452 u64 start, u64 end, int *page_started,
1453 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001454{
Chris Masonbe20aa92007-12-17 20:14:01 -05001455 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001456 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001457
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001458 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001459 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001460 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001461 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001462 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001463 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001464 } else if (!btrfs_test_opt(root, COMPRESS) &&
1465 !(BTRFS_I(inode)->force_compress) &&
1466 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001467 ret = cow_file_range(inode, locked_page, start, end,
1468 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001469 } else {
1470 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1471 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001472 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001473 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001474 }
Chris Masonb888db22007-08-27 16:49:44 -04001475 return ret;
1476}
1477
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001478static void btrfs_split_extent_hook(struct inode *inode,
1479 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001480{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001481 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001482 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001483 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001484
Josef Bacik9e0baf62011-07-15 15:16:44 +00001485 spin_lock(&BTRFS_I(inode)->lock);
1486 BTRFS_I(inode)->outstanding_extents++;
1487 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001488}
1489
1490/*
1491 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1492 * extents so we can keep track of new extents that are just merged onto old
1493 * extents, such as when we are doing sequential writes, so we can properly
1494 * account for the metadata space we'll need.
1495 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001496static void btrfs_merge_extent_hook(struct inode *inode,
1497 struct extent_state *new,
1498 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001499{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001500 /* not delalloc, ignore it */
1501 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001502 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001503
Josef Bacik9e0baf62011-07-15 15:16:44 +00001504 spin_lock(&BTRFS_I(inode)->lock);
1505 BTRFS_I(inode)->outstanding_extents--;
1506 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001507}
1508
Chris Masond352ac62008-09-29 15:18:18 -04001509/*
1510 * extent_io.c set_bit_hook, used to track delayed allocation
1511 * bytes in this file, and to maintain the list of inodes that
1512 * have pending delalloc work to be done.
1513 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001514static void btrfs_set_bit_hook(struct inode *inode,
1515 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001516{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001517
Chris Mason75eff682008-12-15 15:54:40 -05001518 /*
1519 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001520 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001521 * bit, which is only set or cleared with irqs on
1522 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001523 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001524 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001525 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001526 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001527
Josef Bacik9e0baf62011-07-15 15:16:44 +00001528 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001529 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001530 } else {
1531 spin_lock(&BTRFS_I(inode)->lock);
1532 BTRFS_I(inode)->outstanding_extents++;
1533 spin_unlock(&BTRFS_I(inode)->lock);
1534 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001535
Miao Xie963d6782013-01-29 10:10:51 +00001536 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1537 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001538 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001539 BTRFS_I(inode)->delalloc_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001540 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1541 &BTRFS_I(inode)->runtime_flags)) {
1542 spin_lock(&root->fs_info->delalloc_lock);
1543 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1544 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1545 &root->fs_info->delalloc_inodes);
1546 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1547 &BTRFS_I(inode)->runtime_flags);
1548 }
1549 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001550 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001551 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001552 }
Chris Mason291d6732008-01-29 15:55:23 -05001553}
1554
Chris Masond352ac62008-09-29 15:18:18 -04001555/*
1556 * extent_io.c clear_bit_hook, see set_bit_hook for why
1557 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001558static void btrfs_clear_bit_hook(struct inode *inode,
1559 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001560{
Chris Mason75eff682008-12-15 15:54:40 -05001561 /*
1562 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001563 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001564 * bit, which is only set or cleared with irqs on
1565 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001566 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001567 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001568 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001569 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001570
Josef Bacik9e0baf62011-07-15 15:16:44 +00001571 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001572 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001573 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1574 spin_lock(&BTRFS_I(inode)->lock);
1575 BTRFS_I(inode)->outstanding_extents--;
1576 spin_unlock(&BTRFS_I(inode)->lock);
1577 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001578
1579 if (*bits & EXTENT_DO_ACCOUNTING)
1580 btrfs_delalloc_release_metadata(inode, len);
1581
Josef Bacik0cb59c92010-07-02 12:14:14 -04001582 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1583 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001584 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001585
Miao Xie963d6782013-01-29 10:10:51 +00001586 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1587 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001588 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001589 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001590 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001591 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1592 &BTRFS_I(inode)->runtime_flags)) {
1593 spin_lock(&root->fs_info->delalloc_lock);
1594 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1595 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1596 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1597 &BTRFS_I(inode)->runtime_flags);
1598 }
1599 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001600 }
Miao Xiedf0af1a2013-01-29 10:11:59 +00001601 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001602 }
Chris Mason291d6732008-01-29 15:55:23 -05001603}
1604
Chris Masond352ac62008-09-29 15:18:18 -04001605/*
1606 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1607 * we don't create bios that span stripes or chunks
1608 */
David Woodhouse64a16702009-07-15 23:29:37 +01001609int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001610 size_t size, struct bio *bio,
1611 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001612{
1613 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001614 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001615 u64 length = 0;
1616 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001617 int ret;
1618
Chris Mason771ed682008-11-06 22:02:51 -05001619 if (bio_flags & EXTENT_BIO_COMPRESSED)
1620 return 0;
1621
Chris Masonf2d8d742008-04-21 10:03:05 -04001622 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001623 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001624 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001625 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001626 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001627 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001628 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001629 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001630 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001631}
1632
Chris Masond352ac62008-09-29 15:18:18 -04001633/*
1634 * in order to insert checksums into the metadata in large chunks,
1635 * we wait until bio submission time. All the pages in the bio are
1636 * checksummed and sums are attached onto the ordered extent record.
1637 *
1638 * At IO completion time the cums attached on the ordered extent record
1639 * are inserted into the btree
1640 */
Chris Masond3977122009-01-05 21:25:51 -05001641static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1642 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001643 unsigned long bio_flags,
1644 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001645{
Chris Mason065631f2008-02-20 12:07:25 -05001646 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001647 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001648
Chris Masond20f7042008-12-08 16:58:54 -05001649 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001650 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001651 return 0;
1652}
Chris Masone0156402008-04-16 11:15:20 -04001653
Chris Mason4a69a412008-11-06 22:03:00 -05001654/*
1655 * in order to insert checksums into the metadata in large chunks,
1656 * we wait until bio submission time. All the pages in the bio are
1657 * checksummed and sums are attached onto the ordered extent record.
1658 *
1659 * At IO completion time the cums attached on the ordered extent record
1660 * are inserted into the btree
1661 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001662static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001663 int mirror_num, unsigned long bio_flags,
1664 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001665{
1666 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001667 int ret;
1668
1669 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1670 if (ret)
1671 bio_endio(bio, ret);
1672 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001673}
1674
Chris Masond352ac62008-09-29 15:18:18 -04001675/*
Chris Masoncad321a2008-12-17 14:51:42 -05001676 * extent_io.c submission hook. This does the right thing for csum calculation
1677 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001678 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001679static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001680 int mirror_num, unsigned long bio_flags,
1681 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001682{
1683 struct btrfs_root *root = BTRFS_I(inode)->root;
1684 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001685 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001686 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001687 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001688
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001689 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001690
Liu Bo83eea1f2012-07-10 05:28:39 -06001691 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001692 metadata = 2;
1693
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001694 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001695 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1696 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001697 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001698
Chris Masond20f7042008-12-08 16:58:54 -05001699 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001700 ret = btrfs_submit_compressed_read(inode, bio,
1701 mirror_num,
1702 bio_flags);
1703 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001704 } else if (!skip_sum) {
1705 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1706 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001707 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001708 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001709 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001710 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001711 /* csum items have already been cloned */
1712 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1713 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001714 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001715 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001716 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001717 bio_flags, bio_offset,
1718 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001719 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001720 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001721 } else if (!skip_sum) {
1722 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1723 if (ret)
1724 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001725 }
1726
Chris Mason0b86a832008-03-24 15:01:56 -04001727mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001728 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1729
1730out:
1731 if (ret < 0)
1732 bio_endio(bio, ret);
1733 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001734}
Chris Mason6885f302008-02-20 16:11:05 -05001735
Chris Masond352ac62008-09-29 15:18:18 -04001736/*
1737 * given a list of ordered sums record them in the inode. This happens
1738 * at IO completion time based on sums calculated at bio submission time.
1739 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001740static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001741 struct inode *inode, u64 file_offset,
1742 struct list_head *list)
1743{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001744 struct btrfs_ordered_sum *sum;
1745
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001746 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001747 btrfs_csum_file_blocks(trans,
1748 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001749 }
1750 return 0;
1751}
1752
Josef Bacik2ac55d42010-02-03 19:33:23 +00001753int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1754 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001755{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001756 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001757 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001758 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001759}
1760
Chris Masond352ac62008-09-29 15:18:18 -04001761/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001762struct btrfs_writepage_fixup {
1763 struct page *page;
1764 struct btrfs_work work;
1765};
1766
Christoph Hellwigb2950862008-12-02 09:54:17 -05001767static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001768{
1769 struct btrfs_writepage_fixup *fixup;
1770 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001771 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001772 struct page *page;
1773 struct inode *inode;
1774 u64 page_start;
1775 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001776 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001777
1778 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1779 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001780again:
Chris Mason247e7432008-07-17 12:53:51 -04001781 lock_page(page);
1782 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1783 ClearPageChecked(page);
1784 goto out_page;
1785 }
1786
1787 inode = page->mapping->host;
1788 page_start = page_offset(page);
1789 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1790
Josef Bacik2ac55d42010-02-03 19:33:23 +00001791 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001792 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001793
1794 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001795 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001796 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001797
1798 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1799 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001800 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1801 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001802 unlock_page(page);
1803 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001804 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001805 goto again;
1806 }
Chris Mason247e7432008-07-17 12:53:51 -04001807
Jeff Mahoney87826df2012-02-15 16:23:57 +01001808 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1809 if (ret) {
1810 mapping_set_error(page->mapping, ret);
1811 end_extent_writepage(page, ret, page_start, page_end);
1812 ClearPageChecked(page);
1813 goto out;
1814 }
1815
Josef Bacik2ac55d42010-02-03 19:33:23 +00001816 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001817 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001818 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001819out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001820 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1821 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001822out_page:
1823 unlock_page(page);
1824 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001825 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001826}
1827
1828/*
1829 * There are a few paths in the higher layers of the kernel that directly
1830 * set the page dirty bit without asking the filesystem if it is a
1831 * good idea. This causes problems because we want to make sure COW
1832 * properly happens and the data=ordered rules are followed.
1833 *
Chris Masonc8b97812008-10-29 14:49:59 -04001834 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001835 * hasn't been properly setup for IO. We kick off an async process
1836 * to fix it up. The async helper will wait for ordered extents, set
1837 * the delalloc bit and make it safe to write the page.
1838 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001839static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001840{
1841 struct inode *inode = page->mapping->host;
1842 struct btrfs_writepage_fixup *fixup;
1843 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001844
Chris Mason8b62b722009-09-02 16:53:46 -04001845 /* this page is properly in the ordered list */
1846 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001847 return 0;
1848
1849 if (PageChecked(page))
1850 return -EAGAIN;
1851
1852 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1853 if (!fixup)
1854 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001855
Chris Mason247e7432008-07-17 12:53:51 -04001856 SetPageChecked(page);
1857 page_cache_get(page);
1858 fixup->work.func = btrfs_writepage_fixup_worker;
1859 fixup->page = page;
1860 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001861 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001862}
1863
Yan Zhengd899e052008-10-30 14:25:28 -04001864static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1865 struct inode *inode, u64 file_pos,
1866 u64 disk_bytenr, u64 disk_num_bytes,
1867 u64 num_bytes, u64 ram_bytes,
1868 u8 compression, u8 encryption,
1869 u16 other_encoding, int extent_type)
1870{
1871 struct btrfs_root *root = BTRFS_I(inode)->root;
1872 struct btrfs_file_extent_item *fi;
1873 struct btrfs_path *path;
1874 struct extent_buffer *leaf;
1875 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001876 int ret;
1877
1878 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001879 if (!path)
1880 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001881
Chris Masonb9473432009-03-13 11:00:37 -04001882 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001883
1884 /*
1885 * we may be replacing one extent in the tree with another.
1886 * The new extent is pinned in the extent map, and we don't want
1887 * to drop it from the cache until it is completely in the btree.
1888 *
1889 * So, tell btrfs_drop_extents to leave this extent in the cache.
1890 * the caller is expected to unpin it and allow it to be merged
1891 * with the others.
1892 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001893 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001894 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001895 if (ret)
1896 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001897
Li Zefan33345d012011-04-20 10:31:50 +08001898 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001899 ins.offset = file_pos;
1900 ins.type = BTRFS_EXTENT_DATA_KEY;
1901 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001902 if (ret)
1903 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001904 leaf = path->nodes[0];
1905 fi = btrfs_item_ptr(leaf, path->slots[0],
1906 struct btrfs_file_extent_item);
1907 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1908 btrfs_set_file_extent_type(leaf, fi, extent_type);
1909 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1910 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1911 btrfs_set_file_extent_offset(leaf, fi, 0);
1912 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1913 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1914 btrfs_set_file_extent_compression(leaf, fi, compression);
1915 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1916 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001917
Yan Zhengd899e052008-10-30 14:25:28 -04001918 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001919 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001920
1921 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001922
1923 ins.objectid = disk_bytenr;
1924 ins.offset = disk_num_bytes;
1925 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001926 ret = btrfs_alloc_reserved_file_extent(trans, root,
1927 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001928 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001929out:
Yan Zhengd899e052008-10-30 14:25:28 -04001930 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001931
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001932 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001933}
1934
Chris Mason5d13a982009-03-13 11:41:46 -04001935/*
1936 * helper function for btrfs_finish_ordered_io, this
1937 * just reads in some of the csum leaves to prime them into ram
1938 * before we start the transaction. It limits the amount of btree
1939 * reads required while inside the transaction.
1940 */
Chris Masond352ac62008-09-29 15:18:18 -04001941/* as ordered data IO finishes, this gets called so we can finish
1942 * an ordered extent if the range of bytes in the file it covers are
1943 * fully written.
1944 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001945static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001946{
Josef Bacik5fd02042012-05-02 14:00:54 -04001947 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001948 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001949 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001950 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001951 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001952 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001953 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001954 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001955
Liu Bo83eea1f2012-07-10 05:28:39 -06001956 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001957
Josef Bacik5fd02042012-05-02 14:00:54 -04001958 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1959 ret = -EIO;
1960 goto out;
1961 }
1962
Yan, Zhengc2167752009-11-12 09:34:21 +00001963 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001964 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05001965 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1966 if (nolock)
1967 trans = btrfs_join_transaction_nolock(root);
1968 else
1969 trans = btrfs_join_transaction(root);
1970 if (IS_ERR(trans)) {
1971 ret = PTR_ERR(trans);
1972 trans = NULL;
1973 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00001974 }
Josef Bacik6c760c02012-11-09 10:53:21 -05001975 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1976 ret = btrfs_update_inode_fallback(trans, root, inode);
1977 if (ret) /* -ENOMEM or corruption */
1978 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001979 goto out;
1980 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001981
Josef Bacik2ac55d42010-02-03 19:33:23 +00001982 lock_extent_bits(io_tree, ordered_extent->file_offset,
1983 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001984 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001985
Josef Bacik0cb59c92010-07-02 12:14:14 -04001986 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001987 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001988 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001989 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001990 if (IS_ERR(trans)) {
1991 ret = PTR_ERR(trans);
1992 trans = NULL;
1993 goto out_unlock;
1994 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001995 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001996
Chris Masonc8b97812008-10-29 14:49:59 -04001997 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001998 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001999 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002000 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002001 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002002 ordered_extent->file_offset,
2003 ordered_extent->file_offset +
2004 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04002005 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002006 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002007 ret = insert_reserved_file_extent(trans, inode,
2008 ordered_extent->file_offset,
2009 ordered_extent->start,
2010 ordered_extent->disk_len,
2011 ordered_extent->len,
2012 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08002013 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002014 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04002015 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002016 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2017 ordered_extent->file_offset, ordered_extent->len,
2018 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002019 if (ret < 0) {
2020 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002021 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002022 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002023
Chris Masone6dcd2d2008-07-17 12:53:50 -04002024 add_pending_csums(trans, inode, ordered_extent->file_offset,
2025 &ordered_extent->list);
2026
Josef Bacik6c760c02012-11-09 10:53:21 -05002027 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2028 ret = btrfs_update_inode_fallback(trans, root, inode);
2029 if (ret) { /* -ENOMEM or corruption */
2030 btrfs_abort_transaction(trans, root, ret);
2031 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002032 }
2033 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002034out_unlock:
2035 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2036 ordered_extent->file_offset +
2037 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002038out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002039 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002040 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002041 if (trans)
2042 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002043
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002044 if (ret) {
Josef Bacik5fd02042012-05-02 14:00:54 -04002045 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2046 ordered_extent->file_offset +
2047 ordered_extent->len - 1, NULL, GFP_NOFS);
2048
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002049 /*
2050 * If the ordered extent had an IOERR or something else went
2051 * wrong we need to return the space for this ordered extent
2052 * back to the allocator.
2053 */
2054 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2055 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2056 btrfs_free_reserved_extent(root, ordered_extent->start,
2057 ordered_extent->disk_len);
2058 }
2059
2060
Josef Bacik5fd02042012-05-02 14:00:54 -04002061 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002062 * This needs to be done to make sure anybody waiting knows we are done
2063 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002064 */
2065 btrfs_remove_ordered_extent(inode, ordered_extent);
2066
Chris Masone6dcd2d2008-07-17 12:53:50 -04002067 /* once for us */
2068 btrfs_put_ordered_extent(ordered_extent);
2069 /* once for the tree */
2070 btrfs_put_ordered_extent(ordered_extent);
2071
Josef Bacik5fd02042012-05-02 14:00:54 -04002072 return ret;
2073}
2074
2075static void finish_ordered_fn(struct btrfs_work *work)
2076{
2077 struct btrfs_ordered_extent *ordered_extent;
2078 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2079 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002080}
2081
Christoph Hellwigb2950862008-12-02 09:54:17 -05002082static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002083 struct extent_state *state, int uptodate)
2084{
Josef Bacik5fd02042012-05-02 14:00:54 -04002085 struct inode *inode = page->mapping->host;
2086 struct btrfs_root *root = BTRFS_I(inode)->root;
2087 struct btrfs_ordered_extent *ordered_extent = NULL;
2088 struct btrfs_workers *workers;
2089
liubo1abe9b82011-03-24 11:18:59 +00002090 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2091
Chris Mason8b62b722009-09-02 16:53:46 -04002092 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002093 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2094 end - start + 1, uptodate))
2095 return 0;
2096
2097 ordered_extent->work.func = finish_ordered_fn;
2098 ordered_extent->work.flags = 0;
2099
Liu Bo83eea1f2012-07-10 05:28:39 -06002100 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002101 workers = &root->fs_info->endio_freespace_worker;
2102 else
2103 workers = &root->fs_info->endio_write_workers;
2104 btrfs_queue_worker(workers, &ordered_extent->work);
2105
2106 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002107}
2108
Chris Masond352ac62008-09-29 15:18:18 -04002109/*
Chris Masond352ac62008-09-29 15:18:18 -04002110 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002111 * if there's a match, we allow the bio to finish. If not, the code in
2112 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002113 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002114static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002115 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002116{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002117 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002118 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002119 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002120 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002121 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002122 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002123 struct btrfs_root *root = BTRFS_I(inode)->root;
2124 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002125
Chris Masond20f7042008-12-08 16:58:54 -05002126 if (PageChecked(page)) {
2127 ClearPageChecked(page);
2128 goto good;
2129 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002130
2131 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002132 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002133
Yan Zheng17d217f2008-12-12 10:03:38 -05002134 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002135 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002136 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2137 GFP_NOFS);
2138 return 0;
2139 }
2140
Yanc2e639f2008-02-04 08:57:25 -05002141 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002142 private = state->private;
2143 ret = 0;
2144 } else {
2145 ret = get_state_private(io_tree, start, &private);
2146 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002147 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002148 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002149 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002150
Chris Masonff79f812007-10-15 16:22:25 -04002151 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2152 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002153 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002154 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002155
Cong Wang7ac687d2011-11-25 23:14:28 +08002156 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002157good:
Chris Mason07157aa2007-08-30 08:50:51 -04002158 return 0;
2159
2160zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002161 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002162 "private %llu\n",
2163 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002164 (unsigned long long)start, csum,
2165 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002166 memset(kaddr + offset, 1, end - start + 1);
2167 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002168 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002169 if (private == 0)
2170 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002171 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002172}
Chris Masonb888db22007-08-27 16:49:44 -04002173
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002174struct delayed_iput {
2175 struct list_head list;
2176 struct inode *inode;
2177};
2178
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002179/* JDM: If this is fs-wide, why can't we add a pointer to
2180 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002181void btrfs_add_delayed_iput(struct inode *inode)
2182{
2183 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2184 struct delayed_iput *delayed;
2185
2186 if (atomic_add_unless(&inode->i_count, -1, 1))
2187 return;
2188
2189 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2190 delayed->inode = inode;
2191
2192 spin_lock(&fs_info->delayed_iput_lock);
2193 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2194 spin_unlock(&fs_info->delayed_iput_lock);
2195}
2196
2197void btrfs_run_delayed_iputs(struct btrfs_root *root)
2198{
2199 LIST_HEAD(list);
2200 struct btrfs_fs_info *fs_info = root->fs_info;
2201 struct delayed_iput *delayed;
2202 int empty;
2203
2204 spin_lock(&fs_info->delayed_iput_lock);
2205 empty = list_empty(&fs_info->delayed_iputs);
2206 spin_unlock(&fs_info->delayed_iput_lock);
2207 if (empty)
2208 return;
2209
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002210 spin_lock(&fs_info->delayed_iput_lock);
2211 list_splice_init(&fs_info->delayed_iputs, &list);
2212 spin_unlock(&fs_info->delayed_iput_lock);
2213
2214 while (!list_empty(&list)) {
2215 delayed = list_entry(list.next, struct delayed_iput, list);
2216 list_del(&delayed->list);
2217 iput(delayed->inode);
2218 kfree(delayed);
2219 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002220}
2221
Yan, Zhengd68fc572010-05-16 10:49:58 -04002222/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002223 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002224 * files in the subvolume, it removes orphan item and frees block_rsv
2225 * structure.
2226 */
2227void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2228 struct btrfs_root *root)
2229{
Josef Bacik90290e12011-12-02 15:44:12 -05002230 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002231 int ret;
2232
Josef Bacik8a35d952012-05-23 14:26:42 -04002233 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002234 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2235 return;
2236
Josef Bacik90290e12011-12-02 15:44:12 -05002237 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002238 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002239 spin_unlock(&root->orphan_lock);
2240 return;
2241 }
2242
2243 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2244 spin_unlock(&root->orphan_lock);
2245 return;
2246 }
2247
2248 block_rsv = root->orphan_block_rsv;
2249 root->orphan_block_rsv = NULL;
2250 spin_unlock(&root->orphan_lock);
2251
Yan, Zhengd68fc572010-05-16 10:49:58 -04002252 if (root->orphan_item_inserted &&
2253 btrfs_root_refs(&root->root_item) > 0) {
2254 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2255 root->root_key.objectid);
2256 BUG_ON(ret);
2257 root->orphan_item_inserted = 0;
2258 }
2259
Josef Bacik90290e12011-12-02 15:44:12 -05002260 if (block_rsv) {
2261 WARN_ON(block_rsv->size > 0);
2262 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002263 }
2264}
2265
2266/*
Josef Bacik7b128762008-07-24 12:17:14 -04002267 * This creates an orphan entry for the given inode in case something goes
2268 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002269 *
2270 * NOTE: caller of this function should reserve 5 units of metadata for
2271 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002272 */
2273int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2274{
2275 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002276 struct btrfs_block_rsv *block_rsv = NULL;
2277 int reserve = 0;
2278 int insert = 0;
2279 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002280
Yan, Zhengd68fc572010-05-16 10:49:58 -04002281 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002282 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002283 if (!block_rsv)
2284 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002285 }
2286
Yan, Zhengd68fc572010-05-16 10:49:58 -04002287 spin_lock(&root->orphan_lock);
2288 if (!root->orphan_block_rsv) {
2289 root->orphan_block_rsv = block_rsv;
2290 } else if (block_rsv) {
2291 btrfs_free_block_rsv(root, block_rsv);
2292 block_rsv = NULL;
2293 }
Josef Bacik7b128762008-07-24 12:17:14 -04002294
Josef Bacik8a35d952012-05-23 14:26:42 -04002295 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2296 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002297#if 0
2298 /*
2299 * For proper ENOSPC handling, we should do orphan
2300 * cleanup when mounting. But this introduces backward
2301 * compatibility issue.
2302 */
2303 if (!xchg(&root->orphan_item_inserted, 1))
2304 insert = 2;
2305 else
2306 insert = 1;
2307#endif
2308 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002309 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002310 }
Josef Bacik7b128762008-07-24 12:17:14 -04002311
Josef Bacik72ac3c02012-05-23 14:13:11 -04002312 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2313 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002314 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002315 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002316
Yan, Zhengd68fc572010-05-16 10:49:58 -04002317 /* grab metadata reservation from transaction handle */
2318 if (reserve) {
2319 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002320 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002321 }
2322
2323 /* insert an orphan item to track this unlinked/truncated file */
2324 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002325 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002326 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002327 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2328 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002329 btrfs_abort_transaction(trans, root, ret);
2330 return ret;
2331 }
2332 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002333 }
2334
2335 /* insert an orphan item to track subvolume contains orphan files */
2336 if (insert >= 2) {
2337 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2338 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002339 if (ret && ret != -EEXIST) {
2340 btrfs_abort_transaction(trans, root, ret);
2341 return ret;
2342 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002343 }
2344 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002345}
2346
2347/*
2348 * We have done the truncate/delete so we can go ahead and remove the orphan
2349 * item for this particular inode.
2350 */
2351int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2352{
2353 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002354 int delete_item = 0;
2355 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002356 int ret = 0;
2357
Yan, Zhengd68fc572010-05-16 10:49:58 -04002358 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002359 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2360 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002361 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002362
Josef Bacik72ac3c02012-05-23 14:13:11 -04002363 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2364 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002365 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002366 spin_unlock(&root->orphan_lock);
2367
2368 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002369 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002370 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002371 }
2372
Josef Bacik8a35d952012-05-23 14:26:42 -04002373 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002374 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002375 atomic_dec(&root->orphan_inodes);
2376 }
Josef Bacik7b128762008-07-24 12:17:14 -04002377
Yan, Zhengd68fc572010-05-16 10:49:58 -04002378 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002379}
2380
2381/*
2382 * this cleans up any orphans that may be left on the list from the last use
2383 * of this root.
2384 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002385int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002386{
2387 struct btrfs_path *path;
2388 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002389 struct btrfs_key key, found_key;
2390 struct btrfs_trans_handle *trans;
2391 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002392 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002393 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2394
Yan, Zhengd68fc572010-05-16 10:49:58 -04002395 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002396 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002397
2398 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002399 if (!path) {
2400 ret = -ENOMEM;
2401 goto out;
2402 }
Josef Bacik7b128762008-07-24 12:17:14 -04002403 path->reada = -1;
2404
2405 key.objectid = BTRFS_ORPHAN_OBJECTID;
2406 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2407 key.offset = (u64)-1;
2408
Josef Bacik7b128762008-07-24 12:17:14 -04002409 while (1) {
2410 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002411 if (ret < 0)
2412 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002413
2414 /*
2415 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002416 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002417 * find the key and see if we have stuff that matches
2418 */
2419 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002420 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002421 if (path->slots[0] == 0)
2422 break;
2423 path->slots[0]--;
2424 }
2425
2426 /* pull out the item */
2427 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002428 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2429
2430 /* make sure the item matches what we want */
2431 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2432 break;
2433 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2434 break;
2435
2436 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002437 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002438
2439 /*
2440 * this is where we are basically btrfs_lookup, without the
2441 * crossing root thing. we store the inode number in the
2442 * offset of the orphan item.
2443 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002444
2445 if (found_key.offset == last_objectid) {
2446 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2447 "stopping orphan cleanup\n");
2448 ret = -EINVAL;
2449 goto out;
2450 }
2451
2452 last_objectid = found_key.offset;
2453
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002454 found_key.objectid = found_key.offset;
2455 found_key.type = BTRFS_INODE_ITEM_KEY;
2456 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002457 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002458 ret = PTR_RET(inode);
2459 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002460 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002461
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002462 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2463 struct btrfs_root *dead_root;
2464 struct btrfs_fs_info *fs_info = root->fs_info;
2465 int is_dead_root = 0;
2466
2467 /*
2468 * this is an orphan in the tree root. Currently these
2469 * could come from 2 sources:
2470 * a) a snapshot deletion in progress
2471 * b) a free space cache inode
2472 * We need to distinguish those two, as the snapshot
2473 * orphan must not get deleted.
2474 * find_dead_roots already ran before us, so if this
2475 * is a snapshot deletion, we should find the root
2476 * in the dead_roots list
2477 */
2478 spin_lock(&fs_info->trans_lock);
2479 list_for_each_entry(dead_root, &fs_info->dead_roots,
2480 root_list) {
2481 if (dead_root->root_key.objectid ==
2482 found_key.objectid) {
2483 is_dead_root = 1;
2484 break;
2485 }
2486 }
2487 spin_unlock(&fs_info->trans_lock);
2488 if (is_dead_root) {
2489 /* prevent this orphan from being found again */
2490 key.offset = found_key.objectid - 1;
2491 continue;
2492 }
2493 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002494 /*
2495 * Inode is already gone but the orphan item is still there,
2496 * kill the orphan item.
2497 */
2498 if (ret == -ESTALE) {
2499 trans = btrfs_start_transaction(root, 1);
2500 if (IS_ERR(trans)) {
2501 ret = PTR_ERR(trans);
2502 goto out;
2503 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002504 printk(KERN_ERR "auto deleting %Lu\n",
2505 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002506 ret = btrfs_del_orphan_item(trans, root,
2507 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002508 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002509 btrfs_end_transaction(trans, root);
2510 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002511 }
Josef Bacik7b128762008-07-24 12:17:14 -04002512
Josef Bacik7b128762008-07-24 12:17:14 -04002513 /*
2514 * add this inode to the orphan list so btrfs_orphan_del does
2515 * the proper thing when we hit it
2516 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002517 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2518 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05002519 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04002520
Josef Bacik7b128762008-07-24 12:17:14 -04002521 /* if we have links, this was a truncate, lets do that */
2522 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002523 if (!S_ISREG(inode->i_mode)) {
2524 WARN_ON(1);
2525 iput(inode);
2526 continue;
2527 }
Josef Bacik7b128762008-07-24 12:17:14 -04002528 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05002529
2530 /* 1 for the orphan item deletion. */
2531 trans = btrfs_start_transaction(root, 1);
2532 if (IS_ERR(trans)) {
2533 ret = PTR_ERR(trans);
2534 goto out;
2535 }
2536 ret = btrfs_orphan_add(trans, inode);
2537 btrfs_end_transaction(trans, root);
2538 if (ret)
2539 goto out;
2540
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002541 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05002542 if (ret)
2543 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002544 } else {
2545 nr_unlink++;
2546 }
2547
2548 /* this will do delete_inode and everything for us */
2549 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002550 if (ret)
2551 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002552 }
Miao Xie3254c872011-11-10 20:45:05 -05002553 /* release the path since we're done with it */
2554 btrfs_release_path(path);
2555
Yan, Zhengd68fc572010-05-16 10:49:58 -04002556 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2557
2558 if (root->orphan_block_rsv)
2559 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2560 (u64)-1);
2561
2562 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002563 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002564 if (!IS_ERR(trans))
2565 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002566 }
Josef Bacik7b128762008-07-24 12:17:14 -04002567
2568 if (nr_unlink)
2569 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2570 if (nr_truncate)
2571 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002572
2573out:
2574 if (ret)
2575 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2576 btrfs_free_path(path);
2577 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002578}
2579
Chris Masond352ac62008-09-29 15:18:18 -04002580/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002581 * very simple check to peek ahead in the leaf looking for xattrs. If we
2582 * don't find any xattrs, we know there can't be any acls.
2583 *
2584 * slot is the slot the inode is in, objectid is the objectid of the inode
2585 */
2586static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2587 int slot, u64 objectid)
2588{
2589 u32 nritems = btrfs_header_nritems(leaf);
2590 struct btrfs_key found_key;
2591 int scanned = 0;
2592
2593 slot++;
2594 while (slot < nritems) {
2595 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2596
2597 /* we found a different objectid, there must not be acls */
2598 if (found_key.objectid != objectid)
2599 return 0;
2600
2601 /* we found an xattr, assume we've got an acl */
2602 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2603 return 1;
2604
2605 /*
2606 * we found a key greater than an xattr key, there can't
2607 * be any acls later on
2608 */
2609 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2610 return 0;
2611
2612 slot++;
2613 scanned++;
2614
2615 /*
2616 * it goes inode, inode backrefs, xattrs, extents,
2617 * so if there are a ton of hard links to an inode there can
2618 * be a lot of backrefs. Don't waste time searching too hard,
2619 * this is just an optimization
2620 */
2621 if (scanned >= 8)
2622 break;
2623 }
2624 /* we hit the end of the leaf before we found an xattr or
2625 * something larger than an xattr. We have to assume the inode
2626 * has acls
2627 */
2628 return 1;
2629}
2630
2631/*
Chris Masond352ac62008-09-29 15:18:18 -04002632 * read an inode from the btree into the in-memory inode
2633 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002634static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002635{
2636 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002637 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002638 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002639 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002640 struct btrfs_root *root = BTRFS_I(inode)->root;
2641 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002642 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002643 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002644 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002645 bool filled = false;
2646
2647 ret = btrfs_fill_inode(inode, &rdev);
2648 if (!ret)
2649 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002650
2651 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002652 if (!path)
2653 goto make_bad;
2654
Josef Bacikd90c7322011-05-17 09:50:54 -04002655 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002656 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002657
Chris Mason39279cc2007-06-12 06:35:45 -04002658 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002659 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002660 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002661
Chris Mason5f39d392007-10-15 16:14:19 -04002662 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002663
2664 if (filled)
2665 goto cache_acl;
2666
Chris Mason5f39d392007-10-15 16:14:19 -04002667 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2668 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002669 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002670 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002671 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2672 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002673 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002674
2675 tspec = btrfs_inode_atime(inode_item);
2676 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2677 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2678
2679 tspec = btrfs_inode_mtime(inode_item);
2680 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2681 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2682
2683 tspec = btrfs_inode_ctime(inode_item);
2684 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2685 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2686
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002687 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002688 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002689 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2690
2691 /*
2692 * If we were modified in the current generation and evicted from memory
2693 * and then re-read we need to do a full sync since we don't have any
2694 * idea about which extents were modified before we were evicted from
2695 * cache.
2696 */
2697 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2698 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2699 &BTRFS_I(inode)->runtime_flags);
2700
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002701 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002702 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002703 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002704 rdev = btrfs_inode_rdev(leaf, inode_item);
2705
Josef Bacikaec74772008-07-24 12:12:38 -04002706 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002707 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002708cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002709 /*
2710 * try to precache a NULL acl entry for files that don't have
2711 * any xattrs or acls
2712 */
Li Zefan33345d012011-04-20 10:31:50 +08002713 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2714 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002715 if (!maybe_acls)
2716 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002717
Chris Mason39279cc2007-06-12 06:35:45 -04002718 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002719
Chris Mason39279cc2007-06-12 06:35:45 -04002720 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002721 case S_IFREG:
2722 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002723 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002724 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002725 inode->i_fop = &btrfs_file_operations;
2726 inode->i_op = &btrfs_file_inode_operations;
2727 break;
2728 case S_IFDIR:
2729 inode->i_fop = &btrfs_dir_file_operations;
2730 if (root == root->fs_info->tree_root)
2731 inode->i_op = &btrfs_dir_ro_inode_operations;
2732 else
2733 inode->i_op = &btrfs_dir_inode_operations;
2734 break;
2735 case S_IFLNK:
2736 inode->i_op = &btrfs_symlink_inode_operations;
2737 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002738 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002739 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002740 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002741 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002742 init_special_inode(inode, inode->i_mode, rdev);
2743 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002744 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002745
2746 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002747 return;
2748
2749make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002750 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002751 make_bad_inode(inode);
2752}
2753
Chris Masond352ac62008-09-29 15:18:18 -04002754/*
2755 * given a leaf and an inode, copy the inode fields into the leaf
2756 */
Chris Masone02119d2008-09-05 16:13:11 -04002757static void fill_inode_item(struct btrfs_trans_handle *trans,
2758 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002759 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002760 struct inode *inode)
2761{
Liu Bo51fab692012-12-27 09:01:21 +00002762 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002763
Liu Bo51fab692012-12-27 09:01:21 +00002764 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04002765
Liu Bo51fab692012-12-27 09:01:21 +00002766 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
2767 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
2768 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
2769 &token);
2770 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
2771 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002772
Liu Bo51fab692012-12-27 09:01:21 +00002773 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
2774 inode->i_atime.tv_sec, &token);
2775 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
2776 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002777
Liu Bo51fab692012-12-27 09:01:21 +00002778 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
2779 inode->i_mtime.tv_sec, &token);
2780 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
2781 inode->i_mtime.tv_nsec, &token);
2782
2783 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
2784 inode->i_ctime.tv_sec, &token);
2785 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
2786 inode->i_ctime.tv_nsec, &token);
2787
2788 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
2789 &token);
2790 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
2791 &token);
2792 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
2793 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
2794 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
2795 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
2796 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04002797}
2798
Chris Masond352ac62008-09-29 15:18:18 -04002799/*
2800 * copy everything in the in-memory inode into the btree.
2801 */
Chris Mason21151332011-11-10 20:39:08 -05002802static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002803 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002804{
2805 struct btrfs_inode_item *inode_item;
2806 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002807 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002808 int ret;
2809
2810 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002811 if (!path)
2812 return -ENOMEM;
2813
Chris Masonb9473432009-03-13 11:00:37 -04002814 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002815 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2816 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002817 if (ret) {
2818 if (ret > 0)
2819 ret = -ENOENT;
2820 goto failed;
2821 }
2822
Chris Masonb4ce94d2009-02-04 09:25:08 -05002823 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002824 leaf = path->nodes[0];
2825 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002826 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002827
Chris Masone02119d2008-09-05 16:13:11 -04002828 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002829 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002830 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002831 ret = 0;
2832failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002833 btrfs_free_path(path);
2834 return ret;
2835}
2836
Chris Masond352ac62008-09-29 15:18:18 -04002837/*
Chris Mason21151332011-11-10 20:39:08 -05002838 * copy everything in the in-memory inode into the btree.
2839 */
2840noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2841 struct btrfs_root *root, struct inode *inode)
2842{
2843 int ret;
2844
2845 /*
2846 * If the inode is a free space inode, we can deadlock during commit
2847 * if we put it into the delayed code.
2848 *
2849 * The data relocation inode should also be directly updated
2850 * without delay
2851 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002852 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002853 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002854 btrfs_update_root_times(trans, root);
2855
Chris Mason21151332011-11-10 20:39:08 -05002856 ret = btrfs_delayed_update_inode(trans, root, inode);
2857 if (!ret)
2858 btrfs_set_inode_last_trans(trans, inode);
2859 return ret;
2860 }
2861
2862 return btrfs_update_inode_item(trans, root, inode);
2863}
2864
Josef Bacikbe6aef62012-10-22 15:43:12 -04002865noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2866 struct btrfs_root *root,
2867 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002868{
2869 int ret;
2870
2871 ret = btrfs_update_inode(trans, root, inode);
2872 if (ret == -ENOSPC)
2873 return btrfs_update_inode_item(trans, root, inode);
2874 return ret;
2875}
2876
2877/*
Chris Masond352ac62008-09-29 15:18:18 -04002878 * unlink helper that gets used here in inode.c and in the tree logging
2879 * recovery code. It remove a link in a directory with a given name, and
2880 * also drops the back refs in the inode to the directory
2881 */
Al Viro92986792011-03-04 17:14:37 +00002882static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2883 struct btrfs_root *root,
2884 struct inode *dir, struct inode *inode,
2885 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002886{
2887 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002888 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002889 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002890 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002891 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002892 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002893 u64 ino = btrfs_ino(inode);
2894 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002895
2896 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002897 if (!path) {
2898 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002899 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002900 }
2901
Chris Masonb9473432009-03-13 11:00:37 -04002902 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002903 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002904 name, name_len, -1);
2905 if (IS_ERR(di)) {
2906 ret = PTR_ERR(di);
2907 goto err;
2908 }
2909 if (!di) {
2910 ret = -ENOENT;
2911 goto err;
2912 }
Chris Mason5f39d392007-10-15 16:14:19 -04002913 leaf = path->nodes[0];
2914 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002915 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002916 if (ret)
2917 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002918 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002919
Li Zefan33345d012011-04-20 10:31:50 +08002920 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2921 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002922 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002923 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002924 "inode %llu parent %llu\n", name_len, name,
2925 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002926 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002927 goto err;
2928 }
2929
Miao Xie16cdcec2011-04-22 18:12:22 +08002930 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002931 if (ret) {
2932 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002933 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002934 }
Chris Mason39279cc2007-06-12 06:35:45 -04002935
Chris Masone02119d2008-09-05 16:13:11 -04002936 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002937 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002938 if (ret != 0 && ret != -ENOENT) {
2939 btrfs_abort_transaction(trans, root, ret);
2940 goto err;
2941 }
Chris Masone02119d2008-09-05 16:13:11 -04002942
2943 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2944 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002945 if (ret == -ENOENT)
2946 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002947err:
2948 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002949 if (ret)
2950 goto out;
2951
2952 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002953 inode_inc_iversion(inode);
2954 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002955 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002956 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002957out:
Chris Mason39279cc2007-06-12 06:35:45 -04002958 return ret;
2959}
2960
Al Viro92986792011-03-04 17:14:37 +00002961int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2962 struct btrfs_root *root,
2963 struct inode *dir, struct inode *inode,
2964 const char *name, int name_len)
2965{
2966 int ret;
2967 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2968 if (!ret) {
2969 btrfs_drop_nlink(inode);
2970 ret = btrfs_update_inode(trans, root, inode);
2971 }
2972 return ret;
2973}
2974
2975
Yan, Zhenga22285a2010-05-16 10:48:46 -04002976/* helper to check if there is any shared block in the path */
2977static int check_path_shared(struct btrfs_root *root,
2978 struct btrfs_path *path)
2979{
2980 struct extent_buffer *eb;
2981 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002982 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002983
2984 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002985 int ret;
2986
Yan, Zhenga22285a2010-05-16 10:48:46 -04002987 if (!path->nodes[level])
2988 break;
2989 eb = path->nodes[level];
2990 if (!btrfs_block_can_be_shared(root, eb))
2991 continue;
2992 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2993 &refs, NULL);
2994 if (refs > 1)
2995 return 1;
2996 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002997 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002998}
2999
3000/*
3001 * helper to start transaction for unlink and rmdir.
3002 *
3003 * unlink and rmdir are special in btrfs, they do not always free space.
3004 * so in enospc case, we should make sure they will free space before
3005 * allowing them to use the global metadata reservation.
3006 */
3007static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
3008 struct dentry *dentry)
3009{
3010 struct btrfs_trans_handle *trans;
3011 struct btrfs_root *root = BTRFS_I(dir)->root;
3012 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003013 struct btrfs_dir_item *di;
3014 struct inode *inode = dentry->d_inode;
3015 u64 index;
3016 int check_link = 1;
3017 int err = -ENOSPC;
3018 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003019 u64 ino = btrfs_ino(inode);
3020 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003021
Josef Bacike70bea52011-10-11 14:18:24 -04003022 /*
3023 * 1 for the possible orphan item
3024 * 1 for the dir item
3025 * 1 for the dir index
3026 * 1 for the inode ref
3027 * 1 for the inode ref in the tree log
3028 * 2 for the dir entries in the log
3029 * 1 for the inode
3030 */
3031 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003032 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3033 return trans;
3034
Li Zefan33345d012011-04-20 10:31:50 +08003035 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003036 return ERR_PTR(-ENOSPC);
3037
3038 /* check if there is someone else holds reference */
3039 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3040 return ERR_PTR(-ENOSPC);
3041
3042 if (atomic_read(&inode->i_count) > 2)
3043 return ERR_PTR(-ENOSPC);
3044
3045 if (xchg(&root->fs_info->enospc_unlink, 1))
3046 return ERR_PTR(-ENOSPC);
3047
3048 path = btrfs_alloc_path();
3049 if (!path) {
3050 root->fs_info->enospc_unlink = 0;
3051 return ERR_PTR(-ENOMEM);
3052 }
3053
Josef Bacik3880a1b2011-10-14 14:46:51 -04003054 /* 1 for the orphan item */
3055 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003056 if (IS_ERR(trans)) {
3057 btrfs_free_path(path);
3058 root->fs_info->enospc_unlink = 0;
3059 return trans;
3060 }
3061
3062 path->skip_locking = 1;
3063 path->search_commit_root = 1;
3064
3065 ret = btrfs_lookup_inode(trans, root, path,
3066 &BTRFS_I(dir)->location, 0);
3067 if (ret < 0) {
3068 err = ret;
3069 goto out;
3070 }
3071 if (ret == 0) {
3072 if (check_path_shared(root, path))
3073 goto out;
3074 } else {
3075 check_link = 0;
3076 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003077 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003078
3079 ret = btrfs_lookup_inode(trans, root, path,
3080 &BTRFS_I(inode)->location, 0);
3081 if (ret < 0) {
3082 err = ret;
3083 goto out;
3084 }
3085 if (ret == 0) {
3086 if (check_path_shared(root, path))
3087 goto out;
3088 } else {
3089 check_link = 0;
3090 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003091 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003092
3093 if (ret == 0 && S_ISREG(inode->i_mode)) {
3094 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003095 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003096 if (ret < 0) {
3097 err = ret;
3098 goto out;
3099 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003100 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003101 if (check_path_shared(root, path))
3102 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003103 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003104 }
3105
3106 if (!check_link) {
3107 err = 0;
3108 goto out;
3109 }
3110
Li Zefan33345d012011-04-20 10:31:50 +08003111 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003112 dentry->d_name.name, dentry->d_name.len, 0);
3113 if (IS_ERR(di)) {
3114 err = PTR_ERR(di);
3115 goto out;
3116 }
3117 if (di) {
3118 if (check_path_shared(root, path))
3119 goto out;
3120 } else {
3121 err = 0;
3122 goto out;
3123 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003124 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003125
Mark Fashehf1863732012-08-08 11:32:27 -07003126 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3127 dentry->d_name.len, ino, dir_ino, 0,
3128 &index);
3129 if (ret) {
3130 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003131 goto out;
3132 }
Mark Fashehf1863732012-08-08 11:32:27 -07003133
Yan, Zhenga22285a2010-05-16 10:48:46 -04003134 if (check_path_shared(root, path))
3135 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003136
David Sterbab3b4aa72011-04-21 01:20:15 +02003137 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003138
Miao Xie16cdcec2011-04-22 18:12:22 +08003139 /*
3140 * This is a commit root search, if we can lookup inode item and other
3141 * relative items in the commit root, it means the transaction of
3142 * dir/file creation has been committed, and the dir index item that we
3143 * delay to insert has also been inserted into the commit root. So
3144 * we needn't worry about the delayed insertion of the dir index item
3145 * here.
3146 */
Li Zefan33345d012011-04-20 10:31:50 +08003147 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003148 dentry->d_name.name, dentry->d_name.len, 0);
3149 if (IS_ERR(di)) {
3150 err = PTR_ERR(di);
3151 goto out;
3152 }
3153 BUG_ON(ret == -ENOENT);
3154 if (check_path_shared(root, path))
3155 goto out;
3156
3157 err = 0;
3158out:
3159 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003160 /* Migrate the orphan reservation over */
3161 if (!err)
3162 err = btrfs_block_rsv_migrate(trans->block_rsv,
3163 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003164 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003165
Yan, Zhenga22285a2010-05-16 10:48:46 -04003166 if (err) {
3167 btrfs_end_transaction(trans, root);
3168 root->fs_info->enospc_unlink = 0;
3169 return ERR_PTR(err);
3170 }
3171
3172 trans->block_rsv = &root->fs_info->global_block_rsv;
3173 return trans;
3174}
3175
3176static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3177 struct btrfs_root *root)
3178{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003179 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003180 btrfs_block_rsv_release(root, trans->block_rsv,
3181 trans->bytes_reserved);
3182 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003183 BUG_ON(!root->fs_info->enospc_unlink);
3184 root->fs_info->enospc_unlink = 0;
3185 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003186 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003187}
3188
Chris Mason39279cc2007-06-12 06:35:45 -04003189static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3190{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003191 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003192 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003193 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003194 int ret;
3195
Yan, Zhenga22285a2010-05-16 10:48:46 -04003196 trans = __unlink_start_trans(dir, dentry);
3197 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003198 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003199
Chris Mason12fcfd22009-03-24 10:24:20 -04003200 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3201
Chris Masone02119d2008-09-05 16:13:11 -04003202 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3203 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003204 if (ret)
3205 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003206
Yan, Zhenga22285a2010-05-16 10:48:46 -04003207 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003208 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003209 if (ret)
3210 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003211 }
Josef Bacik7b128762008-07-24 12:17:14 -04003212
Tsutomu Itohb5324022011-07-19 07:27:20 +00003213out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003214 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003215 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003216 return ret;
3217}
3218
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003219int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3220 struct btrfs_root *root,
3221 struct inode *dir, u64 objectid,
3222 const char *name, int name_len)
3223{
3224 struct btrfs_path *path;
3225 struct extent_buffer *leaf;
3226 struct btrfs_dir_item *di;
3227 struct btrfs_key key;
3228 u64 index;
3229 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003230 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003231
3232 path = btrfs_alloc_path();
3233 if (!path)
3234 return -ENOMEM;
3235
Li Zefan33345d012011-04-20 10:31:50 +08003236 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003237 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003238 if (IS_ERR_OR_NULL(di)) {
3239 if (!di)
3240 ret = -ENOENT;
3241 else
3242 ret = PTR_ERR(di);
3243 goto out;
3244 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003245
3246 leaf = path->nodes[0];
3247 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3248 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3249 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003250 if (ret) {
3251 btrfs_abort_transaction(trans, root, ret);
3252 goto out;
3253 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003254 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003255
3256 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3257 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003258 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003259 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003260 if (ret != -ENOENT) {
3261 btrfs_abort_transaction(trans, root, ret);
3262 goto out;
3263 }
Li Zefan33345d012011-04-20 10:31:50 +08003264 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003265 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003266 if (IS_ERR_OR_NULL(di)) {
3267 if (!di)
3268 ret = -ENOENT;
3269 else
3270 ret = PTR_ERR(di);
3271 btrfs_abort_transaction(trans, root, ret);
3272 goto out;
3273 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003274
3275 leaf = path->nodes[0];
3276 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003277 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003278 index = key.offset;
3279 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003280 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003281
Miao Xie16cdcec2011-04-22 18:12:22 +08003282 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003283 if (ret) {
3284 btrfs_abort_transaction(trans, root, ret);
3285 goto out;
3286 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003287
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003288 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003289 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003290 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003291 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003292 if (ret)
3293 btrfs_abort_transaction(trans, root, ret);
3294out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003295 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003296 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003297}
3298
Chris Mason39279cc2007-06-12 06:35:45 -04003299static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3300{
3301 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003302 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003303 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003304 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003305
David Sterbab3ae2442012-09-13 16:04:34 -06003306 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003307 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003308 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3309 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003310
Yan, Zhenga22285a2010-05-16 10:48:46 -04003311 trans = __unlink_start_trans(dir, dentry);
3312 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003313 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003314
Li Zefan33345d012011-04-20 10:31:50 +08003315 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003316 err = btrfs_unlink_subvol(trans, root, dir,
3317 BTRFS_I(inode)->location.objectid,
3318 dentry->d_name.name,
3319 dentry->d_name.len);
3320 goto out;
3321 }
3322
Josef Bacik7b128762008-07-24 12:17:14 -04003323 err = btrfs_orphan_add(trans, inode);
3324 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003325 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003326
Chris Mason39279cc2007-06-12 06:35:45 -04003327 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003328 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3329 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003330 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003331 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003332out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003333 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003334 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003335
Chris Mason39279cc2007-06-12 06:35:45 -04003336 return err;
3337}
3338
Chris Mason323ac952008-10-01 19:05:46 -04003339/*
Chris Mason39279cc2007-06-12 06:35:45 -04003340 * this can truncate away extent items, csum items and directory items.
3341 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003342 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003343 *
3344 * csum items that cross the new i_size are truncated to the new size
3345 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003346 *
3347 * min_type is the minimum key type to truncate down to. If set to 0, this
3348 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003349 */
Yan, Zheng80825102009-11-12 09:35:36 +00003350int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3351 struct btrfs_root *root,
3352 struct inode *inode,
3353 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003354{
Chris Mason39279cc2007-06-12 06:35:45 -04003355 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003356 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003357 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003358 struct btrfs_key key;
3359 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003360 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003361 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003362 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003363 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003364 u64 mask = root->sectorsize - 1;
3365 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003366 int found_extent;
3367 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003368 int pending_del_nr = 0;
3369 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003370 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003371 int ret;
3372 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003373 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003374
3375 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003376
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003377 path = btrfs_alloc_path();
3378 if (!path)
3379 return -ENOMEM;
3380 path->reada = -1;
3381
Josef Bacik5dc562c2012-08-17 13:14:17 -04003382 /*
3383 * We want to drop from the next block forward in case this new size is
3384 * not block aligned since we will be keeping the last block of the
3385 * extent just the way it is.
3386 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003387 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003388 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003389
Miao Xie16cdcec2011-04-22 18:12:22 +08003390 /*
3391 * This function is also used to drop the items in the log tree before
3392 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3393 * it is used to drop the loged items. So we shouldn't kill the delayed
3394 * items.
3395 */
3396 if (min_type == 0 && root == BTRFS_I(inode)->root)
3397 btrfs_kill_delayed_inode_items(inode);
3398
Li Zefan33345d012011-04-20 10:31:50 +08003399 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003400 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003401 key.type = (u8)-1;
3402
Chris Mason85e21ba2008-01-29 15:11:36 -05003403search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003404 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003405 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003406 if (ret < 0) {
3407 err = ret;
3408 goto out;
3409 }
Chris Masond3977122009-01-05 21:25:51 -05003410
Chris Mason85e21ba2008-01-29 15:11:36 -05003411 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003412 /* there are no items in the tree for us to truncate, we're
3413 * done
3414 */
Yan, Zheng80825102009-11-12 09:35:36 +00003415 if (path->slots[0] == 0)
3416 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003417 path->slots[0]--;
3418 }
3419
Chris Masond3977122009-01-05 21:25:51 -05003420 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003421 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003422 leaf = path->nodes[0];
3423 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3424 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003425
Li Zefan33345d012011-04-20 10:31:50 +08003426 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003427 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003428
Chris Mason85e21ba2008-01-29 15:11:36 -05003429 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003430 break;
3431
Chris Mason5f39d392007-10-15 16:14:19 -04003432 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003433 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003434 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003435 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003436 extent_type = btrfs_file_extent_type(leaf, fi);
3437 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003438 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003439 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003440 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003441 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003442 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003443 }
Yan008630c2007-11-07 13:31:09 -05003444 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003445 }
Yan, Zheng80825102009-11-12 09:35:36 +00003446 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003447 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003448 } else {
3449 if (item_end < new_size)
3450 break;
3451 if (found_key.offset >= new_size)
3452 del_item = 1;
3453 else
3454 del_item = 0;
3455 }
Chris Mason39279cc2007-06-12 06:35:45 -04003456 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003457 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003458 if (found_type != BTRFS_EXTENT_DATA_KEY)
3459 goto delete;
3460
3461 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003462 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003463 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003464 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003465 u64 orig_num_bytes =
3466 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003467 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003468 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003469 extent_num_bytes = extent_num_bytes &
3470 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003471 btrfs_set_file_extent_num_bytes(leaf, fi,
3472 extent_num_bytes);
3473 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003474 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003475 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003476 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003477 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003478 } else {
Chris Masondb945352007-10-15 16:15:53 -04003479 extent_num_bytes =
3480 btrfs_file_extent_disk_num_bytes(leaf,
3481 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003482 extent_offset = found_key.offset -
3483 btrfs_file_extent_offset(leaf, fi);
3484
Chris Mason39279cc2007-06-12 06:35:45 -04003485 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003486 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003487 if (extent_start != 0) {
3488 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003489 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003490 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003491 }
3492 }
Chris Mason90692182008-02-08 13:49:28 -05003493 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003494 /*
3495 * we can't truncate inline items that have had
3496 * special encodings
3497 */
3498 if (!del_item &&
3499 btrfs_file_extent_compression(leaf, fi) == 0 &&
3500 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3501 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003502 u32 size = new_size - found_key.offset;
3503
3504 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003505 inode_sub_bytes(inode, item_end + 1 -
3506 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003507 }
3508 size =
3509 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003510 btrfs_truncate_item(trans, root, path,
3511 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003512 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003513 inode_sub_bytes(inode, item_end + 1 -
3514 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003515 }
Chris Mason39279cc2007-06-12 06:35:45 -04003516 }
Chris Mason179e29e2007-11-01 11:28:41 -04003517delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003518 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003519 if (!pending_del_nr) {
3520 /* no pending yet, add ourselves */
3521 pending_del_slot = path->slots[0];
3522 pending_del_nr = 1;
3523 } else if (pending_del_nr &&
3524 path->slots[0] + 1 == pending_del_slot) {
3525 /* hop on the pending chunk */
3526 pending_del_nr++;
3527 pending_del_slot = path->slots[0];
3528 } else {
Chris Masond3977122009-01-05 21:25:51 -05003529 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003530 }
Chris Mason39279cc2007-06-12 06:35:45 -04003531 } else {
3532 break;
3533 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003534 if (found_extent && (root->ref_cows ||
3535 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003536 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003537 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003538 extent_num_bytes, 0,
3539 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003540 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003541 BUG_ON(ret);
3542 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003543
Yan, Zheng80825102009-11-12 09:35:36 +00003544 if (found_type == BTRFS_INODE_ITEM_KEY)
3545 break;
3546
3547 if (path->slots[0] == 0 ||
3548 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003549 if (pending_del_nr) {
3550 ret = btrfs_del_items(trans, root, path,
3551 pending_del_slot,
3552 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003553 if (ret) {
3554 btrfs_abort_transaction(trans,
3555 root, ret);
3556 goto error;
3557 }
Yan, Zheng80825102009-11-12 09:35:36 +00003558 pending_del_nr = 0;
3559 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003560 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003561 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003562 } else {
3563 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003564 }
Chris Mason39279cc2007-06-12 06:35:45 -04003565 }
Yan, Zheng80825102009-11-12 09:35:36 +00003566out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003567 if (pending_del_nr) {
3568 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3569 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003570 if (ret)
3571 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003572 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003573error:
Chris Mason39279cc2007-06-12 06:35:45 -04003574 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003575 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003576}
3577
Chris Masona52d9a82007-08-27 16:49:44 -04003578/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003579 * btrfs_truncate_page - read, zero a chunk and write a page
3580 * @inode - inode that we're zeroing
3581 * @from - the offset to start zeroing
3582 * @len - the length to zero, 0 to zero the entire range respective to the
3583 * offset
3584 * @front - zero up to the offset instead of from the offset on
3585 *
3586 * This will find the page for the "from" offset and cow the page and zero the
3587 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003588 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003589int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3590 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003591{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003592 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003593 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003594 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3595 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003596 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003597 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003598 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003599 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3600 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3601 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003602 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003603 int ret = 0;
3604 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003605 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003606
Josef Bacik2aaa6652012-08-29 14:27:18 -04003607 if ((offset & (blocksize - 1)) == 0 &&
3608 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003609 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003610 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003611 if (ret)
3612 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003613
Chris Mason211c17f2008-05-15 09:13:45 -04003614again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003615 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003616 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003617 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00003618 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04003619 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003620 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003621
3622 page_start = page_offset(page);
3623 page_end = page_start + PAGE_CACHE_SIZE - 1;
3624
Chris Masona52d9a82007-08-27 16:49:44 -04003625 if (!PageUptodate(page)) {
3626 ret = btrfs_readpage(NULL, page);
3627 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003628 if (page->mapping != mapping) {
3629 unlock_page(page);
3630 page_cache_release(page);
3631 goto again;
3632 }
Chris Masona52d9a82007-08-27 16:49:44 -04003633 if (!PageUptodate(page)) {
3634 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003635 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003636 }
3637 }
Chris Mason211c17f2008-05-15 09:13:45 -04003638 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003639
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003640 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003641 set_page_extent_mapped(page);
3642
3643 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3644 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003645 unlock_extent_cached(io_tree, page_start, page_end,
3646 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003647 unlock_page(page);
3648 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003649 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003650 btrfs_put_ordered_extent(ordered);
3651 goto again;
3652 }
3653
Josef Bacik2ac55d42010-02-03 19:33:23 +00003654 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003655 EXTENT_DIRTY | EXTENT_DELALLOC |
3656 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003657 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003658
Josef Bacik2ac55d42010-02-03 19:33:23 +00003659 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3660 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003661 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003662 unlock_extent_cached(io_tree, page_start, page_end,
3663 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003664 goto out_unlock;
3665 }
3666
Chris Masone6dcd2d2008-07-17 12:53:50 -04003667 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003668 if (!len)
3669 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003670 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003671 if (front)
3672 memset(kaddr, 0, offset);
3673 else
3674 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003675 flush_dcache_page(page);
3676 kunmap(page);
3677 }
Chris Mason247e7432008-07-17 12:53:51 -04003678 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003679 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003680 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3681 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003682
Chris Mason89642222008-07-24 09:41:53 -04003683out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003684 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003685 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003686 unlock_page(page);
3687 page_cache_release(page);
3688out:
3689 return ret;
3690}
3691
Josef Bacik695a0d02011-03-04 15:46:53 -05003692/*
3693 * This function puts in dummy file extents for the area we're creating a hole
3694 * for. So if we are truncating this file to a larger size we need to insert
3695 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3696 * the range between oldsize and size
3697 */
Josef Bacika41ad392011-01-31 15:30:16 -05003698int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003699{
3700 struct btrfs_trans_handle *trans;
3701 struct btrfs_root *root = BTRFS_I(inode)->root;
3702 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003703 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003704 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003705 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003706 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003707 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003708 u64 block_end = (size + mask) & ~mask;
3709 u64 last_byte;
3710 u64 cur_offset;
3711 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003712 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003713
3714 if (size <= hole_start)
3715 return 0;
3716
Yan Zheng9036c102008-10-30 14:19:41 -04003717 while (1) {
3718 struct btrfs_ordered_extent *ordered;
3719 btrfs_wait_ordered_range(inode, hole_start,
3720 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003721 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003722 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003723 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3724 if (!ordered)
3725 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003726 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3727 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003728 btrfs_put_ordered_extent(ordered);
3729 }
3730
Yan Zheng9036c102008-10-30 14:19:41 -04003731 cur_offset = hole_start;
3732 while (1) {
3733 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3734 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003735 if (IS_ERR(em)) {
3736 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00003737 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003738 break;
3739 }
Yan Zheng9036c102008-10-30 14:19:41 -04003740 last_byte = min(extent_map_end(em), block_end);
3741 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003742 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003743 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003744 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003745
Miao Xie36423202011-12-14 20:12:02 -05003746 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003747 if (IS_ERR(trans)) {
3748 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003749 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003750 }
Yan, Zheng80825102009-11-12 09:35:36 +00003751
Josef Bacik5dc562c2012-08-17 13:14:17 -04003752 err = btrfs_drop_extents(trans, root, inode,
3753 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003754 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003755 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003756 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003757 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003758 break;
Miao Xie5b397372011-09-11 10:52:24 -04003759 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003760
Yan Zheng9036c102008-10-30 14:19:41 -04003761 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003762 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003763 0, hole_size, 0, hole_size,
3764 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003765 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003766 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003767 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003768 break;
Miao Xie5b397372011-09-11 10:52:24 -04003769 }
Yan, Zheng80825102009-11-12 09:35:36 +00003770
Josef Bacik5dc562c2012-08-17 13:14:17 -04003771 btrfs_drop_extent_cache(inode, cur_offset,
3772 cur_offset + hole_size - 1, 0);
3773 hole_em = alloc_extent_map();
3774 if (!hole_em) {
3775 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3776 &BTRFS_I(inode)->runtime_flags);
3777 goto next;
3778 }
3779 hole_em->start = cur_offset;
3780 hole_em->len = hole_size;
3781 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003782
Josef Bacik5dc562c2012-08-17 13:14:17 -04003783 hole_em->block_start = EXTENT_MAP_HOLE;
3784 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05003785 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003786 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3787 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3788 hole_em->generation = trans->transid;
3789
3790 while (1) {
3791 write_lock(&em_tree->lock);
3792 err = add_extent_mapping(em_tree, hole_em);
3793 if (!err)
3794 list_move(&hole_em->list,
3795 &em_tree->modified_extents);
3796 write_unlock(&em_tree->lock);
3797 if (err != -EEXIST)
3798 break;
3799 btrfs_drop_extent_cache(inode, cur_offset,
3800 cur_offset +
3801 hole_size - 1, 0);
3802 }
3803 free_extent_map(hole_em);
3804next:
Miao Xie36423202011-12-14 20:12:02 -05003805 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003806 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003807 }
3808 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003809 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003810 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003811 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003812 break;
3813 }
3814
Yan, Zhenga22285a2010-05-16 10:48:46 -04003815 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003816 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3817 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003818 return err;
3819}
3820
Eric Sandeen3972f262013-01-12 02:57:22 +00003821static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00003822{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003823 struct btrfs_root *root = BTRFS_I(inode)->root;
3824 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003825 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00003826 loff_t newsize = attr->ia_size;
3827 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00003828 int ret;
3829
Josef Bacika41ad392011-01-31 15:30:16 -05003830 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003831 return 0;
3832
Eric Sandeen3972f262013-01-12 02:57:22 +00003833 /*
3834 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3835 * special case where we need to update the times despite not having
3836 * these flags set. For all other operations the VFS set these flags
3837 * explicitly if it wants a timestamp update.
3838 */
3839 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
3840 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
3841
Josef Bacika41ad392011-01-31 15:30:16 -05003842 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003843 truncate_pagecache(inode, oldsize, newsize);
3844 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003845 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003846 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003847
Miao Xief4a2f4c2011-12-14 20:12:01 -05003848 trans = btrfs_start_transaction(root, 1);
3849 if (IS_ERR(trans))
3850 return PTR_ERR(trans);
3851
3852 i_size_write(inode, newsize);
3853 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3854 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003855 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003856 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003857
Josef Bacika41ad392011-01-31 15:30:16 -05003858 /*
3859 * We're truncating a file that used to have good data down to
3860 * zero. Make sure it gets into the ordered flush list so that
3861 * any new writes get down to disk quickly.
3862 */
3863 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003864 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3865 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003866
Josef Bacikf3fe8202013-01-07 17:03:21 -05003867 /*
3868 * 1 for the orphan item we're going to add
3869 * 1 for the orphan item deletion.
3870 */
3871 trans = btrfs_start_transaction(root, 2);
3872 if (IS_ERR(trans))
3873 return PTR_ERR(trans);
3874
3875 /*
3876 * We need to do this in case we fail at _any_ point during the
3877 * actual truncate. Once we do the truncate_setsize we could
3878 * invalidate pages which forces any outstanding ordered io to
3879 * be instantly completed which will give us extents that need
3880 * to be truncated. If we fail to get an orphan inode down we
3881 * could have left over extents that were never meant to live,
3882 * so we need to garuntee from this point on that everything
3883 * will be consistent.
3884 */
3885 ret = btrfs_orphan_add(trans, inode);
3886 btrfs_end_transaction(trans, root);
3887 if (ret)
3888 return ret;
3889
Josef Bacika41ad392011-01-31 15:30:16 -05003890 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3891 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00003892
3893 /* Disable nonlocked read DIO to avoid the end less truncate */
3894 btrfs_inode_block_unlocked_dio(inode);
3895 inode_dio_wait(inode);
3896 btrfs_inode_resume_unlocked_dio(inode);
3897
Josef Bacika41ad392011-01-31 15:30:16 -05003898 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003899 if (ret && inode->i_nlink)
3900 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003901 }
3902
Josef Bacika41ad392011-01-31 15:30:16 -05003903 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003904}
3905
Chris Mason39279cc2007-06-12 06:35:45 -04003906static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3907{
3908 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003909 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003910 int err;
3911
Li Zefanb83cc962010-12-20 16:04:08 +08003912 if (btrfs_root_readonly(root))
3913 return -EROFS;
3914
Chris Mason39279cc2007-06-12 06:35:45 -04003915 err = inode_change_ok(inode, attr);
3916 if (err)
3917 return err;
3918
Chris Mason5a3f23d2009-03-31 13:27:11 -04003919 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00003920 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00003921 if (err)
3922 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003923 }
Yan Zheng9036c102008-10-30 14:19:41 -04003924
Christoph Hellwig10257742010-06-04 11:30:02 +02003925 if (attr->ia_valid) {
3926 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003927 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003928 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003929
Josef Bacik22c44fe2011-11-30 10:45:38 -05003930 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003931 err = btrfs_acl_chmod(inode);
3932 }
3933
Chris Mason39279cc2007-06-12 06:35:45 -04003934 return err;
3935}
Chris Mason61295eb2008-01-14 16:24:38 -05003936
Al Virobd555972010-06-07 11:35:40 -04003937void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003938{
3939 struct btrfs_trans_handle *trans;
3940 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003941 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003942 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003943 int ret;
3944
liubo1abe9b82011-03-24 11:18:59 +00003945 trace_btrfs_inode_evict(inode);
3946
Chris Mason39279cc2007-06-12 06:35:45 -04003947 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003948 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003949 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003950 goto no_delete;
3951
Chris Mason39279cc2007-06-12 06:35:45 -04003952 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003953 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003954 goto no_delete;
3955 }
Al Virobd555972010-06-07 11:35:40 -04003956 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003957 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003958
Yan, Zhengc71bf092009-11-12 09:34:40 +00003959 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003960 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003961 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003962 goto no_delete;
3963 }
3964
Yan, Zheng76dda932009-09-21 16:00:26 -04003965 if (inode->i_nlink > 0) {
3966 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3967 goto no_delete;
3968 }
3969
Miao Xie0e8c36a2012-12-19 06:59:51 +00003970 ret = btrfs_commit_inode_delayed_inode(inode);
3971 if (ret) {
3972 btrfs_orphan_del(NULL, inode);
3973 goto no_delete;
3974 }
3975
Miao Xie66d8f3d2012-09-06 04:02:28 -06003976 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003977 if (!rsv) {
3978 btrfs_orphan_del(NULL, inode);
3979 goto no_delete;
3980 }
Josef Bacik4a338542011-08-29 11:01:31 -04003981 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003982 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003983 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003984
Chris Masondbe674a2008-07-17 12:54:05 -04003985 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003986
Josef Bacik4289a662011-08-05 13:22:24 -04003987 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003988 * This is a bit simpler than btrfs_truncate since we've already
3989 * reserved our space for our orphan item in the unlink, so we just
3990 * need to reserve some slack space in case we add bytes and update
3991 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003992 */
Yan, Zheng80825102009-11-12 09:35:36 +00003993 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00003994 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3995 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00003996
Josef Bacik726c35f2011-09-26 15:46:06 -04003997 /*
3998 * Try and steal from the global reserve since we will
3999 * likely not use this space anyway, we want to try as
4000 * hard as possible to get this to work.
4001 */
4002 if (ret)
4003 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4004
Yan, Zhengd68fc572010-05-16 10:49:58 -04004005 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04004006 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04004007 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04004008 btrfs_orphan_del(NULL, inode);
4009 btrfs_free_block_rsv(root, rsv);
4010 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04004011 }
4012
Miao Xie0e8c36a2012-12-19 06:59:51 +00004013 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04004014 if (IS_ERR(trans)) {
4015 btrfs_orphan_del(NULL, inode);
4016 btrfs_free_block_rsv(root, rsv);
4017 goto no_delete;
4018 }
4019
4020 trans->block_rsv = rsv;
4021
Yan, Zhengd68fc572010-05-16 10:49:58 -04004022 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04004023 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00004024 break;
4025
Miao Xie8407aa42012-09-07 01:43:32 -06004026 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004027 btrfs_end_transaction(trans, root);
4028 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00004029 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04004030 }
4031
Josef Bacik4289a662011-08-05 13:22:24 -04004032 btrfs_free_block_rsv(root, rsv);
4033
Yan, Zheng80825102009-11-12 09:35:36 +00004034 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004035 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004036 ret = btrfs_orphan_del(trans, inode);
4037 BUG_ON(ret);
4038 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004039
Josef Bacik4289a662011-08-05 13:22:24 -04004040 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004041 if (!(root == root->fs_info->tree_root ||
4042 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004043 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004044
Chris Mason54aa1f42007-06-22 14:16:25 -04004045 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004046 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004047no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004048 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004049 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004050}
4051
4052/*
4053 * this returns the key found in the dir entry in the location pointer.
4054 * If no dir entries were found, location->objectid is 0.
4055 */
4056static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4057 struct btrfs_key *location)
4058{
4059 const char *name = dentry->d_name.name;
4060 int namelen = dentry->d_name.len;
4061 struct btrfs_dir_item *di;
4062 struct btrfs_path *path;
4063 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004064 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004065
4066 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004067 if (!path)
4068 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004069
Li Zefan33345d012011-04-20 10:31:50 +08004070 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004071 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004072 if (IS_ERR(di))
4073 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004074
David Sterbac7040052011-04-19 18:00:01 +02004075 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004076 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004077
Chris Mason5f39d392007-10-15 16:14:19 -04004078 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004079out:
Chris Mason39279cc2007-06-12 06:35:45 -04004080 btrfs_free_path(path);
4081 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004082out_err:
4083 location->objectid = 0;
4084 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004085}
4086
4087/*
4088 * when we hit a tree root in a directory, the btrfs part of the inode
4089 * needs to be changed to reflect the root directory of the tree root. This
4090 * is kind of like crossing a mount point.
4091 */
4092static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004093 struct inode *dir,
4094 struct dentry *dentry,
4095 struct btrfs_key *location,
4096 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004097{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004098 struct btrfs_path *path;
4099 struct btrfs_root *new_root;
4100 struct btrfs_root_ref *ref;
4101 struct extent_buffer *leaf;
4102 int ret;
4103 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004104
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004105 path = btrfs_alloc_path();
4106 if (!path) {
4107 err = -ENOMEM;
4108 goto out;
4109 }
Chris Mason39279cc2007-06-12 06:35:45 -04004110
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004111 err = -ENOENT;
4112 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4113 BTRFS_I(dir)->root->root_key.objectid,
4114 location->objectid);
4115 if (ret) {
4116 if (ret < 0)
4117 err = ret;
4118 goto out;
4119 }
Chris Mason39279cc2007-06-12 06:35:45 -04004120
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004121 leaf = path->nodes[0];
4122 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004123 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004124 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4125 goto out;
4126
4127 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4128 (unsigned long)(ref + 1),
4129 dentry->d_name.len);
4130 if (ret)
4131 goto out;
4132
David Sterbab3b4aa72011-04-21 01:20:15 +02004133 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004134
4135 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4136 if (IS_ERR(new_root)) {
4137 err = PTR_ERR(new_root);
4138 goto out;
4139 }
4140
4141 if (btrfs_root_refs(&new_root->root_item) == 0) {
4142 err = -ENOENT;
4143 goto out;
4144 }
4145
4146 *sub_root = new_root;
4147 location->objectid = btrfs_root_dirid(&new_root->root_item);
4148 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004149 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004150 err = 0;
4151out:
4152 btrfs_free_path(path);
4153 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004154}
4155
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004156static void inode_tree_add(struct inode *inode)
4157{
4158 struct btrfs_root *root = BTRFS_I(inode)->root;
4159 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004160 struct rb_node **p;
4161 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004162 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004163again:
4164 p = &root->inode_tree.rb_node;
4165 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004166
Al Viro1d3382cb2010-10-23 15:19:20 -04004167 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004168 return;
4169
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004170 spin_lock(&root->inode_lock);
4171 while (*p) {
4172 parent = *p;
4173 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4174
Li Zefan33345d012011-04-20 10:31:50 +08004175 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004176 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004177 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004178 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004179 else {
4180 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004181 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004182 rb_erase(parent, &root->inode_tree);
4183 RB_CLEAR_NODE(parent);
4184 spin_unlock(&root->inode_lock);
4185 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004186 }
4187 }
4188 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4189 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4190 spin_unlock(&root->inode_lock);
4191}
4192
4193static void inode_tree_del(struct inode *inode)
4194{
4195 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004196 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004197
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004198 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004199 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004200 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004201 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004202 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004203 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004204 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004205
Josef Bacik0af3d002010-06-21 14:48:16 -04004206 /*
4207 * Free space cache has inodes in the tree root, but the tree root has a
4208 * root_refs of 0, so this could end up dropping the tree root as a
4209 * snapshot, so we need the extra !root->fs_info->tree_root check to
4210 * make sure we don't drop it.
4211 */
4212 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4213 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004214 synchronize_srcu(&root->fs_info->subvol_srcu);
4215 spin_lock(&root->inode_lock);
4216 empty = RB_EMPTY_ROOT(&root->inode_tree);
4217 spin_unlock(&root->inode_lock);
4218 if (empty)
4219 btrfs_add_dead_root(root);
4220 }
4221}
4222
Jeff Mahoney143bede2012-03-01 14:56:26 +01004223void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004224{
4225 struct rb_node *node;
4226 struct rb_node *prev;
4227 struct btrfs_inode *entry;
4228 struct inode *inode;
4229 u64 objectid = 0;
4230
4231 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4232
4233 spin_lock(&root->inode_lock);
4234again:
4235 node = root->inode_tree.rb_node;
4236 prev = NULL;
4237 while (node) {
4238 prev = node;
4239 entry = rb_entry(node, struct btrfs_inode, rb_node);
4240
Li Zefan33345d012011-04-20 10:31:50 +08004241 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004242 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004243 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004244 node = node->rb_right;
4245 else
4246 break;
4247 }
4248 if (!node) {
4249 while (prev) {
4250 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004251 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004252 node = prev;
4253 break;
4254 }
4255 prev = rb_next(prev);
4256 }
4257 }
4258 while (node) {
4259 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004260 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004261 inode = igrab(&entry->vfs_inode);
4262 if (inode) {
4263 spin_unlock(&root->inode_lock);
4264 if (atomic_read(&inode->i_count) > 1)
4265 d_prune_aliases(inode);
4266 /*
Al Viro45321ac2010-06-07 13:43:19 -04004267 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004268 * the inode cache when its usage count
4269 * hits zero.
4270 */
4271 iput(inode);
4272 cond_resched();
4273 spin_lock(&root->inode_lock);
4274 goto again;
4275 }
4276
4277 if (cond_resched_lock(&root->inode_lock))
4278 goto again;
4279
4280 node = rb_next(node);
4281 }
4282 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004283}
4284
Chris Masone02119d2008-09-05 16:13:11 -04004285static int btrfs_init_locked_inode(struct inode *inode, void *p)
4286{
4287 struct btrfs_iget_args *args = p;
4288 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004289 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004290 return 0;
4291}
4292
4293static int btrfs_find_actor(struct inode *inode, void *opaque)
4294{
4295 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004296 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004297 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004298}
4299
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004300static struct inode *btrfs_iget_locked(struct super_block *s,
4301 u64 objectid,
4302 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004303{
4304 struct inode *inode;
4305 struct btrfs_iget_args args;
4306 args.ino = objectid;
4307 args.root = root;
4308
4309 inode = iget5_locked(s, objectid, btrfs_find_actor,
4310 btrfs_init_locked_inode,
4311 (void *)&args);
4312 return inode;
4313}
4314
Balaji Rao1a54ef82008-07-21 02:01:04 +05304315/* Get an inode object given its location and corresponding root.
4316 * Returns in *is_new if the inode was read from disk
4317 */
4318struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004319 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304320{
4321 struct inode *inode;
4322
4323 inode = btrfs_iget_locked(s, location->objectid, root);
4324 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004325 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304326
4327 if (inode->i_state & I_NEW) {
4328 BTRFS_I(inode)->root = root;
4329 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4330 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004331 if (!is_bad_inode(inode)) {
4332 inode_tree_add(inode);
4333 unlock_new_inode(inode);
4334 if (new)
4335 *new = 1;
4336 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004337 unlock_new_inode(inode);
4338 iput(inode);
4339 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004340 }
4341 }
4342
Balaji Rao1a54ef82008-07-21 02:01:04 +05304343 return inode;
4344}
4345
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004346static struct inode *new_simple_dir(struct super_block *s,
4347 struct btrfs_key *key,
4348 struct btrfs_root *root)
4349{
4350 struct inode *inode = new_inode(s);
4351
4352 if (!inode)
4353 return ERR_PTR(-ENOMEM);
4354
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004355 BTRFS_I(inode)->root = root;
4356 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004357 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004358
4359 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004360 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004361 inode->i_fop = &simple_dir_operations;
4362 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4363 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4364
4365 return inode;
4366}
4367
Chris Mason3de45862008-11-17 21:02:50 -05004368struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004369{
Chris Masond3977122009-01-05 21:25:51 -05004370 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004371 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004372 struct btrfs_root *sub_root = root;
4373 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004374 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004375 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004376
4377 if (dentry->d_name.len > BTRFS_NAME_LEN)
4378 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004379
Jeff Layton39e3c952012-11-28 11:30:53 -05004380 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004381 if (ret < 0)
4382 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004383
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004384 if (location.objectid == 0)
4385 return NULL;
4386
4387 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004388 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004389 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004390 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004391
4392 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4393
Yan, Zheng76dda932009-09-21 16:00:26 -04004394 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004395 ret = fixup_tree_root_location(root, dir, dentry,
4396 &location, &sub_root);
4397 if (ret < 0) {
4398 if (ret != -ENOENT)
4399 inode = ERR_PTR(ret);
4400 else
4401 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4402 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004403 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004404 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004405 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4406
Julia Lawall34d19ba2011-01-24 19:55:19 +00004407 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004408 down_read(&root->fs_info->cleanup_work_sem);
4409 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004410 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004411 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004412 if (ret)
4413 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004414 }
4415
Chris Mason3de45862008-11-17 21:02:50 -05004416 return inode;
4417}
4418
Nick Pigginfe15ce42011-01-07 17:49:23 +11004419static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004420{
4421 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004422 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004423
Li Zefan848cce02012-02-21 17:04:28 +08004424 if (!inode && !IS_ROOT(dentry))
4425 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004426
Li Zefan848cce02012-02-21 17:04:28 +08004427 if (inode) {
4428 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004429 if (btrfs_root_refs(&root->root_item) == 0)
4430 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004431
4432 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4433 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004434 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004435 return 0;
4436}
4437
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004438static void btrfs_dentry_release(struct dentry *dentry)
4439{
4440 if (dentry->d_fsdata)
4441 kfree(dentry->d_fsdata);
4442}
4443
Chris Mason3de45862008-11-17 21:02:50 -05004444static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004445 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004446{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004447 struct dentry *ret;
4448
4449 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004450 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004451}
4452
Miao Xie16cdcec2011-04-22 18:12:22 +08004453unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004454 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4455};
4456
David Woodhousecbdf5a22008-08-06 19:42:33 +01004457static int btrfs_real_readdir(struct file *filp, void *dirent,
4458 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004459{
Chris Mason6da6aba2007-12-18 16:15:09 -05004460 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004461 struct btrfs_root *root = BTRFS_I(inode)->root;
4462 struct btrfs_item *item;
4463 struct btrfs_dir_item *di;
4464 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004465 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004466 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004467 struct list_head ins_list;
4468 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004469 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004470 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004471 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004472 unsigned char d_type;
4473 int over = 0;
4474 u32 di_cur;
4475 u32 di_total;
4476 u32 di_len;
4477 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004478 char tmp_name[32];
4479 char *name_ptr;
4480 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004481 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004482
4483 /* FIXME, use a real flag for deciding about the key type */
4484 if (root->fs_info->tree_root == root)
4485 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004486
Chris Mason39544012007-12-12 14:38:19 -05004487 /* special case for "." */
4488 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004489 over = filldir(dirent, ".", 1,
4490 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004491 if (over)
4492 return 0;
4493 filp->f_pos = 1;
4494 }
Chris Mason39544012007-12-12 14:38:19 -05004495 /* special case for .., just use the back ref */
4496 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004497 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004498 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004499 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004500 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004501 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004502 filp->f_pos = 2;
4503 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004504 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004505 if (!path)
4506 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004507
Josef Bacik026fd312011-05-13 10:32:11 -04004508 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004509
Miao Xie16cdcec2011-04-22 18:12:22 +08004510 if (key_type == BTRFS_DIR_INDEX_KEY) {
4511 INIT_LIST_HEAD(&ins_list);
4512 INIT_LIST_HEAD(&del_list);
4513 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4514 }
4515
Chris Mason39279cc2007-06-12 06:35:45 -04004516 btrfs_set_key_type(&key, key_type);
4517 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004518 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004519
Chris Mason39279cc2007-06-12 06:35:45 -04004520 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4521 if (ret < 0)
4522 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004523
4524 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004525 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004526 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004527 if (slot >= btrfs_header_nritems(leaf)) {
4528 ret = btrfs_next_leaf(root, path);
4529 if (ret < 0)
4530 goto err;
4531 else if (ret > 0)
4532 break;
4533 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004534 }
Chris Mason3de45862008-11-17 21:02:50 -05004535
Chris Mason5f39d392007-10-15 16:14:19 -04004536 item = btrfs_item_nr(leaf, slot);
4537 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4538
4539 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004540 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004541 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004542 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004543 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004544 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004545 if (key_type == BTRFS_DIR_INDEX_KEY &&
4546 btrfs_should_delete_dir_index(&del_list,
4547 found_key.offset))
4548 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004549
4550 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004551 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004552
Chris Mason39279cc2007-06-12 06:35:45 -04004553 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4554 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004555 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004556
4557 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004558 struct btrfs_key location;
4559
Josef Bacik22a94d42011-03-16 16:47:17 -04004560 if (verify_dir_item(root, leaf, di))
4561 break;
4562
Chris Mason5f39d392007-10-15 16:14:19 -04004563 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004564 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004565 name_ptr = tmp_name;
4566 } else {
4567 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004568 if (!name_ptr) {
4569 ret = -ENOMEM;
4570 goto err;
4571 }
Chris Mason5f39d392007-10-15 16:14:19 -04004572 }
4573 read_extent_buffer(leaf, name_ptr,
4574 (unsigned long)(di + 1), name_len);
4575
4576 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4577 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004578
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004579
Chris Mason3de45862008-11-17 21:02:50 -05004580 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004581 * skip it.
4582 *
4583 * In contrast to old kernels, we insert the snapshot's
4584 * dir item and dir index after it has been created, so
4585 * we won't find a reference to our own snapshot. We
4586 * still keep the following code for backward
4587 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004588 */
4589 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4590 location.objectid == root->root_key.objectid) {
4591 over = 0;
4592 goto skip;
4593 }
Chris Mason5f39d392007-10-15 16:14:19 -04004594 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004595 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004596 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004597
Chris Mason3de45862008-11-17 21:02:50 -05004598skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004599 if (name_ptr != tmp_name)
4600 kfree(name_ptr);
4601
Chris Mason39279cc2007-06-12 06:35:45 -04004602 if (over)
4603 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004604 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004605 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004606 di_cur += di_len;
4607 di = (struct btrfs_dir_item *)((char *)di + di_len);
4608 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004609next:
4610 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004611 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004612
Miao Xie16cdcec2011-04-22 18:12:22 +08004613 if (key_type == BTRFS_DIR_INDEX_KEY) {
4614 if (is_curr)
4615 filp->f_pos++;
4616 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4617 &ins_list);
4618 if (ret)
4619 goto nopos;
4620 }
4621
David Woodhouse49593bf2008-08-17 17:08:36 +01004622 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004623 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004624 /*
4625 * 32-bit glibc will use getdents64, but then strtol -
4626 * so the last number we can serve is this.
4627 */
4628 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004629 else
4630 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004631nopos:
4632 ret = 0;
4633err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004634 if (key_type == BTRFS_DIR_INDEX_KEY)
4635 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004636 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004637 return ret;
4638}
4639
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004640int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004641{
4642 struct btrfs_root *root = BTRFS_I(inode)->root;
4643 struct btrfs_trans_handle *trans;
4644 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004645 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004646
Josef Bacik72ac3c02012-05-23 14:13:11 -04004647 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004648 return 0;
4649
Liu Bo83eea1f2012-07-10 05:28:39 -06004650 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004651 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004652
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004653 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004654 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004655 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004656 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004657 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004658 if (IS_ERR(trans))
4659 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004660 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004661 }
4662 return ret;
4663}
4664
4665/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004666 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004667 * inode changes. But, it is most likely to find the inode in cache.
4668 * FIXME, needs more benchmarking...there are no reasons other than performance
4669 * to keep or drop this code.
4670 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004671int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004672{
4673 struct btrfs_root *root = BTRFS_I(inode)->root;
4674 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004675 int ret;
4676
Josef Bacik72ac3c02012-05-23 14:13:11 -04004677 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004678 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004679
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004680 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004681 if (IS_ERR(trans))
4682 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004683
4684 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004685 if (ret && ret == -ENOSPC) {
4686 /* whoops, lets try again with the full transaction */
4687 btrfs_end_transaction(trans, root);
4688 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004689 if (IS_ERR(trans))
4690 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004691
Chris Mason94b60442010-05-26 11:02:00 -04004692 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004693 }
Chris Mason39279cc2007-06-12 06:35:45 -04004694 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004695 if (BTRFS_I(inode)->delayed_node)
4696 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004697
4698 return ret;
4699}
4700
4701/*
4702 * This is a copy of file_update_time. We need this so we can return error on
4703 * ENOSPC for updating the inode in the case of file write and mmap writes.
4704 */
Josef Bacike41f9412012-03-26 09:46:47 -04004705static int btrfs_update_time(struct inode *inode, struct timespec *now,
4706 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004707{
Alexander Block2bc5565282012-06-15 09:49:33 +02004708 struct btrfs_root *root = BTRFS_I(inode)->root;
4709
4710 if (btrfs_root_readonly(root))
4711 return -EROFS;
4712
Josef Bacike41f9412012-03-26 09:46:47 -04004713 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004714 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004715 if (flags & S_CTIME)
4716 inode->i_ctime = *now;
4717 if (flags & S_MTIME)
4718 inode->i_mtime = *now;
4719 if (flags & S_ATIME)
4720 inode->i_atime = *now;
4721 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004722}
4723
Chris Masond352ac62008-09-29 15:18:18 -04004724/*
4725 * find the highest existing sequence number in a directory
4726 * and then set the in-memory index_cnt variable to reflect
4727 * free sequence numbers
4728 */
Josef Bacikaec74772008-07-24 12:12:38 -04004729static int btrfs_set_inode_index_count(struct inode *inode)
4730{
4731 struct btrfs_root *root = BTRFS_I(inode)->root;
4732 struct btrfs_key key, found_key;
4733 struct btrfs_path *path;
4734 struct extent_buffer *leaf;
4735 int ret;
4736
Li Zefan33345d012011-04-20 10:31:50 +08004737 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004738 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4739 key.offset = (u64)-1;
4740
4741 path = btrfs_alloc_path();
4742 if (!path)
4743 return -ENOMEM;
4744
4745 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4746 if (ret < 0)
4747 goto out;
4748 /* FIXME: we should be able to handle this */
4749 if (ret == 0)
4750 goto out;
4751 ret = 0;
4752
4753 /*
4754 * MAGIC NUMBER EXPLANATION:
4755 * since we search a directory based on f_pos we have to start at 2
4756 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4757 * else has to start at 2
4758 */
4759 if (path->slots[0] == 0) {
4760 BTRFS_I(inode)->index_cnt = 2;
4761 goto out;
4762 }
4763
4764 path->slots[0]--;
4765
4766 leaf = path->nodes[0];
4767 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4768
Li Zefan33345d012011-04-20 10:31:50 +08004769 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004770 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4771 BTRFS_I(inode)->index_cnt = 2;
4772 goto out;
4773 }
4774
4775 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4776out:
4777 btrfs_free_path(path);
4778 return ret;
4779}
4780
Chris Masond352ac62008-09-29 15:18:18 -04004781/*
4782 * helper to find a free sequence number in a given directory. This current
4783 * code is very simple, later versions will do smarter things in the btree
4784 */
Chris Mason3de45862008-11-17 21:02:50 -05004785int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004786{
4787 int ret = 0;
4788
4789 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004790 ret = btrfs_inode_delayed_dir_index_count(dir);
4791 if (ret) {
4792 ret = btrfs_set_inode_index_count(dir);
4793 if (ret)
4794 return ret;
4795 }
Josef Bacikaec74772008-07-24 12:12:38 -04004796 }
4797
Chris Mason00e4e6b2008-08-05 11:18:09 -04004798 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004799 BTRFS_I(dir)->index_cnt++;
4800
4801 return ret;
4802}
4803
Chris Mason39279cc2007-06-12 06:35:45 -04004804static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4805 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004806 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004807 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004808 u64 ref_objectid, u64 objectid,
4809 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004810{
4811 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004812 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004813 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004814 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004815 struct btrfs_inode_ref *ref;
4816 struct btrfs_key key[2];
4817 u32 sizes[2];
4818 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004819 int ret;
4820 int owner;
4821
Chris Mason5f39d392007-10-15 16:14:19 -04004822 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004823 if (!path)
4824 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004825
Chris Mason39279cc2007-06-12 06:35:45 -04004826 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004827 if (!inode) {
4828 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004829 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004830 }
Chris Mason39279cc2007-06-12 06:35:45 -04004831
Li Zefan581bb052011-04-20 10:06:11 +08004832 /*
4833 * we have to initialize this early, so we can reclaim the inode
4834 * number if we fail afterwards in this function.
4835 */
4836 inode->i_ino = objectid;
4837
Josef Bacikaec74772008-07-24 12:12:38 -04004838 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004839 trace_btrfs_inode_request(dir);
4840
Chris Mason3de45862008-11-17 21:02:50 -05004841 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004842 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004843 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004844 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004845 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004846 }
Josef Bacikaec74772008-07-24 12:12:38 -04004847 }
4848 /*
4849 * index_cnt is ignored for everything but a dir,
4850 * btrfs_get_inode_index_count has an explanation for the magic
4851 * number
4852 */
4853 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004854 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004855 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004856 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04004857
Josef Bacik5dc562c2012-08-17 13:14:17 -04004858 /*
4859 * We could have gotten an inode number from somebody who was fsynced
4860 * and then removed in this same transaction, so let's just set full
4861 * sync since it will be a full sync anyway and this will blow away the
4862 * old info in the log.
4863 */
4864 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4865
Al Viro569254b2011-07-24 17:08:40 -04004866 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004867 owner = 0;
4868 else
4869 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004870
4871 key[0].objectid = objectid;
4872 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4873 key[0].offset = 0;
4874
Mark Fashehf1863732012-08-08 11:32:27 -07004875 /*
4876 * Start new inodes with an inode_ref. This is slightly more
4877 * efficient for small numbers of hard links since they will
4878 * be packed into one item. Extended refs will kick in if we
4879 * add more hard links than can fit in the ref item.
4880 */
Chris Mason9c583092008-01-29 15:15:18 -05004881 key[1].objectid = objectid;
4882 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4883 key[1].offset = ref_objectid;
4884
4885 sizes[0] = sizeof(struct btrfs_inode_item);
4886 sizes[1] = name_len + sizeof(*ref);
4887
Chris Masonb9473432009-03-13 11:00:37 -04004888 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004889 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4890 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004891 goto fail;
4892
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004893 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004894 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004895 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004896 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4897 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004898 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4899 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004900 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004901
4902 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4903 struct btrfs_inode_ref);
4904 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004905 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004906 ptr = (unsigned long)(ref + 1);
4907 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4908
Chris Mason5f39d392007-10-15 16:14:19 -04004909 btrfs_mark_buffer_dirty(path->nodes[0]);
4910 btrfs_free_path(path);
4911
Chris Mason39279cc2007-06-12 06:35:45 -04004912 location = &BTRFS_I(inode)->location;
4913 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004914 location->offset = 0;
4915 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4916
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004917 btrfs_inherit_iflags(inode, dir);
4918
Al Viro569254b2011-07-24 17:08:40 -04004919 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004920 if (btrfs_test_opt(root, NODATASUM))
4921 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06004922 if (btrfs_test_opt(root, NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004923 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4924 }
4925
Chris Mason39279cc2007-06-12 06:35:45 -04004926 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004927 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004928
4929 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004930 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004931
Alexander Block8ea05e32012-07-25 17:35:53 +02004932 btrfs_update_root_times(trans, root);
4933
Chris Mason39279cc2007-06-12 06:35:45 -04004934 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004935fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004936 if (dir)
4937 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004938 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004939 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004940 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004941}
4942
4943static inline u8 btrfs_inode_type(struct inode *inode)
4944{
4945 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4946}
4947
Chris Masond352ac62008-09-29 15:18:18 -04004948/*
4949 * utility function to add 'inode' into 'parent_inode' with
4950 * a give name and a given sequence number.
4951 * if 'add_backref' is true, also insert a backref from the
4952 * inode to the parent directory.
4953 */
Chris Masone02119d2008-09-05 16:13:11 -04004954int btrfs_add_link(struct btrfs_trans_handle *trans,
4955 struct inode *parent_inode, struct inode *inode,
4956 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004957{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004958 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004959 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004960 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004961 u64 ino = btrfs_ino(inode);
4962 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004963
Li Zefan33345d012011-04-20 10:31:50 +08004964 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004965 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4966 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004967 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004968 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4969 key.offset = 0;
4970 }
Chris Mason39279cc2007-06-12 06:35:45 -04004971
Li Zefan33345d012011-04-20 10:31:50 +08004972 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004973 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4974 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004975 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004976 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004977 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4978 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004979 }
4980
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004981 /* Nothing to clean up yet */
4982 if (ret)
4983 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004984
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004985 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4986 parent_inode, &key,
4987 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05004988 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004989 goto fail_dir_item;
4990 else if (ret) {
4991 btrfs_abort_transaction(trans, root, ret);
4992 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004993 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004994
4995 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4996 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004997 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004998 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4999 ret = btrfs_update_inode(trans, root, parent_inode);
5000 if (ret)
5001 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005002 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05005003
5004fail_dir_item:
5005 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5006 u64 local_index;
5007 int err;
5008 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5009 key.objectid, root->root_key.objectid,
5010 parent_ino, &local_index, name, name_len);
5011
5012 } else if (add_backref) {
5013 u64 local_index;
5014 int err;
5015
5016 err = btrfs_del_inode_ref(trans, root, name, name_len,
5017 ino, parent_ino, &local_index);
5018 }
5019 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005020}
5021
5022static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005023 struct inode *dir, struct dentry *dentry,
5024 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005025{
Josef Bacika1b075d2010-11-19 20:36:11 +00005026 int err = btrfs_add_link(trans, dir, inode,
5027 dentry->d_name.name, dentry->d_name.len,
5028 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005029 if (err > 0)
5030 err = -EEXIST;
5031 return err;
5032}
5033
Josef Bacik618e21d2007-07-11 10:18:17 -04005034static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005035 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005036{
5037 struct btrfs_trans_handle *trans;
5038 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005039 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005040 int err;
5041 int drop_inode = 0;
5042 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005043 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005044
5045 if (!new_valid_dev(rdev))
5046 return -EINVAL;
5047
Josef Bacik9ed74f22009-09-11 16:12:44 -04005048 /*
5049 * 2 for inode item and ref
5050 * 2 for dir items
5051 * 1 for xattr if selinux is on
5052 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005053 trans = btrfs_start_transaction(root, 5);
5054 if (IS_ERR(trans))
5055 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005056
Li Zefan581bb052011-04-20 10:06:11 +08005057 err = btrfs_find_free_ino(root, &objectid);
5058 if (err)
5059 goto out_unlock;
5060
Josef Bacikaec74772008-07-24 12:12:38 -04005061 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005062 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005063 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005064 if (IS_ERR(inode)) {
5065 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005066 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005067 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005068
Eric Paris2a7dba32011-02-01 11:05:39 -05005069 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005070 if (err) {
5071 drop_inode = 1;
5072 goto out_unlock;
5073 }
5074
Casey Schauflerad19db72011-12-15 10:09:07 -05005075 /*
5076 * If the active LSM wants to access the inode during
5077 * d_instantiate it needs these. Smack checks to see
5078 * if the filesystem supports xattrs by looking at the
5079 * ops vector.
5080 */
5081
5082 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005083 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005084 if (err)
5085 drop_inode = 1;
5086 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005087 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005088 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005089 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005090 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005091out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005092 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005093 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005094 if (drop_inode) {
5095 inode_dec_link_count(inode);
5096 iput(inode);
5097 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005098 return err;
5099}
5100
Chris Mason39279cc2007-06-12 06:35:45 -04005101static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005102 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005103{
5104 struct btrfs_trans_handle *trans;
5105 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005106 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005107 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005108 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005109 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005110 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005111
Josef Bacik9ed74f22009-09-11 16:12:44 -04005112 /*
5113 * 2 for inode item and ref
5114 * 2 for dir items
5115 * 1 for xattr if selinux is on
5116 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005117 trans = btrfs_start_transaction(root, 5);
5118 if (IS_ERR(trans))
5119 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005120
Li Zefan581bb052011-04-20 10:06:11 +08005121 err = btrfs_find_free_ino(root, &objectid);
5122 if (err)
5123 goto out_unlock;
5124
Josef Bacikaec74772008-07-24 12:12:38 -04005125 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005126 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005127 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005128 if (IS_ERR(inode)) {
5129 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005130 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005131 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005132 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005133
Eric Paris2a7dba32011-02-01 11:05:39 -05005134 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005135 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005136 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005137
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005138 err = btrfs_update_inode(trans, root, inode);
5139 if (err)
5140 goto out_unlock;
5141
Casey Schauflerad19db72011-12-15 10:09:07 -05005142 /*
5143 * If the active LSM wants to access the inode during
5144 * d_instantiate it needs these. Smack checks to see
5145 * if the filesystem supports xattrs by looking at the
5146 * ops vector.
5147 */
5148 inode->i_fop = &btrfs_file_operations;
5149 inode->i_op = &btrfs_file_inode_operations;
5150
Josef Bacika1b075d2010-11-19 20:36:11 +00005151 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005152 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005153 goto out_unlock;
5154
5155 inode->i_mapping->a_ops = &btrfs_aops;
5156 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5157 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5158 d_instantiate(dentry, inode);
5159
Chris Mason39279cc2007-06-12 06:35:45 -04005160out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005161 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005162 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005163 inode_dec_link_count(inode);
5164 iput(inode);
5165 }
Liu Bob53d3f52012-11-14 14:34:34 +00005166 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005167 return err;
5168}
5169
5170static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5171 struct dentry *dentry)
5172{
5173 struct btrfs_trans_handle *trans;
5174 struct btrfs_root *root = BTRFS_I(dir)->root;
5175 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005176 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005177 int err;
5178 int drop_inode = 0;
5179
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005180 /* do not allow sys_link's with other subvols of the same device */
5181 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005182 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005183
Mark Fashehf1863732012-08-08 11:32:27 -07005184 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005185 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005186
Chris Mason3de45862008-11-17 21:02:50 -05005187 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005188 if (err)
5189 goto fail;
5190
Yan, Zhenga22285a2010-05-16 10:48:46 -04005191 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005192 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005193 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005194 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005195 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005196 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005197 if (IS_ERR(trans)) {
5198 err = PTR_ERR(trans);
5199 goto fail;
5200 }
Chris Mason5f39d392007-10-15 16:14:19 -04005201
Miao Xie31534952011-04-13 13:19:21 +08005202 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005203 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005204 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005205 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005206 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005207
Josef Bacika1b075d2010-11-19 20:36:11 +00005208 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005209
Yan, Zhenga5719522009-09-24 09:17:31 -04005210 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005211 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005212 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005213 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005214 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005215 if (err)
5216 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005217 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005218 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005219 }
Chris Mason39279cc2007-06-12 06:35:45 -04005220
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005221 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005222fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005223 if (drop_inode) {
5224 inode_dec_link_count(inode);
5225 iput(inode);
5226 }
Liu Bob53d3f52012-11-14 14:34:34 +00005227 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005228 return err;
5229}
5230
Al Viro18bb1db2011-07-26 01:41:39 -04005231static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005232{
Chris Masonb9d86662008-05-02 16:13:49 -04005233 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005234 struct btrfs_trans_handle *trans;
5235 struct btrfs_root *root = BTRFS_I(dir)->root;
5236 int err = 0;
5237 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005238 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005239 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005240
Josef Bacik9ed74f22009-09-11 16:12:44 -04005241 /*
5242 * 2 items for inode and ref
5243 * 2 items for dir items
5244 * 1 for xattr if selinux is on
5245 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005246 trans = btrfs_start_transaction(root, 5);
5247 if (IS_ERR(trans))
5248 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005249
Li Zefan581bb052011-04-20 10:06:11 +08005250 err = btrfs_find_free_ino(root, &objectid);
5251 if (err)
5252 goto out_fail;
5253
Josef Bacikaec74772008-07-24 12:12:38 -04005254 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005255 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005256 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005257 if (IS_ERR(inode)) {
5258 err = PTR_ERR(inode);
5259 goto out_fail;
5260 }
Chris Mason5f39d392007-10-15 16:14:19 -04005261
Chris Mason39279cc2007-06-12 06:35:45 -04005262 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005263
Eric Paris2a7dba32011-02-01 11:05:39 -05005264 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005265 if (err)
5266 goto out_fail;
5267
Chris Mason39279cc2007-06-12 06:35:45 -04005268 inode->i_op = &btrfs_dir_inode_operations;
5269 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005270
Chris Masondbe674a2008-07-17 12:54:05 -04005271 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005272 err = btrfs_update_inode(trans, root, inode);
5273 if (err)
5274 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005275
Josef Bacika1b075d2010-11-19 20:36:11 +00005276 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5277 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005278 if (err)
5279 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005280
Chris Mason39279cc2007-06-12 06:35:45 -04005281 d_instantiate(dentry, inode);
5282 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005283
5284out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005285 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005286 if (drop_on_err)
5287 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005288 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005289 return err;
5290}
5291
Chris Masond352ac62008-09-29 15:18:18 -04005292/* helper for btfs_get_extent. Given an existing extent in the tree,
5293 * and an extent that you want to insert, deal with overlap and insert
5294 * the new extent into the tree.
5295 */
Chris Mason3b951512008-04-17 11:29:12 -04005296static int merge_extent_mapping(struct extent_map_tree *em_tree,
5297 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005298 struct extent_map *em,
5299 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005300{
5301 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005302
Chris Masone6dcd2d2008-07-17 12:53:50 -04005303 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5304 start_diff = map_start - em->start;
5305 em->start = map_start;
5306 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005307 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5308 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005309 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005310 em->block_len -= start_diff;
5311 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005312 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005313}
5314
Chris Masonc8b97812008-10-29 14:49:59 -04005315static noinline int uncompress_inline(struct btrfs_path *path,
5316 struct inode *inode, struct page *page,
5317 size_t pg_offset, u64 extent_offset,
5318 struct btrfs_file_extent_item *item)
5319{
5320 int ret;
5321 struct extent_buffer *leaf = path->nodes[0];
5322 char *tmp;
5323 size_t max_size;
5324 unsigned long inline_size;
5325 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005326 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005327
5328 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005329 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005330 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5331 inline_size = btrfs_file_extent_inline_item_len(leaf,
5332 btrfs_item_nr(leaf, path->slots[0]));
5333 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005334 if (!tmp)
5335 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005336 ptr = btrfs_file_extent_inline_start(item);
5337
5338 read_extent_buffer(leaf, tmp, ptr, inline_size);
5339
Chris Mason5b050f02008-11-11 09:34:41 -05005340 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005341 ret = btrfs_decompress(compress_type, tmp, page,
5342 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005343 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005344 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005345 unsigned long copy_size = min_t(u64,
5346 PAGE_CACHE_SIZE - pg_offset,
5347 max_size - extent_offset);
5348 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005349 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005350 }
5351 kfree(tmp);
5352 return 0;
5353}
5354
Chris Masond352ac62008-09-29 15:18:18 -04005355/*
5356 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005357 * the ugly parts come from merging extents from the disk with the in-ram
5358 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005359 * where the in-ram extents might be locked pending data=ordered completion.
5360 *
5361 * This also copies inline extents directly into the page.
5362 */
Chris Masond3977122009-01-05 21:25:51 -05005363
Chris Masona52d9a82007-08-27 16:49:44 -04005364struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005365 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005366 int create)
5367{
5368 int ret;
5369 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005370 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005371 u64 extent_start = 0;
5372 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005373 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005374 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005375 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005376 struct btrfs_root *root = BTRFS_I(inode)->root;
5377 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005378 struct extent_buffer *leaf;
5379 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005380 struct extent_map *em = NULL;
5381 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005382 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005383 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005384 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005385
Chris Masona52d9a82007-08-27 16:49:44 -04005386again:
Chris Mason890871b2009-09-02 16:24:52 -04005387 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005388 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005389 if (em)
5390 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005391 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005392
Chris Masona52d9a82007-08-27 16:49:44 -04005393 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005394 if (em->start > start || em->start + em->len <= start)
5395 free_extent_map(em);
5396 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005397 free_extent_map(em);
5398 else
5399 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005400 }
David Sterba172ddd62011-04-21 00:48:27 +02005401 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005402 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005403 err = -ENOMEM;
5404 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005405 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005406 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005407 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005408 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005409 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005410 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005411
5412 if (!path) {
5413 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005414 if (!path) {
5415 err = -ENOMEM;
5416 goto out;
5417 }
5418 /*
5419 * Chances are we'll be called again, so go ahead and do
5420 * readahead
5421 */
5422 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005423 }
5424
Chris Mason179e29e2007-11-01 11:28:41 -04005425 ret = btrfs_lookup_file_extent(trans, root, path,
5426 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005427 if (ret < 0) {
5428 err = ret;
5429 goto out;
5430 }
5431
5432 if (ret != 0) {
5433 if (path->slots[0] == 0)
5434 goto not_found;
5435 path->slots[0]--;
5436 }
5437
Chris Mason5f39d392007-10-15 16:14:19 -04005438 leaf = path->nodes[0];
5439 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005440 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005441 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005442 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5443 found_type = btrfs_key_type(&found_key);
5444 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005445 found_type != BTRFS_EXTENT_DATA_KEY) {
5446 goto not_found;
5447 }
5448
Chris Mason5f39d392007-10-15 16:14:19 -04005449 found_type = btrfs_file_extent_type(leaf, item);
5450 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005451 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005452 if (found_type == BTRFS_FILE_EXTENT_REG ||
5453 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005454 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005455 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005456 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5457 size_t size;
5458 size = btrfs_file_extent_inline_len(leaf, item);
5459 extent_end = (extent_start + size + root->sectorsize - 1) &
5460 ~((u64)root->sectorsize - 1);
5461 }
5462
5463 if (start >= extent_end) {
5464 path->slots[0]++;
5465 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5466 ret = btrfs_next_leaf(root, path);
5467 if (ret < 0) {
5468 err = ret;
5469 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005470 }
Yan Zheng9036c102008-10-30 14:19:41 -04005471 if (ret > 0)
5472 goto not_found;
5473 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005474 }
Yan Zheng9036c102008-10-30 14:19:41 -04005475 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5476 if (found_key.objectid != objectid ||
5477 found_key.type != BTRFS_EXTENT_DATA_KEY)
5478 goto not_found;
5479 if (start + len <= found_key.offset)
5480 goto not_found;
5481 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005482 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04005483 em->len = found_key.offset - start;
5484 goto not_found_em;
5485 }
5486
Yan Zhengd899e052008-10-30 14:25:28 -04005487 if (found_type == BTRFS_FILE_EXTENT_REG ||
5488 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005489 em->start = extent_start;
5490 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005491 em->orig_start = extent_start -
5492 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05005493 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5494 item);
Chris Masondb945352007-10-15 16:15:53 -04005495 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5496 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005497 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005498 goto insert;
5499 }
Li Zefan261507a02010-12-17 14:21:50 +08005500 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005501 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005502 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005503 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05005504 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005505 } else {
5506 bytenr += btrfs_file_extent_offset(leaf, item);
5507 em->block_start = bytenr;
5508 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005509 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5510 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005511 }
Chris Masona52d9a82007-08-27 16:49:44 -04005512 goto insert;
5513 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005514 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005515 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005516 size_t size;
5517 size_t extent_offset;
5518 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005519
Chris Masona52d9a82007-08-27 16:49:44 -04005520 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005521 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005522 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005523 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005524 goto out;
5525 }
5526
Yan Zheng9036c102008-10-30 14:19:41 -04005527 size = btrfs_file_extent_inline_len(leaf, item);
5528 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005529 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005530 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005531 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005532 em->len = (copy_size + root->sectorsize - 1) &
5533 ~((u64)root->sectorsize - 1);
Josef Bacikb4939682012-12-03 10:31:19 -05005534 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005535 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08005536 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005537 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005538 em->compress_type = compress_type;
5539 }
Yan689f9342007-10-29 11:41:07 -04005540 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005541 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005542 if (btrfs_file_extent_compression(leaf, item) !=
5543 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005544 ret = uncompress_inline(path, inode, page,
5545 pg_offset,
5546 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005547 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005548 } else {
5549 map = kmap(page);
5550 read_extent_buffer(leaf, map + pg_offset, ptr,
5551 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005552 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5553 memset(map + pg_offset + copy_size, 0,
5554 PAGE_CACHE_SIZE - pg_offset -
5555 copy_size);
5556 }
Chris Masonc8b97812008-10-29 14:49:59 -04005557 kunmap(page);
5558 }
Chris Mason179e29e2007-11-01 11:28:41 -04005559 flush_dcache_page(page);
5560 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005561 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005562 if (!trans) {
5563 kunmap(page);
5564 free_extent_map(em);
5565 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005566
David Sterbab3b4aa72011-04-21 01:20:15 +02005567 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005568 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005569
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005570 if (IS_ERR(trans))
5571 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005572 goto again;
5573 }
Chris Masonc8b97812008-10-29 14:49:59 -04005574 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005575 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005576 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005577 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005578 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005579 }
Chris Masond1310b22008-01-24 16:13:08 -05005580 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005581 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005582 goto insert;
5583 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005584 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005585 }
5586not_found:
5587 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005588 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005589 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005590not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005591 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005592 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005593insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005594 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005595 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005596 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5597 "[%llu %llu]\n", (unsigned long long)em->start,
5598 (unsigned long long)em->len,
5599 (unsigned long long)start,
5600 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005601 err = -EIO;
5602 goto out;
5603 }
Chris Masond1310b22008-01-24 16:13:08 -05005604
5605 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005606 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005607 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005608 /* it is possible that someone inserted the extent into the tree
5609 * while we had the lock dropped. It is also possible that
5610 * an overlapping map exists in the tree
5611 */
Chris Masona52d9a82007-08-27 16:49:44 -04005612 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005613 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005614
5615 ret = 0;
5616
Chris Mason3b951512008-04-17 11:29:12 -04005617 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005618 if (existing && (existing->start > start ||
5619 existing->start + existing->len <= start)) {
5620 free_extent_map(existing);
5621 existing = NULL;
5622 }
Chris Mason3b951512008-04-17 11:29:12 -04005623 if (!existing) {
5624 existing = lookup_extent_mapping(em_tree, em->start,
5625 em->len);
5626 if (existing) {
5627 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005628 em, start,
5629 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005630 free_extent_map(existing);
5631 if (err) {
5632 free_extent_map(em);
5633 em = NULL;
5634 }
5635 } else {
5636 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005637 free_extent_map(em);
5638 em = NULL;
5639 }
5640 } else {
5641 free_extent_map(em);
5642 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005643 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005644 }
Chris Masona52d9a82007-08-27 16:49:44 -04005645 }
Chris Mason890871b2009-09-02 16:24:52 -04005646 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005647out:
liubo1abe9b82011-03-24 11:18:59 +00005648
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005649 if (em)
5650 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005651
Chris Masonf4219502008-07-22 11:18:09 -04005652 if (path)
5653 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005654 if (trans) {
5655 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005656 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005657 err = ret;
5658 }
Chris Masona52d9a82007-08-27 16:49:44 -04005659 if (err) {
5660 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005661 return ERR_PTR(err);
5662 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005663 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005664 return em;
5665}
5666
Chris Masonec29ed52011-02-23 16:23:20 -05005667struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5668 size_t pg_offset, u64 start, u64 len,
5669 int create)
5670{
5671 struct extent_map *em;
5672 struct extent_map *hole_em = NULL;
5673 u64 range_start = start;
5674 u64 end;
5675 u64 found;
5676 u64 found_end;
5677 int err = 0;
5678
5679 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5680 if (IS_ERR(em))
5681 return em;
5682 if (em) {
5683 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00005684 * if our em maps to
5685 * - a hole or
5686 * - a pre-alloc extent,
5687 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05005688 */
Liu Bof9e4fb52013-01-07 10:10:12 +00005689 if (em->block_start != EXTENT_MAP_HOLE &&
5690 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05005691 return em;
5692 else
5693 hole_em = em;
5694 }
5695
5696 /* check to see if we've wrapped (len == -1 or similar) */
5697 end = start + len;
5698 if (end < start)
5699 end = (u64)-1;
5700 else
5701 end -= 1;
5702
5703 em = NULL;
5704
5705 /* ok, we didn't find anything, lets look for delalloc */
5706 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5707 end, len, EXTENT_DELALLOC, 1);
5708 found_end = range_start + found;
5709 if (found_end < range_start)
5710 found_end = (u64)-1;
5711
5712 /*
5713 * we didn't find anything useful, return
5714 * the original results from get_extent()
5715 */
5716 if (range_start > end || found_end <= start) {
5717 em = hole_em;
5718 hole_em = NULL;
5719 goto out;
5720 }
5721
5722 /* adjust the range_start to make sure it doesn't
5723 * go backwards from the start they passed in
5724 */
5725 range_start = max(start,range_start);
5726 found = found_end - range_start;
5727
5728 if (found > 0) {
5729 u64 hole_start = start;
5730 u64 hole_len = len;
5731
David Sterba172ddd62011-04-21 00:48:27 +02005732 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005733 if (!em) {
5734 err = -ENOMEM;
5735 goto out;
5736 }
5737 /*
5738 * when btrfs_get_extent can't find anything it
5739 * returns one huge hole
5740 *
5741 * make sure what it found really fits our range, and
5742 * adjust to make sure it is based on the start from
5743 * the caller
5744 */
5745 if (hole_em) {
5746 u64 calc_end = extent_map_end(hole_em);
5747
5748 if (calc_end <= start || (hole_em->start > end)) {
5749 free_extent_map(hole_em);
5750 hole_em = NULL;
5751 } else {
5752 hole_start = max(hole_em->start, start);
5753 hole_len = calc_end - hole_start;
5754 }
5755 }
5756 em->bdev = NULL;
5757 if (hole_em && range_start > hole_start) {
5758 /* our hole starts before our delalloc, so we
5759 * have to return just the parts of the hole
5760 * that go until the delalloc starts
5761 */
5762 em->len = min(hole_len,
5763 range_start - hole_start);
5764 em->start = hole_start;
5765 em->orig_start = hole_start;
5766 /*
5767 * don't adjust block start at all,
5768 * it is fixed at EXTENT_MAP_HOLE
5769 */
5770 em->block_start = hole_em->block_start;
5771 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00005772 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
5773 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05005774 } else {
5775 em->start = range_start;
5776 em->len = found;
5777 em->orig_start = range_start;
5778 em->block_start = EXTENT_MAP_DELALLOC;
5779 em->block_len = found;
5780 }
5781 } else if (hole_em) {
5782 return hole_em;
5783 }
5784out:
5785
5786 free_extent_map(hole_em);
5787 if (err) {
5788 free_extent_map(em);
5789 return ERR_PTR(err);
5790 }
5791 return em;
5792}
5793
Josef Bacik4b46fce2010-05-23 11:00:55 -04005794static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5795 u64 start, u64 len)
5796{
5797 struct btrfs_root *root = BTRFS_I(inode)->root;
5798 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04005799 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005800 struct btrfs_key ins;
5801 u64 alloc_hint;
5802 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005803
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005804 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005805 if (IS_ERR(trans))
5806 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005807
5808 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5809
5810 alloc_hint = get_extent_allocation_hint(inode, start, len);
5811 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005812 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005813 if (ret) {
5814 em = ERR_PTR(ret);
5815 goto out;
5816 }
5817
Josef Bacik70c8a912012-10-11 16:54:30 -04005818 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
5819 ins.offset, ins.offset, 0);
5820 if (IS_ERR(em))
5821 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005822
5823 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5824 ins.offset, ins.offset, 0);
5825 if (ret) {
5826 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5827 em = ERR_PTR(ret);
5828 }
5829out:
5830 btrfs_end_transaction(trans, root);
5831 return em;
5832}
5833
Chris Mason46bfbb52010-05-26 11:04:10 -04005834/*
5835 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5836 * block must be cow'd
5837 */
5838static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5839 struct inode *inode, u64 offset, u64 len)
5840{
5841 struct btrfs_path *path;
5842 int ret;
5843 struct extent_buffer *leaf;
5844 struct btrfs_root *root = BTRFS_I(inode)->root;
5845 struct btrfs_file_extent_item *fi;
5846 struct btrfs_key key;
5847 u64 disk_bytenr;
5848 u64 backref_offset;
5849 u64 extent_end;
5850 u64 num_bytes;
5851 int slot;
5852 int found_type;
5853
5854 path = btrfs_alloc_path();
5855 if (!path)
5856 return -ENOMEM;
5857
Li Zefan33345d012011-04-20 10:31:50 +08005858 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005859 offset, 0);
5860 if (ret < 0)
5861 goto out;
5862
5863 slot = path->slots[0];
5864 if (ret == 1) {
5865 if (slot == 0) {
5866 /* can't find the item, must cow */
5867 ret = 0;
5868 goto out;
5869 }
5870 slot--;
5871 }
5872 ret = 0;
5873 leaf = path->nodes[0];
5874 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005875 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005876 key.type != BTRFS_EXTENT_DATA_KEY) {
5877 /* not our file or wrong item type, must cow */
5878 goto out;
5879 }
5880
5881 if (key.offset > offset) {
5882 /* Wrong offset, must cow */
5883 goto out;
5884 }
5885
5886 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5887 found_type = btrfs_file_extent_type(leaf, fi);
5888 if (found_type != BTRFS_FILE_EXTENT_REG &&
5889 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5890 /* not a regular extent, must cow */
5891 goto out;
5892 }
5893 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5894 backref_offset = btrfs_file_extent_offset(leaf, fi);
5895
5896 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5897 if (extent_end < offset + len) {
5898 /* extent doesn't include our full range, must cow */
5899 goto out;
5900 }
5901
5902 if (btrfs_extent_readonly(root, disk_bytenr))
5903 goto out;
5904
5905 /*
5906 * look for other files referencing this extent, if we
5907 * find any we must cow
5908 */
Li Zefan33345d012011-04-20 10:31:50 +08005909 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005910 key.offset - backref_offset, disk_bytenr))
5911 goto out;
5912
5913 /*
5914 * adjust disk_bytenr and num_bytes to cover just the bytes
5915 * in this extent we are about to write. If there
5916 * are any csums in that range we have to cow in order
5917 * to keep the csums correct
5918 */
5919 disk_bytenr += backref_offset;
5920 disk_bytenr += offset - key.offset;
5921 num_bytes = min(offset + len, extent_end) - offset;
5922 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5923 goto out;
5924 /*
5925 * all of the above have passed, it is safe to overwrite this extent
5926 * without cow
5927 */
5928 ret = 1;
5929out:
5930 btrfs_free_path(path);
5931 return ret;
5932}
5933
Josef Bacikeb838e72012-07-31 16:28:48 -04005934static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5935 struct extent_state **cached_state, int writing)
5936{
5937 struct btrfs_ordered_extent *ordered;
5938 int ret = 0;
5939
5940 while (1) {
5941 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5942 0, cached_state);
5943 /*
5944 * We're concerned with the entire range that we're going to be
5945 * doing DIO to, so we need to make sure theres no ordered
5946 * extents in this range.
5947 */
5948 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5949 lockend - lockstart + 1);
5950
5951 /*
5952 * We need to make sure there are no buffered pages in this
5953 * range either, we could have raced between the invalidate in
5954 * generic_file_direct_write and locking the extent. The
5955 * invalidate needs to happen so that reads after a write do not
5956 * get stale data.
5957 */
5958 if (!ordered && (!writing ||
5959 !test_range_bit(&BTRFS_I(inode)->io_tree,
5960 lockstart, lockend, EXTENT_UPTODATE, 0,
5961 *cached_state)))
5962 break;
5963
5964 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5965 cached_state, GFP_NOFS);
5966
5967 if (ordered) {
5968 btrfs_start_ordered_extent(inode, ordered, 1);
5969 btrfs_put_ordered_extent(ordered);
5970 } else {
5971 /* Screw you mmap */
5972 ret = filemap_write_and_wait_range(inode->i_mapping,
5973 lockstart,
5974 lockend);
5975 if (ret)
5976 break;
5977
5978 /*
5979 * If we found a page that couldn't be invalidated just
5980 * fall back to buffered.
5981 */
5982 ret = invalidate_inode_pages2_range(inode->i_mapping,
5983 lockstart >> PAGE_CACHE_SHIFT,
5984 lockend >> PAGE_CACHE_SHIFT);
5985 if (ret)
5986 break;
5987 }
5988
5989 cond_resched();
5990 }
5991
5992 return ret;
5993}
5994
Josef Bacik69ffb542012-09-11 15:40:07 -04005995static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5996 u64 len, u64 orig_start,
5997 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05005998 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04005999{
6000 struct extent_map_tree *em_tree;
6001 struct extent_map *em;
6002 struct btrfs_root *root = BTRFS_I(inode)->root;
6003 int ret;
6004
6005 em_tree = &BTRFS_I(inode)->extent_tree;
6006 em = alloc_extent_map();
6007 if (!em)
6008 return ERR_PTR(-ENOMEM);
6009
6010 em->start = start;
6011 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04006012 em->mod_start = start;
6013 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006014 em->len = len;
6015 em->block_len = block_len;
6016 em->block_start = block_start;
6017 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05006018 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006019 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04006020 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6021 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006022 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006023
6024 do {
6025 btrfs_drop_extent_cache(inode, em->start,
6026 em->start + em->len - 1, 0);
6027 write_lock(&em_tree->lock);
6028 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04006029 if (!ret)
6030 list_move(&em->list,
6031 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006032 write_unlock(&em_tree->lock);
6033 } while (ret == -EEXIST);
6034
6035 if (ret) {
6036 free_extent_map(em);
6037 return ERR_PTR(ret);
6038 }
6039
6040 return em;
6041}
6042
6043
Josef Bacik4b46fce2010-05-23 11:00:55 -04006044static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6045 struct buffer_head *bh_result, int create)
6046{
6047 struct extent_map *em;
6048 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006049 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006050 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006051 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006052 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006053 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006054 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00006055 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006056
Josef Bacikeb838e72012-07-31 16:28:48 -04006057 if (create) {
Miao Xie09348562013-02-07 10:12:07 +00006058 spin_lock(&BTRFS_I(inode)->lock);
6059 BTRFS_I(inode)->outstanding_extents++;
6060 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikeb838e72012-07-31 16:28:48 -04006061 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Miao Xie09348562013-02-07 10:12:07 +00006062 } else
Josef Bacikc3298612012-08-03 16:49:19 -04006063 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006064
Josef Bacikc3298612012-08-03 16:49:19 -04006065 lockstart = start;
6066 lockend = start + len - 1;
6067
Josef Bacikeb838e72012-07-31 16:28:48 -04006068 /*
6069 * If this errors out it's because we couldn't invalidate pagecache for
6070 * this range and we need to fallback to buffered.
6071 */
6072 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6073 return -ENOTBLK;
6074
Josef Bacik4b46fce2010-05-23 11:00:55 -04006075 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006076 if (IS_ERR(em)) {
6077 ret = PTR_ERR(em);
6078 goto unlock_err;
6079 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006080
6081 /*
6082 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6083 * io. INLINE is special, and we could probably kludge it in here, but
6084 * it's still buffered so for safety lets just fall back to the generic
6085 * buffered path.
6086 *
6087 * For COMPRESSED we _have_ to read the entire extent in so we can
6088 * decompress it, so there will be buffering required no matter what we
6089 * do, so go ahead and fallback to buffered.
6090 *
6091 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6092 * to buffered IO. Don't blame me, this is the price we pay for using
6093 * the generic code.
6094 */
6095 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6096 em->block_start == EXTENT_MAP_INLINE) {
6097 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006098 ret = -ENOTBLK;
6099 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006100 }
6101
6102 /* Just a good old fashioned hole, return */
6103 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6104 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6105 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006106 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006107 }
6108
6109 /*
6110 * We don't allocate a new extent in the following cases
6111 *
6112 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6113 * existing extent.
6114 * 2) The extent is marked as PREALLOC. We're good to go here and can
6115 * just use the extent.
6116 *
6117 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006118 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006119 len = min(len, em->len - (start - em->start));
6120 lockstart = start + len;
6121 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006122 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006123
6124 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6125 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6126 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006127 int type;
6128 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006129 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006130
6131 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6132 type = BTRFS_ORDERED_PREALLOC;
6133 else
6134 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006135 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006136 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006137
6138 /*
6139 * we're not going to log anything, but we do need
6140 * to make sure the current transaction stays open
6141 * while we look for nocow cross refs
6142 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006143 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006144 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006145 goto must_cow;
6146
6147 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006148 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006149 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006150
6151 if (type == BTRFS_ORDERED_PREALLOC) {
6152 free_extent_map(em);
6153 em = create_pinned_em(inode, start, len,
6154 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006155 block_start, len,
6156 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006157 if (IS_ERR(em)) {
6158 btrfs_end_transaction(trans, root);
6159 goto unlock_err;
6160 }
6161 }
6162
Chris Mason46bfbb52010-05-26 11:04:10 -04006163 ret = btrfs_add_ordered_extent_dio(inode, start,
6164 block_start, len, len, type);
6165 btrfs_end_transaction(trans, root);
6166 if (ret) {
6167 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006168 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006169 }
6170 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006171 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006172 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006173 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006174must_cow:
6175 /*
6176 * this will cow the extent, reset the len in case we changed
6177 * it above
6178 */
6179 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006180 free_extent_map(em);
6181 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006182 if (IS_ERR(em)) {
6183 ret = PTR_ERR(em);
6184 goto unlock_err;
6185 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006186 len = min(len, em->len - (start - em->start));
6187unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006188 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6189 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006190 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006191 bh_result->b_bdev = em->bdev;
6192 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006193 if (create) {
6194 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6195 set_buffer_new(bh_result);
6196
6197 /*
6198 * Need to update the i_size under the extent lock so buffered
6199 * readers will get the updated i_size when we unlock.
6200 */
6201 if (start + len > i_size_read(inode))
6202 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00006203
6204 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6205 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6206 &cached_state, GFP_NOFS);
6207 BUG_ON(ret);
Josef Bacikc3473e82012-06-19 10:59:00 -04006208 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006209
Josef Bacikeb838e72012-07-31 16:28:48 -04006210 /*
6211 * In the case of write we need to clear and unlock the entire range,
6212 * in the case of read we need to unlock only the end area that we
6213 * aren't using if there is any left over space.
6214 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006215 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00006216 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6217 lockend, unlock_bits, 1, 0,
6218 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006219 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006220 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006221 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006222
Josef Bacik4b46fce2010-05-23 11:00:55 -04006223 free_extent_map(em);
6224
6225 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006226
6227unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04006228 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6229 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6230 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006231}
6232
6233struct btrfs_dio_private {
6234 struct inode *inode;
6235 u64 logical_offset;
6236 u64 disk_bytenr;
6237 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006238 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006239
6240 /* number of bios pending for this dio */
6241 atomic_t pending_bios;
6242
6243 /* IO errors */
6244 int errors;
6245
6246 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006247};
6248
6249static void btrfs_endio_direct_read(struct bio *bio, int err)
6250{
Miao Xiee65e1532010-11-22 03:04:43 +00006251 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006252 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6253 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006254 struct inode *inode = dip->inode;
6255 struct btrfs_root *root = BTRFS_I(inode)->root;
6256 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006257
6258 start = dip->logical_offset;
6259 do {
6260 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6261 struct page *page = bvec->bv_page;
6262 char *kaddr;
6263 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006264 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006265 unsigned long flags;
6266
Josef Bacikc3298612012-08-03 16:49:19 -04006267 if (get_state_private(&BTRFS_I(inode)->io_tree,
6268 start, &private))
6269 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006270 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006271 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006272 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6273 csum, bvec->bv_len);
6274 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006275 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006276 local_irq_restore(flags);
6277
6278 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006279 if (csum != private) {
6280failed:
Li Zefan33345d012011-04-20 10:31:50 +08006281 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006282 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006283 (unsigned long long)btrfs_ino(inode),
6284 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006285 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006286 err = -EIO;
6287 }
6288 }
6289
6290 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006291 bvec++;
6292 } while (bvec <= bvec_end);
6293
6294 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006295 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006296 bio->bi_private = dip->private;
6297
Josef Bacik4b46fce2010-05-23 11:00:55 -04006298 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006299
6300 /* If we had a csum failure make sure to clear the uptodate flag */
6301 if (err)
6302 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006303 dio_end_io(bio, err);
6304}
6305
6306static void btrfs_endio_direct_write(struct bio *bio, int err)
6307{
6308 struct btrfs_dio_private *dip = bio->bi_private;
6309 struct inode *inode = dip->inode;
6310 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006311 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006312 u64 ordered_offset = dip->logical_offset;
6313 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006314 int ret;
6315
6316 if (err)
6317 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006318again:
6319 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6320 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006321 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006322 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006323 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006324
Josef Bacik5fd02042012-05-02 14:00:54 -04006325 ordered->work.func = finish_ordered_fn;
6326 ordered->work.flags = 0;
6327 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6328 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006329out_test:
6330 /*
6331 * our bio might span multiple ordered extents. If we haven't
6332 * completed the accounting for the whole dio, go back and try again
6333 */
6334 if (ordered_offset < dip->logical_offset + dip->bytes) {
6335 ordered_bytes = dip->logical_offset + dip->bytes -
6336 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006337 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006338 goto again;
6339 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006340out_done:
6341 bio->bi_private = dip->private;
6342
Josef Bacik4b46fce2010-05-23 11:00:55 -04006343 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006344
6345 /* If we had an error make sure to clear the uptodate flag */
6346 if (err)
6347 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006348 dio_end_io(bio, err);
6349}
6350
Chris Masoneaf25d92010-05-25 09:48:28 -04006351static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6352 struct bio *bio, int mirror_num,
6353 unsigned long bio_flags, u64 offset)
6354{
6355 int ret;
6356 struct btrfs_root *root = BTRFS_I(inode)->root;
6357 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006358 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006359 return 0;
6360}
6361
Miao Xiee65e1532010-11-22 03:04:43 +00006362static void btrfs_end_dio_bio(struct bio *bio, int err)
6363{
6364 struct btrfs_dio_private *dip = bio->bi_private;
6365
6366 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006367 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006368 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006369 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006370 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006371 dip->errors = 1;
6372
6373 /*
6374 * before atomic variable goto zero, we must make sure
6375 * dip->errors is perceived to be set.
6376 */
6377 smp_mb__before_atomic_dec();
6378 }
6379
6380 /* if there are more bios still pending for this dio, just exit */
6381 if (!atomic_dec_and_test(&dip->pending_bios))
6382 goto out;
6383
6384 if (dip->errors)
6385 bio_io_error(dip->orig_bio);
6386 else {
6387 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6388 bio_endio(dip->orig_bio, 0);
6389 }
6390out:
6391 bio_put(bio);
6392}
6393
6394static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6395 u64 first_sector, gfp_t gfp_flags)
6396{
6397 int nr_vecs = bio_get_nr_vecs(bdev);
6398 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6399}
6400
6401static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6402 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006403 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006404{
6405 int write = rw & REQ_WRITE;
6406 struct btrfs_root *root = BTRFS_I(inode)->root;
6407 int ret;
6408
Josef Bacikb812ce22012-11-16 13:56:32 -05006409 if (async_submit)
6410 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6411
Miao Xiee65e1532010-11-22 03:04:43 +00006412 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006413
6414 if (!write) {
6415 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6416 if (ret)
6417 goto err;
6418 }
Miao Xiee65e1532010-11-22 03:04:43 +00006419
Josef Bacik1ae39932011-04-06 14:41:34 -04006420 if (skip_sum)
6421 goto map;
6422
6423 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006424 ret = btrfs_wq_submit_bio(root->fs_info,
6425 inode, rw, bio, 0, 0,
6426 file_offset,
6427 __btrfs_submit_bio_start_direct_io,
6428 __btrfs_submit_bio_done);
6429 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006430 } else if (write) {
6431 /*
6432 * If we aren't doing async submit, calculate the csum of the
6433 * bio now.
6434 */
6435 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6436 if (ret)
6437 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006438 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006439 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006440 if (ret)
6441 goto err;
6442 }
Miao Xiee65e1532010-11-22 03:04:43 +00006443
Josef Bacik1ae39932011-04-06 14:41:34 -04006444map:
6445 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006446err:
6447 bio_put(bio);
6448 return ret;
6449}
6450
6451static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6452 int skip_sum)
6453{
6454 struct inode *inode = dip->inode;
6455 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006456 struct bio *bio;
6457 struct bio *orig_bio = dip->orig_bio;
6458 struct bio_vec *bvec = orig_bio->bi_io_vec;
6459 u64 start_sector = orig_bio->bi_sector;
6460 u64 file_offset = dip->logical_offset;
6461 u64 submit_len = 0;
6462 u64 map_length;
6463 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006464 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006465 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006466
Miao Xiee65e1532010-11-22 03:04:43 +00006467 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05006468 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006469 &map_length, NULL, 0);
6470 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006471 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006472 return -EIO;
6473 }
Josef Bacik02f57c72011-04-06 14:25:44 -04006474 if (map_length >= orig_bio->bi_size) {
6475 bio = orig_bio;
6476 goto submit;
6477 }
6478
David Woodhouse53b381b2013-01-29 18:40:14 -05006479 /* async crcs make it difficult to collect full stripe writes. */
6480 if (btrfs_get_alloc_profile(root, 1) &
6481 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
6482 async_submit = 0;
6483 else
6484 async_submit = 1;
6485
Josef Bacik02f57c72011-04-06 14:25:44 -04006486 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6487 if (!bio)
6488 return -ENOMEM;
6489 bio->bi_private = dip;
6490 bio->bi_end_io = btrfs_end_dio_bio;
6491 atomic_inc(&dip->pending_bios);
6492
Miao Xiee65e1532010-11-22 03:04:43 +00006493 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6494 if (unlikely(map_length < submit_len + bvec->bv_len ||
6495 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6496 bvec->bv_offset) < bvec->bv_len)) {
6497 /*
6498 * inc the count before we submit the bio so
6499 * we know the end IO handler won't happen before
6500 * we inc the count. Otherwise, the dip might get freed
6501 * before we're done setting it up
6502 */
6503 atomic_inc(&dip->pending_bios);
6504 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6505 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006506 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006507 if (ret) {
6508 bio_put(bio);
6509 atomic_dec(&dip->pending_bios);
6510 goto out_err;
6511 }
6512
Miao Xiee65e1532010-11-22 03:04:43 +00006513 start_sector += submit_len >> 9;
6514 file_offset += submit_len;
6515
6516 submit_len = 0;
6517 nr_pages = 0;
6518
6519 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6520 start_sector, GFP_NOFS);
6521 if (!bio)
6522 goto out_err;
6523 bio->bi_private = dip;
6524 bio->bi_end_io = btrfs_end_dio_bio;
6525
6526 map_length = orig_bio->bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05006527 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006528 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006529 &map_length, NULL, 0);
6530 if (ret) {
6531 bio_put(bio);
6532 goto out_err;
6533 }
6534 } else {
6535 submit_len += bvec->bv_len;
6536 nr_pages ++;
6537 bvec++;
6538 }
6539 }
6540
Josef Bacik02f57c72011-04-06 14:25:44 -04006541submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006542 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006543 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006544 if (!ret)
6545 return 0;
6546
6547 bio_put(bio);
6548out_err:
6549 dip->errors = 1;
6550 /*
6551 * before atomic variable goto zero, we must
6552 * make sure dip->errors is perceived to be set.
6553 */
6554 smp_mb__before_atomic_dec();
6555 if (atomic_dec_and_test(&dip->pending_bios))
6556 bio_io_error(dip->orig_bio);
6557
6558 /* bio_end_io() will handle error, so we needn't return it */
6559 return 0;
6560}
6561
Josef Bacik4b46fce2010-05-23 11:00:55 -04006562static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6563 loff_t file_offset)
6564{
6565 struct btrfs_root *root = BTRFS_I(inode)->root;
6566 struct btrfs_dio_private *dip;
6567 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006568 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006569 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006570 int ret = 0;
6571
6572 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6573
6574 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6575 if (!dip) {
6576 ret = -ENOMEM;
6577 goto free_ordered;
6578 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006579
6580 dip->private = bio->bi_private;
6581 dip->inode = inode;
6582 dip->logical_offset = file_offset;
6583
Josef Bacik4b46fce2010-05-23 11:00:55 -04006584 dip->bytes = 0;
6585 do {
6586 dip->bytes += bvec->bv_len;
6587 bvec++;
6588 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6589
Chris Mason46bfbb52010-05-26 11:04:10 -04006590 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006591 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006592 dip->errors = 0;
6593 dip->orig_bio = bio;
6594 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006595
6596 if (write)
6597 bio->bi_end_io = btrfs_endio_direct_write;
6598 else
6599 bio->bi_end_io = btrfs_endio_direct_read;
6600
Miao Xiee65e1532010-11-22 03:04:43 +00006601 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6602 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006603 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006604free_ordered:
6605 /*
6606 * If this is a write, we need to clean up the reserved space and kill
6607 * the ordered extent.
6608 */
6609 if (write) {
6610 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006611 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006612 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6613 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6614 btrfs_free_reserved_extent(root, ordered->start,
6615 ordered->disk_len);
6616 btrfs_put_ordered_extent(ordered);
6617 btrfs_put_ordered_extent(ordered);
6618 }
6619 bio_endio(bio, ret);
6620}
6621
Chris Mason5a5f79b2010-05-26 21:33:37 -04006622static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6623 const struct iovec *iov, loff_t offset,
6624 unsigned long nr_segs)
6625{
6626 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006627 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006628 size_t size;
6629 unsigned long addr;
6630 unsigned blocksize_mask = root->sectorsize - 1;
6631 ssize_t retval = -EINVAL;
6632 loff_t end = offset;
6633
6634 if (offset & blocksize_mask)
6635 goto out;
6636
6637 /* Check the memory alignment. Blocks cannot straddle pages */
6638 for (seg = 0; seg < nr_segs; seg++) {
6639 addr = (unsigned long)iov[seg].iov_base;
6640 size = iov[seg].iov_len;
6641 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006642 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006643 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006644
6645 /* If this is a write we don't need to check anymore */
6646 if (rw & WRITE)
6647 continue;
6648
6649 /*
6650 * Check to make sure we don't have duplicate iov_base's in this
6651 * iovec, if so return EINVAL, otherwise we'll get csum errors
6652 * when reading back.
6653 */
6654 for (i = seg + 1; i < nr_segs; i++) {
6655 if (iov[seg].iov_base == iov[i].iov_base)
6656 goto out;
6657 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006658 }
6659 retval = 0;
6660out:
6661 return retval;
6662}
Josef Bacikeb838e72012-07-31 16:28:48 -04006663
Chris Mason16432982008-04-10 10:23:21 -04006664static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6665 const struct iovec *iov, loff_t offset,
6666 unsigned long nr_segs)
6667{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006668 struct file *file = iocb->ki_filp;
6669 struct inode *inode = file->f_mapping->host;
Miao Xie09348562013-02-07 10:12:07 +00006670 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00006671 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00006672 bool wakeup = true;
6673 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00006674 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006675
Chris Mason5a5f79b2010-05-26 21:33:37 -04006676 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006677 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006678 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006679
Miao Xie38851cc2013-02-08 07:04:11 +00006680 atomic_inc(&inode->i_dio_count);
6681 smp_mb__after_atomic_inc();
6682
Miao Xie09348562013-02-07 10:12:07 +00006683 if (rw & WRITE) {
6684 count = iov_length(iov, nr_segs);
Miao Xie38851cc2013-02-08 07:04:11 +00006685 /*
6686 * If the write DIO is beyond the EOF, we need update
6687 * the isize, but it is protected by i_mutex. So we can
6688 * not unlock the i_mutex at this case.
6689 */
6690 if (offset + count <= inode->i_size) {
6691 mutex_unlock(&inode->i_mutex);
6692 relock = true;
6693 }
Miao Xie09348562013-02-07 10:12:07 +00006694 ret = btrfs_delalloc_reserve_space(inode, count);
6695 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00006696 goto out;
6697 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
6698 &BTRFS_I(inode)->runtime_flags))) {
6699 inode_dio_done(inode);
6700 flags = DIO_LOCKING | DIO_SKIP_HOLES;
6701 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00006702 }
6703
6704 ret = __blockdev_direct_IO(rw, iocb, inode,
6705 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6706 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
Miao Xie2e60a512013-02-08 07:01:08 +00006707 btrfs_submit_direct, flags);
Miao Xie09348562013-02-07 10:12:07 +00006708 if (rw & WRITE) {
6709 if (ret < 0 && ret != -EIOCBQUEUED)
6710 btrfs_delalloc_release_space(inode, count);
6711 else if (ret > 0 && (size_t)ret < count) {
6712 spin_lock(&BTRFS_I(inode)->lock);
6713 BTRFS_I(inode)->outstanding_extents++;
6714 spin_unlock(&BTRFS_I(inode)->lock);
6715 btrfs_delalloc_release_space(inode,
6716 count - (size_t)ret);
6717 }
6718 btrfs_delalloc_release_metadata(inode, 0);
6719 }
Miao Xie38851cc2013-02-08 07:04:11 +00006720out:
Miao Xie2e60a512013-02-08 07:01:08 +00006721 if (wakeup)
6722 inode_dio_done(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00006723 if (relock)
6724 mutex_lock(&inode->i_mutex);
Miao Xie09348562013-02-07 10:12:07 +00006725
6726 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006727}
6728
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006729#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6730
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006731static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6732 __u64 start, __u64 len)
6733{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006734 int ret;
6735
6736 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6737 if (ret)
6738 return ret;
6739
Chris Masonec29ed52011-02-23 16:23:20 -05006740 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006741}
6742
Chris Mason9ebefb182007-06-15 13:50:00 -04006743int btrfs_readpage(struct file *file, struct page *page)
6744{
Chris Masond1310b22008-01-24 16:13:08 -05006745 struct extent_io_tree *tree;
6746 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006747 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006748}
Chris Mason1832a6d2007-12-21 16:27:21 -05006749
Chris Mason39279cc2007-06-12 06:35:45 -04006750static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6751{
Chris Masond1310b22008-01-24 16:13:08 -05006752 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006753
6754
6755 if (current->flags & PF_MEMALLOC) {
6756 redirty_page_for_writepage(wbc, page);
6757 unlock_page(page);
6758 return 0;
6759 }
Chris Masond1310b22008-01-24 16:13:08 -05006760 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006761 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6762}
Chris Mason39279cc2007-06-12 06:35:45 -04006763
Chris Masonf4219502008-07-22 11:18:09 -04006764int btrfs_writepages(struct address_space *mapping,
6765 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006766{
Chris Masond1310b22008-01-24 16:13:08 -05006767 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006768
Chris Masond1310b22008-01-24 16:13:08 -05006769 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006770 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6771}
6772
Chris Mason3ab2fb52007-11-08 10:59:22 -05006773static int
6774btrfs_readpages(struct file *file, struct address_space *mapping,
6775 struct list_head *pages, unsigned nr_pages)
6776{
Chris Masond1310b22008-01-24 16:13:08 -05006777 struct extent_io_tree *tree;
6778 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006779 return extent_readpages(tree, mapping, pages, nr_pages,
6780 btrfs_get_extent);
6781}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006782static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006783{
Chris Masond1310b22008-01-24 16:13:08 -05006784 struct extent_io_tree *tree;
6785 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006786 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006787
Chris Masond1310b22008-01-24 16:13:08 -05006788 tree = &BTRFS_I(page->mapping->host)->io_tree;
6789 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006790 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006791 if (ret == 1) {
6792 ClearPagePrivate(page);
6793 set_page_private(page, 0);
6794 page_cache_release(page);
6795 }
6796 return ret;
6797}
Chris Mason39279cc2007-06-12 06:35:45 -04006798
Chris Masone6dcd2d2008-07-17 12:53:50 -04006799static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6800{
Chris Mason98509cf2008-09-11 15:51:43 -04006801 if (PageWriteback(page) || PageDirty(page))
6802 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006803 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006804}
6805
Chris Masona52d9a82007-08-27 16:49:44 -04006806static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6807{
Josef Bacik5fd02042012-05-02 14:00:54 -04006808 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006809 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006810 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006811 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006812 u64 page_start = page_offset(page);
6813 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006814
Chris Mason8b62b722009-09-02 16:53:46 -04006815 /*
6816 * we have the page locked, so new writeback can't start,
6817 * and the dirty bit won't be cleared while we are here.
6818 *
6819 * Wait for IO on this page so that we can safely clear
6820 * the PagePrivate2 bit and do ordered accounting
6821 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006822 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006823
Josef Bacik5fd02042012-05-02 14:00:54 -04006824 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006825 if (offset) {
6826 btrfs_releasepage(page, GFP_NOFS);
6827 return;
6828 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006829 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00006830 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04006831 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006832 /*
6833 * IO on this page will never be started, so we need
6834 * to account for any ordered extents now
6835 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006836 clear_extent_bit(tree, page_start, page_end,
6837 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006838 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6839 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006840 /*
6841 * whoever cleared the private bit is responsible
6842 * for the finish_ordered_io
6843 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006844 if (TestClearPagePrivate2(page) &&
6845 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6846 PAGE_CACHE_SIZE, 1)) {
6847 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006848 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006849 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006850 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006851 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006852 }
6853 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006854 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006855 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6856 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006857 __btrfs_releasepage(page, GFP_NOFS);
6858
Chris Mason4a096752008-07-21 10:29:44 -04006859 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006860 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006861 ClearPagePrivate(page);
6862 set_page_private(page, 0);
6863 page_cache_release(page);
6864 }
Chris Mason39279cc2007-06-12 06:35:45 -04006865}
6866
Chris Mason9ebefb182007-06-15 13:50:00 -04006867/*
6868 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6869 * called from a page fault handler when a page is first dirtied. Hence we must
6870 * be careful to check for EOF conditions here. We set the page up correctly
6871 * for a written page which means we get ENOSPC checking when writing into
6872 * holes and correct delalloc and unwritten extent mapping on filesystems that
6873 * support these features.
6874 *
6875 * We are not allowed to take the i_mutex here so we have to play games to
6876 * protect against truncate races as the page could now be beyond EOF. Because
6877 * vmtruncate() writes the inode size before removing pages, once we have the
6878 * page lock we can determine safely if the page is beyond EOF. If it is not
6879 * beyond EOF, then the page is guaranteed safe against truncation until we
6880 * unlock the page.
6881 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006882int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006883{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006884 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006885 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006886 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006887 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6888 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006889 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006890 char *kaddr;
6891 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006892 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006893 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006894 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006895 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006896 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006897
Jan Karab2b5ef52012-06-12 16:20:45 +02006898 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006899 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006900 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006901 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006902 reserved = 1;
6903 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006904 if (ret) {
6905 if (ret == -ENOMEM)
6906 ret = VM_FAULT_OOM;
6907 else /* -ENOSPC, -EIO, etc */
6908 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006909 if (reserved)
6910 goto out;
6911 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006912 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006913
Nick Piggin56a76f82009-03-31 15:23:23 -07006914 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006915again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006916 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006917 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006918 page_start = page_offset(page);
6919 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006920
Chris Mason9ebefb182007-06-15 13:50:00 -04006921 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006922 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006923 /* page got truncated out from underneath us */
6924 goto out_unlock;
6925 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006926 wait_on_page_writeback(page);
6927
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006928 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006929 set_page_extent_mapped(page);
6930
Chris Masoneb84ae02008-07-17 13:53:27 -04006931 /*
6932 * we can't set the delalloc bits if there are pending ordered
6933 * extents. Drop our locks and wait for them to finish
6934 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006935 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6936 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006937 unlock_extent_cached(io_tree, page_start, page_end,
6938 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006939 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006940 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006941 btrfs_put_ordered_extent(ordered);
6942 goto again;
6943 }
6944
Josef Bacikfbf19082009-10-01 17:10:23 -04006945 /*
6946 * XXX - page_mkwrite gets called every time the page is dirtied, even
6947 * if it was already dirty, so for space accounting reasons we need to
6948 * clear any delalloc bits for the range we are fixing to save. There
6949 * is probably a better way to do this, but for now keep consistent with
6950 * prepare_pages in the normal write path.
6951 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006952 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006953 EXTENT_DIRTY | EXTENT_DELALLOC |
6954 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006955 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006956
Josef Bacik2ac55d42010-02-03 19:33:23 +00006957 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6958 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006959 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006960 unlock_extent_cached(io_tree, page_start, page_end,
6961 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006962 ret = VM_FAULT_SIGBUS;
6963 goto out_unlock;
6964 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006965 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006966
6967 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006968 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006969 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006970 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006971 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006972
Chris Masone6dcd2d2008-07-17 12:53:50 -04006973 if (zero_start != PAGE_CACHE_SIZE) {
6974 kaddr = kmap(page);
6975 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6976 flush_dcache_page(page);
6977 kunmap(page);
6978 }
Chris Mason247e7432008-07-17 12:53:51 -04006979 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006980 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006981 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006982
Chris Mason257c62e2009-10-13 13:21:08 -04006983 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6984 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006985 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006986
Josef Bacik2ac55d42010-02-03 19:33:23 +00006987 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006988
6989out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006990 if (!ret) {
6991 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006992 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006993 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006994 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006995out:
Josef Bacikec39e182012-01-12 19:10:12 -05006996 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006997out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006998 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006999 return ret;
7000}
7001
Josef Bacika41ad392011-01-31 15:30:16 -05007002static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04007003{
7004 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04007005 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04007006 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05007007 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007008 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04007009 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04007010 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04007011
Josef Bacik2aaa6652012-08-29 14:27:18 -04007012 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04007013 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05007014 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007015
Chris Mason4a096752008-07-21 10:29:44 -04007016 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00007017 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007018
Josef Bacikfcb80c22011-05-03 10:40:22 -04007019 /*
7020 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7021 * 3 things going on here
7022 *
7023 * 1) We need to reserve space for our orphan item and the space to
7024 * delete our orphan item. Lord knows we don't want to have a dangling
7025 * orphan item because we didn't reserve space to remove it.
7026 *
7027 * 2) We need to reserve space to update our inode.
7028 *
7029 * 3) We need to have something to cache all the space that is going to
7030 * be free'd up by the truncate operation, but also have some slack
7031 * space reserved in case it uses space during the truncate (thank you
7032 * very much snapshotting).
7033 *
7034 * And we need these to all be seperate. The fact is we can use alot of
7035 * space doing the truncate, and we have no earthly idea how much space
7036 * we will use, so we need the truncate reservation to be seperate so it
7037 * doesn't end up using space reserved for updating the inode or
7038 * removing the orphan item. We also need to be able to stop the
7039 * transaction and start a new one, which means we need to be able to
7040 * update the inode several times, and we have no idea of knowing how
7041 * many times that will be, so we can't just reserve 1 item for the
7042 * entirety of the opration, so that has to be done seperately as well.
7043 * Then there is the orphan item, which does indeed need to be held on
7044 * to for the whole operation, and we need nobody to touch this reserved
7045 * space except the orphan code.
7046 *
7047 * So that leaves us with
7048 *
7049 * 1) root->orphan_block_rsv - for the orphan deletion.
7050 * 2) rsv - for the truncate reservation, which we will steal from the
7051 * transaction reservation.
7052 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7053 * updating the inode.
7054 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007055 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007056 if (!rsv)
7057 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007058 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007059 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007060
Josef Bacik907cbce2011-08-08 13:46:15 -04007061 /*
Josef Bacik07127182011-08-19 10:29:59 -04007062 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007063 * 1 for updating the inode.
7064 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007065 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007066 if (IS_ERR(trans)) {
7067 err = PTR_ERR(trans);
7068 goto out;
7069 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007070
Josef Bacik907cbce2011-08-08 13:46:15 -04007071 /* Migrate the slack space for the truncate to our reserve */
7072 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7073 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007074 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007075
Chris Mason5a3f23d2009-03-31 13:27:11 -04007076 /*
7077 * setattr is responsible for setting the ordered_data_close flag,
7078 * but that is only tested during the last file release. That
7079 * could happen well after the next commit, leaving a great big
7080 * window where new writes may get lost if someone chooses to write
7081 * to this file after truncating to zero
7082 *
7083 * The inode doesn't have any dirty data here, and so if we commit
7084 * this is a noop. If someone immediately starts writing to the inode
7085 * it is very likely we'll catch some of their writes in this
7086 * transaction, and the commit will find this file on the ordered
7087 * data list with good things to send down.
7088 *
7089 * This is a best effort solution, there is still a window where
7090 * using truncate to replace the contents of the file will
7091 * end up with a zero length file after a crash.
7092 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007093 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7094 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007095 btrfs_add_ordered_operation(trans, root, inode);
7096
Josef Bacik5dc562c2012-08-17 13:14:17 -04007097 /*
7098 * So if we truncate and then write and fsync we normally would just
7099 * write the extents that changed, which is a problem if we need to
7100 * first truncate that entire inode. So set this flag so we write out
7101 * all of the extents in the inode to the sync log so we're completely
7102 * safe.
7103 */
7104 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007105 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007106
Yan, Zheng80825102009-11-12 09:35:36 +00007107 while (1) {
7108 ret = btrfs_truncate_inode_items(trans, root, inode,
7109 inode->i_size,
7110 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007111 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007112 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007113 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007114 }
Chris Mason39279cc2007-06-12 06:35:45 -04007115
Josef Bacikfcb80c22011-05-03 10:40:22 -04007116 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007117 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007118 if (ret) {
7119 err = ret;
7120 break;
7121 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007122
Yan, Zheng80825102009-11-12 09:35:36 +00007123 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007124 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007125
7126 trans = btrfs_start_transaction(root, 2);
7127 if (IS_ERR(trans)) {
7128 ret = err = PTR_ERR(trans);
7129 trans = NULL;
7130 break;
7131 }
7132
7133 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7134 rsv, min_size);
7135 BUG_ON(ret); /* shouldn't happen */
7136 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007137 }
7138
7139 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007140 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007141 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007142 if (ret)
7143 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007144 }
7145
Chris Mason917c16b2011-11-08 14:49:59 -05007146 if (trans) {
7147 trans->block_rsv = &root->fs_info->trans_block_rsv;
7148 ret = btrfs_update_inode(trans, root, inode);
7149 if (ret && !err)
7150 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007151
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007152 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007153 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007154 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007155
7156out:
7157 btrfs_free_block_rsv(root, rsv);
7158
Josef Bacik3893e332011-01-31 16:03:11 -05007159 if (ret && !err)
7160 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007161
Josef Bacik3893e332011-01-31 16:03:11 -05007162 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007163}
7164
Sven Wegener3b963622008-06-09 21:57:42 -04007165/*
Chris Masond352ac62008-09-29 15:18:18 -04007166 * create a new subvolume directory/inode (helper for the ioctl).
7167 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007168int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007169 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007170{
Chris Mason39279cc2007-06-12 06:35:45 -04007171 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007172 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007173 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007174
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007175 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7176 new_dirid, new_dirid,
7177 S_IFDIR | (~current_umask() & S_IRWXUGO),
7178 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007179 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007180 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007181 inode->i_op = &btrfs_dir_inode_operations;
7182 inode->i_fop = &btrfs_dir_file_operations;
7183
Miklos Szeredibfe86842011-10-28 14:13:29 +02007184 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007185 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007186
Yan, Zheng76dda932009-09-21 16:00:26 -04007187 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007188
Yan, Zheng76dda932009-09-21 16:00:26 -04007189 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007190 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007191}
7192
Chris Mason39279cc2007-06-12 06:35:45 -04007193struct inode *btrfs_alloc_inode(struct super_block *sb)
7194{
7195 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007196 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007197
7198 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7199 if (!ei)
7200 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007201
7202 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007203 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007204 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007205 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007206 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007207 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007208 ei->disk_i_size = 0;
7209 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007210 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007211 ei->index_cnt = (u64)-1;
7212 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007213 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007214
Josef Bacik9e0baf62011-07-15 15:16:44 +00007215 spin_lock_init(&ei->lock);
7216 ei->outstanding_extents = 0;
7217 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007218
Josef Bacik72ac3c02012-05-23 14:13:11 -04007219 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007220 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007221
Miao Xie16cdcec2011-04-22 18:12:22 +08007222 ei->delayed_node = NULL;
7223
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007224 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007225 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007226 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7227 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007228 ei->io_tree.track_uptodate = 1;
7229 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007230 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007231 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007232 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007233 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007234 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007235 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007236 RB_CLEAR_NODE(&ei->rb_node);
7237
7238 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007239}
7240
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007241static void btrfs_i_callback(struct rcu_head *head)
7242{
7243 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007244 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7245}
7246
Chris Mason39279cc2007-06-12 06:35:45 -04007247void btrfs_destroy_inode(struct inode *inode)
7248{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007249 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007250 struct btrfs_root *root = BTRFS_I(inode)->root;
7251
Al Virob3d9b7a2012-06-09 13:51:19 -04007252 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007253 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007254 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7255 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007256 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7257 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007258
Chris Mason5a3f23d2009-03-31 13:27:11 -04007259 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007260 * This can happen where we create an inode, but somebody else also
7261 * created the same inode and we need to destroy the one we already
7262 * created.
7263 */
7264 if (!root)
7265 goto free;
7266
7267 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007268 * Make sure we're properly removed from the ordered operation
7269 * lists.
7270 */
7271 smp_mb();
7272 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7273 spin_lock(&root->fs_info->ordered_extent_lock);
7274 list_del_init(&BTRFS_I(inode)->ordered_operations);
7275 spin_unlock(&root->fs_info->ordered_extent_lock);
7276 }
7277
Josef Bacik8a35d952012-05-23 14:26:42 -04007278 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7279 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007280 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7281 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007282 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007283 }
Josef Bacik7b128762008-07-24 12:17:14 -04007284
Chris Masond3977122009-01-05 21:25:51 -05007285 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007286 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7287 if (!ordered)
7288 break;
7289 else {
Chris Masond3977122009-01-05 21:25:51 -05007290 printk(KERN_ERR "btrfs found ordered "
7291 "extent %llu %llu on inode cleanup\n",
7292 (unsigned long long)ordered->file_offset,
7293 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007294 btrfs_remove_ordered_extent(inode, ordered);
7295 btrfs_put_ordered_extent(ordered);
7296 btrfs_put_ordered_extent(ordered);
7297 }
7298 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007299 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007300 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007301free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007302 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007303 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007304}
7305
Al Viro45321ac2010-06-07 13:43:19 -04007306int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007307{
7308 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007309
Liu Bofa6ac872013-02-20 14:10:23 +00007310 /* the snap/subvol tree is on deleting */
Josef Bacik0af3d002010-06-21 14:48:16 -04007311 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bofa6ac872013-02-20 14:10:23 +00007312 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04007313 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007314 else
Al Viro45321ac2010-06-07 13:43:19 -04007315 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007316}
7317
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007318static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007319{
7320 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7321
7322 inode_init_once(&ei->vfs_inode);
7323}
7324
7325void btrfs_destroy_cachep(void)
7326{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007327 /*
7328 * Make sure all delayed rcu free inodes are flushed before we
7329 * destroy cache.
7330 */
7331 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007332 if (btrfs_inode_cachep)
7333 kmem_cache_destroy(btrfs_inode_cachep);
7334 if (btrfs_trans_handle_cachep)
7335 kmem_cache_destroy(btrfs_trans_handle_cachep);
7336 if (btrfs_transaction_cachep)
7337 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007338 if (btrfs_path_cachep)
7339 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007340 if (btrfs_free_space_cachep)
7341 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007342 if (btrfs_delalloc_work_cachep)
7343 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007344}
7345
7346int btrfs_init_cachep(void)
7347{
David Sterba837e1972012-09-07 03:00:48 -06007348 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007349 sizeof(struct btrfs_inode), 0,
7350 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007351 if (!btrfs_inode_cachep)
7352 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007353
David Sterba837e1972012-09-07 03:00:48 -06007354 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007355 sizeof(struct btrfs_trans_handle), 0,
7356 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007357 if (!btrfs_trans_handle_cachep)
7358 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007359
David Sterba837e1972012-09-07 03:00:48 -06007360 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007361 sizeof(struct btrfs_transaction), 0,
7362 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007363 if (!btrfs_transaction_cachep)
7364 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007365
David Sterba837e1972012-09-07 03:00:48 -06007366 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007367 sizeof(struct btrfs_path), 0,
7368 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007369 if (!btrfs_path_cachep)
7370 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007371
David Sterba837e1972012-09-07 03:00:48 -06007372 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007373 sizeof(struct btrfs_free_space), 0,
7374 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7375 if (!btrfs_free_space_cachep)
7376 goto fail;
7377
Miao Xie8ccf6f192012-10-25 09:28:04 +00007378 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7379 sizeof(struct btrfs_delalloc_work), 0,
7380 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7381 NULL);
7382 if (!btrfs_delalloc_work_cachep)
7383 goto fail;
7384
Chris Mason39279cc2007-06-12 06:35:45 -04007385 return 0;
7386fail:
7387 btrfs_destroy_cachep();
7388 return -ENOMEM;
7389}
7390
7391static int btrfs_getattr(struct vfsmount *mnt,
7392 struct dentry *dentry, struct kstat *stat)
7393{
Miao Xiedf0af1a2013-01-29 10:11:59 +00007394 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007395 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007396 u32 blocksize = inode->i_sb->s_blocksize;
7397
Chris Mason39279cc2007-06-12 06:35:45 -04007398 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007399 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007400 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007401
7402 spin_lock(&BTRFS_I(inode)->lock);
7403 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7404 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007405 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00007406 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007407 return 0;
7408}
7409
Liu Bo75e7cb72011-03-22 10:12:20 +00007410/*
7411 * If a file is moved, it will inherit the cow and compression flags of the new
7412 * directory.
7413 */
7414static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7415{
7416 struct btrfs_inode *b_dir = BTRFS_I(dir);
7417 struct btrfs_inode *b_inode = BTRFS_I(inode);
7418
7419 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7420 b_inode->flags |= BTRFS_INODE_NODATACOW;
7421 else
7422 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7423
Liu Bobc178232012-06-14 02:23:18 -06007424 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007425 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007426 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7427 } else {
7428 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7429 BTRFS_INODE_NOCOMPRESS);
7430 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007431}
7432
Chris Masond3977122009-01-05 21:25:51 -05007433static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7434 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007435{
7436 struct btrfs_trans_handle *trans;
7437 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007438 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007439 struct inode *new_inode = new_dentry->d_inode;
7440 struct inode *old_inode = old_dentry->d_inode;
7441 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007442 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007443 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007444 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007445 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007446
Li Zefan33345d012011-04-20 10:31:50 +08007447 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007448 return -EPERM;
7449
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007450 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007451 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007452 return -EXDEV;
7453
Li Zefan33345d012011-04-20 10:31:50 +08007454 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7455 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007456 return -ENOTEMPTY;
7457
Chris Mason39279cc2007-06-12 06:35:45 -04007458 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007459 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007460 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007461
7462
7463 /* check for collisions, even if the name isn't there */
7464 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7465 new_dentry->d_name.name,
7466 new_dentry->d_name.len);
7467
7468 if (ret) {
7469 if (ret == -EEXIST) {
7470 /* we shouldn't get
7471 * eexist without a new_inode */
7472 if (!new_inode) {
7473 WARN_ON(1);
7474 return ret;
7475 }
7476 } else {
7477 /* maybe -EOVERFLOW */
7478 return ret;
7479 }
7480 }
7481 ret = 0;
7482
Chris Mason5a3f23d2009-03-31 13:27:11 -04007483 /*
7484 * we're using rename to replace one file with another.
7485 * and the replacement file is large. Start IO on it now so
7486 * we don't add too much work to the end of the transaction
7487 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007488 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007489 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7490 filemap_flush(old_inode->i_mapping);
7491
Yan, Zheng76dda932009-09-21 16:00:26 -04007492 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007493 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007494 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007495 /*
7496 * We want to reserve the absolute worst case amount of items. So if
7497 * both inodes are subvols and we need to unlink them then that would
7498 * require 4 item modifications, but if they are both normal inodes it
7499 * would require 5 item modifications, so we'll assume their normal
7500 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7501 * should cover the worst case number of items we'll modify.
7502 */
7503 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007504 if (IS_ERR(trans)) {
7505 ret = PTR_ERR(trans);
7506 goto out_notrans;
7507 }
Chris Mason5f39d392007-10-15 16:14:19 -04007508
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007509 if (dest != root)
7510 btrfs_record_root_in_trans(trans, dest);
7511
Yan, Zhenga5719522009-09-24 09:17:31 -04007512 ret = btrfs_set_inode_index(new_dir, &index);
7513 if (ret)
7514 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007515
Li Zefan33345d012011-04-20 10:31:50 +08007516 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007517 /* force full log commit if subvolume involved. */
7518 root->fs_info->last_trans_log_full_commit = trans->transid;
7519 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007520 ret = btrfs_insert_inode_ref(trans, dest,
7521 new_dentry->d_name.name,
7522 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007523 old_ino,
7524 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007525 if (ret)
7526 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007527 /*
7528 * this is an ugly little race, but the rename is required
7529 * to make sure that if we crash, the inode is either at the
7530 * old name or the new one. pinning the log transaction lets
7531 * us make sure we don't allow a log commit to come in after
7532 * we unlink the name but before we add the new name back in.
7533 */
7534 btrfs_pin_log_trans(root);
7535 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007536 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007537 * make sure the inode gets flushed if it is replacing
7538 * something.
7539 */
Li Zefan33345d012011-04-20 10:31:50 +08007540 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007541 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007542
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007543 inode_inc_iversion(old_dir);
7544 inode_inc_iversion(new_dir);
7545 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007546 old_dir->i_ctime = old_dir->i_mtime = ctime;
7547 new_dir->i_ctime = new_dir->i_mtime = ctime;
7548 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007549
Chris Mason12fcfd22009-03-24 10:24:20 -04007550 if (old_dentry->d_parent != new_dentry->d_parent)
7551 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7552
Li Zefan33345d012011-04-20 10:31:50 +08007553 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007554 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7555 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7556 old_dentry->d_name.name,
7557 old_dentry->d_name.len);
7558 } else {
Al Viro92986792011-03-04 17:14:37 +00007559 ret = __btrfs_unlink_inode(trans, root, old_dir,
7560 old_dentry->d_inode,
7561 old_dentry->d_name.name,
7562 old_dentry->d_name.len);
7563 if (!ret)
7564 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007565 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007566 if (ret) {
7567 btrfs_abort_transaction(trans, root, ret);
7568 goto out_fail;
7569 }
Chris Mason39279cc2007-06-12 06:35:45 -04007570
7571 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007572 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007573 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007574 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007575 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7576 root_objectid = BTRFS_I(new_inode)->location.objectid;
7577 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7578 root_objectid,
7579 new_dentry->d_name.name,
7580 new_dentry->d_name.len);
7581 BUG_ON(new_inode->i_nlink == 0);
7582 } else {
7583 ret = btrfs_unlink_inode(trans, dest, new_dir,
7584 new_dentry->d_inode,
7585 new_dentry->d_name.name,
7586 new_dentry->d_name.len);
7587 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007588 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007589 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007590 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007591 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007592 if (ret) {
7593 btrfs_abort_transaction(trans, root, ret);
7594 goto out_fail;
7595 }
Chris Mason39279cc2007-06-12 06:35:45 -04007596 }
Josef Bacikaec74772008-07-24 12:12:38 -04007597
Liu Bo75e7cb72011-03-22 10:12:20 +00007598 fixup_inode_flags(new_dir, old_inode);
7599
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007600 ret = btrfs_add_link(trans, new_dir, old_inode,
7601 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007602 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007603 if (ret) {
7604 btrfs_abort_transaction(trans, root, ret);
7605 goto out_fail;
7606 }
Chris Mason39279cc2007-06-12 06:35:45 -04007607
Li Zefan33345d012011-04-20 10:31:50 +08007608 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007609 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007610 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007611 btrfs_end_log_trans(root);
7612 }
Chris Mason39279cc2007-06-12 06:35:45 -04007613out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007614 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007615out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007616 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007617 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007618
Chris Mason39279cc2007-06-12 06:35:45 -04007619 return ret;
7620}
7621
Miao Xie8ccf6f192012-10-25 09:28:04 +00007622static void btrfs_run_delalloc_work(struct btrfs_work *work)
7623{
7624 struct btrfs_delalloc_work *delalloc_work;
7625
7626 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7627 work);
7628 if (delalloc_work->wait)
7629 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7630 else
7631 filemap_flush(delalloc_work->inode->i_mapping);
7632
7633 if (delalloc_work->delay_iput)
7634 btrfs_add_delayed_iput(delalloc_work->inode);
7635 else
7636 iput(delalloc_work->inode);
7637 complete(&delalloc_work->completion);
7638}
7639
7640struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7641 int wait, int delay_iput)
7642{
7643 struct btrfs_delalloc_work *work;
7644
7645 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7646 if (!work)
7647 return NULL;
7648
7649 init_completion(&work->completion);
7650 INIT_LIST_HEAD(&work->list);
7651 work->inode = inode;
7652 work->wait = wait;
7653 work->delay_iput = delay_iput;
7654 work->work.func = btrfs_run_delalloc_work;
7655
7656 return work;
7657}
7658
7659void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7660{
7661 wait_for_completion(&work->completion);
7662 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7663}
7664
Chris Masond352ac62008-09-29 15:18:18 -04007665/*
7666 * some fairly slow code that needs optimization. This walks the list
7667 * of all the inodes with pending delalloc and forces them to disk.
7668 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007669int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007670{
Chris Masonea8c2812008-08-04 23:17:27 -04007671 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007672 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007673 struct btrfs_delalloc_work *work, *next;
7674 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00007675 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007676 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04007677
Yan Zhengc146afa2008-11-12 14:34:12 -05007678 if (root->fs_info->sb->s_flags & MS_RDONLY)
7679 return -EROFS;
7680
Miao Xie8ccf6f192012-10-25 09:28:04 +00007681 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007682 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00007683
Chris Mason75eff682008-12-15 15:54:40 -05007684 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007685 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
7686 while (!list_empty(&splice)) {
7687 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04007688 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007689
7690 list_del_init(&binode->delalloc_inodes);
7691
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007692 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00007693 if (!inode) {
7694 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
7695 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007696 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00007697 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007698
7699 list_add_tail(&binode->delalloc_inodes,
7700 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007701 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007702
7703 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7704 if (unlikely(!work)) {
7705 ret = -ENOMEM;
7706 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007707 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007708 list_add_tail(&work->list, &works);
7709 btrfs_queue_worker(&root->fs_info->flush_workers,
7710 &work->work);
7711
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007712 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007713 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007714 }
Chris Mason75eff682008-12-15 15:54:40 -05007715 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007716
Miao Xie1eafa6c2013-01-22 10:49:00 +00007717 list_for_each_entry_safe(work, next, &works, list) {
7718 list_del_init(&work->list);
7719 btrfs_wait_and_free_delalloc_work(work);
7720 }
7721
Chris Mason8c8bee12008-09-29 11:19:10 -04007722 /* the filemap_flush will queue IO into the worker threads, but
7723 * we have to make sure the IO is actually started and that
7724 * ordered extents get created before we return
7725 */
7726 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007727 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007728 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007729 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007730 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7731 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007732 }
7733 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007734 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007735out:
7736 list_for_each_entry_safe(work, next, &works, list) {
7737 list_del_init(&work->list);
7738 btrfs_wait_and_free_delalloc_work(work);
7739 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007740
7741 if (!list_empty_careful(&splice)) {
7742 spin_lock(&root->fs_info->delalloc_lock);
7743 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
7744 spin_unlock(&root->fs_info->delalloc_lock);
7745 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00007746 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04007747}
7748
Chris Mason39279cc2007-06-12 06:35:45 -04007749static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7750 const char *symname)
7751{
7752 struct btrfs_trans_handle *trans;
7753 struct btrfs_root *root = BTRFS_I(dir)->root;
7754 struct btrfs_path *path;
7755 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007756 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007757 int err;
7758 int drop_inode = 0;
7759 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007760 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007761 int name_len;
7762 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007763 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007764 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007765 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04007766
7767 name_len = strlen(symname) + 1;
7768 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7769 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007770
Josef Bacik9ed74f22009-09-11 16:12:44 -04007771 /*
7772 * 2 items for inode item and ref
7773 * 2 items for dir items
7774 * 1 item for xattr if selinux is on
7775 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007776 trans = btrfs_start_transaction(root, 5);
7777 if (IS_ERR(trans))
7778 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007779
Li Zefan581bb052011-04-20 10:06:11 +08007780 err = btrfs_find_free_ino(root, &objectid);
7781 if (err)
7782 goto out_unlock;
7783
Josef Bacikaec74772008-07-24 12:12:38 -04007784 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007785 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007786 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007787 if (IS_ERR(inode)) {
7788 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007789 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007790 }
Chris Mason39279cc2007-06-12 06:35:45 -04007791
Eric Paris2a7dba32011-02-01 11:05:39 -05007792 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007793 if (err) {
7794 drop_inode = 1;
7795 goto out_unlock;
7796 }
7797
Casey Schauflerad19db72011-12-15 10:09:07 -05007798 /*
7799 * If the active LSM wants to access the inode during
7800 * d_instantiate it needs these. Smack checks to see
7801 * if the filesystem supports xattrs by looking at the
7802 * ops vector.
7803 */
7804 inode->i_fop = &btrfs_file_operations;
7805 inode->i_op = &btrfs_file_inode_operations;
7806
Josef Bacika1b075d2010-11-19 20:36:11 +00007807 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007808 if (err)
7809 drop_inode = 1;
7810 else {
7811 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007812 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007813 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007814 }
Chris Mason39279cc2007-06-12 06:35:45 -04007815 if (drop_inode)
7816 goto out_unlock;
7817
7818 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007819 if (!path) {
7820 err = -ENOMEM;
7821 drop_inode = 1;
7822 goto out_unlock;
7823 }
Li Zefan33345d012011-04-20 10:31:50 +08007824 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007825 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007826 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7827 datasize = btrfs_file_extent_calc_inline_size(name_len);
7828 err = btrfs_insert_empty_item(trans, root, path, &key,
7829 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007830 if (err) {
7831 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007832 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007833 goto out_unlock;
7834 }
Chris Mason5f39d392007-10-15 16:14:19 -04007835 leaf = path->nodes[0];
7836 ei = btrfs_item_ptr(leaf, path->slots[0],
7837 struct btrfs_file_extent_item);
7838 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7839 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007840 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007841 btrfs_set_file_extent_encryption(leaf, ei, 0);
7842 btrfs_set_file_extent_compression(leaf, ei, 0);
7843 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7844 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7845
Chris Mason39279cc2007-06-12 06:35:45 -04007846 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007847 write_extent_buffer(leaf, symname, ptr, name_len);
7848 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007849 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007850
Chris Mason39279cc2007-06-12 06:35:45 -04007851 inode->i_op = &btrfs_symlink_inode_operations;
7852 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007853 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007854 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007855 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007856 err = btrfs_update_inode(trans, root, inode);
7857 if (err)
7858 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007859
7860out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007861 if (!err)
7862 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007863 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007864 if (drop_inode) {
7865 inode_dec_link_count(inode);
7866 iput(inode);
7867 }
Liu Bob53d3f52012-11-14 14:34:34 +00007868 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04007869 return err;
7870}
Chris Mason16432982008-04-10 10:23:21 -04007871
Josef Bacik0af3d002010-06-21 14:48:16 -04007872static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7873 u64 start, u64 num_bytes, u64 min_size,
7874 loff_t actual_len, u64 *alloc_hint,
7875 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007876{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007877 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7878 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007879 struct btrfs_root *root = BTRFS_I(inode)->root;
7880 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007881 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007882 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007883 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007884 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007885
Josef Bacik0af3d002010-06-21 14:48:16 -04007886 if (trans)
7887 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007888 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007889 if (own_trans) {
7890 trans = btrfs_start_transaction(root, 3);
7891 if (IS_ERR(trans)) {
7892 ret = PTR_ERR(trans);
7893 break;
7894 }
Yan Zhengd899e052008-10-30 14:25:28 -04007895 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007896
Zach Brown24542bf2012-11-16 00:04:43 +00007897 ret = btrfs_reserve_extent(trans, root,
7898 min(num_bytes, 256ULL * 1024 * 1024),
7899 min_size, 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007900 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007901 if (own_trans)
7902 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007903 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007904 }
7905
Yan Zhengd899e052008-10-30 14:25:28 -04007906 ret = insert_reserved_file_extent(trans, inode,
7907 cur_offset, ins.objectid,
7908 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007909 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007910 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007911 if (ret) {
7912 btrfs_abort_transaction(trans, root, ret);
7913 if (own_trans)
7914 btrfs_end_transaction(trans, root);
7915 break;
7916 }
Chris Masona1ed8352009-09-11 12:27:37 -04007917 btrfs_drop_extent_cache(inode, cur_offset,
7918 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007919
Josef Bacik5dc562c2012-08-17 13:14:17 -04007920 em = alloc_extent_map();
7921 if (!em) {
7922 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7923 &BTRFS_I(inode)->runtime_flags);
7924 goto next;
7925 }
7926
7927 em->start = cur_offset;
7928 em->orig_start = cur_offset;
7929 em->len = ins.offset;
7930 em->block_start = ins.objectid;
7931 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05007932 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007933 em->bdev = root->fs_info->fs_devices->latest_bdev;
7934 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7935 em->generation = trans->transid;
7936
7937 while (1) {
7938 write_lock(&em_tree->lock);
7939 ret = add_extent_mapping(em_tree, em);
7940 if (!ret)
7941 list_move(&em->list,
7942 &em_tree->modified_extents);
7943 write_unlock(&em_tree->lock);
7944 if (ret != -EEXIST)
7945 break;
7946 btrfs_drop_extent_cache(inode, cur_offset,
7947 cur_offset + ins.offset - 1,
7948 0);
7949 }
7950 free_extent_map(em);
7951next:
Yan Zhengd899e052008-10-30 14:25:28 -04007952 num_bytes -= ins.offset;
7953 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007954 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007955
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007956 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007957 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007958 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007959 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007960 (actual_len > inode->i_size) &&
7961 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007962 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007963 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007964 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007965 i_size = cur_offset;
7966 i_size_write(inode, i_size);
7967 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007968 }
7969
Yan Zhengd899e052008-10-30 14:25:28 -04007970 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007971
7972 if (ret) {
7973 btrfs_abort_transaction(trans, root, ret);
7974 if (own_trans)
7975 btrfs_end_transaction(trans, root);
7976 break;
7977 }
Yan Zhengd899e052008-10-30 14:25:28 -04007978
Josef Bacik0af3d002010-06-21 14:48:16 -04007979 if (own_trans)
7980 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007981 }
Yan Zhengd899e052008-10-30 14:25:28 -04007982 return ret;
7983}
7984
Josef Bacik0af3d002010-06-21 14:48:16 -04007985int btrfs_prealloc_file_range(struct inode *inode, int mode,
7986 u64 start, u64 num_bytes, u64 min_size,
7987 loff_t actual_len, u64 *alloc_hint)
7988{
7989 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7990 min_size, actual_len, alloc_hint,
7991 NULL);
7992}
7993
7994int btrfs_prealloc_file_range_trans(struct inode *inode,
7995 struct btrfs_trans_handle *trans, int mode,
7996 u64 start, u64 num_bytes, u64 min_size,
7997 loff_t actual_len, u64 *alloc_hint)
7998{
7999 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8000 min_size, actual_len, alloc_hint, trans);
8001}
8002
Chris Masone6dcd2d2008-07-17 12:53:50 -04008003static int btrfs_set_page_dirty(struct page *page)
8004{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008005 return __set_page_dirty_nobuffers(page);
8006}
8007
Al Viro10556cb2011-06-20 19:28:19 -04008008static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05008009{
Li Zefanb83cc962010-12-20 16:04:08 +08008010 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008011 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08008012
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00008013 if (mask & MAY_WRITE &&
8014 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8015 if (btrfs_root_readonly(root))
8016 return -EROFS;
8017 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8018 return -EACCES;
8019 }
Al Viro2830ba72011-06-20 19:16:29 -04008020 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05008021}
Chris Mason39279cc2007-06-12 06:35:45 -04008022
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008023static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05008024 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04008025 .lookup = btrfs_lookup,
8026 .create = btrfs_create,
8027 .unlink = btrfs_unlink,
8028 .link = btrfs_link,
8029 .mkdir = btrfs_mkdir,
8030 .rmdir = btrfs_rmdir,
8031 .rename = btrfs_rename,
8032 .symlink = btrfs_symlink,
8033 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04008034 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008035 .setxattr = btrfs_setxattr,
8036 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008037 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008038 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008039 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008040 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008041};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008042static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008043 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05008044 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008045 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04008046};
Yan, Zheng76dda932009-09-21 16:00:26 -04008047
Alexey Dobriyan828c0952009-10-01 15:43:56 -07008048static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008049 .llseek = generic_file_llseek,
8050 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01008051 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008052 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008053#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008054 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008055#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008056 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008057 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008058};
8059
Chris Masond1310b22008-01-24 16:13:08 -05008060static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008061 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008062 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008063 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008064 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008065 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008066 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008067 .set_bit_hook = btrfs_set_bit_hook,
8068 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008069 .merge_extent_hook = btrfs_merge_extent_hook,
8070 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008071};
8072
Chris Mason35054392009-01-21 13:11:13 -05008073/*
8074 * btrfs doesn't support the bmap operation because swapfiles
8075 * use bmap to make a mapping of extents in the file. They assume
8076 * these extents won't change over the life of the file and they
8077 * use the bmap result to do IO directly to the drive.
8078 *
8079 * the btrfs bmap call would return logical addresses that aren't
8080 * suitable for IO and they also will change frequently as COW
8081 * operations happen. So, swapfile + btrfs == corruption.
8082 *
8083 * For now we're avoiding this by dropping bmap.
8084 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008085static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008086 .readpage = btrfs_readpage,
8087 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008088 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008089 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008090 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008091 .invalidatepage = btrfs_invalidatepage,
8092 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008093 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008094 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008095};
8096
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008097static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008098 .readpage = btrfs_readpage,
8099 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008100 .invalidatepage = btrfs_invalidatepage,
8101 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008102};
8103
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008104static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008105 .getattr = btrfs_getattr,
8106 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008107 .setxattr = btrfs_setxattr,
8108 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008109 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008110 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008111 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008112 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008113 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008114 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008115};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008116static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008117 .getattr = btrfs_getattr,
8118 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008119 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008120 .setxattr = btrfs_setxattr,
8121 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008122 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008123 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008124 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008125 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008126};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008127static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008128 .readlink = generic_readlink,
8129 .follow_link = page_follow_link_light,
8130 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008131 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008132 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008133 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008134 .setxattr = btrfs_setxattr,
8135 .getxattr = btrfs_getxattr,
8136 .listxattr = btrfs_listxattr,
8137 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008138 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008139 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008140};
Yan, Zheng76dda932009-09-21 16:00:26 -04008141
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008142const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008143 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008144 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008145};