blob: 95542a1b3dfc99632219310f0108788789247fc9 [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050042#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040043#include "ctree.h"
44#include "disk-io.h"
45#include "transaction.h"
46#include "btrfs_inode.h"
47#include "ioctl.h"
48#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040049#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040050#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040051#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020052#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040053#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050054#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050055#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080056#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040057
58struct btrfs_iget_args {
59 u64 ino;
60 struct btrfs_root *root;
61};
62
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070063static const struct inode_operations btrfs_dir_inode_operations;
64static const struct inode_operations btrfs_symlink_inode_operations;
65static const struct inode_operations btrfs_dir_ro_inode_operations;
66static const struct inode_operations btrfs_special_inode_operations;
67static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070068static const struct address_space_operations btrfs_aops;
69static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070070static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050071static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
74struct kmem_cache *btrfs_trans_handle_cachep;
75struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078
79#define S_SHIFT 12
80static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
81 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
82 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
83 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
84 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
85 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
86 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
87 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
88};
89
Josef Bacika41ad392011-01-31 15:30:16 -050090static int btrfs_setsize(struct inode *inode, loff_t newsize);
91static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040092static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050093static noinline int cow_file_range(struct inode *inode,
94 struct page *locked_page,
95 u64 start, u64 end, int *page_started,
96 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040097
Yan, Zhengf34f57a2009-11-12 09:35:27 +000098static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -050099 struct inode *inode, struct inode *dir,
100 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500101{
102 int err;
103
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000104 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500105 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500106 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500107 return err;
108}
109
Chris Masond352ac62008-09-29 15:18:18 -0400110/*
Chris Masonc8b97812008-10-29 14:49:59 -0400111 * this does all the hard work for inserting an inline extent into
112 * the btree. The caller should have done a btrfs_drop_extents so that
113 * no overlapping inline items exist in the btree
114 */
Chris Masond3977122009-01-05 21:25:51 -0500115static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400116 struct btrfs_root *root, struct inode *inode,
117 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000118 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400119 struct page **compressed_pages)
120{
121 struct btrfs_key key;
122 struct btrfs_path *path;
123 struct extent_buffer *leaf;
124 struct page *page = NULL;
125 char *kaddr;
126 unsigned long ptr;
127 struct btrfs_file_extent_item *ei;
128 int err = 0;
129 int ret;
130 size_t cur_size = size;
131 size_t datasize;
132 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400133
Li Zefanfe3f5662011-03-28 08:30:38 +0000134 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400135 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400136
Chris Masond3977122009-01-05 21:25:51 -0500137 path = btrfs_alloc_path();
138 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400139 return -ENOMEM;
140
Chris Masonb9473432009-03-13 11:00:37 -0400141 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400142
Li Zefan33345d012011-04-20 10:31:50 +0800143 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400144 key.offset = start;
145 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400146 datasize = btrfs_file_extent_calc_inline_size(cur_size);
147
148 inode_add_bytes(inode, size);
149 ret = btrfs_insert_empty_item(trans, root, path, &key,
150 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400151 if (ret) {
152 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400153 goto fail;
154 }
155 leaf = path->nodes[0];
156 ei = btrfs_item_ptr(leaf, path->slots[0],
157 struct btrfs_file_extent_item);
158 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
159 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
160 btrfs_set_file_extent_encryption(leaf, ei, 0);
161 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
162 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
163 ptr = btrfs_file_extent_inline_start(ei);
164
Li Zefan261507a02010-12-17 14:21:50 +0800165 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400166 struct page *cpage;
167 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500168 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400169 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500170 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400171 PAGE_CACHE_SIZE);
172
Cong Wang7ac687d2011-11-25 23:14:28 +0800173 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400174 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800175 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400176
177 i++;
178 ptr += cur_size;
179 compressed_size -= cur_size;
180 }
181 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800182 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400183 } else {
184 page = find_get_page(inode->i_mapping,
185 start >> PAGE_CACHE_SHIFT);
186 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800187 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400188 offset = start & (PAGE_CACHE_SIZE - 1);
189 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800190 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400191 page_cache_release(page);
192 }
193 btrfs_mark_buffer_dirty(leaf);
194 btrfs_free_path(path);
195
Yan, Zhengc2167752009-11-12 09:34:21 +0000196 /*
197 * we're an inline extent, so nobody can
198 * extend the file past i_size without locking
199 * a page we already have locked.
200 *
201 * We must do any isize and inode updates
202 * before we unlock the pages. Otherwise we
203 * could end up racing with unlink.
204 */
Chris Masonc8b97812008-10-29 14:49:59 -0400205 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100206 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000207
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100208 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400209fail:
210 btrfs_free_path(path);
211 return err;
212}
213
214
215/*
216 * conditionally insert an inline extent into the file. This
217 * does the checks required to make sure the data is small enough
218 * to fit as an inline extent.
219 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400220static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400221 struct btrfs_root *root,
222 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000223 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400224 struct page **compressed_pages)
225{
226 u64 isize = i_size_read(inode);
227 u64 actual_end = min(end + 1, isize);
228 u64 inline_len = actual_end - start;
229 u64 aligned_end = (end + root->sectorsize - 1) &
230 ~((u64)root->sectorsize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400231 u64 data_len = inline_len;
232 int ret;
233
234 if (compressed_size)
235 data_len = compressed_size;
236
237 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400238 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400239 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
240 (!compressed_size &&
241 (actual_end & (root->sectorsize - 1)) == 0) ||
242 end + 1 < isize ||
243 data_len > root->fs_info->max_inline) {
244 return 1;
245 }
246
Josef Bacik26714852012-08-29 12:24:27 -0400247 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100248 if (ret)
249 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400250
251 if (isize > actual_end)
252 inline_len = min_t(u64, isize, actual_end);
253 ret = insert_inline_extent(trans, root, inode, start,
254 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000255 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400256 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100257 btrfs_abort_transaction(trans, root, ret);
258 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400259 } else if (ret == -ENOSPC) {
260 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100261 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400262
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400263 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400264 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400265 return 0;
266}
267
Chris Mason771ed682008-11-06 22:02:51 -0500268struct async_extent {
269 u64 start;
270 u64 ram_size;
271 u64 compressed_size;
272 struct page **pages;
273 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800274 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500275 struct list_head list;
276};
277
278struct async_cow {
279 struct inode *inode;
280 struct btrfs_root *root;
281 struct page *locked_page;
282 u64 start;
283 u64 end;
284 struct list_head extents;
285 struct btrfs_work work;
286};
287
288static noinline int add_async_extent(struct async_cow *cow,
289 u64 start, u64 ram_size,
290 u64 compressed_size,
291 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800292 unsigned long nr_pages,
293 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500294{
295 struct async_extent *async_extent;
296
297 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100298 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500299 async_extent->start = start;
300 async_extent->ram_size = ram_size;
301 async_extent->compressed_size = compressed_size;
302 async_extent->pages = pages;
303 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800304 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500305 list_add_tail(&async_extent->list, &cow->extents);
306 return 0;
307}
308
Chris Masonc8b97812008-10-29 14:49:59 -0400309/*
Chris Mason771ed682008-11-06 22:02:51 -0500310 * we create compressed extents in two phases. The first
311 * phase compresses a range of pages that have already been
312 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400313 *
Chris Mason771ed682008-11-06 22:02:51 -0500314 * This is done inside an ordered work queue, and the compression
315 * is spread across many cpus. The actual IO submission is step
316 * two, and the ordered work queue takes care of making sure that
317 * happens in the same order things were put onto the queue by
318 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400319 *
Chris Mason771ed682008-11-06 22:02:51 -0500320 * If this code finds it can't get good compression, it puts an
321 * entry onto the work queue to write the uncompressed bytes. This
322 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300323 * are written in the same order that the flusher thread sent them
324 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400325 */
Chris Mason771ed682008-11-06 22:02:51 -0500326static noinline int compress_file_range(struct inode *inode,
327 struct page *locked_page,
328 u64 start, u64 end,
329 struct async_cow *async_cow,
330 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400331{
332 struct btrfs_root *root = BTRFS_I(inode)->root;
333 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400334 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400335 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400336 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500337 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400338 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400339 struct page **pages = NULL;
340 unsigned long nr_pages;
341 unsigned long nr_pages_ret = 0;
342 unsigned long total_compressed = 0;
343 unsigned long total_in = 0;
344 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500345 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400346 int i;
347 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800348 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400349
Liu Bo4cb13e52012-03-29 09:57:45 -0400350 /* if this is a small write inside eof, kick off a defrag */
351 if ((end - start + 1) < 16 * 1024 &&
352 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400353 btrfs_add_inode_defrag(NULL, inode);
354
Chris Mason42dc7ba2008-12-15 11:44:56 -0500355 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400356again:
357 will_compress = 0;
358 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
359 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
360
Chris Masonf03d9302009-02-04 09:31:06 -0500361 /*
362 * we don't want to send crud past the end of i_size through
363 * compression, that's just a waste of CPU time. So, if the
364 * end of the file is before the start of our current
365 * requested range of bytes, we bail out to the uncompressed
366 * cleanup code that can deal with all of this.
367 *
368 * It isn't really the fastest way to fix things, but this is a
369 * very uncommon corner.
370 */
371 if (actual_end <= start)
372 goto cleanup_and_bail_uncompressed;
373
Chris Masonc8b97812008-10-29 14:49:59 -0400374 total_compressed = actual_end - start;
375
376 /* we want to make sure that amount of ram required to uncompress
377 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500378 * of a compressed extent to 128k. This is a crucial number
379 * because it also controls how easily we can spread reads across
380 * cpus for decompression.
381 *
382 * We also want to make sure the amount of IO required to do
383 * a random read is reasonably small, so we limit the size of
384 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400385 */
386 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400387 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500388 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400389 total_in = 0;
390 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400391
Chris Mason771ed682008-11-06 22:02:51 -0500392 /*
393 * we do compression for mount -o compress and when the
394 * inode has not been flagged as nocompress. This flag can
395 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400396 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200397 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500398 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000399 (BTRFS_I(inode)->force_compress) ||
400 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400401 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400402 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800403 if (!pages) {
404 /* just bail out to the uncompressed code */
405 goto cont;
406 }
Chris Mason179e29e2007-11-01 11:28:41 -0400407
Li Zefan261507a02010-12-17 14:21:50 +0800408 if (BTRFS_I(inode)->force_compress)
409 compress_type = BTRFS_I(inode)->force_compress;
410
411 ret = btrfs_compress_pages(compress_type,
412 inode->i_mapping, start,
413 total_compressed, pages,
414 nr_pages, &nr_pages_ret,
415 &total_in,
416 &total_compressed,
417 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400418
419 if (!ret) {
420 unsigned long offset = total_compressed &
421 (PAGE_CACHE_SIZE - 1);
422 struct page *page = pages[nr_pages_ret - 1];
423 char *kaddr;
424
425 /* zero the tail end of the last page, we might be
426 * sending it down to disk
427 */
428 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800429 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400430 memset(kaddr + offset, 0,
431 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800432 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400433 }
434 will_compress = 1;
435 }
436 }
Li Zefan560f7d72011-09-08 10:22:01 +0800437cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400438 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400439 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100440 if (IS_ERR(trans)) {
441 ret = PTR_ERR(trans);
442 trans = NULL;
443 goto cleanup_and_out;
444 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400445 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500446
Chris Masonc8b97812008-10-29 14:49:59 -0400447 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500448 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400449 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500450 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400451 */
452 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000453 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400454 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500455 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400456 ret = cow_file_range_inline(trans, root, inode,
457 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000458 total_compressed,
459 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400460 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100461 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500462 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100463 * inline extent creation worked or returned error,
464 * we don't need to create any more async work items.
465 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500466 */
Chris Masonc8b97812008-10-29 14:49:59 -0400467 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400468 &BTRFS_I(inode)->io_tree,
469 start, end, NULL,
470 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400471 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400472 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000473
474 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400475 goto free_pages_out;
476 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000477 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400478 }
479
480 if (will_compress) {
481 /*
482 * we aren't doing an inline extent round the compressed size
483 * up to a block size boundary so the allocator does sane
484 * things
485 */
486 total_compressed = (total_compressed + blocksize - 1) &
487 ~(blocksize - 1);
488
489 /*
490 * one last check to make sure the compression is really a
491 * win, compare the page count read with the blocks on disk
492 */
493 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
494 ~(PAGE_CACHE_SIZE - 1);
495 if (total_compressed >= total_in) {
496 will_compress = 0;
497 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400498 num_bytes = total_in;
499 }
500 }
501 if (!will_compress && pages) {
502 /*
503 * the compression code ran but failed to make things smaller,
504 * free any pages it allocated and our page pointer array
505 */
506 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400507 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400508 page_cache_release(pages[i]);
509 }
510 kfree(pages);
511 pages = NULL;
512 total_compressed = 0;
513 nr_pages_ret = 0;
514
515 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500516 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
517 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500518 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500519 }
Chris Masonc8b97812008-10-29 14:49:59 -0400520 }
Chris Mason771ed682008-11-06 22:02:51 -0500521 if (will_compress) {
522 *num_added += 1;
523
524 /* the async work queues will take care of doing actual
525 * allocation on disk for these compressed pages,
526 * and will submit them to the elevator.
527 */
528 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800529 total_compressed, pages, nr_pages_ret,
530 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500531
Yan, Zheng24ae6362010-12-06 07:02:36 +0000532 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500533 start += num_bytes;
534 pages = NULL;
535 cond_resched();
536 goto again;
537 }
538 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500539cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500540 /*
541 * No compression, but we still need to write the pages in
542 * the file we've been given so far. redirty the locked
543 * page if it corresponds to our extent and set things up
544 * for the async work queue to run cow_file_range to do
545 * the normal delalloc dance
546 */
547 if (page_offset(locked_page) >= start &&
548 page_offset(locked_page) <= end) {
549 __set_page_dirty_nobuffers(locked_page);
550 /* unlocked later on in the async handlers */
551 }
Li Zefan261507a02010-12-17 14:21:50 +0800552 add_async_extent(async_cow, start, end - start + 1,
553 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500554 *num_added += 1;
555 }
556
557out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100558 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500559
560free_pages_out:
561 for (i = 0; i < nr_pages_ret; i++) {
562 WARN_ON(pages[i]->mapping);
563 page_cache_release(pages[i]);
564 }
Chris Masond3977122009-01-05 21:25:51 -0500565 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500566
567 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100568
569cleanup_and_out:
570 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
571 start, end, NULL,
572 EXTENT_CLEAR_UNLOCK_PAGE |
573 EXTENT_CLEAR_DIRTY |
574 EXTENT_CLEAR_DELALLOC |
575 EXTENT_SET_WRITEBACK |
576 EXTENT_END_WRITEBACK);
577 if (!trans || IS_ERR(trans))
578 btrfs_error(root->fs_info, ret, "Failed to join transaction");
579 else
580 btrfs_abort_transaction(trans, root, ret);
581 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500582}
583
584/*
585 * phase two of compressed writeback. This is the ordered portion
586 * of the code, which only gets called in the order the work was
587 * queued. We walk all the async extents created by compress_file_range
588 * and send them down to the disk.
589 */
590static noinline int submit_compressed_extents(struct inode *inode,
591 struct async_cow *async_cow)
592{
593 struct async_extent *async_extent;
594 u64 alloc_hint = 0;
595 struct btrfs_trans_handle *trans;
596 struct btrfs_key ins;
597 struct extent_map *em;
598 struct btrfs_root *root = BTRFS_I(inode)->root;
599 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
600 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500601 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500602
603 if (list_empty(&async_cow->extents))
604 return 0;
605
Chris Mason771ed682008-11-06 22:02:51 -0500606
Chris Masond3977122009-01-05 21:25:51 -0500607 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500608 async_extent = list_entry(async_cow->extents.next,
609 struct async_extent, list);
610 list_del(&async_extent->list);
611
612 io_tree = &BTRFS_I(inode)->io_tree;
613
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500614retry:
Chris Mason771ed682008-11-06 22:02:51 -0500615 /* did the compression code fall back to uncompressed IO? */
616 if (!async_extent->pages) {
617 int page_started = 0;
618 unsigned long nr_written = 0;
619
620 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000621 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100622 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500623
624 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500625 ret = cow_file_range(inode, async_cow->locked_page,
626 async_extent->start,
627 async_extent->start +
628 async_extent->ram_size - 1,
629 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500630
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100631 /* JDM XXX */
632
Chris Mason771ed682008-11-06 22:02:51 -0500633 /*
634 * if page_started, cow_file_range inserted an
635 * inline extent and took care of all the unlocking
636 * and IO for us. Otherwise, we need to submit
637 * all those pages down to the drive.
638 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500639 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500640 extent_write_locked_range(io_tree,
641 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500642 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500643 async_extent->ram_size - 1,
644 btrfs_get_extent,
645 WB_SYNC_ALL);
646 kfree(async_extent);
647 cond_resched();
648 continue;
649 }
650
651 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100652 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500653
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400654 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100655 if (IS_ERR(trans)) {
656 ret = PTR_ERR(trans);
657 } else {
658 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
659 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500660 async_extent->compressed_size,
661 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500662 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600663 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100664 btrfs_abort_transaction(trans, root, ret);
665 btrfs_end_transaction(trans, root);
666 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000667
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500668 if (ret) {
669 int i;
670 for (i = 0; i < async_extent->nr_pages; i++) {
671 WARN_ON(async_extent->pages[i]->mapping);
672 page_cache_release(async_extent->pages[i]);
673 }
674 kfree(async_extent->pages);
675 async_extent->nr_pages = 0;
676 async_extent->pages = NULL;
677 unlock_extent(io_tree, async_extent->start,
678 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100679 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100680 if (ret == -ENOSPC)
681 goto retry;
682 goto out_free; /* JDM: Requeue? */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500683 }
684
Yan, Zhengc2167752009-11-12 09:34:21 +0000685 /*
686 * here we're doing allocation and writeback of the
687 * compressed pages
688 */
689 btrfs_drop_extent_cache(inode, async_extent->start,
690 async_extent->start +
691 async_extent->ram_size - 1, 0);
692
David Sterba172ddd62011-04-21 00:48:27 +0200693 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100694 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500695 em->start = async_extent->start;
696 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500697 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500698
699 em->block_start = ins.objectid;
700 em->block_len = ins.offset;
701 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800702 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500703 set_bit(EXTENT_FLAG_PINNED, &em->flags);
704 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
705
Chris Masond3977122009-01-05 21:25:51 -0500706 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400707 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500708 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400709 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500710 if (ret != -EEXIST) {
711 free_extent_map(em);
712 break;
713 }
714 btrfs_drop_extent_cache(inode, async_extent->start,
715 async_extent->start +
716 async_extent->ram_size - 1, 0);
717 }
718
Li Zefan261507a02010-12-17 14:21:50 +0800719 ret = btrfs_add_ordered_extent_compress(inode,
720 async_extent->start,
721 ins.objectid,
722 async_extent->ram_size,
723 ins.offset,
724 BTRFS_ORDERED_COMPRESSED,
725 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100726 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500727
Chris Mason771ed682008-11-06 22:02:51 -0500728 /*
729 * clear dirty, set writeback and unlock the pages.
730 */
731 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400732 &BTRFS_I(inode)->io_tree,
733 async_extent->start,
734 async_extent->start +
735 async_extent->ram_size - 1,
736 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
737 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400738 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400739 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500740
741 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500742 async_extent->start,
743 async_extent->ram_size,
744 ins.objectid,
745 ins.offset, async_extent->pages,
746 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500747
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100748 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500749 alloc_hint = ins.objectid + ins.offset;
750 kfree(async_extent);
751 cond_resched();
752 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100753 ret = 0;
754out:
755 return ret;
756out_free:
757 kfree(async_extent);
758 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500759}
760
Josef Bacik4b46fce2010-05-23 11:00:55 -0400761static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
762 u64 num_bytes)
763{
764 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
765 struct extent_map *em;
766 u64 alloc_hint = 0;
767
768 read_lock(&em_tree->lock);
769 em = search_extent_mapping(em_tree, start, num_bytes);
770 if (em) {
771 /*
772 * if block start isn't an actual block number then find the
773 * first block in this inode and use that as a hint. If that
774 * block is also bogus then just don't worry about it.
775 */
776 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
777 free_extent_map(em);
778 em = search_extent_mapping(em_tree, 0, 0);
779 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
780 alloc_hint = em->block_start;
781 if (em)
782 free_extent_map(em);
783 } else {
784 alloc_hint = em->block_start;
785 free_extent_map(em);
786 }
787 }
788 read_unlock(&em_tree->lock);
789
790 return alloc_hint;
791}
792
Chris Mason771ed682008-11-06 22:02:51 -0500793/*
794 * when extent_io.c finds a delayed allocation range in the file,
795 * the call backs end up in this code. The basic idea is to
796 * allocate extents on disk for the range, and create ordered data structs
797 * in ram to track those extents.
798 *
799 * locked_page is the page that writepage had locked already. We use
800 * it to make sure we don't do extra locks or unlocks.
801 *
802 * *page_started is set to one if we unlock locked_page and do everything
803 * required to start IO on it. It may be clean and already done with
804 * IO when we return.
805 */
806static noinline int cow_file_range(struct inode *inode,
807 struct page *locked_page,
808 u64 start, u64 end, int *page_started,
809 unsigned long *nr_written,
810 int unlock)
811{
812 struct btrfs_root *root = BTRFS_I(inode)->root;
813 struct btrfs_trans_handle *trans;
814 u64 alloc_hint = 0;
815 u64 num_bytes;
816 unsigned long ram_size;
817 u64 disk_num_bytes;
818 u64 cur_alloc_size;
819 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500820 struct btrfs_key ins;
821 struct extent_map *em;
822 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
823 int ret = 0;
824
Liu Bo83eea1f2012-07-10 05:28:39 -0600825 BUG_ON(btrfs_is_free_space_inode(inode));
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400826 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100827 if (IS_ERR(trans)) {
828 extent_clear_unlock_delalloc(inode,
829 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400830 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100831 EXTENT_CLEAR_UNLOCK_PAGE |
832 EXTENT_CLEAR_UNLOCK |
833 EXTENT_CLEAR_DELALLOC |
834 EXTENT_CLEAR_DIRTY |
835 EXTENT_SET_WRITEBACK |
836 EXTENT_END_WRITEBACK);
837 return PTR_ERR(trans);
838 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400839 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500840
Chris Mason771ed682008-11-06 22:02:51 -0500841 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
842 num_bytes = max(blocksize, num_bytes);
843 disk_num_bytes = num_bytes;
844 ret = 0;
845
Chris Mason4cb53002011-05-24 15:35:30 -0400846 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400847 if (num_bytes < 64 * 1024 &&
848 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400849 btrfs_add_inode_defrag(trans, inode);
850
Chris Mason771ed682008-11-06 22:02:51 -0500851 if (start == 0) {
852 /* lets try to make an inline extent */
853 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000854 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500855 if (ret == 0) {
856 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400857 &BTRFS_I(inode)->io_tree,
858 start, end, NULL,
859 EXTENT_CLEAR_UNLOCK_PAGE |
860 EXTENT_CLEAR_UNLOCK |
861 EXTENT_CLEAR_DELALLOC |
862 EXTENT_CLEAR_DIRTY |
863 EXTENT_SET_WRITEBACK |
864 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000865
Chris Mason771ed682008-11-06 22:02:51 -0500866 *nr_written = *nr_written +
867 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
868 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500869 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100870 } else if (ret < 0) {
871 btrfs_abort_transaction(trans, root, ret);
872 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500873 }
874 }
Chris Masonc8b97812008-10-29 14:49:59 -0400875
876 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200877 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400878
Josef Bacik4b46fce2010-05-23 11:00:55 -0400879 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400880 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400881
Chris Masond3977122009-01-05 21:25:51 -0500882 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400883 unsigned long op;
884
Josef Bacik287a0ab2010-03-19 18:07:23 +0000885 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400886 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500887 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500888 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100889 if (ret < 0) {
890 btrfs_abort_transaction(trans, root, ret);
891 goto out_unlock;
892 }
Chris Masond3977122009-01-05 21:25:51 -0500893
David Sterba172ddd62011-04-21 00:48:27 +0200894 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100895 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400896 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500897 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500898 ram_size = ins.offset;
899 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400900
Chris Masone6dcd2d2008-07-17 12:53:50 -0400901 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400902 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400903 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400904 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400905
Chris Masond3977122009-01-05 21:25:51 -0500906 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400907 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400908 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400909 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400910 if (ret != -EEXIST) {
911 free_extent_map(em);
912 break;
913 }
914 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400915 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400916 }
917
Chris Mason98d20f62008-04-14 09:46:10 -0400918 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400919 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500920 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100921 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400922
Yan Zheng17d217f2008-12-12 10:03:38 -0500923 if (root->root_key.objectid ==
924 BTRFS_DATA_RELOC_TREE_OBJECTID) {
925 ret = btrfs_reloc_clone_csums(inode, start,
926 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100927 if (ret) {
928 btrfs_abort_transaction(trans, root, ret);
929 goto out_unlock;
930 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500931 }
932
Chris Masond3977122009-01-05 21:25:51 -0500933 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400934 break;
Chris Masond3977122009-01-05 21:25:51 -0500935
Chris Masonc8b97812008-10-29 14:49:59 -0400936 /* we're not doing compressed IO, don't unlock the first
937 * page (which the caller expects to stay locked), don't
938 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400939 *
940 * Do set the Private2 bit so we know this page was properly
941 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400942 */
Chris Masona791e352009-10-08 11:27:10 -0400943 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
944 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
945 EXTENT_SET_PRIVATE2;
946
Chris Masonc8b97812008-10-29 14:49:59 -0400947 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
948 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400949 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400950 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500951 num_bytes -= cur_alloc_size;
952 alloc_hint = ins.objectid + ins.offset;
953 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400954 }
Chris Mason771ed682008-11-06 22:02:51 -0500955 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100956out:
Chris Masonb888db22007-08-27 16:49:44 -0400957 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400958
Chris Masonbe20aa92007-12-17 20:14:01 -0500959 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100960out_unlock:
961 extent_clear_unlock_delalloc(inode,
962 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400963 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100964 EXTENT_CLEAR_UNLOCK_PAGE |
965 EXTENT_CLEAR_UNLOCK |
966 EXTENT_CLEAR_DELALLOC |
967 EXTENT_CLEAR_DIRTY |
968 EXTENT_SET_WRITEBACK |
969 EXTENT_END_WRITEBACK);
970
971 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500972}
Chris Masonc8b97812008-10-29 14:49:59 -0400973
Chris Mason771ed682008-11-06 22:02:51 -0500974/*
975 * work queue call back to started compression on a file and pages
976 */
977static noinline void async_cow_start(struct btrfs_work *work)
978{
979 struct async_cow *async_cow;
980 int num_added = 0;
981 async_cow = container_of(work, struct async_cow, work);
982
983 compress_file_range(async_cow->inode, async_cow->locked_page,
984 async_cow->start, async_cow->end, async_cow,
985 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -0400986 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -0600987 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -0500988 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -0400989 }
Chris Mason771ed682008-11-06 22:02:51 -0500990}
991
992/*
993 * work queue call back to submit previously compressed pages
994 */
995static noinline void async_cow_submit(struct btrfs_work *work)
996{
997 struct async_cow *async_cow;
998 struct btrfs_root *root;
999 unsigned long nr_pages;
1000
1001 async_cow = container_of(work, struct async_cow, work);
1002
1003 root = async_cow->root;
1004 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1005 PAGE_CACHE_SHIFT;
1006
Josef Bacik66657b32012-08-01 15:36:24 -04001007 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001008 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001009 waitqueue_active(&root->fs_info->async_submit_wait))
1010 wake_up(&root->fs_info->async_submit_wait);
1011
Chris Masond3977122009-01-05 21:25:51 -05001012 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001013 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001014}
Chris Masonc8b97812008-10-29 14:49:59 -04001015
Chris Mason771ed682008-11-06 22:02:51 -05001016static noinline void async_cow_free(struct btrfs_work *work)
1017{
1018 struct async_cow *async_cow;
1019 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001020 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001021 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001022 kfree(async_cow);
1023}
1024
1025static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1026 u64 start, u64 end, int *page_started,
1027 unsigned long *nr_written)
1028{
1029 struct async_cow *async_cow;
1030 struct btrfs_root *root = BTRFS_I(inode)->root;
1031 unsigned long nr_pages;
1032 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001033 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001034
Chris Masona3429ab2009-10-08 12:30:20 -04001035 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1036 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001037 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001038 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001039 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001040 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001041 async_cow->root = root;
1042 async_cow->locked_page = locked_page;
1043 async_cow->start = start;
1044
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001045 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001046 cur_end = end;
1047 else
1048 cur_end = min(end, start + 512 * 1024 - 1);
1049
1050 async_cow->end = cur_end;
1051 INIT_LIST_HEAD(&async_cow->extents);
1052
1053 async_cow->work.func = async_cow_start;
1054 async_cow->work.ordered_func = async_cow_submit;
1055 async_cow->work.ordered_free = async_cow_free;
1056 async_cow->work.flags = 0;
1057
Chris Mason771ed682008-11-06 22:02:51 -05001058 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1059 PAGE_CACHE_SHIFT;
1060 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1061
1062 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1063 &async_cow->work);
1064
1065 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1066 wait_event(root->fs_info->async_submit_wait,
1067 (atomic_read(&root->fs_info->async_delalloc_pages) <
1068 limit));
1069 }
1070
Chris Masond3977122009-01-05 21:25:51 -05001071 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001072 atomic_read(&root->fs_info->async_delalloc_pages)) {
1073 wait_event(root->fs_info->async_submit_wait,
1074 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1075 0));
1076 }
1077
1078 *nr_written += nr_pages;
1079 start = cur_end + 1;
1080 }
1081 *page_started = 1;
1082 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001083}
1084
Chris Masond3977122009-01-05 21:25:51 -05001085static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001086 u64 bytenr, u64 num_bytes)
1087{
1088 int ret;
1089 struct btrfs_ordered_sum *sums;
1090 LIST_HEAD(list);
1091
Yan Zheng07d400a2009-01-06 11:42:00 -05001092 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001093 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001094 if (ret == 0 && list_empty(&list))
1095 return 0;
1096
1097 while (!list_empty(&list)) {
1098 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1099 list_del(&sums->list);
1100 kfree(sums);
1101 }
1102 return 1;
1103}
1104
Chris Masond352ac62008-09-29 15:18:18 -04001105/*
1106 * when nowcow writeback call back. This checks for snapshots or COW copies
1107 * of the extents that exist in the file, and COWs the file as required.
1108 *
1109 * If no cow copies or snapshots exist, we write directly to the existing
1110 * blocks on disk
1111 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001112static noinline int run_delalloc_nocow(struct inode *inode,
1113 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001114 u64 start, u64 end, int *page_started, int force,
1115 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001116{
Chris Masonbe20aa92007-12-17 20:14:01 -05001117 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001118 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001119 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001120 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001121 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001122 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001123 u64 cow_start;
1124 u64 cur_offset;
1125 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001126 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001127 u64 disk_bytenr;
1128 u64 num_bytes;
1129 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001130 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001131 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001132 int nocow;
1133 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001134 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001135 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001136
1137 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001138 if (!path) {
1139 extent_clear_unlock_delalloc(inode,
1140 &BTRFS_I(inode)->io_tree,
1141 start, end, locked_page,
1142 EXTENT_CLEAR_UNLOCK_PAGE |
1143 EXTENT_CLEAR_UNLOCK |
1144 EXTENT_CLEAR_DELALLOC |
1145 EXTENT_CLEAR_DIRTY |
1146 EXTENT_SET_WRITEBACK |
1147 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001148 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001149 }
Li Zefan82d59022011-04-20 10:33:24 +08001150
Liu Bo83eea1f2012-07-10 05:28:39 -06001151 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001152
1153 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001154 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001155 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001156 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001157
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001158 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001159 extent_clear_unlock_delalloc(inode,
1160 &BTRFS_I(inode)->io_tree,
1161 start, end, locked_page,
1162 EXTENT_CLEAR_UNLOCK_PAGE |
1163 EXTENT_CLEAR_UNLOCK |
1164 EXTENT_CLEAR_DELALLOC |
1165 EXTENT_CLEAR_DIRTY |
1166 EXTENT_SET_WRITEBACK |
1167 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001168 btrfs_free_path(path);
1169 return PTR_ERR(trans);
1170 }
1171
Josef Bacik74b21072011-04-13 12:02:53 -04001172 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001173
Yan Zheng80ff3852008-10-30 14:20:02 -04001174 cow_start = (u64)-1;
1175 cur_offset = start;
1176 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001177 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001178 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001179 if (ret < 0) {
1180 btrfs_abort_transaction(trans, root, ret);
1181 goto error;
1182 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001183 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1184 leaf = path->nodes[0];
1185 btrfs_item_key_to_cpu(leaf, &found_key,
1186 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001187 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001188 found_key.type == BTRFS_EXTENT_DATA_KEY)
1189 path->slots[0]--;
1190 }
1191 check_prev = 0;
1192next_slot:
1193 leaf = path->nodes[0];
1194 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1195 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001196 if (ret < 0) {
1197 btrfs_abort_transaction(trans, root, ret);
1198 goto error;
1199 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 if (ret > 0)
1201 break;
1202 leaf = path->nodes[0];
1203 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001204
Yan Zheng80ff3852008-10-30 14:20:02 -04001205 nocow = 0;
1206 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001207 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001209
Li Zefan33345d012011-04-20 10:31:50 +08001210 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1212 found_key.offset > end)
1213 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001214
Yan Zheng80ff3852008-10-30 14:20:02 -04001215 if (found_key.offset > cur_offset) {
1216 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001217 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001218 goto out_check;
1219 }
Chris Masonc31f8832008-01-08 15:46:31 -05001220
Yan Zheng80ff3852008-10-30 14:20:02 -04001221 fi = btrfs_item_ptr(leaf, path->slots[0],
1222 struct btrfs_file_extent_item);
1223 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001224
Yan Zhengd899e052008-10-30 14:25:28 -04001225 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1226 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001227 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001228 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001229 extent_end = found_key.offset +
1230 btrfs_file_extent_num_bytes(leaf, fi);
1231 if (extent_end <= start) {
1232 path->slots[0]++;
1233 goto next_slot;
1234 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001235 if (disk_bytenr == 0)
1236 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 if (btrfs_file_extent_compression(leaf, fi) ||
1238 btrfs_file_extent_encryption(leaf, fi) ||
1239 btrfs_file_extent_other_encoding(leaf, fi))
1240 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001241 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1242 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001243 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001245 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001246 found_key.offset -
1247 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001248 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001249 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001250 disk_bytenr += cur_offset - found_key.offset;
1251 num_bytes = min(end + 1, extent_end) - cur_offset;
1252 /*
1253 * force cow if csum exists in the range.
1254 * this ensure that csum for a given extent are
1255 * either valid or do not exist.
1256 */
1257 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1258 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 nocow = 1;
1260 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1261 extent_end = found_key.offset +
1262 btrfs_file_extent_inline_len(leaf, fi);
1263 extent_end = ALIGN(extent_end, root->sectorsize);
1264 } else {
1265 BUG_ON(1);
1266 }
1267out_check:
1268 if (extent_end <= start) {
1269 path->slots[0]++;
1270 goto next_slot;
1271 }
1272 if (!nocow) {
1273 if (cow_start == (u64)-1)
1274 cow_start = cur_offset;
1275 cur_offset = extent_end;
1276 if (cur_offset > end)
1277 break;
1278 path->slots[0]++;
1279 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001280 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001281
David Sterbab3b4aa72011-04-21 01:20:15 +02001282 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 if (cow_start != (u64)-1) {
1284 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001285 found_key.offset - 1, page_started,
1286 nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001287 if (ret) {
1288 btrfs_abort_transaction(trans, root, ret);
1289 goto error;
1290 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001291 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001292 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001293
Yan Zhengd899e052008-10-30 14:25:28 -04001294 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1295 struct extent_map *em;
1296 struct extent_map_tree *em_tree;
1297 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001298 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001299 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001300 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001301 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001302 em->len = num_bytes;
1303 em->block_len = num_bytes;
1304 em->block_start = disk_bytenr;
1305 em->bdev = root->fs_info->fs_devices->latest_bdev;
1306 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -06001307 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Yan Zhengd899e052008-10-30 14:25:28 -04001308 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001309 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001310 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001311 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001312 if (ret != -EEXIST) {
1313 free_extent_map(em);
1314 break;
1315 }
1316 btrfs_drop_extent_cache(inode, em->start,
1317 em->start + em->len - 1, 0);
1318 }
1319 type = BTRFS_ORDERED_PREALLOC;
1320 } else {
1321 type = BTRFS_ORDERED_NOCOW;
1322 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001323
1324 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001325 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001326 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001327
Yan, Zhengefa56462010-05-16 10:49:59 -04001328 if (root->root_key.objectid ==
1329 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1330 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1331 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001332 if (ret) {
1333 btrfs_abort_transaction(trans, root, ret);
1334 goto error;
1335 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001336 }
1337
Yan Zhengd899e052008-10-30 14:25:28 -04001338 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001339 cur_offset, cur_offset + num_bytes - 1,
1340 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1341 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1342 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 cur_offset = extent_end;
1344 if (cur_offset > end)
1345 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001346 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001347 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001348
Josef Bacik17ca04a2012-05-31 15:58:55 -04001349 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001350 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001351 cur_offset = end;
1352 }
1353
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 if (cow_start != (u64)-1) {
1355 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001356 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001357 if (ret) {
1358 btrfs_abort_transaction(trans, root, ret);
1359 goto error;
1360 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 }
1362
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001363error:
Miao Xiea698d0752012-09-20 01:51:59 -06001364 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001365 if (!ret)
1366 ret = err;
1367
Josef Bacik17ca04a2012-05-31 15:58:55 -04001368 if (ret && cur_offset < end)
1369 extent_clear_unlock_delalloc(inode,
1370 &BTRFS_I(inode)->io_tree,
1371 cur_offset, end, locked_page,
1372 EXTENT_CLEAR_UNLOCK_PAGE |
1373 EXTENT_CLEAR_UNLOCK |
1374 EXTENT_CLEAR_DELALLOC |
1375 EXTENT_CLEAR_DIRTY |
1376 EXTENT_SET_WRITEBACK |
1377 EXTENT_END_WRITEBACK);
1378
Yan Zheng7ea394f2008-08-05 13:05:02 -04001379 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001380 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001381}
1382
Chris Masond352ac62008-09-29 15:18:18 -04001383/*
1384 * extent_io.c call back to do delayed allocation processing
1385 */
Chris Masonc8b97812008-10-29 14:49:59 -04001386static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001387 u64 start, u64 end, int *page_started,
1388 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001389{
Chris Masonbe20aa92007-12-17 20:14:01 -05001390 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001391 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001392
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001393 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001394 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001395 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001396 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001397 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001398 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001399 } else if (!btrfs_test_opt(root, COMPRESS) &&
1400 !(BTRFS_I(inode)->force_compress) &&
1401 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001402 ret = cow_file_range(inode, locked_page, start, end,
1403 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001404 } else {
1405 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1406 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001407 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001408 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001409 }
Chris Masonb888db22007-08-27 16:49:44 -04001410 return ret;
1411}
1412
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001413static void btrfs_split_extent_hook(struct inode *inode,
1414 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001415{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001416 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001417 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001418 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001419
Josef Bacik9e0baf62011-07-15 15:16:44 +00001420 spin_lock(&BTRFS_I(inode)->lock);
1421 BTRFS_I(inode)->outstanding_extents++;
1422 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001423}
1424
1425/*
1426 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1427 * extents so we can keep track of new extents that are just merged onto old
1428 * extents, such as when we are doing sequential writes, so we can properly
1429 * account for the metadata space we'll need.
1430 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001431static void btrfs_merge_extent_hook(struct inode *inode,
1432 struct extent_state *new,
1433 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001434{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001435 /* not delalloc, ignore it */
1436 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001437 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001438
Josef Bacik9e0baf62011-07-15 15:16:44 +00001439 spin_lock(&BTRFS_I(inode)->lock);
1440 BTRFS_I(inode)->outstanding_extents--;
1441 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001442}
1443
Chris Masond352ac62008-09-29 15:18:18 -04001444/*
1445 * extent_io.c set_bit_hook, used to track delayed allocation
1446 * bytes in this file, and to maintain the list of inodes that
1447 * have pending delalloc work to be done.
1448 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001449static void btrfs_set_bit_hook(struct inode *inode,
1450 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001451{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001452
Chris Mason75eff682008-12-15 15:54:40 -05001453 /*
1454 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001455 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001456 * bit, which is only set or cleared with irqs on
1457 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001458 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001459 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001460 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001461 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001462
Josef Bacik9e0baf62011-07-15 15:16:44 +00001463 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001464 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001465 } else {
1466 spin_lock(&BTRFS_I(inode)->lock);
1467 BTRFS_I(inode)->outstanding_extents++;
1468 spin_unlock(&BTRFS_I(inode)->lock);
1469 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001470
Chris Mason75eff682008-12-15 15:54:40 -05001471 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001472 BTRFS_I(inode)->delalloc_bytes += len;
1473 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001474 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001475 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1476 &root->fs_info->delalloc_inodes);
1477 }
Chris Mason75eff682008-12-15 15:54:40 -05001478 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001479 }
Chris Mason291d6732008-01-29 15:55:23 -05001480}
1481
Chris Masond352ac62008-09-29 15:18:18 -04001482/*
1483 * extent_io.c clear_bit_hook, see set_bit_hook for why
1484 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001485static void btrfs_clear_bit_hook(struct inode *inode,
1486 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001487{
Chris Mason75eff682008-12-15 15:54:40 -05001488 /*
1489 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001490 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001491 * bit, which is only set or cleared with irqs on
1492 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001493 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001494 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001495 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001496 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001497
Josef Bacik9e0baf62011-07-15 15:16:44 +00001498 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001499 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001500 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1501 spin_lock(&BTRFS_I(inode)->lock);
1502 BTRFS_I(inode)->outstanding_extents--;
1503 spin_unlock(&BTRFS_I(inode)->lock);
1504 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001505
1506 if (*bits & EXTENT_DO_ACCOUNTING)
1507 btrfs_delalloc_release_metadata(inode, len);
1508
Josef Bacik0cb59c92010-07-02 12:14:14 -04001509 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1510 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001511 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001512
Chris Mason75eff682008-12-15 15:54:40 -05001513 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001514 root->fs_info->delalloc_bytes -= len;
1515 BTRFS_I(inode)->delalloc_bytes -= len;
1516
Josef Bacik0cb59c92010-07-02 12:14:14 -04001517 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001518 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1519 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1520 }
Chris Mason75eff682008-12-15 15:54:40 -05001521 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001522 }
Chris Mason291d6732008-01-29 15:55:23 -05001523}
1524
Chris Masond352ac62008-09-29 15:18:18 -04001525/*
1526 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1527 * we don't create bios that span stripes or chunks
1528 */
Chris Mason239b14b2008-03-24 15:02:07 -04001529int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001530 size_t size, struct bio *bio,
1531 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001532{
1533 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1534 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001535 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001536 u64 length = 0;
1537 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001538 int ret;
1539
Chris Mason771ed682008-11-06 22:02:51 -05001540 if (bio_flags & EXTENT_BIO_COMPRESSED)
1541 return 0;
1542
Chris Masonf2d8d742008-04-21 10:03:05 -04001543 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001544 map_tree = &root->fs_info->mapping_tree;
1545 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001546 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001547 &map_length, NULL, 0);
Jeff Mahoney3444a972011-10-03 23:23:13 -04001548 /* Will always return 0 or 1 with map_multi == NULL */
1549 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001550 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001551 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001552 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001553}
1554
Chris Masond352ac62008-09-29 15:18:18 -04001555/*
1556 * in order to insert checksums into the metadata in large chunks,
1557 * we wait until bio submission time. All the pages in the bio are
1558 * checksummed and sums are attached onto the ordered extent record.
1559 *
1560 * At IO completion time the cums attached on the ordered extent record
1561 * are inserted into the btree
1562 */
Chris Masond3977122009-01-05 21:25:51 -05001563static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1564 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001565 unsigned long bio_flags,
1566 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001567{
Chris Mason065631f2008-02-20 12:07:25 -05001568 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001569 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001570
Chris Masond20f7042008-12-08 16:58:54 -05001571 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001572 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001573 return 0;
1574}
Chris Masone0156402008-04-16 11:15:20 -04001575
Chris Mason4a69a412008-11-06 22:03:00 -05001576/*
1577 * in order to insert checksums into the metadata in large chunks,
1578 * we wait until bio submission time. All the pages in the bio are
1579 * checksummed and sums are attached onto the ordered extent record.
1580 *
1581 * At IO completion time the cums attached on the ordered extent record
1582 * are inserted into the btree
1583 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001584static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001585 int mirror_num, unsigned long bio_flags,
1586 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001587{
1588 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001589 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001590}
1591
Chris Masond352ac62008-09-29 15:18:18 -04001592/*
Chris Masoncad321a2008-12-17 14:51:42 -05001593 * extent_io.c submission hook. This does the right thing for csum calculation
1594 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001595 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001596static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001597 int mirror_num, unsigned long bio_flags,
1598 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001599{
1600 struct btrfs_root *root = BTRFS_I(inode)->root;
1601 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001602 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001603 int metadata = 0;
Chris Mason44b8bd72008-04-16 11:14:51 -04001604
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001605 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001606
Liu Bo83eea1f2012-07-10 05:28:39 -06001607 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001608 metadata = 2;
1609
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001610 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001611 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1612 if (ret)
1613 return ret;
1614
Chris Masond20f7042008-12-08 16:58:54 -05001615 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001616 return btrfs_submit_compressed_read(inode, bio,
1617 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001618 } else if (!skip_sum) {
1619 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1620 if (ret)
1621 return ret;
1622 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001623 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001624 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001625 /* csum items have already been cloned */
1626 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1627 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001628 /* we're doing a write, do the async checksumming */
1629 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001630 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001631 bio_flags, bio_offset,
1632 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001633 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001634 }
1635
Chris Mason0b86a832008-03-24 15:01:56 -04001636mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001637 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001638}
Chris Mason6885f302008-02-20 16:11:05 -05001639
Chris Masond352ac62008-09-29 15:18:18 -04001640/*
1641 * given a list of ordered sums record them in the inode. This happens
1642 * at IO completion time based on sums calculated at bio submission time.
1643 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001644static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001645 struct inode *inode, u64 file_offset,
1646 struct list_head *list)
1647{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001648 struct btrfs_ordered_sum *sum;
1649
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001650 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001651 btrfs_csum_file_blocks(trans,
1652 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001653 }
1654 return 0;
1655}
1656
Josef Bacik2ac55d42010-02-03 19:33:23 +00001657int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1658 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001659{
Chris Masond3977122009-01-05 21:25:51 -05001660 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001661 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001662 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001663 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001664}
1665
Chris Masond352ac62008-09-29 15:18:18 -04001666/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001667struct btrfs_writepage_fixup {
1668 struct page *page;
1669 struct btrfs_work work;
1670};
1671
Christoph Hellwigb2950862008-12-02 09:54:17 -05001672static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001673{
1674 struct btrfs_writepage_fixup *fixup;
1675 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001676 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001677 struct page *page;
1678 struct inode *inode;
1679 u64 page_start;
1680 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001681 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001682
1683 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1684 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001685again:
Chris Mason247e7432008-07-17 12:53:51 -04001686 lock_page(page);
1687 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1688 ClearPageChecked(page);
1689 goto out_page;
1690 }
1691
1692 inode = page->mapping->host;
1693 page_start = page_offset(page);
1694 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1695
Josef Bacik2ac55d42010-02-03 19:33:23 +00001696 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001697 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001698
1699 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001700 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001701 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001702
1703 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1704 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001705 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1706 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001707 unlock_page(page);
1708 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001709 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001710 goto again;
1711 }
Chris Mason247e7432008-07-17 12:53:51 -04001712
Jeff Mahoney87826df2012-02-15 16:23:57 +01001713 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1714 if (ret) {
1715 mapping_set_error(page->mapping, ret);
1716 end_extent_writepage(page, ret, page_start, page_end);
1717 ClearPageChecked(page);
1718 goto out;
1719 }
1720
Josef Bacik2ac55d42010-02-03 19:33:23 +00001721 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001722 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001723 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001724out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001725 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1726 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001727out_page:
1728 unlock_page(page);
1729 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001730 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001731}
1732
1733/*
1734 * There are a few paths in the higher layers of the kernel that directly
1735 * set the page dirty bit without asking the filesystem if it is a
1736 * good idea. This causes problems because we want to make sure COW
1737 * properly happens and the data=ordered rules are followed.
1738 *
Chris Masonc8b97812008-10-29 14:49:59 -04001739 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001740 * hasn't been properly setup for IO. We kick off an async process
1741 * to fix it up. The async helper will wait for ordered extents, set
1742 * the delalloc bit and make it safe to write the page.
1743 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001744static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001745{
1746 struct inode *inode = page->mapping->host;
1747 struct btrfs_writepage_fixup *fixup;
1748 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001749
Chris Mason8b62b722009-09-02 16:53:46 -04001750 /* this page is properly in the ordered list */
1751 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001752 return 0;
1753
1754 if (PageChecked(page))
1755 return -EAGAIN;
1756
1757 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1758 if (!fixup)
1759 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001760
Chris Mason247e7432008-07-17 12:53:51 -04001761 SetPageChecked(page);
1762 page_cache_get(page);
1763 fixup->work.func = btrfs_writepage_fixup_worker;
1764 fixup->page = page;
1765 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001766 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001767}
1768
Yan Zhengd899e052008-10-30 14:25:28 -04001769static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1770 struct inode *inode, u64 file_pos,
1771 u64 disk_bytenr, u64 disk_num_bytes,
1772 u64 num_bytes, u64 ram_bytes,
1773 u8 compression, u8 encryption,
1774 u16 other_encoding, int extent_type)
1775{
1776 struct btrfs_root *root = BTRFS_I(inode)->root;
1777 struct btrfs_file_extent_item *fi;
1778 struct btrfs_path *path;
1779 struct extent_buffer *leaf;
1780 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001781 int ret;
1782
1783 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001784 if (!path)
1785 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001786
Chris Masonb9473432009-03-13 11:00:37 -04001787 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001788
1789 /*
1790 * we may be replacing one extent in the tree with another.
1791 * The new extent is pinned in the extent map, and we don't want
1792 * to drop it from the cache until it is completely in the btree.
1793 *
1794 * So, tell btrfs_drop_extents to leave this extent in the cache.
1795 * the caller is expected to unpin it and allow it to be merged
1796 * with the others.
1797 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001798 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001799 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001800 if (ret)
1801 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001802
Li Zefan33345d012011-04-20 10:31:50 +08001803 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001804 ins.offset = file_pos;
1805 ins.type = BTRFS_EXTENT_DATA_KEY;
1806 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001807 if (ret)
1808 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001809 leaf = path->nodes[0];
1810 fi = btrfs_item_ptr(leaf, path->slots[0],
1811 struct btrfs_file_extent_item);
1812 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1813 btrfs_set_file_extent_type(leaf, fi, extent_type);
1814 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1815 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1816 btrfs_set_file_extent_offset(leaf, fi, 0);
1817 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1818 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1819 btrfs_set_file_extent_compression(leaf, fi, compression);
1820 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1821 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001822
Yan Zhengd899e052008-10-30 14:25:28 -04001823 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001824 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001825
1826 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001827
1828 ins.objectid = disk_bytenr;
1829 ins.offset = disk_num_bytes;
1830 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001831 ret = btrfs_alloc_reserved_file_extent(trans, root,
1832 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001833 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001834out:
Yan Zhengd899e052008-10-30 14:25:28 -04001835 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001836
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001837 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001838}
1839
Chris Mason5d13a982009-03-13 11:41:46 -04001840/*
1841 * helper function for btrfs_finish_ordered_io, this
1842 * just reads in some of the csum leaves to prime them into ram
1843 * before we start the transaction. It limits the amount of btree
1844 * reads required while inside the transaction.
1845 */
Chris Masond352ac62008-09-29 15:18:18 -04001846/* as ordered data IO finishes, this gets called so we can finish
1847 * an ordered extent if the range of bytes in the file it covers are
1848 * fully written.
1849 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001850static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001851{
Josef Bacik5fd02042012-05-02 14:00:54 -04001852 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001853 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001854 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001855 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001856 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001857 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001858 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001859 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001860
Liu Bo83eea1f2012-07-10 05:28:39 -06001861 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001862
Josef Bacik5fd02042012-05-02 14:00:54 -04001863 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1864 ret = -EIO;
1865 goto out;
1866 }
1867
Yan, Zhengc2167752009-11-12 09:34:21 +00001868 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001869 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Yan, Zhengc2167752009-11-12 09:34:21 +00001870 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1871 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001872 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001873 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001874 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001875 trans = btrfs_join_transaction(root);
Liu Bod280e5b2012-08-21 21:13:25 -06001876 if (IS_ERR(trans)) {
1877 ret = PTR_ERR(trans);
1878 trans = NULL;
1879 goto out;
1880 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001881 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason21151332011-11-10 20:39:08 -05001882 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001883 if (ret) /* -ENOMEM or corruption */
1884 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001885 }
1886 goto out;
1887 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001888
Josef Bacik2ac55d42010-02-03 19:33:23 +00001889 lock_extent_bits(io_tree, ordered_extent->file_offset,
1890 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001891 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001892
Josef Bacik0cb59c92010-07-02 12:14:14 -04001893 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001894 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001895 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001896 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001897 if (IS_ERR(trans)) {
1898 ret = PTR_ERR(trans);
1899 trans = NULL;
1900 goto out_unlock;
1901 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001902 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001903
Chris Masonc8b97812008-10-29 14:49:59 -04001904 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001905 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001906 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001907 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001908 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001909 ordered_extent->file_offset,
1910 ordered_extent->file_offset +
1911 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001912 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001913 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001914 ret = insert_reserved_file_extent(trans, inode,
1915 ordered_extent->file_offset,
1916 ordered_extent->start,
1917 ordered_extent->disk_len,
1918 ordered_extent->len,
1919 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001920 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001921 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04001922 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04001923 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1924 ordered_extent->file_offset, ordered_extent->len,
1925 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001926 if (ret < 0) {
1927 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001928 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001929 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001930
Chris Masone6dcd2d2008-07-17 12:53:50 -04001931 add_pending_csums(trans, inode, ordered_extent->file_offset,
1932 &ordered_extent->list);
1933
Josef Bacik1ef30be2011-04-05 19:25:36 -04001934 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Miao Xiea39f7522011-09-11 10:52:25 -04001935 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Chris Mason21151332011-11-10 20:39:08 -05001936 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001937 if (ret) { /* -ENOMEM or corruption */
1938 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001939 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001940 }
Josef Bacik7c735312012-08-13 15:43:26 -04001941 } else {
1942 btrfs_set_inode_last_trans(trans, inode);
Josef Bacik1ef30be2011-04-05 19:25:36 -04001943 }
1944 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04001945out_unlock:
1946 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1947 ordered_extent->file_offset +
1948 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00001949out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001950 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001951 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06001952 if (trans)
1953 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001954
Josef Bacik5fd02042012-05-02 14:00:54 -04001955 if (ret)
1956 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
1957 ordered_extent->file_offset +
1958 ordered_extent->len - 1, NULL, GFP_NOFS);
1959
1960 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08001961 * This needs to be done to make sure anybody waiting knows we are done
1962 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04001963 */
1964 btrfs_remove_ordered_extent(inode, ordered_extent);
1965
Chris Masone6dcd2d2008-07-17 12:53:50 -04001966 /* once for us */
1967 btrfs_put_ordered_extent(ordered_extent);
1968 /* once for the tree */
1969 btrfs_put_ordered_extent(ordered_extent);
1970
Josef Bacik5fd02042012-05-02 14:00:54 -04001971 return ret;
1972}
1973
1974static void finish_ordered_fn(struct btrfs_work *work)
1975{
1976 struct btrfs_ordered_extent *ordered_extent;
1977 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
1978 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001979}
1980
Christoph Hellwigb2950862008-12-02 09:54:17 -05001981static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001982 struct extent_state *state, int uptodate)
1983{
Josef Bacik5fd02042012-05-02 14:00:54 -04001984 struct inode *inode = page->mapping->host;
1985 struct btrfs_root *root = BTRFS_I(inode)->root;
1986 struct btrfs_ordered_extent *ordered_extent = NULL;
1987 struct btrfs_workers *workers;
1988
liubo1abe9b82011-03-24 11:18:59 +00001989 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1990
Chris Mason8b62b722009-09-02 16:53:46 -04001991 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04001992 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1993 end - start + 1, uptodate))
1994 return 0;
1995
1996 ordered_extent->work.func = finish_ordered_fn;
1997 ordered_extent->work.flags = 0;
1998
Liu Bo83eea1f2012-07-10 05:28:39 -06001999 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002000 workers = &root->fs_info->endio_freespace_worker;
2001 else
2002 workers = &root->fs_info->endio_write_workers;
2003 btrfs_queue_worker(workers, &ordered_extent->work);
2004
2005 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002006}
2007
Chris Masond352ac62008-09-29 15:18:18 -04002008/*
Chris Masond352ac62008-09-29 15:18:18 -04002009 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002010 * if there's a match, we allow the bio to finish. If not, the code in
2011 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002012 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002013static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002014 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002015{
Chris Mason35ebb932007-10-30 16:56:53 -04002016 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04002017 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002018 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002019 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002020 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002021 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002022 struct btrfs_root *root = BTRFS_I(inode)->root;
2023 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002024
Chris Masond20f7042008-12-08 16:58:54 -05002025 if (PageChecked(page)) {
2026 ClearPageChecked(page);
2027 goto good;
2028 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002029
2030 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002031 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002032
Yan Zheng17d217f2008-12-12 10:03:38 -05002033 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002034 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002035 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2036 GFP_NOFS);
2037 return 0;
2038 }
2039
Yanc2e639f2008-02-04 08:57:25 -05002040 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002041 private = state->private;
2042 ret = 0;
2043 } else {
2044 ret = get_state_private(io_tree, start, &private);
2045 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002046 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002047 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002048 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002049
Chris Masonff79f812007-10-15 16:22:25 -04002050 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2051 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002052 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002053 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002054
Cong Wang7ac687d2011-11-25 23:14:28 +08002055 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002056good:
Chris Mason07157aa2007-08-30 08:50:51 -04002057 return 0;
2058
2059zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002060 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002061 "private %llu\n",
2062 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002063 (unsigned long long)start, csum,
2064 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002065 memset(kaddr + offset, 1, end - start + 1);
2066 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002067 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002068 if (private == 0)
2069 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002070 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002071}
Chris Masonb888db22007-08-27 16:49:44 -04002072
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002073struct delayed_iput {
2074 struct list_head list;
2075 struct inode *inode;
2076};
2077
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002078/* JDM: If this is fs-wide, why can't we add a pointer to
2079 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002080void btrfs_add_delayed_iput(struct inode *inode)
2081{
2082 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2083 struct delayed_iput *delayed;
2084
2085 if (atomic_add_unless(&inode->i_count, -1, 1))
2086 return;
2087
2088 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2089 delayed->inode = inode;
2090
2091 spin_lock(&fs_info->delayed_iput_lock);
2092 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2093 spin_unlock(&fs_info->delayed_iput_lock);
2094}
2095
2096void btrfs_run_delayed_iputs(struct btrfs_root *root)
2097{
2098 LIST_HEAD(list);
2099 struct btrfs_fs_info *fs_info = root->fs_info;
2100 struct delayed_iput *delayed;
2101 int empty;
2102
2103 spin_lock(&fs_info->delayed_iput_lock);
2104 empty = list_empty(&fs_info->delayed_iputs);
2105 spin_unlock(&fs_info->delayed_iput_lock);
2106 if (empty)
2107 return;
2108
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002109 spin_lock(&fs_info->delayed_iput_lock);
2110 list_splice_init(&fs_info->delayed_iputs, &list);
2111 spin_unlock(&fs_info->delayed_iput_lock);
2112
2113 while (!list_empty(&list)) {
2114 delayed = list_entry(list.next, struct delayed_iput, list);
2115 list_del(&delayed->list);
2116 iput(delayed->inode);
2117 kfree(delayed);
2118 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002119}
2120
Yan, Zhengd68fc572010-05-16 10:49:58 -04002121enum btrfs_orphan_cleanup_state {
2122 ORPHAN_CLEANUP_STARTED = 1,
2123 ORPHAN_CLEANUP_DONE = 2,
2124};
2125
2126/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002127 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002128 * files in the subvolume, it removes orphan item and frees block_rsv
2129 * structure.
2130 */
2131void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2132 struct btrfs_root *root)
2133{
Josef Bacik90290e12011-12-02 15:44:12 -05002134 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002135 int ret;
2136
Josef Bacik8a35d952012-05-23 14:26:42 -04002137 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002138 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2139 return;
2140
Josef Bacik90290e12011-12-02 15:44:12 -05002141 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002142 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002143 spin_unlock(&root->orphan_lock);
2144 return;
2145 }
2146
2147 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2148 spin_unlock(&root->orphan_lock);
2149 return;
2150 }
2151
2152 block_rsv = root->orphan_block_rsv;
2153 root->orphan_block_rsv = NULL;
2154 spin_unlock(&root->orphan_lock);
2155
Yan, Zhengd68fc572010-05-16 10:49:58 -04002156 if (root->orphan_item_inserted &&
2157 btrfs_root_refs(&root->root_item) > 0) {
2158 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2159 root->root_key.objectid);
2160 BUG_ON(ret);
2161 root->orphan_item_inserted = 0;
2162 }
2163
Josef Bacik90290e12011-12-02 15:44:12 -05002164 if (block_rsv) {
2165 WARN_ON(block_rsv->size > 0);
2166 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002167 }
2168}
2169
2170/*
Josef Bacik7b128762008-07-24 12:17:14 -04002171 * This creates an orphan entry for the given inode in case something goes
2172 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002173 *
2174 * NOTE: caller of this function should reserve 5 units of metadata for
2175 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002176 */
2177int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2178{
2179 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002180 struct btrfs_block_rsv *block_rsv = NULL;
2181 int reserve = 0;
2182 int insert = 0;
2183 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002184
Yan, Zhengd68fc572010-05-16 10:49:58 -04002185 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002186 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002187 if (!block_rsv)
2188 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002189 }
2190
Yan, Zhengd68fc572010-05-16 10:49:58 -04002191 spin_lock(&root->orphan_lock);
2192 if (!root->orphan_block_rsv) {
2193 root->orphan_block_rsv = block_rsv;
2194 } else if (block_rsv) {
2195 btrfs_free_block_rsv(root, block_rsv);
2196 block_rsv = NULL;
2197 }
Josef Bacik7b128762008-07-24 12:17:14 -04002198
Josef Bacik8a35d952012-05-23 14:26:42 -04002199 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2200 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002201#if 0
2202 /*
2203 * For proper ENOSPC handling, we should do orphan
2204 * cleanup when mounting. But this introduces backward
2205 * compatibility issue.
2206 */
2207 if (!xchg(&root->orphan_item_inserted, 1))
2208 insert = 2;
2209 else
2210 insert = 1;
2211#endif
2212 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002213 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002214 }
Josef Bacik7b128762008-07-24 12:17:14 -04002215
Josef Bacik72ac3c02012-05-23 14:13:11 -04002216 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2217 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002218 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002219 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002220
Yan, Zhengd68fc572010-05-16 10:49:58 -04002221 /* grab metadata reservation from transaction handle */
2222 if (reserve) {
2223 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002224 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002225 }
2226
2227 /* insert an orphan item to track this unlinked/truncated file */
2228 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002229 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002230 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002231 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2232 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002233 btrfs_abort_transaction(trans, root, ret);
2234 return ret;
2235 }
2236 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002237 }
2238
2239 /* insert an orphan item to track subvolume contains orphan files */
2240 if (insert >= 2) {
2241 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2242 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002243 if (ret && ret != -EEXIST) {
2244 btrfs_abort_transaction(trans, root, ret);
2245 return ret;
2246 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002247 }
2248 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002249}
2250
2251/*
2252 * We have done the truncate/delete so we can go ahead and remove the orphan
2253 * item for this particular inode.
2254 */
2255int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2256{
2257 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002258 int delete_item = 0;
2259 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002260 int ret = 0;
2261
Yan, Zhengd68fc572010-05-16 10:49:58 -04002262 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002263 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2264 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002265 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002266
Josef Bacik72ac3c02012-05-23 14:13:11 -04002267 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2268 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002269 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002270 spin_unlock(&root->orphan_lock);
2271
2272 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002273 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002274 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002275 }
2276
Josef Bacik8a35d952012-05-23 14:26:42 -04002277 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002278 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002279 atomic_dec(&root->orphan_inodes);
2280 }
Josef Bacik7b128762008-07-24 12:17:14 -04002281
Yan, Zhengd68fc572010-05-16 10:49:58 -04002282 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002283}
2284
2285/*
2286 * this cleans up any orphans that may be left on the list from the last use
2287 * of this root.
2288 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002289int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002290{
2291 struct btrfs_path *path;
2292 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002293 struct btrfs_key key, found_key;
2294 struct btrfs_trans_handle *trans;
2295 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002296 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002297 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2298
Yan, Zhengd68fc572010-05-16 10:49:58 -04002299 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002300 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002301
2302 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002303 if (!path) {
2304 ret = -ENOMEM;
2305 goto out;
2306 }
Josef Bacik7b128762008-07-24 12:17:14 -04002307 path->reada = -1;
2308
2309 key.objectid = BTRFS_ORPHAN_OBJECTID;
2310 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2311 key.offset = (u64)-1;
2312
Josef Bacik7b128762008-07-24 12:17:14 -04002313 while (1) {
2314 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002315 if (ret < 0)
2316 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002317
2318 /*
2319 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002320 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002321 * find the key and see if we have stuff that matches
2322 */
2323 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002324 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002325 if (path->slots[0] == 0)
2326 break;
2327 path->slots[0]--;
2328 }
2329
2330 /* pull out the item */
2331 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002332 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2333
2334 /* make sure the item matches what we want */
2335 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2336 break;
2337 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2338 break;
2339
2340 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002341 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002342
2343 /*
2344 * this is where we are basically btrfs_lookup, without the
2345 * crossing root thing. we store the inode number in the
2346 * offset of the orphan item.
2347 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002348
2349 if (found_key.offset == last_objectid) {
2350 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2351 "stopping orphan cleanup\n");
2352 ret = -EINVAL;
2353 goto out;
2354 }
2355
2356 last_objectid = found_key.offset;
2357
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002358 found_key.objectid = found_key.offset;
2359 found_key.type = BTRFS_INODE_ITEM_KEY;
2360 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002361 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002362 ret = PTR_RET(inode);
2363 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002364 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002365
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002366 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2367 struct btrfs_root *dead_root;
2368 struct btrfs_fs_info *fs_info = root->fs_info;
2369 int is_dead_root = 0;
2370
2371 /*
2372 * this is an orphan in the tree root. Currently these
2373 * could come from 2 sources:
2374 * a) a snapshot deletion in progress
2375 * b) a free space cache inode
2376 * We need to distinguish those two, as the snapshot
2377 * orphan must not get deleted.
2378 * find_dead_roots already ran before us, so if this
2379 * is a snapshot deletion, we should find the root
2380 * in the dead_roots list
2381 */
2382 spin_lock(&fs_info->trans_lock);
2383 list_for_each_entry(dead_root, &fs_info->dead_roots,
2384 root_list) {
2385 if (dead_root->root_key.objectid ==
2386 found_key.objectid) {
2387 is_dead_root = 1;
2388 break;
2389 }
2390 }
2391 spin_unlock(&fs_info->trans_lock);
2392 if (is_dead_root) {
2393 /* prevent this orphan from being found again */
2394 key.offset = found_key.objectid - 1;
2395 continue;
2396 }
2397 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002398 /*
2399 * Inode is already gone but the orphan item is still there,
2400 * kill the orphan item.
2401 */
2402 if (ret == -ESTALE) {
2403 trans = btrfs_start_transaction(root, 1);
2404 if (IS_ERR(trans)) {
2405 ret = PTR_ERR(trans);
2406 goto out;
2407 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002408 printk(KERN_ERR "auto deleting %Lu\n",
2409 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002410 ret = btrfs_del_orphan_item(trans, root,
2411 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002412 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002413 btrfs_end_transaction(trans, root);
2414 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002415 }
Josef Bacik7b128762008-07-24 12:17:14 -04002416
Josef Bacik7b128762008-07-24 12:17:14 -04002417 /*
2418 * add this inode to the orphan list so btrfs_orphan_del does
2419 * the proper thing when we hit it
2420 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002421 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2422 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002423
Josef Bacik7b128762008-07-24 12:17:14 -04002424 /* if we have links, this was a truncate, lets do that */
2425 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002426 if (!S_ISREG(inode->i_mode)) {
2427 WARN_ON(1);
2428 iput(inode);
2429 continue;
2430 }
Josef Bacik7b128762008-07-24 12:17:14 -04002431 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002432 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002433 } else {
2434 nr_unlink++;
2435 }
2436
2437 /* this will do delete_inode and everything for us */
2438 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002439 if (ret)
2440 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002441 }
Miao Xie3254c872011-11-10 20:45:05 -05002442 /* release the path since we're done with it */
2443 btrfs_release_path(path);
2444
Yan, Zhengd68fc572010-05-16 10:49:58 -04002445 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2446
2447 if (root->orphan_block_rsv)
2448 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2449 (u64)-1);
2450
2451 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002452 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002453 if (!IS_ERR(trans))
2454 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002455 }
Josef Bacik7b128762008-07-24 12:17:14 -04002456
2457 if (nr_unlink)
2458 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2459 if (nr_truncate)
2460 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002461
2462out:
2463 if (ret)
2464 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2465 btrfs_free_path(path);
2466 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002467}
2468
Chris Masond352ac62008-09-29 15:18:18 -04002469/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002470 * very simple check to peek ahead in the leaf looking for xattrs. If we
2471 * don't find any xattrs, we know there can't be any acls.
2472 *
2473 * slot is the slot the inode is in, objectid is the objectid of the inode
2474 */
2475static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2476 int slot, u64 objectid)
2477{
2478 u32 nritems = btrfs_header_nritems(leaf);
2479 struct btrfs_key found_key;
2480 int scanned = 0;
2481
2482 slot++;
2483 while (slot < nritems) {
2484 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2485
2486 /* we found a different objectid, there must not be acls */
2487 if (found_key.objectid != objectid)
2488 return 0;
2489
2490 /* we found an xattr, assume we've got an acl */
2491 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2492 return 1;
2493
2494 /*
2495 * we found a key greater than an xattr key, there can't
2496 * be any acls later on
2497 */
2498 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2499 return 0;
2500
2501 slot++;
2502 scanned++;
2503
2504 /*
2505 * it goes inode, inode backrefs, xattrs, extents,
2506 * so if there are a ton of hard links to an inode there can
2507 * be a lot of backrefs. Don't waste time searching too hard,
2508 * this is just an optimization
2509 */
2510 if (scanned >= 8)
2511 break;
2512 }
2513 /* we hit the end of the leaf before we found an xattr or
2514 * something larger than an xattr. We have to assume the inode
2515 * has acls
2516 */
2517 return 1;
2518}
2519
2520/*
Chris Masond352ac62008-09-29 15:18:18 -04002521 * read an inode from the btree into the in-memory inode
2522 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002523static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002524{
2525 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002526 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002527 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002528 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002529 struct btrfs_root *root = BTRFS_I(inode)->root;
2530 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002531 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002532 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002533 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002534 bool filled = false;
2535
2536 ret = btrfs_fill_inode(inode, &rdev);
2537 if (!ret)
2538 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002539
2540 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002541 if (!path)
2542 goto make_bad;
2543
Josef Bacikd90c7322011-05-17 09:50:54 -04002544 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002545 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002546
Chris Mason39279cc2007-06-12 06:35:45 -04002547 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002548 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002549 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002550
Chris Mason5f39d392007-10-15 16:14:19 -04002551 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002552
2553 if (filled)
2554 goto cache_acl;
2555
Chris Mason5f39d392007-10-15 16:14:19 -04002556 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2557 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002558 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002559 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002560 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2561 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002562 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002563
2564 tspec = btrfs_inode_atime(inode_item);
2565 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2566 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2567
2568 tspec = btrfs_inode_mtime(inode_item);
2569 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2570 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2571
2572 tspec = btrfs_inode_ctime(inode_item);
2573 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2574 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2575
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002576 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002577 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002578 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2579
2580 /*
2581 * If we were modified in the current generation and evicted from memory
2582 * and then re-read we need to do a full sync since we don't have any
2583 * idea about which extents were modified before we were evicted from
2584 * cache.
2585 */
2586 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2587 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2588 &BTRFS_I(inode)->runtime_flags);
2589
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002590 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002591 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002592 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002593 rdev = btrfs_inode_rdev(leaf, inode_item);
2594
Josef Bacikaec74772008-07-24 12:12:38 -04002595 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002596 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002597cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002598 /*
2599 * try to precache a NULL acl entry for files that don't have
2600 * any xattrs or acls
2601 */
Li Zefan33345d012011-04-20 10:31:50 +08002602 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2603 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002604 if (!maybe_acls)
2605 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002606
Chris Mason39279cc2007-06-12 06:35:45 -04002607 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002608
Chris Mason39279cc2007-06-12 06:35:45 -04002609 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002610 case S_IFREG:
2611 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002612 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002613 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002614 inode->i_fop = &btrfs_file_operations;
2615 inode->i_op = &btrfs_file_inode_operations;
2616 break;
2617 case S_IFDIR:
2618 inode->i_fop = &btrfs_dir_file_operations;
2619 if (root == root->fs_info->tree_root)
2620 inode->i_op = &btrfs_dir_ro_inode_operations;
2621 else
2622 inode->i_op = &btrfs_dir_inode_operations;
2623 break;
2624 case S_IFLNK:
2625 inode->i_op = &btrfs_symlink_inode_operations;
2626 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002627 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002628 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002629 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002630 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002631 init_special_inode(inode, inode->i_mode, rdev);
2632 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002633 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002634
2635 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002636 return;
2637
2638make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002639 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002640 make_bad_inode(inode);
2641}
2642
Chris Masond352ac62008-09-29 15:18:18 -04002643/*
2644 * given a leaf and an inode, copy the inode fields into the leaf
2645 */
Chris Masone02119d2008-09-05 16:13:11 -04002646static void fill_inode_item(struct btrfs_trans_handle *trans,
2647 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002648 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002649 struct inode *inode)
2650{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002651 btrfs_set_inode_uid(leaf, item, i_uid_read(inode));
2652 btrfs_set_inode_gid(leaf, item, i_gid_read(inode));
Chris Masondbe674a2008-07-17 12:54:05 -04002653 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002654 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2655 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2656
2657 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2658 inode->i_atime.tv_sec);
2659 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2660 inode->i_atime.tv_nsec);
2661
2662 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2663 inode->i_mtime.tv_sec);
2664 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2665 inode->i_mtime.tv_nsec);
2666
2667 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2668 inode->i_ctime.tv_sec);
2669 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2670 inode->i_ctime.tv_nsec);
2671
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002672 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002673 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002674 btrfs_set_inode_sequence(leaf, item, inode->i_version);
Chris Masone02119d2008-09-05 16:13:11 -04002675 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002676 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002677 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f12011-05-11 15:26:06 -04002678 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002679}
2680
Chris Masond352ac62008-09-29 15:18:18 -04002681/*
2682 * copy everything in the in-memory inode into the btree.
2683 */
Chris Mason21151332011-11-10 20:39:08 -05002684static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002685 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002686{
2687 struct btrfs_inode_item *inode_item;
2688 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002689 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002690 int ret;
2691
2692 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002693 if (!path)
2694 return -ENOMEM;
2695
Chris Masonb9473432009-03-13 11:00:37 -04002696 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002697 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2698 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002699 if (ret) {
2700 if (ret > 0)
2701 ret = -ENOENT;
2702 goto failed;
2703 }
2704
Chris Masonb4ce94d2009-02-04 09:25:08 -05002705 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002706 leaf = path->nodes[0];
2707 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002708 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002709
Chris Masone02119d2008-09-05 16:13:11 -04002710 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002711 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002712 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002713 ret = 0;
2714failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002715 btrfs_free_path(path);
2716 return ret;
2717}
2718
Chris Masond352ac62008-09-29 15:18:18 -04002719/*
Chris Mason21151332011-11-10 20:39:08 -05002720 * copy everything in the in-memory inode into the btree.
2721 */
2722noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2723 struct btrfs_root *root, struct inode *inode)
2724{
2725 int ret;
2726
2727 /*
2728 * If the inode is a free space inode, we can deadlock during commit
2729 * if we put it into the delayed code.
2730 *
2731 * The data relocation inode should also be directly updated
2732 * without delay
2733 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002734 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002735 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002736 btrfs_update_root_times(trans, root);
2737
Chris Mason21151332011-11-10 20:39:08 -05002738 ret = btrfs_delayed_update_inode(trans, root, inode);
2739 if (!ret)
2740 btrfs_set_inode_last_trans(trans, inode);
2741 return ret;
2742 }
2743
2744 return btrfs_update_inode_item(trans, root, inode);
2745}
2746
Josef Bacikbe6aef62012-10-22 15:43:12 -04002747noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2748 struct btrfs_root *root,
2749 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002750{
2751 int ret;
2752
2753 ret = btrfs_update_inode(trans, root, inode);
2754 if (ret == -ENOSPC)
2755 return btrfs_update_inode_item(trans, root, inode);
2756 return ret;
2757}
2758
2759/*
Chris Masond352ac62008-09-29 15:18:18 -04002760 * unlink helper that gets used here in inode.c and in the tree logging
2761 * recovery code. It remove a link in a directory with a given name, and
2762 * also drops the back refs in the inode to the directory
2763 */
Al Viro92986792011-03-04 17:14:37 +00002764static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2765 struct btrfs_root *root,
2766 struct inode *dir, struct inode *inode,
2767 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002768{
2769 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002770 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002771 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002772 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002773 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002774 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002775 u64 ino = btrfs_ino(inode);
2776 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002777
2778 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002779 if (!path) {
2780 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002781 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002782 }
2783
Chris Masonb9473432009-03-13 11:00:37 -04002784 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002785 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002786 name, name_len, -1);
2787 if (IS_ERR(di)) {
2788 ret = PTR_ERR(di);
2789 goto err;
2790 }
2791 if (!di) {
2792 ret = -ENOENT;
2793 goto err;
2794 }
Chris Mason5f39d392007-10-15 16:14:19 -04002795 leaf = path->nodes[0];
2796 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002797 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002798 if (ret)
2799 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002800 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002801
Li Zefan33345d012011-04-20 10:31:50 +08002802 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2803 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002804 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002805 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002806 "inode %llu parent %llu\n", name_len, name,
2807 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002808 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002809 goto err;
2810 }
2811
Miao Xie16cdcec2011-04-22 18:12:22 +08002812 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002813 if (ret) {
2814 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002815 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002816 }
Chris Mason39279cc2007-06-12 06:35:45 -04002817
Chris Masone02119d2008-09-05 16:13:11 -04002818 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002819 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002820 if (ret != 0 && ret != -ENOENT) {
2821 btrfs_abort_transaction(trans, root, ret);
2822 goto err;
2823 }
Chris Masone02119d2008-09-05 16:13:11 -04002824
2825 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2826 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002827 if (ret == -ENOENT)
2828 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002829err:
2830 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002831 if (ret)
2832 goto out;
2833
2834 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002835 inode_inc_iversion(inode);
2836 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002837 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002838 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002839out:
Chris Mason39279cc2007-06-12 06:35:45 -04002840 return ret;
2841}
2842
Al Viro92986792011-03-04 17:14:37 +00002843int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2844 struct btrfs_root *root,
2845 struct inode *dir, struct inode *inode,
2846 const char *name, int name_len)
2847{
2848 int ret;
2849 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2850 if (!ret) {
2851 btrfs_drop_nlink(inode);
2852 ret = btrfs_update_inode(trans, root, inode);
2853 }
2854 return ret;
2855}
2856
2857
Yan, Zhenga22285a2010-05-16 10:48:46 -04002858/* helper to check if there is any shared block in the path */
2859static int check_path_shared(struct btrfs_root *root,
2860 struct btrfs_path *path)
2861{
2862 struct extent_buffer *eb;
2863 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002864 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002865
2866 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002867 int ret;
2868
Yan, Zhenga22285a2010-05-16 10:48:46 -04002869 if (!path->nodes[level])
2870 break;
2871 eb = path->nodes[level];
2872 if (!btrfs_block_can_be_shared(root, eb))
2873 continue;
2874 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2875 &refs, NULL);
2876 if (refs > 1)
2877 return 1;
2878 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002879 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002880}
2881
2882/*
2883 * helper to start transaction for unlink and rmdir.
2884 *
2885 * unlink and rmdir are special in btrfs, they do not always free space.
2886 * so in enospc case, we should make sure they will free space before
2887 * allowing them to use the global metadata reservation.
2888 */
2889static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2890 struct dentry *dentry)
2891{
2892 struct btrfs_trans_handle *trans;
2893 struct btrfs_root *root = BTRFS_I(dir)->root;
2894 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002895 struct btrfs_dir_item *di;
2896 struct inode *inode = dentry->d_inode;
2897 u64 index;
2898 int check_link = 1;
2899 int err = -ENOSPC;
2900 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002901 u64 ino = btrfs_ino(inode);
2902 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002903
Josef Bacike70bea52011-10-11 14:18:24 -04002904 /*
2905 * 1 for the possible orphan item
2906 * 1 for the dir item
2907 * 1 for the dir index
2908 * 1 for the inode ref
2909 * 1 for the inode ref in the tree log
2910 * 2 for the dir entries in the log
2911 * 1 for the inode
2912 */
2913 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002914 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2915 return trans;
2916
Li Zefan33345d012011-04-20 10:31:50 +08002917 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002918 return ERR_PTR(-ENOSPC);
2919
2920 /* check if there is someone else holds reference */
2921 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2922 return ERR_PTR(-ENOSPC);
2923
2924 if (atomic_read(&inode->i_count) > 2)
2925 return ERR_PTR(-ENOSPC);
2926
2927 if (xchg(&root->fs_info->enospc_unlink, 1))
2928 return ERR_PTR(-ENOSPC);
2929
2930 path = btrfs_alloc_path();
2931 if (!path) {
2932 root->fs_info->enospc_unlink = 0;
2933 return ERR_PTR(-ENOMEM);
2934 }
2935
Josef Bacik3880a1b2011-10-14 14:46:51 -04002936 /* 1 for the orphan item */
2937 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002938 if (IS_ERR(trans)) {
2939 btrfs_free_path(path);
2940 root->fs_info->enospc_unlink = 0;
2941 return trans;
2942 }
2943
2944 path->skip_locking = 1;
2945 path->search_commit_root = 1;
2946
2947 ret = btrfs_lookup_inode(trans, root, path,
2948 &BTRFS_I(dir)->location, 0);
2949 if (ret < 0) {
2950 err = ret;
2951 goto out;
2952 }
2953 if (ret == 0) {
2954 if (check_path_shared(root, path))
2955 goto out;
2956 } else {
2957 check_link = 0;
2958 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002959 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002960
2961 ret = btrfs_lookup_inode(trans, root, path,
2962 &BTRFS_I(inode)->location, 0);
2963 if (ret < 0) {
2964 err = ret;
2965 goto out;
2966 }
2967 if (ret == 0) {
2968 if (check_path_shared(root, path))
2969 goto out;
2970 } else {
2971 check_link = 0;
2972 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002973 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002974
2975 if (ret == 0 && S_ISREG(inode->i_mode)) {
2976 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002977 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002978 if (ret < 0) {
2979 err = ret;
2980 goto out;
2981 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002982 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002983 if (check_path_shared(root, path))
2984 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02002985 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002986 }
2987
2988 if (!check_link) {
2989 err = 0;
2990 goto out;
2991 }
2992
Li Zefan33345d012011-04-20 10:31:50 +08002993 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002994 dentry->d_name.name, dentry->d_name.len, 0);
2995 if (IS_ERR(di)) {
2996 err = PTR_ERR(di);
2997 goto out;
2998 }
2999 if (di) {
3000 if (check_path_shared(root, path))
3001 goto out;
3002 } else {
3003 err = 0;
3004 goto out;
3005 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003006 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003007
Mark Fashehf1863732012-08-08 11:32:27 -07003008 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3009 dentry->d_name.len, ino, dir_ino, 0,
3010 &index);
3011 if (ret) {
3012 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003013 goto out;
3014 }
Mark Fashehf1863732012-08-08 11:32:27 -07003015
Yan, Zhenga22285a2010-05-16 10:48:46 -04003016 if (check_path_shared(root, path))
3017 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003018
David Sterbab3b4aa72011-04-21 01:20:15 +02003019 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003020
Miao Xie16cdcec2011-04-22 18:12:22 +08003021 /*
3022 * This is a commit root search, if we can lookup inode item and other
3023 * relative items in the commit root, it means the transaction of
3024 * dir/file creation has been committed, and the dir index item that we
3025 * delay to insert has also been inserted into the commit root. So
3026 * we needn't worry about the delayed insertion of the dir index item
3027 * here.
3028 */
Li Zefan33345d012011-04-20 10:31:50 +08003029 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003030 dentry->d_name.name, dentry->d_name.len, 0);
3031 if (IS_ERR(di)) {
3032 err = PTR_ERR(di);
3033 goto out;
3034 }
3035 BUG_ON(ret == -ENOENT);
3036 if (check_path_shared(root, path))
3037 goto out;
3038
3039 err = 0;
3040out:
3041 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003042 /* Migrate the orphan reservation over */
3043 if (!err)
3044 err = btrfs_block_rsv_migrate(trans->block_rsv,
3045 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003046 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003047
Yan, Zhenga22285a2010-05-16 10:48:46 -04003048 if (err) {
3049 btrfs_end_transaction(trans, root);
3050 root->fs_info->enospc_unlink = 0;
3051 return ERR_PTR(err);
3052 }
3053
3054 trans->block_rsv = &root->fs_info->global_block_rsv;
3055 return trans;
3056}
3057
3058static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3059 struct btrfs_root *root)
3060{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003061 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003062 btrfs_block_rsv_release(root, trans->block_rsv,
3063 trans->bytes_reserved);
3064 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003065 BUG_ON(!root->fs_info->enospc_unlink);
3066 root->fs_info->enospc_unlink = 0;
3067 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003068 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003069}
3070
Chris Mason39279cc2007-06-12 06:35:45 -04003071static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3072{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003073 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003074 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003075 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003076 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05003077 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003078
Yan, Zhenga22285a2010-05-16 10:48:46 -04003079 trans = __unlink_start_trans(dir, dentry);
3080 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003081 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003082
Chris Mason12fcfd22009-03-24 10:24:20 -04003083 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3084
Chris Masone02119d2008-09-05 16:13:11 -04003085 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3086 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003087 if (ret)
3088 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003089
Yan, Zhenga22285a2010-05-16 10:48:46 -04003090 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003091 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003092 if (ret)
3093 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003094 }
Josef Bacik7b128762008-07-24 12:17:14 -04003095
Tsutomu Itohb5324022011-07-19 07:27:20 +00003096out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003097 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003098 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003099 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003100 return ret;
3101}
3102
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003103int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3104 struct btrfs_root *root,
3105 struct inode *dir, u64 objectid,
3106 const char *name, int name_len)
3107{
3108 struct btrfs_path *path;
3109 struct extent_buffer *leaf;
3110 struct btrfs_dir_item *di;
3111 struct btrfs_key key;
3112 u64 index;
3113 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003114 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003115
3116 path = btrfs_alloc_path();
3117 if (!path)
3118 return -ENOMEM;
3119
Li Zefan33345d012011-04-20 10:31:50 +08003120 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003121 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003122 if (IS_ERR_OR_NULL(di)) {
3123 if (!di)
3124 ret = -ENOENT;
3125 else
3126 ret = PTR_ERR(di);
3127 goto out;
3128 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003129
3130 leaf = path->nodes[0];
3131 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3132 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3133 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003134 if (ret) {
3135 btrfs_abort_transaction(trans, root, ret);
3136 goto out;
3137 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003138 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003139
3140 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3141 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003142 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003143 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003144 if (ret != -ENOENT) {
3145 btrfs_abort_transaction(trans, root, ret);
3146 goto out;
3147 }
Li Zefan33345d012011-04-20 10:31:50 +08003148 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003149 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003150 if (IS_ERR_OR_NULL(di)) {
3151 if (!di)
3152 ret = -ENOENT;
3153 else
3154 ret = PTR_ERR(di);
3155 btrfs_abort_transaction(trans, root, ret);
3156 goto out;
3157 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003158
3159 leaf = path->nodes[0];
3160 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003161 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003162 index = key.offset;
3163 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003164 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003165
Miao Xie16cdcec2011-04-22 18:12:22 +08003166 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003167 if (ret) {
3168 btrfs_abort_transaction(trans, root, ret);
3169 goto out;
3170 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003171
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003172 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003173 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003174 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003175 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003176 if (ret)
3177 btrfs_abort_transaction(trans, root, ret);
3178out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003179 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003180 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003181}
3182
Chris Mason39279cc2007-06-12 06:35:45 -04003183static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3184{
3185 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003186 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003187 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003188 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003189 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003190
David Sterbab3ae2442012-09-13 16:04:34 -06003191 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003192 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003193 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3194 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003195
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);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003199
Li Zefan33345d012011-04-20 10:31:50 +08003200 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003201 err = btrfs_unlink_subvol(trans, root, dir,
3202 BTRFS_I(inode)->location.objectid,
3203 dentry->d_name.name,
3204 dentry->d_name.len);
3205 goto out;
3206 }
3207
Josef Bacik7b128762008-07-24 12:17:14 -04003208 err = btrfs_orphan_add(trans, inode);
3209 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003210 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003211
Chris Mason39279cc2007-06-12 06:35:45 -04003212 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003213 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3214 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003215 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003216 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003217out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003218 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003219 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003220 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003221
Chris Mason39279cc2007-06-12 06:35:45 -04003222 return err;
3223}
3224
Chris Mason323ac952008-10-01 19:05:46 -04003225/*
Chris Mason39279cc2007-06-12 06:35:45 -04003226 * this can truncate away extent items, csum items and directory items.
3227 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003228 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003229 *
3230 * csum items that cross the new i_size are truncated to the new size
3231 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003232 *
3233 * min_type is the minimum key type to truncate down to. If set to 0, this
3234 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003235 */
Yan, Zheng80825102009-11-12 09:35:36 +00003236int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3237 struct btrfs_root *root,
3238 struct inode *inode,
3239 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003240{
Chris Mason39279cc2007-06-12 06:35:45 -04003241 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003242 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003243 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003244 struct btrfs_key key;
3245 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003246 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003247 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003248 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003249 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003250 u64 mask = root->sectorsize - 1;
3251 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003252 int found_extent;
3253 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003254 int pending_del_nr = 0;
3255 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003256 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003257 int ret;
3258 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003259 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003260
3261 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003262
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003263 path = btrfs_alloc_path();
3264 if (!path)
3265 return -ENOMEM;
3266 path->reada = -1;
3267
Josef Bacik5dc562c2012-08-17 13:14:17 -04003268 /*
3269 * We want to drop from the next block forward in case this new size is
3270 * not block aligned since we will be keeping the last block of the
3271 * extent just the way it is.
3272 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003273 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003274 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003275
Miao Xie16cdcec2011-04-22 18:12:22 +08003276 /*
3277 * This function is also used to drop the items in the log tree before
3278 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3279 * it is used to drop the loged items. So we shouldn't kill the delayed
3280 * items.
3281 */
3282 if (min_type == 0 && root == BTRFS_I(inode)->root)
3283 btrfs_kill_delayed_inode_items(inode);
3284
Li Zefan33345d012011-04-20 10:31:50 +08003285 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003286 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003287 key.type = (u8)-1;
3288
Chris Mason85e21ba2008-01-29 15:11:36 -05003289search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003290 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003291 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003292 if (ret < 0) {
3293 err = ret;
3294 goto out;
3295 }
Chris Masond3977122009-01-05 21:25:51 -05003296
Chris Mason85e21ba2008-01-29 15:11:36 -05003297 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003298 /* there are no items in the tree for us to truncate, we're
3299 * done
3300 */
Yan, Zheng80825102009-11-12 09:35:36 +00003301 if (path->slots[0] == 0)
3302 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003303 path->slots[0]--;
3304 }
3305
Chris Masond3977122009-01-05 21:25:51 -05003306 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003307 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003308 leaf = path->nodes[0];
3309 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3310 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003311
Li Zefan33345d012011-04-20 10:31:50 +08003312 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003313 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003314
Chris Mason85e21ba2008-01-29 15:11:36 -05003315 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003316 break;
3317
Chris Mason5f39d392007-10-15 16:14:19 -04003318 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003319 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003320 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003321 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003322 extent_type = btrfs_file_extent_type(leaf, fi);
3323 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003324 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003325 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003326 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003327 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003328 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003329 }
Yan008630c2007-11-07 13:31:09 -05003330 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003331 }
Yan, Zheng80825102009-11-12 09:35:36 +00003332 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003333 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003334 } else {
3335 if (item_end < new_size)
3336 break;
3337 if (found_key.offset >= new_size)
3338 del_item = 1;
3339 else
3340 del_item = 0;
3341 }
Chris Mason39279cc2007-06-12 06:35:45 -04003342 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003343 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003344 if (found_type != BTRFS_EXTENT_DATA_KEY)
3345 goto delete;
3346
3347 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003348 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003349 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003350 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003351 u64 orig_num_bytes =
3352 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003353 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003354 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003355 extent_num_bytes = extent_num_bytes &
3356 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003357 btrfs_set_file_extent_num_bytes(leaf, fi,
3358 extent_num_bytes);
3359 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003360 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003361 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003362 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003363 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003364 } else {
Chris Masondb945352007-10-15 16:15:53 -04003365 extent_num_bytes =
3366 btrfs_file_extent_disk_num_bytes(leaf,
3367 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003368 extent_offset = found_key.offset -
3369 btrfs_file_extent_offset(leaf, fi);
3370
Chris Mason39279cc2007-06-12 06:35:45 -04003371 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003372 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003373 if (extent_start != 0) {
3374 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003375 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003376 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003377 }
3378 }
Chris Mason90692182008-02-08 13:49:28 -05003379 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003380 /*
3381 * we can't truncate inline items that have had
3382 * special encodings
3383 */
3384 if (!del_item &&
3385 btrfs_file_extent_compression(leaf, fi) == 0 &&
3386 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3387 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003388 u32 size = new_size - found_key.offset;
3389
3390 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003391 inode_sub_bytes(inode, item_end + 1 -
3392 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003393 }
3394 size =
3395 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003396 btrfs_truncate_item(trans, root, path,
3397 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003398 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003399 inode_sub_bytes(inode, item_end + 1 -
3400 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003401 }
Chris Mason39279cc2007-06-12 06:35:45 -04003402 }
Chris Mason179e29e2007-11-01 11:28:41 -04003403delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003404 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003405 if (!pending_del_nr) {
3406 /* no pending yet, add ourselves */
3407 pending_del_slot = path->slots[0];
3408 pending_del_nr = 1;
3409 } else if (pending_del_nr &&
3410 path->slots[0] + 1 == pending_del_slot) {
3411 /* hop on the pending chunk */
3412 pending_del_nr++;
3413 pending_del_slot = path->slots[0];
3414 } else {
Chris Masond3977122009-01-05 21:25:51 -05003415 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003416 }
Chris Mason39279cc2007-06-12 06:35:45 -04003417 } else {
3418 break;
3419 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003420 if (found_extent && (root->ref_cows ||
3421 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003422 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003423 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003424 extent_num_bytes, 0,
3425 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003426 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003427 BUG_ON(ret);
3428 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003429
Yan, Zheng80825102009-11-12 09:35:36 +00003430 if (found_type == BTRFS_INODE_ITEM_KEY)
3431 break;
3432
3433 if (path->slots[0] == 0 ||
3434 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003435 if (pending_del_nr) {
3436 ret = btrfs_del_items(trans, root, path,
3437 pending_del_slot,
3438 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003439 if (ret) {
3440 btrfs_abort_transaction(trans,
3441 root, ret);
3442 goto error;
3443 }
Yan, Zheng80825102009-11-12 09:35:36 +00003444 pending_del_nr = 0;
3445 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003446 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003447 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003448 } else {
3449 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003450 }
Chris Mason39279cc2007-06-12 06:35:45 -04003451 }
Yan, Zheng80825102009-11-12 09:35:36 +00003452out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003453 if (pending_del_nr) {
3454 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3455 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003456 if (ret)
3457 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003458 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003459error:
Chris Mason39279cc2007-06-12 06:35:45 -04003460 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003461 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003462}
3463
Chris Masona52d9a82007-08-27 16:49:44 -04003464/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003465 * btrfs_truncate_page - read, zero a chunk and write a page
3466 * @inode - inode that we're zeroing
3467 * @from - the offset to start zeroing
3468 * @len - the length to zero, 0 to zero the entire range respective to the
3469 * offset
3470 * @front - zero up to the offset instead of from the offset on
3471 *
3472 * This will find the page for the "from" offset and cow the page and zero the
3473 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003474 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003475int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3476 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003477{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003478 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003479 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003480 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3481 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003482 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003483 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003484 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003485 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3486 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3487 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003488 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003489 int ret = 0;
3490 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003491 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003492
Josef Bacik2aaa6652012-08-29 14:27:18 -04003493 if ((offset & (blocksize - 1)) == 0 &&
3494 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003495 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003496 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003497 if (ret)
3498 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003499
3500 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003501again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003502 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003503 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003504 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003505 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003506 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003507
3508 page_start = page_offset(page);
3509 page_end = page_start + PAGE_CACHE_SIZE - 1;
3510
Chris Masona52d9a82007-08-27 16:49:44 -04003511 if (!PageUptodate(page)) {
3512 ret = btrfs_readpage(NULL, page);
3513 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003514 if (page->mapping != mapping) {
3515 unlock_page(page);
3516 page_cache_release(page);
3517 goto again;
3518 }
Chris Masona52d9a82007-08-27 16:49:44 -04003519 if (!PageUptodate(page)) {
3520 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003521 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003522 }
3523 }
Chris Mason211c17f2008-05-15 09:13:45 -04003524 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003525
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003526 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003527 set_page_extent_mapped(page);
3528
3529 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3530 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003531 unlock_extent_cached(io_tree, page_start, page_end,
3532 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003533 unlock_page(page);
3534 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003535 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003536 btrfs_put_ordered_extent(ordered);
3537 goto again;
3538 }
3539
Josef Bacik2ac55d42010-02-03 19:33:23 +00003540 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003541 EXTENT_DIRTY | EXTENT_DELALLOC |
3542 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003543 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003544
Josef Bacik2ac55d42010-02-03 19:33:23 +00003545 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3546 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003547 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003548 unlock_extent_cached(io_tree, page_start, page_end,
3549 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003550 goto out_unlock;
3551 }
3552
Chris Masone6dcd2d2008-07-17 12:53:50 -04003553 ret = 0;
3554 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003555 if (!len)
3556 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003557 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003558 if (front)
3559 memset(kaddr, 0, offset);
3560 else
3561 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003562 flush_dcache_page(page);
3563 kunmap(page);
3564 }
Chris Mason247e7432008-07-17 12:53:51 -04003565 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003566 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003567 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3568 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003569
Chris Mason89642222008-07-24 09:41:53 -04003570out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003571 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003572 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003573 unlock_page(page);
3574 page_cache_release(page);
3575out:
3576 return ret;
3577}
3578
Josef Bacik695a0d02011-03-04 15:46:53 -05003579/*
3580 * This function puts in dummy file extents for the area we're creating a hole
3581 * for. So if we are truncating this file to a larger size we need to insert
3582 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3583 * the range between oldsize and size
3584 */
Josef Bacika41ad392011-01-31 15:30:16 -05003585int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003586{
3587 struct btrfs_trans_handle *trans;
3588 struct btrfs_root *root = BTRFS_I(inode)->root;
3589 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003590 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003591 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003592 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003593 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003594 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003595 u64 block_end = (size + mask) & ~mask;
3596 u64 last_byte;
3597 u64 cur_offset;
3598 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003599 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003600
3601 if (size <= hole_start)
3602 return 0;
3603
Yan Zheng9036c102008-10-30 14:19:41 -04003604 while (1) {
3605 struct btrfs_ordered_extent *ordered;
3606 btrfs_wait_ordered_range(inode, hole_start,
3607 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003608 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003609 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003610 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3611 if (!ordered)
3612 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003613 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3614 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003615 btrfs_put_ordered_extent(ordered);
3616 }
3617
Yan Zheng9036c102008-10-30 14:19:41 -04003618 cur_offset = hole_start;
3619 while (1) {
3620 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3621 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003622 if (IS_ERR(em)) {
3623 err = PTR_ERR(em);
3624 break;
3625 }
Yan Zheng9036c102008-10-30 14:19:41 -04003626 last_byte = min(extent_map_end(em), block_end);
3627 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003628 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003629 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003630 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003631
Miao Xie36423202011-12-14 20:12:02 -05003632 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003633 if (IS_ERR(trans)) {
3634 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003635 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003636 }
Yan, Zheng80825102009-11-12 09:35:36 +00003637
Josef Bacik5dc562c2012-08-17 13:14:17 -04003638 err = btrfs_drop_extents(trans, root, inode,
3639 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003640 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003641 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003642 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003643 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003644 break;
Miao Xie5b397372011-09-11 10:52:24 -04003645 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003646
Yan Zheng9036c102008-10-30 14:19:41 -04003647 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003648 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003649 0, hole_size, 0, hole_size,
3650 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003651 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003652 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003653 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003654 break;
Miao Xie5b397372011-09-11 10:52:24 -04003655 }
Yan, Zheng80825102009-11-12 09:35:36 +00003656
Josef Bacik5dc562c2012-08-17 13:14:17 -04003657 btrfs_drop_extent_cache(inode, cur_offset,
3658 cur_offset + hole_size - 1, 0);
3659 hole_em = alloc_extent_map();
3660 if (!hole_em) {
3661 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3662 &BTRFS_I(inode)->runtime_flags);
3663 goto next;
3664 }
3665 hole_em->start = cur_offset;
3666 hole_em->len = hole_size;
3667 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003668
Josef Bacik5dc562c2012-08-17 13:14:17 -04003669 hole_em->block_start = EXTENT_MAP_HOLE;
3670 hole_em->block_len = 0;
3671 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3672 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3673 hole_em->generation = trans->transid;
3674
3675 while (1) {
3676 write_lock(&em_tree->lock);
3677 err = add_extent_mapping(em_tree, hole_em);
3678 if (!err)
3679 list_move(&hole_em->list,
3680 &em_tree->modified_extents);
3681 write_unlock(&em_tree->lock);
3682 if (err != -EEXIST)
3683 break;
3684 btrfs_drop_extent_cache(inode, cur_offset,
3685 cur_offset +
3686 hole_size - 1, 0);
3687 }
3688 free_extent_map(hole_em);
3689next:
Miao Xie36423202011-12-14 20:12:02 -05003690 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003691 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003692 }
3693 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003694 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003695 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003696 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003697 break;
3698 }
3699
Yan, Zhenga22285a2010-05-16 10:48:46 -04003700 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003701 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3702 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003703 return err;
3704}
3705
Josef Bacika41ad392011-01-31 15:30:16 -05003706static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003707{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003708 struct btrfs_root *root = BTRFS_I(inode)->root;
3709 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003710 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003711 int ret;
3712
Josef Bacika41ad392011-01-31 15:30:16 -05003713 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003714 return 0;
3715
Josef Bacika41ad392011-01-31 15:30:16 -05003716 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003717 truncate_pagecache(inode, oldsize, newsize);
3718 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003719 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003720 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003721
Miao Xief4a2f4c2011-12-14 20:12:01 -05003722 trans = btrfs_start_transaction(root, 1);
3723 if (IS_ERR(trans))
3724 return PTR_ERR(trans);
3725
3726 i_size_write(inode, newsize);
3727 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3728 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003729 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003730 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003731
Josef Bacika41ad392011-01-31 15:30:16 -05003732 /*
3733 * We're truncating a file that used to have good data down to
3734 * zero. Make sure it gets into the ordered flush list so that
3735 * any new writes get down to disk quickly.
3736 */
3737 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003738 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3739 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003740
Josef Bacika41ad392011-01-31 15:30:16 -05003741 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3742 truncate_setsize(inode, newsize);
3743 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003744 }
3745
Josef Bacika41ad392011-01-31 15:30:16 -05003746 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003747}
3748
Chris Mason39279cc2007-06-12 06:35:45 -04003749static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3750{
3751 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003752 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003753 int err;
3754
Li Zefanb83cc962010-12-20 16:04:08 +08003755 if (btrfs_root_readonly(root))
3756 return -EROFS;
3757
Chris Mason39279cc2007-06-12 06:35:45 -04003758 err = inode_change_ok(inode, attr);
3759 if (err)
3760 return err;
3761
Chris Mason5a3f23d2009-03-31 13:27:11 -04003762 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003763 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003764 if (err)
3765 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003766 }
Yan Zheng9036c102008-10-30 14:19:41 -04003767
Christoph Hellwig10257742010-06-04 11:30:02 +02003768 if (attr->ia_valid) {
3769 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003770 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003771 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003772
Josef Bacik22c44fe2011-11-30 10:45:38 -05003773 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003774 err = btrfs_acl_chmod(inode);
3775 }
3776
Chris Mason39279cc2007-06-12 06:35:45 -04003777 return err;
3778}
Chris Mason61295eb2008-01-14 16:24:38 -05003779
Al Virobd555972010-06-07 11:35:40 -04003780void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003781{
3782 struct btrfs_trans_handle *trans;
3783 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003784 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003785 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003786 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003787 int ret;
3788
liubo1abe9b82011-03-24 11:18:59 +00003789 trace_btrfs_inode_evict(inode);
3790
Chris Mason39279cc2007-06-12 06:35:45 -04003791 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003792 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003793 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003794 goto no_delete;
3795
Chris Mason39279cc2007-06-12 06:35:45 -04003796 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003797 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003798 goto no_delete;
3799 }
Al Virobd555972010-06-07 11:35:40 -04003800 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003801 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003802
Yan, Zhengc71bf092009-11-12 09:34:40 +00003803 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003804 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003805 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003806 goto no_delete;
3807 }
3808
Yan, Zheng76dda932009-09-21 16:00:26 -04003809 if (inode->i_nlink > 0) {
3810 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3811 goto no_delete;
3812 }
3813
Miao Xie66d8f3d2012-09-06 04:02:28 -06003814 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003815 if (!rsv) {
3816 btrfs_orphan_del(NULL, inode);
3817 goto no_delete;
3818 }
Josef Bacik4a338542011-08-29 11:01:31 -04003819 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003820 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003821 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003822
Chris Masondbe674a2008-07-17 12:54:05 -04003823 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003824
Josef Bacik4289a662011-08-05 13:22:24 -04003825 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003826 * This is a bit simpler than btrfs_truncate since we've already
3827 * reserved our space for our orphan item in the unlink, so we just
3828 * need to reserve some slack space in case we add bytes and update
3829 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003830 */
Yan, Zheng80825102009-11-12 09:35:36 +00003831 while (1) {
Miao Xieaa38a712011-11-18 17:43:00 +08003832 ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003833
Josef Bacik726c35f2011-09-26 15:46:06 -04003834 /*
3835 * Try and steal from the global reserve since we will
3836 * likely not use this space anyway, we want to try as
3837 * hard as possible to get this to work.
3838 */
3839 if (ret)
3840 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3841
Yan, Zhengd68fc572010-05-16 10:49:58 -04003842 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003843 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003844 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003845 btrfs_orphan_del(NULL, inode);
3846 btrfs_free_block_rsv(root, rsv);
3847 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003848 }
3849
Miao Xie8407aa42012-09-07 01:43:32 -06003850 trans = btrfs_start_transaction_noflush(root, 1);
Josef Bacik4289a662011-08-05 13:22:24 -04003851 if (IS_ERR(trans)) {
3852 btrfs_orphan_del(NULL, inode);
3853 btrfs_free_block_rsv(root, rsv);
3854 goto no_delete;
3855 }
3856
3857 trans->block_rsv = rsv;
3858
Yan, Zhengd68fc572010-05-16 10:49:58 -04003859 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04003860 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00003861 break;
3862
Miao Xie8407aa42012-09-07 01:43:32 -06003863 trans->block_rsv = &root->fs_info->trans_block_rsv;
3864 ret = btrfs_update_inode(trans, root, inode);
3865 BUG_ON(ret);
3866
Yan, Zheng80825102009-11-12 09:35:36 +00003867 nr = trans->blocks_used;
3868 btrfs_end_transaction(trans, root);
3869 trans = NULL;
3870 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003871 }
3872
Josef Bacik4289a662011-08-05 13:22:24 -04003873 btrfs_free_block_rsv(root, rsv);
3874
Yan, Zheng80825102009-11-12 09:35:36 +00003875 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003876 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003877 ret = btrfs_orphan_del(trans, inode);
3878 BUG_ON(ret);
3879 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003880
Josef Bacik4289a662011-08-05 13:22:24 -04003881 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003882 if (!(root == root->fs_info->tree_root ||
3883 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003884 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003885
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003886 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003887 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003888 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003889no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02003890 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003891 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003892}
3893
3894/*
3895 * this returns the key found in the dir entry in the location pointer.
3896 * If no dir entries were found, location->objectid is 0.
3897 */
3898static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3899 struct btrfs_key *location)
3900{
3901 const char *name = dentry->d_name.name;
3902 int namelen = dentry->d_name.len;
3903 struct btrfs_dir_item *di;
3904 struct btrfs_path *path;
3905 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003906 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003907
3908 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003909 if (!path)
3910 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003911
Li Zefan33345d012011-04-20 10:31:50 +08003912 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003913 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003914 if (IS_ERR(di))
3915 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003916
David Sterbac7040052011-04-19 18:00:01 +02003917 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003918 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003919
Chris Mason5f39d392007-10-15 16:14:19 -04003920 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003921out:
Chris Mason39279cc2007-06-12 06:35:45 -04003922 btrfs_free_path(path);
3923 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003924out_err:
3925 location->objectid = 0;
3926 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003927}
3928
3929/*
3930 * when we hit a tree root in a directory, the btrfs part of the inode
3931 * needs to be changed to reflect the root directory of the tree root. This
3932 * is kind of like crossing a mount point.
3933 */
3934static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003935 struct inode *dir,
3936 struct dentry *dentry,
3937 struct btrfs_key *location,
3938 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003939{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003940 struct btrfs_path *path;
3941 struct btrfs_root *new_root;
3942 struct btrfs_root_ref *ref;
3943 struct extent_buffer *leaf;
3944 int ret;
3945 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003946
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003947 path = btrfs_alloc_path();
3948 if (!path) {
3949 err = -ENOMEM;
3950 goto out;
3951 }
Chris Mason39279cc2007-06-12 06:35:45 -04003952
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003953 err = -ENOENT;
3954 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3955 BTRFS_I(dir)->root->root_key.objectid,
3956 location->objectid);
3957 if (ret) {
3958 if (ret < 0)
3959 err = ret;
3960 goto out;
3961 }
Chris Mason39279cc2007-06-12 06:35:45 -04003962
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003963 leaf = path->nodes[0];
3964 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003965 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003966 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3967 goto out;
3968
3969 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3970 (unsigned long)(ref + 1),
3971 dentry->d_name.len);
3972 if (ret)
3973 goto out;
3974
David Sterbab3b4aa72011-04-21 01:20:15 +02003975 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003976
3977 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3978 if (IS_ERR(new_root)) {
3979 err = PTR_ERR(new_root);
3980 goto out;
3981 }
3982
3983 if (btrfs_root_refs(&new_root->root_item) == 0) {
3984 err = -ENOENT;
3985 goto out;
3986 }
3987
3988 *sub_root = new_root;
3989 location->objectid = btrfs_root_dirid(&new_root->root_item);
3990 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003991 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003992 err = 0;
3993out:
3994 btrfs_free_path(path);
3995 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003996}
3997
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003998static void inode_tree_add(struct inode *inode)
3999{
4000 struct btrfs_root *root = BTRFS_I(inode)->root;
4001 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004002 struct rb_node **p;
4003 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004004 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004005again:
4006 p = &root->inode_tree.rb_node;
4007 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004008
Al Viro1d3382cb2010-10-23 15:19:20 -04004009 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004010 return;
4011
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004012 spin_lock(&root->inode_lock);
4013 while (*p) {
4014 parent = *p;
4015 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4016
Li Zefan33345d012011-04-20 10:31:50 +08004017 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004018 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004019 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004020 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004021 else {
4022 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004023 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004024 rb_erase(parent, &root->inode_tree);
4025 RB_CLEAR_NODE(parent);
4026 spin_unlock(&root->inode_lock);
4027 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004028 }
4029 }
4030 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4031 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4032 spin_unlock(&root->inode_lock);
4033}
4034
4035static void inode_tree_del(struct inode *inode)
4036{
4037 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004038 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004039
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004040 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004041 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004042 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004043 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004044 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004045 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004046 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004047
Josef Bacik0af3d002010-06-21 14:48:16 -04004048 /*
4049 * Free space cache has inodes in the tree root, but the tree root has a
4050 * root_refs of 0, so this could end up dropping the tree root as a
4051 * snapshot, so we need the extra !root->fs_info->tree_root check to
4052 * make sure we don't drop it.
4053 */
4054 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4055 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004056 synchronize_srcu(&root->fs_info->subvol_srcu);
4057 spin_lock(&root->inode_lock);
4058 empty = RB_EMPTY_ROOT(&root->inode_tree);
4059 spin_unlock(&root->inode_lock);
4060 if (empty)
4061 btrfs_add_dead_root(root);
4062 }
4063}
4064
Jeff Mahoney143bede2012-03-01 14:56:26 +01004065void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004066{
4067 struct rb_node *node;
4068 struct rb_node *prev;
4069 struct btrfs_inode *entry;
4070 struct inode *inode;
4071 u64 objectid = 0;
4072
4073 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4074
4075 spin_lock(&root->inode_lock);
4076again:
4077 node = root->inode_tree.rb_node;
4078 prev = NULL;
4079 while (node) {
4080 prev = node;
4081 entry = rb_entry(node, struct btrfs_inode, rb_node);
4082
Li Zefan33345d012011-04-20 10:31:50 +08004083 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004084 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004085 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004086 node = node->rb_right;
4087 else
4088 break;
4089 }
4090 if (!node) {
4091 while (prev) {
4092 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004093 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004094 node = prev;
4095 break;
4096 }
4097 prev = rb_next(prev);
4098 }
4099 }
4100 while (node) {
4101 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004102 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004103 inode = igrab(&entry->vfs_inode);
4104 if (inode) {
4105 spin_unlock(&root->inode_lock);
4106 if (atomic_read(&inode->i_count) > 1)
4107 d_prune_aliases(inode);
4108 /*
Al Viro45321ac2010-06-07 13:43:19 -04004109 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004110 * the inode cache when its usage count
4111 * hits zero.
4112 */
4113 iput(inode);
4114 cond_resched();
4115 spin_lock(&root->inode_lock);
4116 goto again;
4117 }
4118
4119 if (cond_resched_lock(&root->inode_lock))
4120 goto again;
4121
4122 node = rb_next(node);
4123 }
4124 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004125}
4126
Chris Masone02119d2008-09-05 16:13:11 -04004127static int btrfs_init_locked_inode(struct inode *inode, void *p)
4128{
4129 struct btrfs_iget_args *args = p;
4130 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004131 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004132 return 0;
4133}
4134
4135static int btrfs_find_actor(struct inode *inode, void *opaque)
4136{
4137 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004138 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004139 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004140}
4141
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004142static struct inode *btrfs_iget_locked(struct super_block *s,
4143 u64 objectid,
4144 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004145{
4146 struct inode *inode;
4147 struct btrfs_iget_args args;
4148 args.ino = objectid;
4149 args.root = root;
4150
4151 inode = iget5_locked(s, objectid, btrfs_find_actor,
4152 btrfs_init_locked_inode,
4153 (void *)&args);
4154 return inode;
4155}
4156
Balaji Rao1a54ef82008-07-21 02:01:04 +05304157/* Get an inode object given its location and corresponding root.
4158 * Returns in *is_new if the inode was read from disk
4159 */
4160struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004161 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304162{
4163 struct inode *inode;
4164
4165 inode = btrfs_iget_locked(s, location->objectid, root);
4166 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004167 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304168
4169 if (inode->i_state & I_NEW) {
4170 BTRFS_I(inode)->root = root;
4171 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4172 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004173 if (!is_bad_inode(inode)) {
4174 inode_tree_add(inode);
4175 unlock_new_inode(inode);
4176 if (new)
4177 *new = 1;
4178 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004179 unlock_new_inode(inode);
4180 iput(inode);
4181 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004182 }
4183 }
4184
Balaji Rao1a54ef82008-07-21 02:01:04 +05304185 return inode;
4186}
4187
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004188static struct inode *new_simple_dir(struct super_block *s,
4189 struct btrfs_key *key,
4190 struct btrfs_root *root)
4191{
4192 struct inode *inode = new_inode(s);
4193
4194 if (!inode)
4195 return ERR_PTR(-ENOMEM);
4196
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004197 BTRFS_I(inode)->root = root;
4198 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004199 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004200
4201 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004202 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004203 inode->i_fop = &simple_dir_operations;
4204 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4205 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4206
4207 return inode;
4208}
4209
Chris Mason3de45862008-11-17 21:02:50 -05004210struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004211{
Chris Masond3977122009-01-05 21:25:51 -05004212 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004213 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004214 struct btrfs_root *sub_root = root;
4215 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004216 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004217 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004218
4219 if (dentry->d_name.len > BTRFS_NAME_LEN)
4220 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004221
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004222 if (unlikely(d_need_lookup(dentry))) {
4223 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4224 kfree(dentry->d_fsdata);
4225 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004226 /* This thing is hashed, drop it for now */
4227 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004228 } else {
4229 ret = btrfs_inode_by_name(dir, dentry, &location);
4230 }
Chris Mason5f39d392007-10-15 16:14:19 -04004231
Chris Mason39279cc2007-06-12 06:35:45 -04004232 if (ret < 0)
4233 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004234
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004235 if (location.objectid == 0)
4236 return NULL;
4237
4238 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004239 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004240 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004241 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004242
4243 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4244
Yan, Zheng76dda932009-09-21 16:00:26 -04004245 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246 ret = fixup_tree_root_location(root, dir, dentry,
4247 &location, &sub_root);
4248 if (ret < 0) {
4249 if (ret != -ENOENT)
4250 inode = ERR_PTR(ret);
4251 else
4252 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4253 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004254 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004255 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004256 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4257
Julia Lawall34d19ba2011-01-24 19:55:19 +00004258 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004259 down_read(&root->fs_info->cleanup_work_sem);
4260 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004261 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004262 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004263 if (ret)
4264 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004265 }
4266
Chris Mason3de45862008-11-17 21:02:50 -05004267 return inode;
4268}
4269
Nick Pigginfe15ce42011-01-07 17:49:23 +11004270static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004271{
4272 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004273 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004274
Li Zefan848cce02012-02-21 17:04:28 +08004275 if (!inode && !IS_ROOT(dentry))
4276 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004277
Li Zefan848cce02012-02-21 17:04:28 +08004278 if (inode) {
4279 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004280 if (btrfs_root_refs(&root->root_item) == 0)
4281 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004282
4283 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4284 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004285 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004286 return 0;
4287}
4288
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004289static void btrfs_dentry_release(struct dentry *dentry)
4290{
4291 if (dentry->d_fsdata)
4292 kfree(dentry->d_fsdata);
4293}
4294
Chris Mason3de45862008-11-17 21:02:50 -05004295static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004296 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004297{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004298 struct dentry *ret;
4299
4300 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4301 if (unlikely(d_need_lookup(dentry))) {
4302 spin_lock(&dentry->d_lock);
4303 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4304 spin_unlock(&dentry->d_lock);
4305 }
4306 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004307}
4308
Miao Xie16cdcec2011-04-22 18:12:22 +08004309unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004310 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4311};
4312
David Woodhousecbdf5a22008-08-06 19:42:33 +01004313static int btrfs_real_readdir(struct file *filp, void *dirent,
4314 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004315{
Chris Mason6da6aba2007-12-18 16:15:09 -05004316 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004317 struct btrfs_root *root = BTRFS_I(inode)->root;
4318 struct btrfs_item *item;
4319 struct btrfs_dir_item *di;
4320 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004321 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004322 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004323 struct list_head ins_list;
4324 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004325 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004326 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004327 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004328 unsigned char d_type;
4329 int over = 0;
4330 u32 di_cur;
4331 u32 di_total;
4332 u32 di_len;
4333 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004334 char tmp_name[32];
4335 char *name_ptr;
4336 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004337 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004338
4339 /* FIXME, use a real flag for deciding about the key type */
4340 if (root->fs_info->tree_root == root)
4341 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004342
Chris Mason39544012007-12-12 14:38:19 -05004343 /* special case for "." */
4344 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004345 over = filldir(dirent, ".", 1,
4346 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004347 if (over)
4348 return 0;
4349 filp->f_pos = 1;
4350 }
Chris Mason39544012007-12-12 14:38:19 -05004351 /* special case for .., just use the back ref */
4352 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004353 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004354 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004355 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004356 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004357 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004358 filp->f_pos = 2;
4359 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004360 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004361 if (!path)
4362 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004363
Josef Bacik026fd312011-05-13 10:32:11 -04004364 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004365
Miao Xie16cdcec2011-04-22 18:12:22 +08004366 if (key_type == BTRFS_DIR_INDEX_KEY) {
4367 INIT_LIST_HEAD(&ins_list);
4368 INIT_LIST_HEAD(&del_list);
4369 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4370 }
4371
Chris Mason39279cc2007-06-12 06:35:45 -04004372 btrfs_set_key_type(&key, key_type);
4373 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004374 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004375
Chris Mason39279cc2007-06-12 06:35:45 -04004376 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4377 if (ret < 0)
4378 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004379
4380 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004381 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004382 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004383 if (slot >= btrfs_header_nritems(leaf)) {
4384 ret = btrfs_next_leaf(root, path);
4385 if (ret < 0)
4386 goto err;
4387 else if (ret > 0)
4388 break;
4389 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004390 }
Chris Mason3de45862008-11-17 21:02:50 -05004391
Chris Mason5f39d392007-10-15 16:14:19 -04004392 item = btrfs_item_nr(leaf, slot);
4393 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4394
4395 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004396 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004397 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004398 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004399 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004400 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004401 if (key_type == BTRFS_DIR_INDEX_KEY &&
4402 btrfs_should_delete_dir_index(&del_list,
4403 found_key.offset))
4404 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004405
4406 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004407 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004408
Chris Mason39279cc2007-06-12 06:35:45 -04004409 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4410 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004411 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004412
4413 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004414 struct btrfs_key location;
4415
Josef Bacik22a94d42011-03-16 16:47:17 -04004416 if (verify_dir_item(root, leaf, di))
4417 break;
4418
Chris Mason5f39d392007-10-15 16:14:19 -04004419 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004420 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004421 name_ptr = tmp_name;
4422 } else {
4423 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004424 if (!name_ptr) {
4425 ret = -ENOMEM;
4426 goto err;
4427 }
Chris Mason5f39d392007-10-15 16:14:19 -04004428 }
4429 read_extent_buffer(leaf, name_ptr,
4430 (unsigned long)(di + 1), name_len);
4431
4432 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4433 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004434
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004435
Chris Mason3de45862008-11-17 21:02:50 -05004436 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004437 * skip it.
4438 *
4439 * In contrast to old kernels, we insert the snapshot's
4440 * dir item and dir index after it has been created, so
4441 * we won't find a reference to our own snapshot. We
4442 * still keep the following code for backward
4443 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004444 */
4445 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4446 location.objectid == root->root_key.objectid) {
4447 over = 0;
4448 goto skip;
4449 }
Chris Mason5f39d392007-10-15 16:14:19 -04004450 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004451 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004452 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004453
Chris Mason3de45862008-11-17 21:02:50 -05004454skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004455 if (name_ptr != tmp_name)
4456 kfree(name_ptr);
4457
Chris Mason39279cc2007-06-12 06:35:45 -04004458 if (over)
4459 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004460 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004461 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004462 di_cur += di_len;
4463 di = (struct btrfs_dir_item *)((char *)di + di_len);
4464 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004465next:
4466 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004467 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004468
Miao Xie16cdcec2011-04-22 18:12:22 +08004469 if (key_type == BTRFS_DIR_INDEX_KEY) {
4470 if (is_curr)
4471 filp->f_pos++;
4472 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4473 &ins_list);
4474 if (ret)
4475 goto nopos;
4476 }
4477
David Woodhouse49593bf2008-08-17 17:08:36 +01004478 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004479 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004480 /*
4481 * 32-bit glibc will use getdents64, but then strtol -
4482 * so the last number we can serve is this.
4483 */
4484 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004485 else
4486 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004487nopos:
4488 ret = 0;
4489err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004490 if (key_type == BTRFS_DIR_INDEX_KEY)
4491 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004492 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004493 return ret;
4494}
4495
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004496int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004497{
4498 struct btrfs_root *root = BTRFS_I(inode)->root;
4499 struct btrfs_trans_handle *trans;
4500 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004501 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004502
Josef Bacik72ac3c02012-05-23 14:13:11 -04004503 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004504 return 0;
4505
Liu Bo83eea1f2012-07-10 05:28:39 -06004506 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004507 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004508
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004509 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004510 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004511 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004512 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004513 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004514 if (IS_ERR(trans))
4515 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004516 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004517 }
4518 return ret;
4519}
4520
4521/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004522 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004523 * inode changes. But, it is most likely to find the inode in cache.
4524 * FIXME, needs more benchmarking...there are no reasons other than performance
4525 * to keep or drop this code.
4526 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004527int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004528{
4529 struct btrfs_root *root = BTRFS_I(inode)->root;
4530 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004531 int ret;
4532
Josef Bacik72ac3c02012-05-23 14:13:11 -04004533 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004534 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004535
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004536 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004537 if (IS_ERR(trans))
4538 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004539
4540 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004541 if (ret && ret == -ENOSPC) {
4542 /* whoops, lets try again with the full transaction */
4543 btrfs_end_transaction(trans, root);
4544 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004545 if (IS_ERR(trans))
4546 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004547
Chris Mason94b60442010-05-26 11:02:00 -04004548 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004549 }
Chris Mason39279cc2007-06-12 06:35:45 -04004550 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004551 if (BTRFS_I(inode)->delayed_node)
4552 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004553
4554 return ret;
4555}
4556
4557/*
4558 * This is a copy of file_update_time. We need this so we can return error on
4559 * ENOSPC for updating the inode in the case of file write and mmap writes.
4560 */
Josef Bacike41f9412012-03-26 09:46:47 -04004561static int btrfs_update_time(struct inode *inode, struct timespec *now,
4562 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004563{
Alexander Block2bc5565282012-06-15 09:49:33 +02004564 struct btrfs_root *root = BTRFS_I(inode)->root;
4565
4566 if (btrfs_root_readonly(root))
4567 return -EROFS;
4568
Josef Bacike41f9412012-03-26 09:46:47 -04004569 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004570 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004571 if (flags & S_CTIME)
4572 inode->i_ctime = *now;
4573 if (flags & S_MTIME)
4574 inode->i_mtime = *now;
4575 if (flags & S_ATIME)
4576 inode->i_atime = *now;
4577 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004578}
4579
Chris Masond352ac62008-09-29 15:18:18 -04004580/*
4581 * find the highest existing sequence number in a directory
4582 * and then set the in-memory index_cnt variable to reflect
4583 * free sequence numbers
4584 */
Josef Bacikaec74772008-07-24 12:12:38 -04004585static int btrfs_set_inode_index_count(struct inode *inode)
4586{
4587 struct btrfs_root *root = BTRFS_I(inode)->root;
4588 struct btrfs_key key, found_key;
4589 struct btrfs_path *path;
4590 struct extent_buffer *leaf;
4591 int ret;
4592
Li Zefan33345d012011-04-20 10:31:50 +08004593 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004594 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4595 key.offset = (u64)-1;
4596
4597 path = btrfs_alloc_path();
4598 if (!path)
4599 return -ENOMEM;
4600
4601 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4602 if (ret < 0)
4603 goto out;
4604 /* FIXME: we should be able to handle this */
4605 if (ret == 0)
4606 goto out;
4607 ret = 0;
4608
4609 /*
4610 * MAGIC NUMBER EXPLANATION:
4611 * since we search a directory based on f_pos we have to start at 2
4612 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4613 * else has to start at 2
4614 */
4615 if (path->slots[0] == 0) {
4616 BTRFS_I(inode)->index_cnt = 2;
4617 goto out;
4618 }
4619
4620 path->slots[0]--;
4621
4622 leaf = path->nodes[0];
4623 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4624
Li Zefan33345d012011-04-20 10:31:50 +08004625 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004626 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4627 BTRFS_I(inode)->index_cnt = 2;
4628 goto out;
4629 }
4630
4631 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4632out:
4633 btrfs_free_path(path);
4634 return ret;
4635}
4636
Chris Masond352ac62008-09-29 15:18:18 -04004637/*
4638 * helper to find a free sequence number in a given directory. This current
4639 * code is very simple, later versions will do smarter things in the btree
4640 */
Chris Mason3de45862008-11-17 21:02:50 -05004641int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004642{
4643 int ret = 0;
4644
4645 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004646 ret = btrfs_inode_delayed_dir_index_count(dir);
4647 if (ret) {
4648 ret = btrfs_set_inode_index_count(dir);
4649 if (ret)
4650 return ret;
4651 }
Josef Bacikaec74772008-07-24 12:12:38 -04004652 }
4653
Chris Mason00e4e6b2008-08-05 11:18:09 -04004654 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004655 BTRFS_I(dir)->index_cnt++;
4656
4657 return ret;
4658}
4659
Chris Mason39279cc2007-06-12 06:35:45 -04004660static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4661 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004662 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004663 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004664 u64 ref_objectid, u64 objectid,
4665 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004666{
4667 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004668 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004669 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004670 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004671 struct btrfs_inode_ref *ref;
4672 struct btrfs_key key[2];
4673 u32 sizes[2];
4674 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004675 int ret;
4676 int owner;
4677
Chris Mason5f39d392007-10-15 16:14:19 -04004678 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004679 if (!path)
4680 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004681
Chris Mason39279cc2007-06-12 06:35:45 -04004682 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004683 if (!inode) {
4684 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004685 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004686 }
Chris Mason39279cc2007-06-12 06:35:45 -04004687
Li Zefan581bb052011-04-20 10:06:11 +08004688 /*
4689 * we have to initialize this early, so we can reclaim the inode
4690 * number if we fail afterwards in this function.
4691 */
4692 inode->i_ino = objectid;
4693
Josef Bacikaec74772008-07-24 12:12:38 -04004694 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004695 trace_btrfs_inode_request(dir);
4696
Chris Mason3de45862008-11-17 21:02:50 -05004697 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004698 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004699 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004700 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004701 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004702 }
Josef Bacikaec74772008-07-24 12:12:38 -04004703 }
4704 /*
4705 * index_cnt is ignored for everything but a dir,
4706 * btrfs_get_inode_index_count has an explanation for the magic
4707 * number
4708 */
4709 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004710 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004711 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004712 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04004713
Josef Bacik5dc562c2012-08-17 13:14:17 -04004714 /*
4715 * We could have gotten an inode number from somebody who was fsynced
4716 * and then removed in this same transaction, so let's just set full
4717 * sync since it will be a full sync anyway and this will blow away the
4718 * old info in the log.
4719 */
4720 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4721
Al Viro569254b2011-07-24 17:08:40 -04004722 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004723 owner = 0;
4724 else
4725 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004726
4727 key[0].objectid = objectid;
4728 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4729 key[0].offset = 0;
4730
Mark Fashehf1863732012-08-08 11:32:27 -07004731 /*
4732 * Start new inodes with an inode_ref. This is slightly more
4733 * efficient for small numbers of hard links since they will
4734 * be packed into one item. Extended refs will kick in if we
4735 * add more hard links than can fit in the ref item.
4736 */
Chris Mason9c583092008-01-29 15:15:18 -05004737 key[1].objectid = objectid;
4738 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4739 key[1].offset = ref_objectid;
4740
4741 sizes[0] = sizeof(struct btrfs_inode_item);
4742 sizes[1] = name_len + sizeof(*ref);
4743
Chris Masonb9473432009-03-13 11:00:37 -04004744 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004745 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4746 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004747 goto fail;
4748
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004749 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004750 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004751 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004752 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4753 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004754 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4755 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004756 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004757
4758 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4759 struct btrfs_inode_ref);
4760 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004761 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004762 ptr = (unsigned long)(ref + 1);
4763 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4764
Chris Mason5f39d392007-10-15 16:14:19 -04004765 btrfs_mark_buffer_dirty(path->nodes[0]);
4766 btrfs_free_path(path);
4767
Chris Mason39279cc2007-06-12 06:35:45 -04004768 location = &BTRFS_I(inode)->location;
4769 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004770 location->offset = 0;
4771 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4772
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004773 btrfs_inherit_iflags(inode, dir);
4774
Al Viro569254b2011-07-24 17:08:40 -04004775 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004776 if (btrfs_test_opt(root, NODATASUM))
4777 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004778 if (btrfs_test_opt(root, NODATACOW) ||
4779 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004780 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4781 }
4782
Chris Mason39279cc2007-06-12 06:35:45 -04004783 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004784 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004785
4786 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004787 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004788
Alexander Block8ea05e32012-07-25 17:35:53 +02004789 btrfs_update_root_times(trans, root);
4790
Chris Mason39279cc2007-06-12 06:35:45 -04004791 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004792fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004793 if (dir)
4794 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004795 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004796 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004797 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004798}
4799
4800static inline u8 btrfs_inode_type(struct inode *inode)
4801{
4802 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4803}
4804
Chris Masond352ac62008-09-29 15:18:18 -04004805/*
4806 * utility function to add 'inode' into 'parent_inode' with
4807 * a give name and a given sequence number.
4808 * if 'add_backref' is true, also insert a backref from the
4809 * inode to the parent directory.
4810 */
Chris Masone02119d2008-09-05 16:13:11 -04004811int btrfs_add_link(struct btrfs_trans_handle *trans,
4812 struct inode *parent_inode, struct inode *inode,
4813 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004814{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004815 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004816 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004817 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004818 u64 ino = btrfs_ino(inode);
4819 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004820
Li Zefan33345d012011-04-20 10:31:50 +08004821 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004822 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4823 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004824 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004825 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4826 key.offset = 0;
4827 }
Chris Mason39279cc2007-06-12 06:35:45 -04004828
Li Zefan33345d012011-04-20 10:31:50 +08004829 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004830 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4831 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004832 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004833 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004834 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4835 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004836 }
4837
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004838 /* Nothing to clean up yet */
4839 if (ret)
4840 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004841
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004842 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4843 parent_inode, &key,
4844 btrfs_inode_type(inode), index);
4845 if (ret == -EEXIST)
4846 goto fail_dir_item;
4847 else if (ret) {
4848 btrfs_abort_transaction(trans, root, ret);
4849 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004850 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004851
4852 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4853 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004854 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004855 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4856 ret = btrfs_update_inode(trans, root, parent_inode);
4857 if (ret)
4858 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004859 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004860
4861fail_dir_item:
4862 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4863 u64 local_index;
4864 int err;
4865 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4866 key.objectid, root->root_key.objectid,
4867 parent_ino, &local_index, name, name_len);
4868
4869 } else if (add_backref) {
4870 u64 local_index;
4871 int err;
4872
4873 err = btrfs_del_inode_ref(trans, root, name, name_len,
4874 ino, parent_ino, &local_index);
4875 }
4876 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004877}
4878
4879static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004880 struct inode *dir, struct dentry *dentry,
4881 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004882{
Josef Bacika1b075d2010-11-19 20:36:11 +00004883 int err = btrfs_add_link(trans, dir, inode,
4884 dentry->d_name.name, dentry->d_name.len,
4885 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004886 if (err > 0)
4887 err = -EEXIST;
4888 return err;
4889}
4890
Josef Bacik618e21d2007-07-11 10:18:17 -04004891static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004892 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004893{
4894 struct btrfs_trans_handle *trans;
4895 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004896 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004897 int err;
4898 int drop_inode = 0;
4899 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004900 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004901 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004902
4903 if (!new_valid_dev(rdev))
4904 return -EINVAL;
4905
Josef Bacik9ed74f22009-09-11 16:12:44 -04004906 /*
4907 * 2 for inode item and ref
4908 * 2 for dir items
4909 * 1 for xattr if selinux is on
4910 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004911 trans = btrfs_start_transaction(root, 5);
4912 if (IS_ERR(trans))
4913 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004914
Li Zefan581bb052011-04-20 10:06:11 +08004915 err = btrfs_find_free_ino(root, &objectid);
4916 if (err)
4917 goto out_unlock;
4918
Josef Bacikaec74772008-07-24 12:12:38 -04004919 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004920 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004921 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004922 if (IS_ERR(inode)) {
4923 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004924 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004925 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004926
Eric Paris2a7dba32011-02-01 11:05:39 -05004927 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004928 if (err) {
4929 drop_inode = 1;
4930 goto out_unlock;
4931 }
4932
Casey Schauflerad19db72011-12-15 10:09:07 -05004933 /*
4934 * If the active LSM wants to access the inode during
4935 * d_instantiate it needs these. Smack checks to see
4936 * if the filesystem supports xattrs by looking at the
4937 * ops vector.
4938 */
4939
4940 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004941 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004942 if (err)
4943 drop_inode = 1;
4944 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004945 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004946 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05004947 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004948 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004949out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004950 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004951 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004952 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004953 if (drop_inode) {
4954 inode_dec_link_count(inode);
4955 iput(inode);
4956 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004957 return err;
4958}
4959
Chris Mason39279cc2007-06-12 06:35:45 -04004960static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04004961 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04004962{
4963 struct btrfs_trans_handle *trans;
4964 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004965 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004966 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004967 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004968 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004969 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004970 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004971
Josef Bacik9ed74f22009-09-11 16:12:44 -04004972 /*
4973 * 2 for inode item and ref
4974 * 2 for dir items
4975 * 1 for xattr if selinux is on
4976 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004977 trans = btrfs_start_transaction(root, 5);
4978 if (IS_ERR(trans))
4979 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004980
Li Zefan581bb052011-04-20 10:06:11 +08004981 err = btrfs_find_free_ino(root, &objectid);
4982 if (err)
4983 goto out_unlock;
4984
Josef Bacikaec74772008-07-24 12:12:38 -04004985 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004986 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004987 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004988 if (IS_ERR(inode)) {
4989 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004990 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004991 }
Chris Mason39279cc2007-06-12 06:35:45 -04004992
Eric Paris2a7dba32011-02-01 11:05:39 -05004993 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004994 if (err) {
4995 drop_inode = 1;
4996 goto out_unlock;
4997 }
4998
Casey Schauflerad19db72011-12-15 10:09:07 -05004999 /*
5000 * If the active LSM wants to access the inode during
5001 * d_instantiate it needs these. Smack checks to see
5002 * if the filesystem supports xattrs by looking at the
5003 * ops vector.
5004 */
5005 inode->i_fop = &btrfs_file_operations;
5006 inode->i_op = &btrfs_file_inode_operations;
5007
Josef Bacika1b075d2010-11-19 20:36:11 +00005008 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005009 if (err)
5010 drop_inode = 1;
5011 else {
5012 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005013 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05005014 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro08c422c2011-12-23 07:58:13 -05005015 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005016 }
Chris Mason39279cc2007-06-12 06:35:45 -04005017out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005018 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005019 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005020 if (drop_inode) {
5021 inode_dec_link_count(inode);
5022 iput(inode);
5023 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005024 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005025 return err;
5026}
5027
5028static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5029 struct dentry *dentry)
5030{
5031 struct btrfs_trans_handle *trans;
5032 struct btrfs_root *root = BTRFS_I(dir)->root;
5033 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005034 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05005035 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005036 int err;
5037 int drop_inode = 0;
5038
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005039 /* do not allow sys_link's with other subvols of the same device */
5040 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005041 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005042
Mark Fashehf1863732012-08-08 11:32:27 -07005043 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005044 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005045
Chris Mason3de45862008-11-17 21:02:50 -05005046 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005047 if (err)
5048 goto fail;
5049
Yan, Zhenga22285a2010-05-16 10:48:46 -04005050 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005051 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005052 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005053 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005054 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005055 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005056 if (IS_ERR(trans)) {
5057 err = PTR_ERR(trans);
5058 goto fail;
5059 }
Chris Mason5f39d392007-10-15 16:14:19 -04005060
Miao Xie31534952011-04-13 13:19:21 +08005061 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005062 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005063 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005064 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005065
Josef Bacika1b075d2010-11-19 20:36:11 +00005066 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005067
Yan, Zhenga5719522009-09-24 09:17:31 -04005068 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005069 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005070 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005071 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005072 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005073 if (err)
5074 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005075 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005076 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005077 }
Chris Mason39279cc2007-06-12 06:35:45 -04005078
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005079 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005080 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005081fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005082 if (drop_inode) {
5083 inode_dec_link_count(inode);
5084 iput(inode);
5085 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005086 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005087 return err;
5088}
5089
Al Viro18bb1db2011-07-26 01:41:39 -04005090static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005091{
Chris Masonb9d86662008-05-02 16:13:49 -04005092 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005093 struct btrfs_trans_handle *trans;
5094 struct btrfs_root *root = BTRFS_I(dir)->root;
5095 int err = 0;
5096 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005097 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005098 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005099 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005100
Josef Bacik9ed74f22009-09-11 16:12:44 -04005101 /*
5102 * 2 items for inode and ref
5103 * 2 items for dir items
5104 * 1 for xattr if selinux is on
5105 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005106 trans = btrfs_start_transaction(root, 5);
5107 if (IS_ERR(trans))
5108 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005109
Li Zefan581bb052011-04-20 10:06:11 +08005110 err = btrfs_find_free_ino(root, &objectid);
5111 if (err)
5112 goto out_fail;
5113
Josef Bacikaec74772008-07-24 12:12:38 -04005114 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005115 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005116 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005117 if (IS_ERR(inode)) {
5118 err = PTR_ERR(inode);
5119 goto out_fail;
5120 }
Chris Mason5f39d392007-10-15 16:14:19 -04005121
Chris Mason39279cc2007-06-12 06:35:45 -04005122 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005123
Eric Paris2a7dba32011-02-01 11:05:39 -05005124 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005125 if (err)
5126 goto out_fail;
5127
Chris Mason39279cc2007-06-12 06:35:45 -04005128 inode->i_op = &btrfs_dir_inode_operations;
5129 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005130
Chris Masondbe674a2008-07-17 12:54:05 -04005131 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005132 err = btrfs_update_inode(trans, root, inode);
5133 if (err)
5134 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005135
Josef Bacika1b075d2010-11-19 20:36:11 +00005136 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5137 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005138 if (err)
5139 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005140
Chris Mason39279cc2007-06-12 06:35:45 -04005141 d_instantiate(dentry, inode);
5142 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005143
5144out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005145 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005146 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005147 if (drop_on_err)
5148 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005149 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005150 return err;
5151}
5152
Chris Masond352ac62008-09-29 15:18:18 -04005153/* helper for btfs_get_extent. Given an existing extent in the tree,
5154 * and an extent that you want to insert, deal with overlap and insert
5155 * the new extent into the tree.
5156 */
Chris Mason3b951512008-04-17 11:29:12 -04005157static int merge_extent_mapping(struct extent_map_tree *em_tree,
5158 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005159 struct extent_map *em,
5160 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005161{
5162 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005163
Chris Masone6dcd2d2008-07-17 12:53:50 -04005164 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5165 start_diff = map_start - em->start;
5166 em->start = map_start;
5167 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005168 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5169 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005170 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005171 em->block_len -= start_diff;
5172 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005173 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005174}
5175
Chris Masonc8b97812008-10-29 14:49:59 -04005176static noinline int uncompress_inline(struct btrfs_path *path,
5177 struct inode *inode, struct page *page,
5178 size_t pg_offset, u64 extent_offset,
5179 struct btrfs_file_extent_item *item)
5180{
5181 int ret;
5182 struct extent_buffer *leaf = path->nodes[0];
5183 char *tmp;
5184 size_t max_size;
5185 unsigned long inline_size;
5186 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005187 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005188
5189 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005190 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005191 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5192 inline_size = btrfs_file_extent_inline_item_len(leaf,
5193 btrfs_item_nr(leaf, path->slots[0]));
5194 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005195 if (!tmp)
5196 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005197 ptr = btrfs_file_extent_inline_start(item);
5198
5199 read_extent_buffer(leaf, tmp, ptr, inline_size);
5200
Chris Mason5b050f02008-11-11 09:34:41 -05005201 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005202 ret = btrfs_decompress(compress_type, tmp, page,
5203 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005204 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005205 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005206 unsigned long copy_size = min_t(u64,
5207 PAGE_CACHE_SIZE - pg_offset,
5208 max_size - extent_offset);
5209 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005210 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005211 }
5212 kfree(tmp);
5213 return 0;
5214}
5215
Chris Masond352ac62008-09-29 15:18:18 -04005216/*
5217 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005218 * the ugly parts come from merging extents from the disk with the in-ram
5219 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005220 * where the in-ram extents might be locked pending data=ordered completion.
5221 *
5222 * This also copies inline extents directly into the page.
5223 */
Chris Masond3977122009-01-05 21:25:51 -05005224
Chris Masona52d9a82007-08-27 16:49:44 -04005225struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005226 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005227 int create)
5228{
5229 int ret;
5230 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005231 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005232 u64 extent_start = 0;
5233 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005234 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005235 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005236 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005237 struct btrfs_root *root = BTRFS_I(inode)->root;
5238 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005239 struct extent_buffer *leaf;
5240 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005241 struct extent_map *em = NULL;
5242 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005243 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005244 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005245 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005246
Chris Masona52d9a82007-08-27 16:49:44 -04005247again:
Chris Mason890871b2009-09-02 16:24:52 -04005248 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005249 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005250 if (em)
5251 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005252 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005253
Chris Masona52d9a82007-08-27 16:49:44 -04005254 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005255 if (em->start > start || em->start + em->len <= start)
5256 free_extent_map(em);
5257 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005258 free_extent_map(em);
5259 else
5260 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005261 }
David Sterba172ddd62011-04-21 00:48:27 +02005262 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005263 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005264 err = -ENOMEM;
5265 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005266 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005267 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005268 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005269 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005270 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005271 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005272
5273 if (!path) {
5274 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005275 if (!path) {
5276 err = -ENOMEM;
5277 goto out;
5278 }
5279 /*
5280 * Chances are we'll be called again, so go ahead and do
5281 * readahead
5282 */
5283 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005284 }
5285
Chris Mason179e29e2007-11-01 11:28:41 -04005286 ret = btrfs_lookup_file_extent(trans, root, path,
5287 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005288 if (ret < 0) {
5289 err = ret;
5290 goto out;
5291 }
5292
5293 if (ret != 0) {
5294 if (path->slots[0] == 0)
5295 goto not_found;
5296 path->slots[0]--;
5297 }
5298
Chris Mason5f39d392007-10-15 16:14:19 -04005299 leaf = path->nodes[0];
5300 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005301 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005302 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005303 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5304 found_type = btrfs_key_type(&found_key);
5305 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005306 found_type != BTRFS_EXTENT_DATA_KEY) {
5307 goto not_found;
5308 }
5309
Chris Mason5f39d392007-10-15 16:14:19 -04005310 found_type = btrfs_file_extent_type(leaf, item);
5311 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005312 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005313 if (found_type == BTRFS_FILE_EXTENT_REG ||
5314 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005315 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005316 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005317 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5318 size_t size;
5319 size = btrfs_file_extent_inline_len(leaf, item);
5320 extent_end = (extent_start + size + root->sectorsize - 1) &
5321 ~((u64)root->sectorsize - 1);
5322 }
5323
5324 if (start >= extent_end) {
5325 path->slots[0]++;
5326 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5327 ret = btrfs_next_leaf(root, path);
5328 if (ret < 0) {
5329 err = ret;
5330 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005331 }
Yan Zheng9036c102008-10-30 14:19:41 -04005332 if (ret > 0)
5333 goto not_found;
5334 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005335 }
Yan Zheng9036c102008-10-30 14:19:41 -04005336 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5337 if (found_key.objectid != objectid ||
5338 found_key.type != BTRFS_EXTENT_DATA_KEY)
5339 goto not_found;
5340 if (start + len <= found_key.offset)
5341 goto not_found;
5342 em->start = start;
5343 em->len = found_key.offset - start;
5344 goto not_found_em;
5345 }
5346
Yan Zhengd899e052008-10-30 14:25:28 -04005347 if (found_type == BTRFS_FILE_EXTENT_REG ||
5348 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005349 em->start = extent_start;
5350 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005351 em->orig_start = extent_start -
5352 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005353 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5354 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005355 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005356 goto insert;
5357 }
Li Zefan261507a02010-12-17 14:21:50 +08005358 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005359 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005360 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005361 em->block_start = bytenr;
5362 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5363 item);
5364 } else {
5365 bytenr += btrfs_file_extent_offset(leaf, item);
5366 em->block_start = bytenr;
5367 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005368 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5369 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005370 }
Chris Masona52d9a82007-08-27 16:49:44 -04005371 goto insert;
5372 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005373 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005374 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005375 size_t size;
5376 size_t extent_offset;
5377 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005378
Chris Masona52d9a82007-08-27 16:49:44 -04005379 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005380 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005381 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005382 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005383 goto out;
5384 }
5385
Yan Zheng9036c102008-10-30 14:19:41 -04005386 size = btrfs_file_extent_inline_len(leaf, item);
5387 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005388 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005389 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005390 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005391 em->len = (copy_size + root->sectorsize - 1) &
5392 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005393 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005394 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005395 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005396 em->compress_type = compress_type;
5397 }
Yan689f9342007-10-29 11:41:07 -04005398 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005399 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005400 if (btrfs_file_extent_compression(leaf, item) !=
5401 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005402 ret = uncompress_inline(path, inode, page,
5403 pg_offset,
5404 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005405 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005406 } else {
5407 map = kmap(page);
5408 read_extent_buffer(leaf, map + pg_offset, ptr,
5409 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005410 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5411 memset(map + pg_offset + copy_size, 0,
5412 PAGE_CACHE_SIZE - pg_offset -
5413 copy_size);
5414 }
Chris Masonc8b97812008-10-29 14:49:59 -04005415 kunmap(page);
5416 }
Chris Mason179e29e2007-11-01 11:28:41 -04005417 flush_dcache_page(page);
5418 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005419 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005420 if (!trans) {
5421 kunmap(page);
5422 free_extent_map(em);
5423 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005424
David Sterbab3b4aa72011-04-21 01:20:15 +02005425 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005426 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005427
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005428 if (IS_ERR(trans))
5429 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005430 goto again;
5431 }
Chris Masonc8b97812008-10-29 14:49:59 -04005432 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005433 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005434 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005435 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005436 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005437 }
Chris Masond1310b22008-01-24 16:13:08 -05005438 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005439 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005440 goto insert;
5441 } else {
Chris Masond3977122009-01-05 21:25:51 -05005442 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005443 WARN_ON(1);
5444 }
5445not_found:
5446 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005447 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005448not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005449 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005450 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005451insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005452 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005453 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005454 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5455 "[%llu %llu]\n", (unsigned long long)em->start,
5456 (unsigned long long)em->len,
5457 (unsigned long long)start,
5458 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005459 err = -EIO;
5460 goto out;
5461 }
Chris Masond1310b22008-01-24 16:13:08 -05005462
5463 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005464 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005465 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005466 /* it is possible that someone inserted the extent into the tree
5467 * while we had the lock dropped. It is also possible that
5468 * an overlapping map exists in the tree
5469 */
Chris Masona52d9a82007-08-27 16:49:44 -04005470 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005471 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005472
5473 ret = 0;
5474
Chris Mason3b951512008-04-17 11:29:12 -04005475 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005476 if (existing && (existing->start > start ||
5477 existing->start + existing->len <= start)) {
5478 free_extent_map(existing);
5479 existing = NULL;
5480 }
Chris Mason3b951512008-04-17 11:29:12 -04005481 if (!existing) {
5482 existing = lookup_extent_mapping(em_tree, em->start,
5483 em->len);
5484 if (existing) {
5485 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005486 em, start,
5487 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005488 free_extent_map(existing);
5489 if (err) {
5490 free_extent_map(em);
5491 em = NULL;
5492 }
5493 } else {
5494 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005495 free_extent_map(em);
5496 em = NULL;
5497 }
5498 } else {
5499 free_extent_map(em);
5500 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005501 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005502 }
Chris Masona52d9a82007-08-27 16:49:44 -04005503 }
Chris Mason890871b2009-09-02 16:24:52 -04005504 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005505out:
liubo1abe9b82011-03-24 11:18:59 +00005506
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005507 if (em)
5508 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005509
Chris Masonf4219502008-07-22 11:18:09 -04005510 if (path)
5511 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005512 if (trans) {
5513 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005514 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005515 err = ret;
5516 }
Chris Masona52d9a82007-08-27 16:49:44 -04005517 if (err) {
5518 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005519 return ERR_PTR(err);
5520 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005521 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005522 return em;
5523}
5524
Chris Masonec29ed52011-02-23 16:23:20 -05005525struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5526 size_t pg_offset, u64 start, u64 len,
5527 int create)
5528{
5529 struct extent_map *em;
5530 struct extent_map *hole_em = NULL;
5531 u64 range_start = start;
5532 u64 end;
5533 u64 found;
5534 u64 found_end;
5535 int err = 0;
5536
5537 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5538 if (IS_ERR(em))
5539 return em;
5540 if (em) {
5541 /*
5542 * if our em maps to a hole, there might
5543 * actually be delalloc bytes behind it
5544 */
5545 if (em->block_start != EXTENT_MAP_HOLE)
5546 return em;
5547 else
5548 hole_em = em;
5549 }
5550
5551 /* check to see if we've wrapped (len == -1 or similar) */
5552 end = start + len;
5553 if (end < start)
5554 end = (u64)-1;
5555 else
5556 end -= 1;
5557
5558 em = NULL;
5559
5560 /* ok, we didn't find anything, lets look for delalloc */
5561 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5562 end, len, EXTENT_DELALLOC, 1);
5563 found_end = range_start + found;
5564 if (found_end < range_start)
5565 found_end = (u64)-1;
5566
5567 /*
5568 * we didn't find anything useful, return
5569 * the original results from get_extent()
5570 */
5571 if (range_start > end || found_end <= start) {
5572 em = hole_em;
5573 hole_em = NULL;
5574 goto out;
5575 }
5576
5577 /* adjust the range_start to make sure it doesn't
5578 * go backwards from the start they passed in
5579 */
5580 range_start = max(start,range_start);
5581 found = found_end - range_start;
5582
5583 if (found > 0) {
5584 u64 hole_start = start;
5585 u64 hole_len = len;
5586
David Sterba172ddd62011-04-21 00:48:27 +02005587 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005588 if (!em) {
5589 err = -ENOMEM;
5590 goto out;
5591 }
5592 /*
5593 * when btrfs_get_extent can't find anything it
5594 * returns one huge hole
5595 *
5596 * make sure what it found really fits our range, and
5597 * adjust to make sure it is based on the start from
5598 * the caller
5599 */
5600 if (hole_em) {
5601 u64 calc_end = extent_map_end(hole_em);
5602
5603 if (calc_end <= start || (hole_em->start > end)) {
5604 free_extent_map(hole_em);
5605 hole_em = NULL;
5606 } else {
5607 hole_start = max(hole_em->start, start);
5608 hole_len = calc_end - hole_start;
5609 }
5610 }
5611 em->bdev = NULL;
5612 if (hole_em && range_start > hole_start) {
5613 /* our hole starts before our delalloc, so we
5614 * have to return just the parts of the hole
5615 * that go until the delalloc starts
5616 */
5617 em->len = min(hole_len,
5618 range_start - hole_start);
5619 em->start = hole_start;
5620 em->orig_start = hole_start;
5621 /*
5622 * don't adjust block start at all,
5623 * it is fixed at EXTENT_MAP_HOLE
5624 */
5625 em->block_start = hole_em->block_start;
5626 em->block_len = hole_len;
5627 } else {
5628 em->start = range_start;
5629 em->len = found;
5630 em->orig_start = range_start;
5631 em->block_start = EXTENT_MAP_DELALLOC;
5632 em->block_len = found;
5633 }
5634 } else if (hole_em) {
5635 return hole_em;
5636 }
5637out:
5638
5639 free_extent_map(hole_em);
5640 if (err) {
5641 free_extent_map(em);
5642 return ERR_PTR(err);
5643 }
5644 return em;
5645}
5646
Josef Bacik4b46fce2010-05-23 11:00:55 -04005647static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005648 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005649 u64 start, u64 len)
5650{
5651 struct btrfs_root *root = BTRFS_I(inode)->root;
5652 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005653 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5654 struct btrfs_key ins;
5655 u64 alloc_hint;
5656 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005657 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005658
Josef Bacik16d299a2011-04-06 14:53:07 -04005659 /*
5660 * Ok if the extent map we looked up is a hole and is for the exact
5661 * range we want, there is no reason to allocate a new one, however if
5662 * it is not right then we need to free this one and drop the cache for
5663 * our range.
5664 */
5665 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5666 em->len != len) {
5667 free_extent_map(em);
5668 em = NULL;
5669 insert = true;
5670 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5671 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005672
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005673 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005674 if (IS_ERR(trans))
5675 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005676
Chris Mason4cb53002011-05-24 15:35:30 -04005677 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5678 btrfs_add_inode_defrag(trans, inode);
5679
Josef Bacik4b46fce2010-05-23 11:00:55 -04005680 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5681
5682 alloc_hint = get_extent_allocation_hint(inode, start, len);
5683 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005684 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005685 if (ret) {
5686 em = ERR_PTR(ret);
5687 goto out;
5688 }
5689
Josef Bacik4b46fce2010-05-23 11:00:55 -04005690 if (!em) {
David Sterba172ddd62011-04-21 00:48:27 +02005691 em = alloc_extent_map();
Josef Bacik16d299a2011-04-06 14:53:07 -04005692 if (!em) {
5693 em = ERR_PTR(-ENOMEM);
5694 goto out;
5695 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005696 }
5697
5698 em->start = start;
5699 em->orig_start = em->start;
5700 em->len = ins.offset;
5701
5702 em->block_start = ins.objectid;
5703 em->block_len = ins.offset;
5704 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005705
5706 /*
5707 * We need to do this because if we're using the original em we searched
5708 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5709 */
5710 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005711 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5712
Josef Bacik16d299a2011-04-06 14:53:07 -04005713 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005714 write_lock(&em_tree->lock);
5715 ret = add_extent_mapping(em_tree, em);
5716 write_unlock(&em_tree->lock);
5717 if (ret != -EEXIST)
5718 break;
5719 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5720 }
5721
5722 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5723 ins.offset, ins.offset, 0);
5724 if (ret) {
5725 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5726 em = ERR_PTR(ret);
5727 }
5728out:
5729 btrfs_end_transaction(trans, root);
5730 return em;
5731}
5732
Chris Mason46bfbb52010-05-26 11:04:10 -04005733/*
5734 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5735 * block must be cow'd
5736 */
5737static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5738 struct inode *inode, u64 offset, u64 len)
5739{
5740 struct btrfs_path *path;
5741 int ret;
5742 struct extent_buffer *leaf;
5743 struct btrfs_root *root = BTRFS_I(inode)->root;
5744 struct btrfs_file_extent_item *fi;
5745 struct btrfs_key key;
5746 u64 disk_bytenr;
5747 u64 backref_offset;
5748 u64 extent_end;
5749 u64 num_bytes;
5750 int slot;
5751 int found_type;
5752
5753 path = btrfs_alloc_path();
5754 if (!path)
5755 return -ENOMEM;
5756
Li Zefan33345d012011-04-20 10:31:50 +08005757 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005758 offset, 0);
5759 if (ret < 0)
5760 goto out;
5761
5762 slot = path->slots[0];
5763 if (ret == 1) {
5764 if (slot == 0) {
5765 /* can't find the item, must cow */
5766 ret = 0;
5767 goto out;
5768 }
5769 slot--;
5770 }
5771 ret = 0;
5772 leaf = path->nodes[0];
5773 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005774 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005775 key.type != BTRFS_EXTENT_DATA_KEY) {
5776 /* not our file or wrong item type, must cow */
5777 goto out;
5778 }
5779
5780 if (key.offset > offset) {
5781 /* Wrong offset, must cow */
5782 goto out;
5783 }
5784
5785 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5786 found_type = btrfs_file_extent_type(leaf, fi);
5787 if (found_type != BTRFS_FILE_EXTENT_REG &&
5788 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5789 /* not a regular extent, must cow */
5790 goto out;
5791 }
5792 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5793 backref_offset = btrfs_file_extent_offset(leaf, fi);
5794
5795 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5796 if (extent_end < offset + len) {
5797 /* extent doesn't include our full range, must cow */
5798 goto out;
5799 }
5800
5801 if (btrfs_extent_readonly(root, disk_bytenr))
5802 goto out;
5803
5804 /*
5805 * look for other files referencing this extent, if we
5806 * find any we must cow
5807 */
Li Zefan33345d012011-04-20 10:31:50 +08005808 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005809 key.offset - backref_offset, disk_bytenr))
5810 goto out;
5811
5812 /*
5813 * adjust disk_bytenr and num_bytes to cover just the bytes
5814 * in this extent we are about to write. If there
5815 * are any csums in that range we have to cow in order
5816 * to keep the csums correct
5817 */
5818 disk_bytenr += backref_offset;
5819 disk_bytenr += offset - key.offset;
5820 num_bytes = min(offset + len, extent_end) - offset;
5821 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5822 goto out;
5823 /*
5824 * all of the above have passed, it is safe to overwrite this extent
5825 * without cow
5826 */
5827 ret = 1;
5828out:
5829 btrfs_free_path(path);
5830 return ret;
5831}
5832
Josef Bacikeb838e72012-07-31 16:28:48 -04005833static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5834 struct extent_state **cached_state, int writing)
5835{
5836 struct btrfs_ordered_extent *ordered;
5837 int ret = 0;
5838
5839 while (1) {
5840 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5841 0, cached_state);
5842 /*
5843 * We're concerned with the entire range that we're going to be
5844 * doing DIO to, so we need to make sure theres no ordered
5845 * extents in this range.
5846 */
5847 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5848 lockend - lockstart + 1);
5849
5850 /*
5851 * We need to make sure there are no buffered pages in this
5852 * range either, we could have raced between the invalidate in
5853 * generic_file_direct_write and locking the extent. The
5854 * invalidate needs to happen so that reads after a write do not
5855 * get stale data.
5856 */
5857 if (!ordered && (!writing ||
5858 !test_range_bit(&BTRFS_I(inode)->io_tree,
5859 lockstart, lockend, EXTENT_UPTODATE, 0,
5860 *cached_state)))
5861 break;
5862
5863 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5864 cached_state, GFP_NOFS);
5865
5866 if (ordered) {
5867 btrfs_start_ordered_extent(inode, ordered, 1);
5868 btrfs_put_ordered_extent(ordered);
5869 } else {
5870 /* Screw you mmap */
5871 ret = filemap_write_and_wait_range(inode->i_mapping,
5872 lockstart,
5873 lockend);
5874 if (ret)
5875 break;
5876
5877 /*
5878 * If we found a page that couldn't be invalidated just
5879 * fall back to buffered.
5880 */
5881 ret = invalidate_inode_pages2_range(inode->i_mapping,
5882 lockstart >> PAGE_CACHE_SHIFT,
5883 lockend >> PAGE_CACHE_SHIFT);
5884 if (ret)
5885 break;
5886 }
5887
5888 cond_resched();
5889 }
5890
5891 return ret;
5892}
5893
Josef Bacik69ffb542012-09-11 15:40:07 -04005894static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5895 u64 len, u64 orig_start,
5896 u64 block_start, u64 block_len,
5897 int type)
5898{
5899 struct extent_map_tree *em_tree;
5900 struct extent_map *em;
5901 struct btrfs_root *root = BTRFS_I(inode)->root;
5902 int ret;
5903
5904 em_tree = &BTRFS_I(inode)->extent_tree;
5905 em = alloc_extent_map();
5906 if (!em)
5907 return ERR_PTR(-ENOMEM);
5908
5909 em->start = start;
5910 em->orig_start = orig_start;
5911 em->len = len;
5912 em->block_len = block_len;
5913 em->block_start = block_start;
5914 em->bdev = root->fs_info->fs_devices->latest_bdev;
5915 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5916 if (type == BTRFS_ORDERED_PREALLOC)
5917 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
5918
5919 do {
5920 btrfs_drop_extent_cache(inode, em->start,
5921 em->start + em->len - 1, 0);
5922 write_lock(&em_tree->lock);
5923 ret = add_extent_mapping(em_tree, em);
5924 write_unlock(&em_tree->lock);
5925 } while (ret == -EEXIST);
5926
5927 if (ret) {
5928 free_extent_map(em);
5929 return ERR_PTR(ret);
5930 }
5931
5932 return em;
5933}
5934
5935
Josef Bacik4b46fce2010-05-23 11:00:55 -04005936static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5937 struct buffer_head *bh_result, int create)
5938{
5939 struct extent_map *em;
5940 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04005941 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005942 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04005943 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005944 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005945 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04005946 int unlock_bits = EXTENT_LOCKED;
5947 int ret;
5948
Josef Bacikeb838e72012-07-31 16:28:48 -04005949 if (create) {
5950 ret = btrfs_delalloc_reserve_space(inode, len);
5951 if (ret)
5952 return ret;
5953 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04005954 } else {
5955 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04005956 }
5957
Josef Bacikc3298612012-08-03 16:49:19 -04005958 lockstart = start;
5959 lockend = start + len - 1;
5960
Josef Bacikeb838e72012-07-31 16:28:48 -04005961 /*
5962 * If this errors out it's because we couldn't invalidate pagecache for
5963 * this range and we need to fallback to buffered.
5964 */
5965 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
5966 return -ENOTBLK;
5967
5968 if (create) {
5969 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
5970 lockend, EXTENT_DELALLOC, NULL,
5971 &cached_state, GFP_NOFS);
5972 if (ret)
5973 goto unlock_err;
5974 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005975
5976 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04005977 if (IS_ERR(em)) {
5978 ret = PTR_ERR(em);
5979 goto unlock_err;
5980 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005981
5982 /*
5983 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5984 * io. INLINE is special, and we could probably kludge it in here, but
5985 * it's still buffered so for safety lets just fall back to the generic
5986 * buffered path.
5987 *
5988 * For COMPRESSED we _have_ to read the entire extent in so we can
5989 * decompress it, so there will be buffering required no matter what we
5990 * do, so go ahead and fallback to buffered.
5991 *
5992 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5993 * to buffered IO. Don't blame me, this is the price we pay for using
5994 * the generic code.
5995 */
5996 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5997 em->block_start == EXTENT_MAP_INLINE) {
5998 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04005999 ret = -ENOTBLK;
6000 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006001 }
6002
6003 /* Just a good old fashioned hole, return */
6004 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6005 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6006 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006007 ret = 0;
6008 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006009 }
6010
6011 /*
6012 * We don't allocate a new extent in the following cases
6013 *
6014 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6015 * existing extent.
6016 * 2) The extent is marked as PREALLOC. We're good to go here and can
6017 * just use the extent.
6018 *
6019 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006020 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006021 len = min(len, em->len - (start - em->start));
6022 lockstart = start + len;
6023 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006024 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006025
6026 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6027 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6028 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006029 int type;
6030 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006031 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006032
6033 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6034 type = BTRFS_ORDERED_PREALLOC;
6035 else
6036 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006037 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006038 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006039
6040 /*
6041 * we're not going to log anything, but we do need
6042 * to make sure the current transaction stays open
6043 * while we look for nocow cross refs
6044 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006045 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006046 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006047 goto must_cow;
6048
6049 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik69ffb542012-09-11 15:40:07 -04006050 u64 orig_start = em->start;
6051
6052 if (type == BTRFS_ORDERED_PREALLOC) {
6053 free_extent_map(em);
6054 em = create_pinned_em(inode, start, len,
6055 orig_start,
6056 block_start, len, type);
6057 if (IS_ERR(em)) {
6058 btrfs_end_transaction(trans, root);
6059 goto unlock_err;
6060 }
6061 }
6062
Chris Mason46bfbb52010-05-26 11:04:10 -04006063 ret = btrfs_add_ordered_extent_dio(inode, start,
6064 block_start, len, len, type);
6065 btrfs_end_transaction(trans, root);
6066 if (ret) {
6067 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006068 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006069 }
6070 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006071 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006072 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006073 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006074must_cow:
6075 /*
6076 * this will cow the extent, reset the len in case we changed
6077 * it above
6078 */
6079 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04006080 em = btrfs_new_extent_direct(inode, em, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006081 if (IS_ERR(em)) {
6082 ret = PTR_ERR(em);
6083 goto unlock_err;
6084 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006085 len = min(len, em->len - (start - em->start));
6086unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006087 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6088 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006089 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006090 bh_result->b_bdev = em->bdev;
6091 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006092 if (create) {
6093 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6094 set_buffer_new(bh_result);
6095
6096 /*
6097 * Need to update the i_size under the extent lock so buffered
6098 * readers will get the updated i_size when we unlock.
6099 */
6100 if (start + len > i_size_read(inode))
6101 i_size_write(inode, start + len);
6102 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006103
Josef Bacikeb838e72012-07-31 16:28:48 -04006104 /*
6105 * In the case of write we need to clear and unlock the entire range,
6106 * in the case of read we need to unlock only the end area that we
6107 * aren't using if there is any left over space.
6108 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006109 if (lockstart < lockend) {
6110 if (create && len < lockend - lockstart) {
6111 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006112 lockstart + len - 1,
6113 unlock_bits | EXTENT_DEFRAG, 1, 0,
Liu Bo24c03fa2012-08-22 20:10:38 -06006114 &cached_state, GFP_NOFS);
6115 /*
6116 * Beside unlock, we also need to cleanup reserved space
6117 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6118 */
6119 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6120 lockstart + len, lockend,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006121 unlock_bits | EXTENT_DO_ACCOUNTING |
6122 EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006123 } else {
6124 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6125 lockend, unlock_bits, 1, 0,
6126 &cached_state, GFP_NOFS);
6127 }
6128 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006129 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006130 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006131
Josef Bacik4b46fce2010-05-23 11:00:55 -04006132 free_extent_map(em);
6133
6134 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006135
6136unlock_err:
6137 if (create)
6138 unlock_bits |= EXTENT_DO_ACCOUNTING;
6139
6140 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6141 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6142 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006143}
6144
6145struct btrfs_dio_private {
6146 struct inode *inode;
6147 u64 logical_offset;
6148 u64 disk_bytenr;
6149 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006150 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006151
6152 /* number of bios pending for this dio */
6153 atomic_t pending_bios;
6154
6155 /* IO errors */
6156 int errors;
6157
6158 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006159};
6160
6161static void btrfs_endio_direct_read(struct bio *bio, int err)
6162{
Miao Xiee65e1532010-11-22 03:04:43 +00006163 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006164 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6165 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006166 struct inode *inode = dip->inode;
6167 struct btrfs_root *root = BTRFS_I(inode)->root;
6168 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006169
6170 start = dip->logical_offset;
6171 do {
6172 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6173 struct page *page = bvec->bv_page;
6174 char *kaddr;
6175 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006176 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006177 unsigned long flags;
6178
Josef Bacikc3298612012-08-03 16:49:19 -04006179 if (get_state_private(&BTRFS_I(inode)->io_tree,
6180 start, &private))
6181 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006182 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006183 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006184 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6185 csum, bvec->bv_len);
6186 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006187 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006188 local_irq_restore(flags);
6189
6190 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006191 if (csum != private) {
6192failed:
Li Zefan33345d012011-04-20 10:31:50 +08006193 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006194 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006195 (unsigned long long)btrfs_ino(inode),
6196 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006197 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006198 err = -EIO;
6199 }
6200 }
6201
6202 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006203 bvec++;
6204 } while (bvec <= bvec_end);
6205
6206 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006207 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006208 bio->bi_private = dip->private;
6209
Josef Bacik4b46fce2010-05-23 11:00:55 -04006210 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006211
6212 /* If we had a csum failure make sure to clear the uptodate flag */
6213 if (err)
6214 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006215 dio_end_io(bio, err);
6216}
6217
6218static void btrfs_endio_direct_write(struct bio *bio, int err)
6219{
6220 struct btrfs_dio_private *dip = bio->bi_private;
6221 struct inode *inode = dip->inode;
6222 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006223 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006224 u64 ordered_offset = dip->logical_offset;
6225 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006226 int ret;
6227
6228 if (err)
6229 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006230again:
6231 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6232 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006233 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006234 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006235 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006236
Josef Bacik5fd02042012-05-02 14:00:54 -04006237 ordered->work.func = finish_ordered_fn;
6238 ordered->work.flags = 0;
6239 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6240 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006241out_test:
6242 /*
6243 * our bio might span multiple ordered extents. If we haven't
6244 * completed the accounting for the whole dio, go back and try again
6245 */
6246 if (ordered_offset < dip->logical_offset + dip->bytes) {
6247 ordered_bytes = dip->logical_offset + dip->bytes -
6248 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006249 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006250 goto again;
6251 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006252out_done:
6253 bio->bi_private = dip->private;
6254
Josef Bacik4b46fce2010-05-23 11:00:55 -04006255 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006256
6257 /* If we had an error make sure to clear the uptodate flag */
6258 if (err)
6259 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006260 dio_end_io(bio, err);
6261}
6262
Chris Masoneaf25d92010-05-25 09:48:28 -04006263static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6264 struct bio *bio, int mirror_num,
6265 unsigned long bio_flags, u64 offset)
6266{
6267 int ret;
6268 struct btrfs_root *root = BTRFS_I(inode)->root;
6269 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006270 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006271 return 0;
6272}
6273
Miao Xiee65e1532010-11-22 03:04:43 +00006274static void btrfs_end_dio_bio(struct bio *bio, int err)
6275{
6276 struct btrfs_dio_private *dip = bio->bi_private;
6277
6278 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006279 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006280 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006281 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006282 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006283 dip->errors = 1;
6284
6285 /*
6286 * before atomic variable goto zero, we must make sure
6287 * dip->errors is perceived to be set.
6288 */
6289 smp_mb__before_atomic_dec();
6290 }
6291
6292 /* if there are more bios still pending for this dio, just exit */
6293 if (!atomic_dec_and_test(&dip->pending_bios))
6294 goto out;
6295
6296 if (dip->errors)
6297 bio_io_error(dip->orig_bio);
6298 else {
6299 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6300 bio_endio(dip->orig_bio, 0);
6301 }
6302out:
6303 bio_put(bio);
6304}
6305
6306static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6307 u64 first_sector, gfp_t gfp_flags)
6308{
6309 int nr_vecs = bio_get_nr_vecs(bdev);
6310 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6311}
6312
6313static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6314 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006315 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006316{
6317 int write = rw & REQ_WRITE;
6318 struct btrfs_root *root = BTRFS_I(inode)->root;
6319 int ret;
6320
6321 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006322
6323 if (!write) {
6324 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6325 if (ret)
6326 goto err;
6327 }
Miao Xiee65e1532010-11-22 03:04:43 +00006328
Josef Bacik1ae39932011-04-06 14:41:34 -04006329 if (skip_sum)
6330 goto map;
6331
6332 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006333 ret = btrfs_wq_submit_bio(root->fs_info,
6334 inode, rw, bio, 0, 0,
6335 file_offset,
6336 __btrfs_submit_bio_start_direct_io,
6337 __btrfs_submit_bio_done);
6338 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006339 } else if (write) {
6340 /*
6341 * If we aren't doing async submit, calculate the csum of the
6342 * bio now.
6343 */
6344 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6345 if (ret)
6346 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006347 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006348 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006349 if (ret)
6350 goto err;
6351 }
Miao Xiee65e1532010-11-22 03:04:43 +00006352
Josef Bacik1ae39932011-04-06 14:41:34 -04006353map:
6354 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006355err:
6356 bio_put(bio);
6357 return ret;
6358}
6359
6360static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6361 int skip_sum)
6362{
6363 struct inode *inode = dip->inode;
6364 struct btrfs_root *root = BTRFS_I(inode)->root;
6365 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6366 struct bio *bio;
6367 struct bio *orig_bio = dip->orig_bio;
6368 struct bio_vec *bvec = orig_bio->bi_io_vec;
6369 u64 start_sector = orig_bio->bi_sector;
6370 u64 file_offset = dip->logical_offset;
6371 u64 submit_len = 0;
6372 u64 map_length;
6373 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006374 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006375 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006376
Miao Xiee65e1532010-11-22 03:04:43 +00006377 map_length = orig_bio->bi_size;
6378 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6379 &map_length, NULL, 0);
6380 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006381 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006382 return -EIO;
6383 }
6384
Josef Bacik02f57c72011-04-06 14:25:44 -04006385 if (map_length >= orig_bio->bi_size) {
6386 bio = orig_bio;
6387 goto submit;
6388 }
6389
Josef Bacik1ae39932011-04-06 14:41:34 -04006390 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006391 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6392 if (!bio)
6393 return -ENOMEM;
6394 bio->bi_private = dip;
6395 bio->bi_end_io = btrfs_end_dio_bio;
6396 atomic_inc(&dip->pending_bios);
6397
Miao Xiee65e1532010-11-22 03:04:43 +00006398 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6399 if (unlikely(map_length < submit_len + bvec->bv_len ||
6400 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6401 bvec->bv_offset) < bvec->bv_len)) {
6402 /*
6403 * inc the count before we submit the bio so
6404 * we know the end IO handler won't happen before
6405 * we inc the count. Otherwise, the dip might get freed
6406 * before we're done setting it up
6407 */
6408 atomic_inc(&dip->pending_bios);
6409 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6410 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006411 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006412 if (ret) {
6413 bio_put(bio);
6414 atomic_dec(&dip->pending_bios);
6415 goto out_err;
6416 }
6417
Miao Xiee65e1532010-11-22 03:04:43 +00006418 start_sector += submit_len >> 9;
6419 file_offset += submit_len;
6420
6421 submit_len = 0;
6422 nr_pages = 0;
6423
6424 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6425 start_sector, GFP_NOFS);
6426 if (!bio)
6427 goto out_err;
6428 bio->bi_private = dip;
6429 bio->bi_end_io = btrfs_end_dio_bio;
6430
6431 map_length = orig_bio->bi_size;
6432 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6433 &map_length, NULL, 0);
6434 if (ret) {
6435 bio_put(bio);
6436 goto out_err;
6437 }
6438 } else {
6439 submit_len += bvec->bv_len;
6440 nr_pages ++;
6441 bvec++;
6442 }
6443 }
6444
Josef Bacik02f57c72011-04-06 14:25:44 -04006445submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006446 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006447 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006448 if (!ret)
6449 return 0;
6450
6451 bio_put(bio);
6452out_err:
6453 dip->errors = 1;
6454 /*
6455 * before atomic variable goto zero, we must
6456 * make sure dip->errors is perceived to be set.
6457 */
6458 smp_mb__before_atomic_dec();
6459 if (atomic_dec_and_test(&dip->pending_bios))
6460 bio_io_error(dip->orig_bio);
6461
6462 /* bio_end_io() will handle error, so we needn't return it */
6463 return 0;
6464}
6465
Josef Bacik4b46fce2010-05-23 11:00:55 -04006466static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6467 loff_t file_offset)
6468{
6469 struct btrfs_root *root = BTRFS_I(inode)->root;
6470 struct btrfs_dio_private *dip;
6471 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006472 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006473 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006474 int ret = 0;
6475
6476 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6477
6478 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6479 if (!dip) {
6480 ret = -ENOMEM;
6481 goto free_ordered;
6482 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006483
6484 dip->private = bio->bi_private;
6485 dip->inode = inode;
6486 dip->logical_offset = file_offset;
6487
Josef Bacik4b46fce2010-05-23 11:00:55 -04006488 dip->bytes = 0;
6489 do {
6490 dip->bytes += bvec->bv_len;
6491 bvec++;
6492 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6493
Chris Mason46bfbb52010-05-26 11:04:10 -04006494 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006495 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006496 dip->errors = 0;
6497 dip->orig_bio = bio;
6498 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006499
6500 if (write)
6501 bio->bi_end_io = btrfs_endio_direct_write;
6502 else
6503 bio->bi_end_io = btrfs_endio_direct_read;
6504
Miao Xiee65e1532010-11-22 03:04:43 +00006505 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6506 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006507 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006508free_ordered:
6509 /*
6510 * If this is a write, we need to clean up the reserved space and kill
6511 * the ordered extent.
6512 */
6513 if (write) {
6514 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006515 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006516 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6517 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6518 btrfs_free_reserved_extent(root, ordered->start,
6519 ordered->disk_len);
6520 btrfs_put_ordered_extent(ordered);
6521 btrfs_put_ordered_extent(ordered);
6522 }
6523 bio_endio(bio, ret);
6524}
6525
Chris Mason5a5f79b2010-05-26 21:33:37 -04006526static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6527 const struct iovec *iov, loff_t offset,
6528 unsigned long nr_segs)
6529{
6530 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006531 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006532 size_t size;
6533 unsigned long addr;
6534 unsigned blocksize_mask = root->sectorsize - 1;
6535 ssize_t retval = -EINVAL;
6536 loff_t end = offset;
6537
6538 if (offset & blocksize_mask)
6539 goto out;
6540
6541 /* Check the memory alignment. Blocks cannot straddle pages */
6542 for (seg = 0; seg < nr_segs; seg++) {
6543 addr = (unsigned long)iov[seg].iov_base;
6544 size = iov[seg].iov_len;
6545 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006546 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006547 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006548
6549 /* If this is a write we don't need to check anymore */
6550 if (rw & WRITE)
6551 continue;
6552
6553 /*
6554 * Check to make sure we don't have duplicate iov_base's in this
6555 * iovec, if so return EINVAL, otherwise we'll get csum errors
6556 * when reading back.
6557 */
6558 for (i = seg + 1; i < nr_segs; i++) {
6559 if (iov[seg].iov_base == iov[i].iov_base)
6560 goto out;
6561 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006562 }
6563 retval = 0;
6564out:
6565 return retval;
6566}
Josef Bacikeb838e72012-07-31 16:28:48 -04006567
Chris Mason16432982008-04-10 10:23:21 -04006568static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6569 const struct iovec *iov, loff_t offset,
6570 unsigned long nr_segs)
6571{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006572 struct file *file = iocb->ki_filp;
6573 struct inode *inode = file->f_mapping->host;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006574
Chris Mason5a5f79b2010-05-26 21:33:37 -04006575 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006576 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006577 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006578
Josef Bacikeb838e72012-07-31 16:28:48 -04006579 return __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006580 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6581 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6582 btrfs_submit_direct, 0);
Chris Mason16432982008-04-10 10:23:21 -04006583}
6584
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006585static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6586 __u64 start, __u64 len)
6587{
Chris Masonec29ed52011-02-23 16:23:20 -05006588 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006589}
6590
Chris Mason9ebefb182007-06-15 13:50:00 -04006591int btrfs_readpage(struct file *file, struct page *page)
6592{
Chris Masond1310b22008-01-24 16:13:08 -05006593 struct extent_io_tree *tree;
6594 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006595 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006596}
Chris Mason1832a6d2007-12-21 16:27:21 -05006597
Chris Mason39279cc2007-06-12 06:35:45 -04006598static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6599{
Chris Masond1310b22008-01-24 16:13:08 -05006600 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006601
6602
6603 if (current->flags & PF_MEMALLOC) {
6604 redirty_page_for_writepage(wbc, page);
6605 unlock_page(page);
6606 return 0;
6607 }
Chris Masond1310b22008-01-24 16:13:08 -05006608 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006609 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6610}
Chris Mason39279cc2007-06-12 06:35:45 -04006611
Chris Masonf4219502008-07-22 11:18:09 -04006612int btrfs_writepages(struct address_space *mapping,
6613 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006614{
Chris Masond1310b22008-01-24 16:13:08 -05006615 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006616
Chris Masond1310b22008-01-24 16:13:08 -05006617 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006618 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6619}
6620
Chris Mason3ab2fb52007-11-08 10:59:22 -05006621static int
6622btrfs_readpages(struct file *file, struct address_space *mapping,
6623 struct list_head *pages, unsigned nr_pages)
6624{
Chris Masond1310b22008-01-24 16:13:08 -05006625 struct extent_io_tree *tree;
6626 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006627 return extent_readpages(tree, mapping, pages, nr_pages,
6628 btrfs_get_extent);
6629}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006630static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006631{
Chris Masond1310b22008-01-24 16:13:08 -05006632 struct extent_io_tree *tree;
6633 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006634 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006635
Chris Masond1310b22008-01-24 16:13:08 -05006636 tree = &BTRFS_I(page->mapping->host)->io_tree;
6637 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006638 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006639 if (ret == 1) {
6640 ClearPagePrivate(page);
6641 set_page_private(page, 0);
6642 page_cache_release(page);
6643 }
6644 return ret;
6645}
Chris Mason39279cc2007-06-12 06:35:45 -04006646
Chris Masone6dcd2d2008-07-17 12:53:50 -04006647static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6648{
Chris Mason98509cf2008-09-11 15:51:43 -04006649 if (PageWriteback(page) || PageDirty(page))
6650 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006651 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006652}
6653
Chris Masona52d9a82007-08-27 16:49:44 -04006654static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6655{
Josef Bacik5fd02042012-05-02 14:00:54 -04006656 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006657 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006658 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006659 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006660 u64 page_start = page_offset(page);
6661 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006662
Chris Mason8b62b722009-09-02 16:53:46 -04006663 /*
6664 * we have the page locked, so new writeback can't start,
6665 * and the dirty bit won't be cleared while we are here.
6666 *
6667 * Wait for IO on this page so that we can safely clear
6668 * the PagePrivate2 bit and do ordered accounting
6669 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006670 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006671
Josef Bacik5fd02042012-05-02 14:00:54 -04006672 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006673 if (offset) {
6674 btrfs_releasepage(page, GFP_NOFS);
6675 return;
6676 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006677 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Josef Bacik5fd02042012-05-02 14:00:54 -04006678 ordered = btrfs_lookup_ordered_extent(inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006679 page_offset(page));
6680 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006681 /*
6682 * IO on this page will never be started, so we need
6683 * to account for any ordered extents now
6684 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006685 clear_extent_bit(tree, page_start, page_end,
6686 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006687 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6688 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006689 /*
6690 * whoever cleared the private bit is responsible
6691 * for the finish_ordered_io
6692 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006693 if (TestClearPagePrivate2(page) &&
6694 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6695 PAGE_CACHE_SIZE, 1)) {
6696 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006697 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006698 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006699 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006700 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006701 }
6702 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006703 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006704 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6705 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006706 __btrfs_releasepage(page, GFP_NOFS);
6707
Chris Mason4a096752008-07-21 10:29:44 -04006708 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006709 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006710 ClearPagePrivate(page);
6711 set_page_private(page, 0);
6712 page_cache_release(page);
6713 }
Chris Mason39279cc2007-06-12 06:35:45 -04006714}
6715
Chris Mason9ebefb182007-06-15 13:50:00 -04006716/*
6717 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6718 * called from a page fault handler when a page is first dirtied. Hence we must
6719 * be careful to check for EOF conditions here. We set the page up correctly
6720 * for a written page which means we get ENOSPC checking when writing into
6721 * holes and correct delalloc and unwritten extent mapping on filesystems that
6722 * support these features.
6723 *
6724 * We are not allowed to take the i_mutex here so we have to play games to
6725 * protect against truncate races as the page could now be beyond EOF. Because
6726 * vmtruncate() writes the inode size before removing pages, once we have the
6727 * page lock we can determine safely if the page is beyond EOF. If it is not
6728 * beyond EOF, then the page is guaranteed safe against truncation until we
6729 * unlock the page.
6730 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006731int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006732{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006733 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006734 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006735 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006736 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6737 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006738 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006739 char *kaddr;
6740 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006741 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006742 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006743 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006744 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006745 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006746
Jan Karab2b5ef52012-06-12 16:20:45 +02006747 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006748 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006749 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006750 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006751 reserved = 1;
6752 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006753 if (ret) {
6754 if (ret == -ENOMEM)
6755 ret = VM_FAULT_OOM;
6756 else /* -ENOSPC, -EIO, etc */
6757 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006758 if (reserved)
6759 goto out;
6760 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006761 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006762
Nick Piggin56a76f82009-03-31 15:23:23 -07006763 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006764again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006765 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006766 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006767 page_start = page_offset(page);
6768 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006769
Chris Mason9ebefb182007-06-15 13:50:00 -04006770 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006771 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006772 /* page got truncated out from underneath us */
6773 goto out_unlock;
6774 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006775 wait_on_page_writeback(page);
6776
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006777 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006778 set_page_extent_mapped(page);
6779
Chris Masoneb84ae02008-07-17 13:53:27 -04006780 /*
6781 * we can't set the delalloc bits if there are pending ordered
6782 * extents. Drop our locks and wait for them to finish
6783 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006784 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6785 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006786 unlock_extent_cached(io_tree, page_start, page_end,
6787 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006788 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006789 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006790 btrfs_put_ordered_extent(ordered);
6791 goto again;
6792 }
6793
Josef Bacikfbf19082009-10-01 17:10:23 -04006794 /*
6795 * XXX - page_mkwrite gets called every time the page is dirtied, even
6796 * if it was already dirty, so for space accounting reasons we need to
6797 * clear any delalloc bits for the range we are fixing to save. There
6798 * is probably a better way to do this, but for now keep consistent with
6799 * prepare_pages in the normal write path.
6800 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006801 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006802 EXTENT_DIRTY | EXTENT_DELALLOC |
6803 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006804 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006805
Josef Bacik2ac55d42010-02-03 19:33:23 +00006806 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6807 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006808 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006809 unlock_extent_cached(io_tree, page_start, page_end,
6810 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006811 ret = VM_FAULT_SIGBUS;
6812 goto out_unlock;
6813 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006814 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006815
6816 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006817 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006818 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006819 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006820 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006821
Chris Masone6dcd2d2008-07-17 12:53:50 -04006822 if (zero_start != PAGE_CACHE_SIZE) {
6823 kaddr = kmap(page);
6824 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6825 flush_dcache_page(page);
6826 kunmap(page);
6827 }
Chris Mason247e7432008-07-17 12:53:51 -04006828 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006829 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006830 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006831
Chris Mason257c62e2009-10-13 13:21:08 -04006832 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6833 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006834 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006835
Josef Bacik2ac55d42010-02-03 19:33:23 +00006836 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006837
6838out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006839 if (!ret) {
6840 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006841 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006842 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006843 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006844out:
Josef Bacikec39e182012-01-12 19:10:12 -05006845 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006846out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006847 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006848 return ret;
6849}
6850
Josef Bacika41ad392011-01-31 15:30:16 -05006851static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006852{
6853 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006854 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006855 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006856 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006857 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006858 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006859 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006860 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006861
Josef Bacik2aaa6652012-08-29 14:27:18 -04006862 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04006863 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006864 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006865
Chris Mason4a096752008-07-21 10:29:44 -04006866 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006867 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006868
Josef Bacikfcb80c22011-05-03 10:40:22 -04006869 /*
6870 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6871 * 3 things going on here
6872 *
6873 * 1) We need to reserve space for our orphan item and the space to
6874 * delete our orphan item. Lord knows we don't want to have a dangling
6875 * orphan item because we didn't reserve space to remove it.
6876 *
6877 * 2) We need to reserve space to update our inode.
6878 *
6879 * 3) We need to have something to cache all the space that is going to
6880 * be free'd up by the truncate operation, but also have some slack
6881 * space reserved in case it uses space during the truncate (thank you
6882 * very much snapshotting).
6883 *
6884 * And we need these to all be seperate. The fact is we can use alot of
6885 * space doing the truncate, and we have no earthly idea how much space
6886 * we will use, so we need the truncate reservation to be seperate so it
6887 * doesn't end up using space reserved for updating the inode or
6888 * removing the orphan item. We also need to be able to stop the
6889 * transaction and start a new one, which means we need to be able to
6890 * update the inode several times, and we have no idea of knowing how
6891 * many times that will be, so we can't just reserve 1 item for the
6892 * entirety of the opration, so that has to be done seperately as well.
6893 * Then there is the orphan item, which does indeed need to be held on
6894 * to for the whole operation, and we need nobody to touch this reserved
6895 * space except the orphan code.
6896 *
6897 * So that leaves us with
6898 *
6899 * 1) root->orphan_block_rsv - for the orphan deletion.
6900 * 2) rsv - for the truncate reservation, which we will steal from the
6901 * transaction reservation.
6902 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6903 * updating the inode.
6904 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06006905 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006906 if (!rsv)
6907 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006908 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006909 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006910
Josef Bacik907cbce2011-08-08 13:46:15 -04006911 /*
Josef Bacik07127182011-08-19 10:29:59 -04006912 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006913 * 1 for the orphan item we're going to add
6914 * 1 for the orphan item deletion
6915 * 1 for updating the inode.
6916 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04006917 trans = btrfs_start_transaction(root, 4);
6918 if (IS_ERR(trans)) {
6919 err = PTR_ERR(trans);
6920 goto out;
6921 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006922
Josef Bacik907cbce2011-08-08 13:46:15 -04006923 /* Migrate the slack space for the truncate to our reserve */
6924 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6925 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006926 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006927
6928 ret = btrfs_orphan_add(trans, inode);
6929 if (ret) {
6930 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006931 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006932 }
6933
Chris Mason5a3f23d2009-03-31 13:27:11 -04006934 /*
6935 * setattr is responsible for setting the ordered_data_close flag,
6936 * but that is only tested during the last file release. That
6937 * could happen well after the next commit, leaving a great big
6938 * window where new writes may get lost if someone chooses to write
6939 * to this file after truncating to zero
6940 *
6941 * The inode doesn't have any dirty data here, and so if we commit
6942 * this is a noop. If someone immediately starts writing to the inode
6943 * it is very likely we'll catch some of their writes in this
6944 * transaction, and the commit will find this file on the ordered
6945 * data list with good things to send down.
6946 *
6947 * This is a best effort solution, there is still a window where
6948 * using truncate to replace the contents of the file will
6949 * end up with a zero length file after a crash.
6950 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04006951 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
6952 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04006953 btrfs_add_ordered_operation(trans, root, inode);
6954
Josef Bacik5dc562c2012-08-17 13:14:17 -04006955 /*
6956 * So if we truncate and then write and fsync we normally would just
6957 * write the extents that changed, which is a problem if we need to
6958 * first truncate that entire inode. So set this flag so we write out
6959 * all of the extents in the inode to the sync log so we're completely
6960 * safe.
6961 */
6962 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006963 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04006964
Yan, Zheng80825102009-11-12 09:35:36 +00006965 while (1) {
6966 ret = btrfs_truncate_inode_items(trans, root, inode,
6967 inode->i_size,
6968 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006969 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05006970 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006971 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006972 }
Chris Mason39279cc2007-06-12 06:35:45 -04006973
Josef Bacikfcb80c22011-05-03 10:40:22 -04006974 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006975 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006976 if (ret) {
6977 err = ret;
6978 break;
6979 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04006980
Yan, Zheng80825102009-11-12 09:35:36 +00006981 nr = trans->blocks_used;
6982 btrfs_end_transaction(trans, root);
6983 btrfs_btree_balance_dirty(root, nr);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006984
6985 trans = btrfs_start_transaction(root, 2);
6986 if (IS_ERR(trans)) {
6987 ret = err = PTR_ERR(trans);
6988 trans = NULL;
6989 break;
6990 }
6991
6992 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
6993 rsv, min_size);
6994 BUG_ON(ret); /* shouldn't happen */
6995 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006996 }
6997
6998 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04006999 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007000 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007001 if (ret)
7002 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05007003 } else if (ret && inode->i_nlink > 0) {
7004 /*
7005 * Failed to do the truncate, remove us from the in memory
7006 * orphan list.
7007 */
7008 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00007009 }
7010
Chris Mason917c16b2011-11-08 14:49:59 -05007011 if (trans) {
7012 trans->block_rsv = &root->fs_info->trans_block_rsv;
7013 ret = btrfs_update_inode(trans, root, inode);
7014 if (ret && !err)
7015 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007016
Chris Mason917c16b2011-11-08 14:49:59 -05007017 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007018 ret = btrfs_end_transaction(trans, root);
Chris Mason917c16b2011-11-08 14:49:59 -05007019 btrfs_btree_balance_dirty(root, nr);
7020 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007021
7022out:
7023 btrfs_free_block_rsv(root, rsv);
7024
Josef Bacik3893e332011-01-31 16:03:11 -05007025 if (ret && !err)
7026 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007027
Josef Bacik3893e332011-01-31 16:03:11 -05007028 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007029}
7030
Sven Wegener3b963622008-06-09 21:57:42 -04007031/*
Chris Masond352ac62008-09-29 15:18:18 -04007032 * create a new subvolume directory/inode (helper for the ioctl).
7033 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007034int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007035 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007036{
Chris Mason39279cc2007-06-12 06:35:45 -04007037 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007038 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007039 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007040
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007041 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7042 new_dirid, new_dirid,
7043 S_IFDIR | (~current_umask() & S_IRWXUGO),
7044 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007045 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007046 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007047 inode->i_op = &btrfs_dir_inode_operations;
7048 inode->i_fop = &btrfs_dir_file_operations;
7049
Miklos Szeredibfe86842011-10-28 14:13:29 +02007050 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007051 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007052
Yan, Zheng76dda932009-09-21 16:00:26 -04007053 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007054
Yan, Zheng76dda932009-09-21 16:00:26 -04007055 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007056 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007057}
7058
Chris Mason39279cc2007-06-12 06:35:45 -04007059struct inode *btrfs_alloc_inode(struct super_block *sb)
7060{
7061 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007062 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007063
7064 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7065 if (!ei)
7066 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007067
7068 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007069 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007070 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007071 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007072 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007073 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007074 ei->disk_i_size = 0;
7075 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007076 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007077 ei->index_cnt = (u64)-1;
7078 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007079 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007080
Josef Bacik9e0baf62011-07-15 15:16:44 +00007081 spin_lock_init(&ei->lock);
7082 ei->outstanding_extents = 0;
7083 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007084
Josef Bacik72ac3c02012-05-23 14:13:11 -04007085 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007086 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007087
Miao Xie16cdcec2011-04-22 18:12:22 +08007088 ei->delayed_node = NULL;
7089
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007090 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007091 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007092 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7093 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007094 ei->io_tree.track_uptodate = 1;
7095 ei->io_failure_tree.track_uptodate = 1;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007096 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007097 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007098 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007099 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007100 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007101 RB_CLEAR_NODE(&ei->rb_node);
7102
7103 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007104}
7105
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007106static void btrfs_i_callback(struct rcu_head *head)
7107{
7108 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007109 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7110}
7111
Chris Mason39279cc2007-06-12 06:35:45 -04007112void btrfs_destroy_inode(struct inode *inode)
7113{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007114 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007115 struct btrfs_root *root = BTRFS_I(inode)->root;
7116
Al Virob3d9b7a2012-06-09 13:51:19 -04007117 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007118 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007119 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7120 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007121 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7122 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007123
Chris Mason5a3f23d2009-03-31 13:27:11 -04007124 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007125 * This can happen where we create an inode, but somebody else also
7126 * created the same inode and we need to destroy the one we already
7127 * created.
7128 */
7129 if (!root)
7130 goto free;
7131
7132 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007133 * Make sure we're properly removed from the ordered operation
7134 * lists.
7135 */
7136 smp_mb();
7137 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7138 spin_lock(&root->fs_info->ordered_extent_lock);
7139 list_del_init(&BTRFS_I(inode)->ordered_operations);
7140 spin_unlock(&root->fs_info->ordered_extent_lock);
7141 }
7142
Josef Bacik8a35d952012-05-23 14:26:42 -04007143 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7144 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007145 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7146 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007147 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007148 }
Josef Bacik7b128762008-07-24 12:17:14 -04007149
Chris Masond3977122009-01-05 21:25:51 -05007150 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007151 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7152 if (!ordered)
7153 break;
7154 else {
Chris Masond3977122009-01-05 21:25:51 -05007155 printk(KERN_ERR "btrfs found ordered "
7156 "extent %llu %llu on inode cleanup\n",
7157 (unsigned long long)ordered->file_offset,
7158 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007159 btrfs_remove_ordered_extent(inode, ordered);
7160 btrfs_put_ordered_extent(ordered);
7161 btrfs_put_ordered_extent(ordered);
7162 }
7163 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007164 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007165 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007166free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007167 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007168 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007169}
7170
Al Viro45321ac2010-06-07 13:43:19 -04007171int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007172{
7173 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007174
Josef Bacik0af3d002010-06-21 14:48:16 -04007175 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007176 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007177 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007178 else
Al Viro45321ac2010-06-07 13:43:19 -04007179 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007180}
7181
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007182static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007183{
7184 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7185
7186 inode_init_once(&ei->vfs_inode);
7187}
7188
7189void btrfs_destroy_cachep(void)
7190{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007191 /*
7192 * Make sure all delayed rcu free inodes are flushed before we
7193 * destroy cache.
7194 */
7195 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007196 if (btrfs_inode_cachep)
7197 kmem_cache_destroy(btrfs_inode_cachep);
7198 if (btrfs_trans_handle_cachep)
7199 kmem_cache_destroy(btrfs_trans_handle_cachep);
7200 if (btrfs_transaction_cachep)
7201 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007202 if (btrfs_path_cachep)
7203 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007204 if (btrfs_free_space_cachep)
7205 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007206}
7207
7208int btrfs_init_cachep(void)
7209{
David Sterba837e1972012-09-07 03:00:48 -06007210 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007211 sizeof(struct btrfs_inode), 0,
7212 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007213 if (!btrfs_inode_cachep)
7214 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007215
David Sterba837e1972012-09-07 03:00:48 -06007216 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007217 sizeof(struct btrfs_trans_handle), 0,
7218 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007219 if (!btrfs_trans_handle_cachep)
7220 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007221
David Sterba837e1972012-09-07 03:00:48 -06007222 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007223 sizeof(struct btrfs_transaction), 0,
7224 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007225 if (!btrfs_transaction_cachep)
7226 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007227
David Sterba837e1972012-09-07 03:00:48 -06007228 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007229 sizeof(struct btrfs_path), 0,
7230 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007231 if (!btrfs_path_cachep)
7232 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007233
David Sterba837e1972012-09-07 03:00:48 -06007234 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007235 sizeof(struct btrfs_free_space), 0,
7236 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7237 if (!btrfs_free_space_cachep)
7238 goto fail;
7239
Chris Mason39279cc2007-06-12 06:35:45 -04007240 return 0;
7241fail:
7242 btrfs_destroy_cachep();
7243 return -ENOMEM;
7244}
7245
7246static int btrfs_getattr(struct vfsmount *mnt,
7247 struct dentry *dentry, struct kstat *stat)
7248{
7249 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007250 u32 blocksize = inode->i_sb->s_blocksize;
7251
Chris Mason39279cc2007-06-12 06:35:45 -04007252 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007253 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007254 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007255 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7256 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007257 return 0;
7258}
7259
Liu Bo75e7cb72011-03-22 10:12:20 +00007260/*
7261 * If a file is moved, it will inherit the cow and compression flags of the new
7262 * directory.
7263 */
7264static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7265{
7266 struct btrfs_inode *b_dir = BTRFS_I(dir);
7267 struct btrfs_inode *b_inode = BTRFS_I(inode);
7268
7269 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7270 b_inode->flags |= BTRFS_INODE_NODATACOW;
7271 else
7272 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7273
Liu Bobc178232012-06-14 02:23:18 -06007274 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007275 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007276 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7277 } else {
7278 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7279 BTRFS_INODE_NOCOMPRESS);
7280 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007281}
7282
Chris Masond3977122009-01-05 21:25:51 -05007283static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7284 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007285{
7286 struct btrfs_trans_handle *trans;
7287 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007288 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007289 struct inode *new_inode = new_dentry->d_inode;
7290 struct inode *old_inode = old_dentry->d_inode;
7291 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007292 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007293 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007294 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007295 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007296
Li Zefan33345d012011-04-20 10:31:50 +08007297 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007298 return -EPERM;
7299
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007300 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007301 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007302 return -EXDEV;
7303
Li Zefan33345d012011-04-20 10:31:50 +08007304 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7305 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007306 return -ENOTEMPTY;
7307
Chris Mason39279cc2007-06-12 06:35:45 -04007308 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007309 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007310 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007311 /*
7312 * we're using rename to replace one file with another.
7313 * and the replacement file is large. Start IO on it now so
7314 * we don't add too much work to the end of the transaction
7315 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007316 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007317 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7318 filemap_flush(old_inode->i_mapping);
7319
Yan, Zheng76dda932009-09-21 16:00:26 -04007320 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007321 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007322 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007323 /*
7324 * We want to reserve the absolute worst case amount of items. So if
7325 * both inodes are subvols and we need to unlink them then that would
7326 * require 4 item modifications, but if they are both normal inodes it
7327 * would require 5 item modifications, so we'll assume their normal
7328 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7329 * should cover the worst case number of items we'll modify.
7330 */
7331 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007332 if (IS_ERR(trans)) {
7333 ret = PTR_ERR(trans);
7334 goto out_notrans;
7335 }
Chris Mason5f39d392007-10-15 16:14:19 -04007336
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007337 if (dest != root)
7338 btrfs_record_root_in_trans(trans, dest);
7339
Yan, Zhenga5719522009-09-24 09:17:31 -04007340 ret = btrfs_set_inode_index(new_dir, &index);
7341 if (ret)
7342 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007343
Li Zefan33345d012011-04-20 10:31:50 +08007344 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007345 /* force full log commit if subvolume involved. */
7346 root->fs_info->last_trans_log_full_commit = trans->transid;
7347 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007348 ret = btrfs_insert_inode_ref(trans, dest,
7349 new_dentry->d_name.name,
7350 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007351 old_ino,
7352 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007353 if (ret)
7354 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007355 /*
7356 * this is an ugly little race, but the rename is required
7357 * to make sure that if we crash, the inode is either at the
7358 * old name or the new one. pinning the log transaction lets
7359 * us make sure we don't allow a log commit to come in after
7360 * we unlink the name but before we add the new name back in.
7361 */
7362 btrfs_pin_log_trans(root);
7363 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007364 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007365 * make sure the inode gets flushed if it is replacing
7366 * something.
7367 */
Li Zefan33345d012011-04-20 10:31:50 +08007368 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007369 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007370
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007371 inode_inc_iversion(old_dir);
7372 inode_inc_iversion(new_dir);
7373 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007374 old_dir->i_ctime = old_dir->i_mtime = ctime;
7375 new_dir->i_ctime = new_dir->i_mtime = ctime;
7376 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007377
Chris Mason12fcfd22009-03-24 10:24:20 -04007378 if (old_dentry->d_parent != new_dentry->d_parent)
7379 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7380
Li Zefan33345d012011-04-20 10:31:50 +08007381 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007382 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7383 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7384 old_dentry->d_name.name,
7385 old_dentry->d_name.len);
7386 } else {
Al Viro92986792011-03-04 17:14:37 +00007387 ret = __btrfs_unlink_inode(trans, root, old_dir,
7388 old_dentry->d_inode,
7389 old_dentry->d_name.name,
7390 old_dentry->d_name.len);
7391 if (!ret)
7392 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007393 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007394 if (ret) {
7395 btrfs_abort_transaction(trans, root, ret);
7396 goto out_fail;
7397 }
Chris Mason39279cc2007-06-12 06:35:45 -04007398
7399 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007400 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007401 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007402 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007403 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7404 root_objectid = BTRFS_I(new_inode)->location.objectid;
7405 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7406 root_objectid,
7407 new_dentry->d_name.name,
7408 new_dentry->d_name.len);
7409 BUG_ON(new_inode->i_nlink == 0);
7410 } else {
7411 ret = btrfs_unlink_inode(trans, dest, new_dir,
7412 new_dentry->d_inode,
7413 new_dentry->d_name.name,
7414 new_dentry->d_name.len);
7415 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007416 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007417 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007418 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007419 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007420 if (ret) {
7421 btrfs_abort_transaction(trans, root, ret);
7422 goto out_fail;
7423 }
Chris Mason39279cc2007-06-12 06:35:45 -04007424 }
Josef Bacikaec74772008-07-24 12:12:38 -04007425
Liu Bo75e7cb72011-03-22 10:12:20 +00007426 fixup_inode_flags(new_dir, old_inode);
7427
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007428 ret = btrfs_add_link(trans, new_dir, old_inode,
7429 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007430 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007431 if (ret) {
7432 btrfs_abort_transaction(trans, root, ret);
7433 goto out_fail;
7434 }
Chris Mason39279cc2007-06-12 06:35:45 -04007435
Li Zefan33345d012011-04-20 10:31:50 +08007436 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007437 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007438 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007439 btrfs_end_log_trans(root);
7440 }
Chris Mason39279cc2007-06-12 06:35:45 -04007441out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007442 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007443out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007444 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007445 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007446
Chris Mason39279cc2007-06-12 06:35:45 -04007447 return ret;
7448}
7449
Chris Masond352ac62008-09-29 15:18:18 -04007450/*
7451 * some fairly slow code that needs optimization. This walks the list
7452 * of all the inodes with pending delalloc and forces them to disk.
7453 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007454int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007455{
7456 struct list_head *head = &root->fs_info->delalloc_inodes;
7457 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007458 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007459
Yan Zhengc146afa2008-11-12 14:34:12 -05007460 if (root->fs_info->sb->s_flags & MS_RDONLY)
7461 return -EROFS;
7462
Chris Mason75eff682008-12-15 15:54:40 -05007463 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007464 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007465 binode = list_entry(head->next, struct btrfs_inode,
7466 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007467 inode = igrab(&binode->vfs_inode);
7468 if (!inode)
7469 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007470 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007471 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007472 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007473 if (delay_iput)
7474 btrfs_add_delayed_iput(inode);
7475 else
7476 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007477 }
7478 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007479 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007480 }
Chris Mason75eff682008-12-15 15:54:40 -05007481 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007482
7483 /* the filemap_flush will queue IO into the worker threads, but
7484 * we have to make sure the IO is actually started and that
7485 * ordered extents get created before we return
7486 */
7487 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007488 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007489 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007490 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007491 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7492 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007493 }
7494 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007495 return 0;
7496}
7497
Chris Mason39279cc2007-06-12 06:35:45 -04007498static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7499 const char *symname)
7500{
7501 struct btrfs_trans_handle *trans;
7502 struct btrfs_root *root = BTRFS_I(dir)->root;
7503 struct btrfs_path *path;
7504 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007505 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007506 int err;
7507 int drop_inode = 0;
7508 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007509 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007510 int name_len;
7511 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007512 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007513 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007514 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007515 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007516
7517 name_len = strlen(symname) + 1;
7518 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7519 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007520
Josef Bacik9ed74f22009-09-11 16:12:44 -04007521 /*
7522 * 2 items for inode item and ref
7523 * 2 items for dir items
7524 * 1 item for xattr if selinux is on
7525 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007526 trans = btrfs_start_transaction(root, 5);
7527 if (IS_ERR(trans))
7528 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007529
Li Zefan581bb052011-04-20 10:06:11 +08007530 err = btrfs_find_free_ino(root, &objectid);
7531 if (err)
7532 goto out_unlock;
7533
Josef Bacikaec74772008-07-24 12:12:38 -04007534 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007535 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007536 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007537 if (IS_ERR(inode)) {
7538 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007539 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007540 }
Chris Mason39279cc2007-06-12 06:35:45 -04007541
Eric Paris2a7dba32011-02-01 11:05:39 -05007542 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007543 if (err) {
7544 drop_inode = 1;
7545 goto out_unlock;
7546 }
7547
Casey Schauflerad19db72011-12-15 10:09:07 -05007548 /*
7549 * If the active LSM wants to access the inode during
7550 * d_instantiate it needs these. Smack checks to see
7551 * if the filesystem supports xattrs by looking at the
7552 * ops vector.
7553 */
7554 inode->i_fop = &btrfs_file_operations;
7555 inode->i_op = &btrfs_file_inode_operations;
7556
Josef Bacika1b075d2010-11-19 20:36:11 +00007557 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007558 if (err)
7559 drop_inode = 1;
7560 else {
7561 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007562 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007563 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007564 }
Chris Mason39279cc2007-06-12 06:35:45 -04007565 if (drop_inode)
7566 goto out_unlock;
7567
7568 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007569 if (!path) {
7570 err = -ENOMEM;
7571 drop_inode = 1;
7572 goto out_unlock;
7573 }
Li Zefan33345d012011-04-20 10:31:50 +08007574 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007575 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007576 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7577 datasize = btrfs_file_extent_calc_inline_size(name_len);
7578 err = btrfs_insert_empty_item(trans, root, path, &key,
7579 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007580 if (err) {
7581 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007582 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007583 goto out_unlock;
7584 }
Chris Mason5f39d392007-10-15 16:14:19 -04007585 leaf = path->nodes[0];
7586 ei = btrfs_item_ptr(leaf, path->slots[0],
7587 struct btrfs_file_extent_item);
7588 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7589 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007590 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007591 btrfs_set_file_extent_encryption(leaf, ei, 0);
7592 btrfs_set_file_extent_compression(leaf, ei, 0);
7593 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7594 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7595
Chris Mason39279cc2007-06-12 06:35:45 -04007596 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007597 write_extent_buffer(leaf, symname, ptr, name_len);
7598 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007599 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007600
Chris Mason39279cc2007-06-12 06:35:45 -04007601 inode->i_op = &btrfs_symlink_inode_operations;
7602 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007603 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007604 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007605 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007606 err = btrfs_update_inode(trans, root, inode);
7607 if (err)
7608 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007609
7610out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007611 if (!err)
7612 d_instantiate(dentry, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007613 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007614 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007615 if (drop_inode) {
7616 inode_dec_link_count(inode);
7617 iput(inode);
7618 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007619 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007620 return err;
7621}
Chris Mason16432982008-04-10 10:23:21 -04007622
Josef Bacik0af3d002010-06-21 14:48:16 -04007623static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7624 u64 start, u64 num_bytes, u64 min_size,
7625 loff_t actual_len, u64 *alloc_hint,
7626 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007627{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007628 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7629 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007630 struct btrfs_root *root = BTRFS_I(inode)->root;
7631 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007632 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007633 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007634 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007635 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007636
Josef Bacik0af3d002010-06-21 14:48:16 -04007637 if (trans)
7638 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007639 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007640 if (own_trans) {
7641 trans = btrfs_start_transaction(root, 3);
7642 if (IS_ERR(trans)) {
7643 ret = PTR_ERR(trans);
7644 break;
7645 }
Yan Zhengd899e052008-10-30 14:25:28 -04007646 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007647
Yan, Zhengefa56462010-05-16 10:49:59 -04007648 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007649 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007650 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007651 if (own_trans)
7652 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007653 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007654 }
7655
Yan Zhengd899e052008-10-30 14:25:28 -04007656 ret = insert_reserved_file_extent(trans, inode,
7657 cur_offset, ins.objectid,
7658 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007659 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007660 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007661 if (ret) {
7662 btrfs_abort_transaction(trans, root, ret);
7663 if (own_trans)
7664 btrfs_end_transaction(trans, root);
7665 break;
7666 }
Chris Masona1ed8352009-09-11 12:27:37 -04007667 btrfs_drop_extent_cache(inode, cur_offset,
7668 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007669
Josef Bacik5dc562c2012-08-17 13:14:17 -04007670 em = alloc_extent_map();
7671 if (!em) {
7672 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7673 &BTRFS_I(inode)->runtime_flags);
7674 goto next;
7675 }
7676
7677 em->start = cur_offset;
7678 em->orig_start = cur_offset;
7679 em->len = ins.offset;
7680 em->block_start = ins.objectid;
7681 em->block_len = ins.offset;
7682 em->bdev = root->fs_info->fs_devices->latest_bdev;
7683 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7684 em->generation = trans->transid;
7685
7686 while (1) {
7687 write_lock(&em_tree->lock);
7688 ret = add_extent_mapping(em_tree, em);
7689 if (!ret)
7690 list_move(&em->list,
7691 &em_tree->modified_extents);
7692 write_unlock(&em_tree->lock);
7693 if (ret != -EEXIST)
7694 break;
7695 btrfs_drop_extent_cache(inode, cur_offset,
7696 cur_offset + ins.offset - 1,
7697 0);
7698 }
7699 free_extent_map(em);
7700next:
Yan Zhengd899e052008-10-30 14:25:28 -04007701 num_bytes -= ins.offset;
7702 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007703 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007704
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007705 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007706 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007707 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007708 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007709 (actual_len > inode->i_size) &&
7710 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007711 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007712 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007713 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007714 i_size = cur_offset;
7715 i_size_write(inode, i_size);
7716 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007717 }
7718
Yan Zhengd899e052008-10-30 14:25:28 -04007719 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007720
7721 if (ret) {
7722 btrfs_abort_transaction(trans, root, ret);
7723 if (own_trans)
7724 btrfs_end_transaction(trans, root);
7725 break;
7726 }
Yan Zhengd899e052008-10-30 14:25:28 -04007727
Josef Bacik0af3d002010-06-21 14:48:16 -04007728 if (own_trans)
7729 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007730 }
Yan Zhengd899e052008-10-30 14:25:28 -04007731 return ret;
7732}
7733
Josef Bacik0af3d002010-06-21 14:48:16 -04007734int btrfs_prealloc_file_range(struct inode *inode, int mode,
7735 u64 start, u64 num_bytes, u64 min_size,
7736 loff_t actual_len, u64 *alloc_hint)
7737{
7738 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7739 min_size, actual_len, alloc_hint,
7740 NULL);
7741}
7742
7743int btrfs_prealloc_file_range_trans(struct inode *inode,
7744 struct btrfs_trans_handle *trans, int mode,
7745 u64 start, u64 num_bytes, u64 min_size,
7746 loff_t actual_len, u64 *alloc_hint)
7747{
7748 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7749 min_size, actual_len, alloc_hint, trans);
7750}
7751
Chris Masone6dcd2d2008-07-17 12:53:50 -04007752static int btrfs_set_page_dirty(struct page *page)
7753{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007754 return __set_page_dirty_nobuffers(page);
7755}
7756
Al Viro10556cb2011-06-20 19:28:19 -04007757static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007758{
Li Zefanb83cc962010-12-20 16:04:08 +08007759 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007760 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007761
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007762 if (mask & MAY_WRITE &&
7763 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7764 if (btrfs_root_readonly(root))
7765 return -EROFS;
7766 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7767 return -EACCES;
7768 }
Al Viro2830ba72011-06-20 19:16:29 -04007769 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007770}
Chris Mason39279cc2007-06-12 06:35:45 -04007771
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007772static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007773 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007774 .lookup = btrfs_lookup,
7775 .create = btrfs_create,
7776 .unlink = btrfs_unlink,
7777 .link = btrfs_link,
7778 .mkdir = btrfs_mkdir,
7779 .rmdir = btrfs_rmdir,
7780 .rename = btrfs_rename,
7781 .symlink = btrfs_symlink,
7782 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007783 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007784 .setxattr = btrfs_setxattr,
7785 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007786 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007787 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007788 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007789 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007790};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007791static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007792 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007793 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007794 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007795};
Yan, Zheng76dda932009-09-21 16:00:26 -04007796
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007797static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007798 .llseek = generic_file_llseek,
7799 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007800 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007801 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007802#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007803 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007804#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007805 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007806 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007807};
7808
Chris Masond1310b22008-01-24 16:13:08 -05007809static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007810 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007811 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007812 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007813 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007814 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007815 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007816 .set_bit_hook = btrfs_set_bit_hook,
7817 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007818 .merge_extent_hook = btrfs_merge_extent_hook,
7819 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007820};
7821
Chris Mason35054392009-01-21 13:11:13 -05007822/*
7823 * btrfs doesn't support the bmap operation because swapfiles
7824 * use bmap to make a mapping of extents in the file. They assume
7825 * these extents won't change over the life of the file and they
7826 * use the bmap result to do IO directly to the drive.
7827 *
7828 * the btrfs bmap call would return logical addresses that aren't
7829 * suitable for IO and they also will change frequently as COW
7830 * operations happen. So, swapfile + btrfs == corruption.
7831 *
7832 * For now we're avoiding this by dropping bmap.
7833 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007834static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007835 .readpage = btrfs_readpage,
7836 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007837 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007838 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007839 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007840 .invalidatepage = btrfs_invalidatepage,
7841 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007842 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007843 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007844};
7845
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007846static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007847 .readpage = btrfs_readpage,
7848 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007849 .invalidatepage = btrfs_invalidatepage,
7850 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007851};
7852
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007853static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007854 .getattr = btrfs_getattr,
7855 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007856 .setxattr = btrfs_setxattr,
7857 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007858 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007859 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007860 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007861 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007862 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007863 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007864};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007865static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007866 .getattr = btrfs_getattr,
7867 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007868 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007869 .setxattr = btrfs_setxattr,
7870 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007871 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007872 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007873 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007874 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04007875};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007876static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007877 .readlink = generic_readlink,
7878 .follow_link = page_follow_link_light,
7879 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007880 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05007881 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007882 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007883 .setxattr = btrfs_setxattr,
7884 .getxattr = btrfs_getxattr,
7885 .listxattr = btrfs_listxattr,
7886 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007887 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007888 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007889};
Yan, Zheng76dda932009-09-21 16:00:26 -04007890
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007891const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007892 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007893 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04007894};