blob: 3d2c64d4734ac73ee056d9b9a300c4e965fb9d61 [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;
Miao Xie8ccf6f192012-10-25 09:28:04 +000074static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075struct kmem_cache *btrfs_trans_handle_cachep;
76struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050078struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
80#define S_SHIFT 12
81static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
82 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
83 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
84 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
85 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
86 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
87 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
88 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
89};
90
Josef Bacika41ad392011-01-31 15:30:16 -050091static int btrfs_setsize(struct inode *inode, loff_t newsize);
92static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040093static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050094static noinline int cow_file_range(struct inode *inode,
95 struct page *locked_page,
96 u64 start, u64 end, int *page_started,
97 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -040098static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
99 u64 len, u64 orig_start,
100 u64 block_start, u64 block_len,
101 u64 orig_block_len, int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400102
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000103static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500104 struct inode *inode, struct inode *dir,
105 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500106{
107 int err;
108
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000109 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500110 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500111 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500112 return err;
113}
114
Chris Masond352ac62008-09-29 15:18:18 -0400115/*
Chris Masonc8b97812008-10-29 14:49:59 -0400116 * this does all the hard work for inserting an inline extent into
117 * the btree. The caller should have done a btrfs_drop_extents so that
118 * no overlapping inline items exist in the btree
119 */
Chris Masond3977122009-01-05 21:25:51 -0500120static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400121 struct btrfs_root *root, struct inode *inode,
122 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000123 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400124 struct page **compressed_pages)
125{
126 struct btrfs_key key;
127 struct btrfs_path *path;
128 struct extent_buffer *leaf;
129 struct page *page = NULL;
130 char *kaddr;
131 unsigned long ptr;
132 struct btrfs_file_extent_item *ei;
133 int err = 0;
134 int ret;
135 size_t cur_size = size;
136 size_t datasize;
137 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400138
Li Zefanfe3f5662011-03-28 08:30:38 +0000139 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400140 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400141
Chris Masond3977122009-01-05 21:25:51 -0500142 path = btrfs_alloc_path();
143 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400144 return -ENOMEM;
145
Chris Masonb9473432009-03-13 11:00:37 -0400146 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Li Zefan33345d012011-04-20 10:31:50 +0800148 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400149 key.offset = start;
150 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400151 datasize = btrfs_file_extent_calc_inline_size(cur_size);
152
153 inode_add_bytes(inode, size);
154 ret = btrfs_insert_empty_item(trans, root, path, &key,
155 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400156 if (ret) {
157 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400158 goto fail;
159 }
160 leaf = path->nodes[0];
161 ei = btrfs_item_ptr(leaf, path->slots[0],
162 struct btrfs_file_extent_item);
163 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
164 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
165 btrfs_set_file_extent_encryption(leaf, ei, 0);
166 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
167 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
168 ptr = btrfs_file_extent_inline_start(ei);
169
Li Zefan261507a02010-12-17 14:21:50 +0800170 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct page *cpage;
172 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500173 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400174 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500175 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400176 PAGE_CACHE_SIZE);
177
Cong Wang7ac687d2011-11-25 23:14:28 +0800178 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400179 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800180 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400181
182 i++;
183 ptr += cur_size;
184 compressed_size -= cur_size;
185 }
186 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800187 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400188 } else {
189 page = find_get_page(inode->i_mapping,
190 start >> PAGE_CACHE_SHIFT);
191 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800192 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 offset = start & (PAGE_CACHE_SIZE - 1);
194 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800195 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400196 page_cache_release(page);
197 }
198 btrfs_mark_buffer_dirty(leaf);
199 btrfs_free_path(path);
200
Yan, Zhengc2167752009-11-12 09:34:21 +0000201 /*
202 * we're an inline extent, so nobody can
203 * extend the file past i_size without locking
204 * a page we already have locked.
205 *
206 * We must do any isize and inode updates
207 * before we unlock the pages. Otherwise we
208 * could end up racing with unlink.
209 */
Chris Masonc8b97812008-10-29 14:49:59 -0400210 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100211 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000212
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100213 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400214fail:
215 btrfs_free_path(path);
216 return err;
217}
218
219
220/*
221 * conditionally insert an inline extent into the file. This
222 * does the checks required to make sure the data is small enough
223 * to fit as an inline extent.
224 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400225static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400226 struct btrfs_root *root,
227 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000228 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400229 struct page **compressed_pages)
230{
231 u64 isize = i_size_read(inode);
232 u64 actual_end = min(end + 1, isize);
233 u64 inline_len = actual_end - start;
234 u64 aligned_end = (end + root->sectorsize - 1) &
235 ~((u64)root->sectorsize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 u64 data_len = inline_len;
237 int ret;
238
239 if (compressed_size)
240 data_len = compressed_size;
241
242 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400243 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400244 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
245 (!compressed_size &&
246 (actual_end & (root->sectorsize - 1)) == 0) ||
247 end + 1 < isize ||
248 data_len > root->fs_info->max_inline) {
249 return 1;
250 }
251
Josef Bacik26714852012-08-29 12:24:27 -0400252 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100253 if (ret)
254 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400255
256 if (isize > actual_end)
257 inline_len = min_t(u64, isize, actual_end);
258 ret = insert_inline_extent(trans, root, inode, start,
259 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000260 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400261 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100262 btrfs_abort_transaction(trans, root, ret);
263 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400264 } else if (ret == -ENOSPC) {
265 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100266 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400267
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400268 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400269 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400270 return 0;
271}
272
Chris Mason771ed682008-11-06 22:02:51 -0500273struct async_extent {
274 u64 start;
275 u64 ram_size;
276 u64 compressed_size;
277 struct page **pages;
278 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800279 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500280 struct list_head list;
281};
282
283struct async_cow {
284 struct inode *inode;
285 struct btrfs_root *root;
286 struct page *locked_page;
287 u64 start;
288 u64 end;
289 struct list_head extents;
290 struct btrfs_work work;
291};
292
293static noinline int add_async_extent(struct async_cow *cow,
294 u64 start, u64 ram_size,
295 u64 compressed_size,
296 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800297 unsigned long nr_pages,
298 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500299{
300 struct async_extent *async_extent;
301
302 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100303 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500304 async_extent->start = start;
305 async_extent->ram_size = ram_size;
306 async_extent->compressed_size = compressed_size;
307 async_extent->pages = pages;
308 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800309 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500310 list_add_tail(&async_extent->list, &cow->extents);
311 return 0;
312}
313
Chris Masonc8b97812008-10-29 14:49:59 -0400314/*
Chris Mason771ed682008-11-06 22:02:51 -0500315 * we create compressed extents in two phases. The first
316 * phase compresses a range of pages that have already been
317 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400318 *
Chris Mason771ed682008-11-06 22:02:51 -0500319 * This is done inside an ordered work queue, and the compression
320 * is spread across many cpus. The actual IO submission is step
321 * two, and the ordered work queue takes care of making sure that
322 * happens in the same order things were put onto the queue by
323 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400324 *
Chris Mason771ed682008-11-06 22:02:51 -0500325 * If this code finds it can't get good compression, it puts an
326 * entry onto the work queue to write the uncompressed bytes. This
327 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300328 * are written in the same order that the flusher thread sent them
329 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400330 */
Chris Mason771ed682008-11-06 22:02:51 -0500331static noinline int compress_file_range(struct inode *inode,
332 struct page *locked_page,
333 u64 start, u64 end,
334 struct async_cow *async_cow,
335 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400336{
337 struct btrfs_root *root = BTRFS_I(inode)->root;
338 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400339 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400340 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400341 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500342 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400343 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400344 struct page **pages = NULL;
345 unsigned long nr_pages;
346 unsigned long nr_pages_ret = 0;
347 unsigned long total_compressed = 0;
348 unsigned long total_in = 0;
349 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500350 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400351 int i;
352 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800353 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400354
Liu Bo4cb13e52012-03-29 09:57:45 -0400355 /* if this is a small write inside eof, kick off a defrag */
356 if ((end - start + 1) < 16 * 1024 &&
357 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400358 btrfs_add_inode_defrag(NULL, inode);
359
Chris Mason42dc7ba2008-12-15 11:44:56 -0500360 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400361again:
362 will_compress = 0;
363 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
364 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
365
Chris Masonf03d9301f2009-02-04 09:31:06 -0500366 /*
367 * we don't want to send crud past the end of i_size through
368 * compression, that's just a waste of CPU time. So, if the
369 * end of the file is before the start of our current
370 * requested range of bytes, we bail out to the uncompressed
371 * cleanup code that can deal with all of this.
372 *
373 * It isn't really the fastest way to fix things, but this is a
374 * very uncommon corner.
375 */
376 if (actual_end <= start)
377 goto cleanup_and_bail_uncompressed;
378
Chris Masonc8b97812008-10-29 14:49:59 -0400379 total_compressed = actual_end - start;
380
381 /* we want to make sure that amount of ram required to uncompress
382 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500383 * of a compressed extent to 128k. This is a crucial number
384 * because it also controls how easily we can spread reads across
385 * cpus for decompression.
386 *
387 * We also want to make sure the amount of IO required to do
388 * a random read is reasonably small, so we limit the size of
389 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400390 */
391 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400392 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500393 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400394 total_in = 0;
395 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400396
Chris Mason771ed682008-11-06 22:02:51 -0500397 /*
398 * we do compression for mount -o compress and when the
399 * inode has not been flagged as nocompress. This flag can
400 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400401 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200402 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500403 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000404 (BTRFS_I(inode)->force_compress) ||
405 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400406 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400407 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800408 if (!pages) {
409 /* just bail out to the uncompressed code */
410 goto cont;
411 }
Chris Mason179e29e2007-11-01 11:28:41 -0400412
Li Zefan261507a02010-12-17 14:21:50 +0800413 if (BTRFS_I(inode)->force_compress)
414 compress_type = BTRFS_I(inode)->force_compress;
415
416 ret = btrfs_compress_pages(compress_type,
417 inode->i_mapping, start,
418 total_compressed, pages,
419 nr_pages, &nr_pages_ret,
420 &total_in,
421 &total_compressed,
422 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400423
424 if (!ret) {
425 unsigned long offset = total_compressed &
426 (PAGE_CACHE_SIZE - 1);
427 struct page *page = pages[nr_pages_ret - 1];
428 char *kaddr;
429
430 /* zero the tail end of the last page, we might be
431 * sending it down to disk
432 */
433 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800434 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400435 memset(kaddr + offset, 0,
436 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800437 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400438 }
439 will_compress = 1;
440 }
441 }
Li Zefan560f7d72011-09-08 10:22:01 +0800442cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400443 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400444 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100445 if (IS_ERR(trans)) {
446 ret = PTR_ERR(trans);
447 trans = NULL;
448 goto cleanup_and_out;
449 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400450 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500451
Chris Masonc8b97812008-10-29 14:49:59 -0400452 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500453 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400454 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500455 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400456 */
457 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000458 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400459 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500460 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400461 ret = cow_file_range_inline(trans, root, inode,
462 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000463 total_compressed,
464 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400465 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100466 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500467 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100468 * inline extent creation worked or returned error,
469 * we don't need to create any more async work items.
470 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500471 */
Chris Masonc8b97812008-10-29 14:49:59 -0400472 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400473 &BTRFS_I(inode)->io_tree,
474 start, end, NULL,
475 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400476 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400477 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000478
479 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400480 goto free_pages_out;
481 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000482 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400483 }
484
485 if (will_compress) {
486 /*
487 * we aren't doing an inline extent round the compressed size
488 * up to a block size boundary so the allocator does sane
489 * things
490 */
491 total_compressed = (total_compressed + blocksize - 1) &
492 ~(blocksize - 1);
493
494 /*
495 * one last check to make sure the compression is really a
496 * win, compare the page count read with the blocks on disk
497 */
498 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
499 ~(PAGE_CACHE_SIZE - 1);
500 if (total_compressed >= total_in) {
501 will_compress = 0;
502 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400503 num_bytes = total_in;
504 }
505 }
506 if (!will_compress && pages) {
507 /*
508 * the compression code ran but failed to make things smaller,
509 * free any pages it allocated and our page pointer array
510 */
511 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400512 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400513 page_cache_release(pages[i]);
514 }
515 kfree(pages);
516 pages = NULL;
517 total_compressed = 0;
518 nr_pages_ret = 0;
519
520 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500521 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
522 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500523 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500524 }
Chris Masonc8b97812008-10-29 14:49:59 -0400525 }
Chris Mason771ed682008-11-06 22:02:51 -0500526 if (will_compress) {
527 *num_added += 1;
528
529 /* the async work queues will take care of doing actual
530 * allocation on disk for these compressed pages,
531 * and will submit them to the elevator.
532 */
533 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800534 total_compressed, pages, nr_pages_ret,
535 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500536
Yan, Zheng24ae6362010-12-06 07:02:36 +0000537 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500538 start += num_bytes;
539 pages = NULL;
540 cond_resched();
541 goto again;
542 }
543 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500544cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500545 /*
546 * No compression, but we still need to write the pages in
547 * the file we've been given so far. redirty the locked
548 * page if it corresponds to our extent and set things up
549 * for the async work queue to run cow_file_range to do
550 * the normal delalloc dance
551 */
552 if (page_offset(locked_page) >= start &&
553 page_offset(locked_page) <= end) {
554 __set_page_dirty_nobuffers(locked_page);
555 /* unlocked later on in the async handlers */
556 }
Li Zefan261507a02010-12-17 14:21:50 +0800557 add_async_extent(async_cow, start, end - start + 1,
558 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500559 *num_added += 1;
560 }
561
562out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100563 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500564
565free_pages_out:
566 for (i = 0; i < nr_pages_ret; i++) {
567 WARN_ON(pages[i]->mapping);
568 page_cache_release(pages[i]);
569 }
Chris Masond3977122009-01-05 21:25:51 -0500570 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500571
572 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100573
574cleanup_and_out:
575 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
576 start, end, NULL,
577 EXTENT_CLEAR_UNLOCK_PAGE |
578 EXTENT_CLEAR_DIRTY |
579 EXTENT_CLEAR_DELALLOC |
580 EXTENT_SET_WRITEBACK |
581 EXTENT_END_WRITEBACK);
582 if (!trans || IS_ERR(trans))
583 btrfs_error(root->fs_info, ret, "Failed to join transaction");
584 else
585 btrfs_abort_transaction(trans, root, ret);
586 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500587}
588
589/*
590 * phase two of compressed writeback. This is the ordered portion
591 * of the code, which only gets called in the order the work was
592 * queued. We walk all the async extents created by compress_file_range
593 * and send them down to the disk.
594 */
595static noinline int submit_compressed_extents(struct inode *inode,
596 struct async_cow *async_cow)
597{
598 struct async_extent *async_extent;
599 u64 alloc_hint = 0;
600 struct btrfs_trans_handle *trans;
601 struct btrfs_key ins;
602 struct extent_map *em;
603 struct btrfs_root *root = BTRFS_I(inode)->root;
604 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
605 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500606 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500607
608 if (list_empty(&async_cow->extents))
609 return 0;
610
Chris Mason771ed682008-11-06 22:02:51 -0500611
Chris Masond3977122009-01-05 21:25:51 -0500612 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500613 async_extent = list_entry(async_cow->extents.next,
614 struct async_extent, list);
615 list_del(&async_extent->list);
616
617 io_tree = &BTRFS_I(inode)->io_tree;
618
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500619retry:
Chris Mason771ed682008-11-06 22:02:51 -0500620 /* did the compression code fall back to uncompressed IO? */
621 if (!async_extent->pages) {
622 int page_started = 0;
623 unsigned long nr_written = 0;
624
625 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000626 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100627 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500628
629 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500630 ret = cow_file_range(inode, async_cow->locked_page,
631 async_extent->start,
632 async_extent->start +
633 async_extent->ram_size - 1,
634 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500635
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100636 /* JDM XXX */
637
Chris Mason771ed682008-11-06 22:02:51 -0500638 /*
639 * if page_started, cow_file_range inserted an
640 * inline extent and took care of all the unlocking
641 * and IO for us. Otherwise, we need to submit
642 * all those pages down to the drive.
643 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500644 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500645 extent_write_locked_range(io_tree,
646 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500647 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500648 async_extent->ram_size - 1,
649 btrfs_get_extent,
650 WB_SYNC_ALL);
651 kfree(async_extent);
652 cond_resched();
653 continue;
654 }
655
656 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100657 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500658
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400659 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100660 if (IS_ERR(trans)) {
661 ret = PTR_ERR(trans);
662 } else {
663 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
664 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500665 async_extent->compressed_size,
666 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500667 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600668 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100669 btrfs_abort_transaction(trans, root, ret);
670 btrfs_end_transaction(trans, root);
671 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000672
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500673 if (ret) {
674 int i;
675 for (i = 0; i < async_extent->nr_pages; i++) {
676 WARN_ON(async_extent->pages[i]->mapping);
677 page_cache_release(async_extent->pages[i]);
678 }
679 kfree(async_extent->pages);
680 async_extent->nr_pages = 0;
681 async_extent->pages = NULL;
682 unlock_extent(io_tree, async_extent->start,
683 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100684 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100685 if (ret == -ENOSPC)
686 goto retry;
687 goto out_free; /* JDM: Requeue? */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500688 }
689
Yan, Zhengc2167752009-11-12 09:34:21 +0000690 /*
691 * here we're doing allocation and writeback of the
692 * compressed pages
693 */
694 btrfs_drop_extent_cache(inode, async_extent->start,
695 async_extent->start +
696 async_extent->ram_size - 1, 0);
697
David Sterba172ddd62011-04-21 00:48:27 +0200698 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100699 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500700 em->start = async_extent->start;
701 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500702 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500703
704 em->block_start = ins.objectid;
705 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500706 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500707 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800708 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500709 set_bit(EXTENT_FLAG_PINNED, &em->flags);
710 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400711 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500712
Chris Masond3977122009-01-05 21:25:51 -0500713 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400714 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500715 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400716 if (!ret)
717 list_move(&em->list,
718 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400719 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500720 if (ret != -EEXIST) {
721 free_extent_map(em);
722 break;
723 }
724 btrfs_drop_extent_cache(inode, async_extent->start,
725 async_extent->start +
726 async_extent->ram_size - 1, 0);
727 }
728
Li Zefan261507a02010-12-17 14:21:50 +0800729 ret = btrfs_add_ordered_extent_compress(inode,
730 async_extent->start,
731 ins.objectid,
732 async_extent->ram_size,
733 ins.offset,
734 BTRFS_ORDERED_COMPRESSED,
735 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100736 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500737
Chris Mason771ed682008-11-06 22:02:51 -0500738 /*
739 * clear dirty, set writeback and unlock the pages.
740 */
741 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400742 &BTRFS_I(inode)->io_tree,
743 async_extent->start,
744 async_extent->start +
745 async_extent->ram_size - 1,
746 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
747 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400748 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400749 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500750
751 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500752 async_extent->start,
753 async_extent->ram_size,
754 ins.objectid,
755 ins.offset, async_extent->pages,
756 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500757
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100758 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500759 alloc_hint = ins.objectid + ins.offset;
760 kfree(async_extent);
761 cond_resched();
762 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100763 ret = 0;
764out:
765 return ret;
766out_free:
767 kfree(async_extent);
768 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500769}
770
Josef Bacik4b46fce2010-05-23 11:00:55 -0400771static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
772 u64 num_bytes)
773{
774 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
775 struct extent_map *em;
776 u64 alloc_hint = 0;
777
778 read_lock(&em_tree->lock);
779 em = search_extent_mapping(em_tree, start, num_bytes);
780 if (em) {
781 /*
782 * if block start isn't an actual block number then find the
783 * first block in this inode and use that as a hint. If that
784 * block is also bogus then just don't worry about it.
785 */
786 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
787 free_extent_map(em);
788 em = search_extent_mapping(em_tree, 0, 0);
789 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
790 alloc_hint = em->block_start;
791 if (em)
792 free_extent_map(em);
793 } else {
794 alloc_hint = em->block_start;
795 free_extent_map(em);
796 }
797 }
798 read_unlock(&em_tree->lock);
799
800 return alloc_hint;
801}
802
Chris Mason771ed682008-11-06 22:02:51 -0500803/*
804 * when extent_io.c finds a delayed allocation range in the file,
805 * the call backs end up in this code. The basic idea is to
806 * allocate extents on disk for the range, and create ordered data structs
807 * in ram to track those extents.
808 *
809 * locked_page is the page that writepage had locked already. We use
810 * it to make sure we don't do extra locks or unlocks.
811 *
812 * *page_started is set to one if we unlock locked_page and do everything
813 * required to start IO on it. It may be clean and already done with
814 * IO when we return.
815 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000816static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
817 struct inode *inode,
818 struct btrfs_root *root,
819 struct page *locked_page,
820 u64 start, u64 end, int *page_started,
821 unsigned long *nr_written,
822 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500823{
Chris Mason771ed682008-11-06 22:02:51 -0500824 u64 alloc_hint = 0;
825 u64 num_bytes;
826 unsigned long ram_size;
827 u64 disk_num_bytes;
828 u64 cur_alloc_size;
829 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500830 struct btrfs_key ins;
831 struct extent_map *em;
832 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
833 int ret = 0;
834
Liu Bo83eea1f2012-07-10 05:28:39 -0600835 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500836
Chris Mason771ed682008-11-06 22:02:51 -0500837 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
838 num_bytes = max(blocksize, num_bytes);
839 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500840
Chris Mason4cb53002011-05-24 15:35:30 -0400841 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400842 if (num_bytes < 64 * 1024 &&
843 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400844 btrfs_add_inode_defrag(trans, inode);
845
Chris Mason771ed682008-11-06 22:02:51 -0500846 if (start == 0) {
847 /* lets try to make an inline extent */
848 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000849 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500850 if (ret == 0) {
851 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400852 &BTRFS_I(inode)->io_tree,
853 start, end, NULL,
854 EXTENT_CLEAR_UNLOCK_PAGE |
855 EXTENT_CLEAR_UNLOCK |
856 EXTENT_CLEAR_DELALLOC |
857 EXTENT_CLEAR_DIRTY |
858 EXTENT_SET_WRITEBACK |
859 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000860
Chris Mason771ed682008-11-06 22:02:51 -0500861 *nr_written = *nr_written +
862 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
863 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500864 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100865 } else if (ret < 0) {
866 btrfs_abort_transaction(trans, root, ret);
867 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500868 }
869 }
Chris Masonc8b97812008-10-29 14:49:59 -0400870
871 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200872 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400873
Josef Bacik4b46fce2010-05-23 11:00:55 -0400874 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400875 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400876
Chris Masond3977122009-01-05 21:25:51 -0500877 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400878 unsigned long op;
879
Josef Bacik287a0ab2010-03-19 18:07:23 +0000880 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400881 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500882 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500883 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100884 if (ret < 0) {
885 btrfs_abort_transaction(trans, root, ret);
886 goto out_unlock;
887 }
Chris Masond3977122009-01-05 21:25:51 -0500888
David Sterba172ddd62011-04-21 00:48:27 +0200889 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400891 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500892 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500893 ram_size = ins.offset;
894 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400895
Chris Masone6dcd2d2008-07-17 12:53:50 -0400896 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400897 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500898 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400899 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400900 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400901 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400902
Chris Masond3977122009-01-05 21:25:51 -0500903 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400904 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400905 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400906 if (!ret)
907 list_move(&em->list,
908 &em_tree->modified_extents);
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 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100955out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500956 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000957
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100958out_unlock:
959 extent_clear_unlock_delalloc(inode,
960 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400961 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100962 EXTENT_CLEAR_UNLOCK_PAGE |
963 EXTENT_CLEAR_UNLOCK |
964 EXTENT_CLEAR_DELALLOC |
965 EXTENT_CLEAR_DIRTY |
966 EXTENT_SET_WRITEBACK |
967 EXTENT_END_WRITEBACK);
968
969 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500970}
Chris Masonc8b97812008-10-29 14:49:59 -0400971
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000972static noinline int cow_file_range(struct inode *inode,
973 struct page *locked_page,
974 u64 start, u64 end, int *page_started,
975 unsigned long *nr_written,
976 int unlock)
977{
978 struct btrfs_trans_handle *trans;
979 struct btrfs_root *root = BTRFS_I(inode)->root;
980 int ret;
981
982 trans = btrfs_join_transaction(root);
983 if (IS_ERR(trans)) {
984 extent_clear_unlock_delalloc(inode,
985 &BTRFS_I(inode)->io_tree,
986 start, end, locked_page,
987 EXTENT_CLEAR_UNLOCK_PAGE |
988 EXTENT_CLEAR_UNLOCK |
989 EXTENT_CLEAR_DELALLOC |
990 EXTENT_CLEAR_DIRTY |
991 EXTENT_SET_WRITEBACK |
992 EXTENT_END_WRITEBACK);
993 return PTR_ERR(trans);
994 }
995 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
996
997 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
998 page_started, nr_written, unlock);
999
1000 btrfs_end_transaction(trans, root);
1001
1002 return ret;
1003}
1004
Chris Mason771ed682008-11-06 22:02:51 -05001005/*
1006 * work queue call back to started compression on a file and pages
1007 */
1008static noinline void async_cow_start(struct btrfs_work *work)
1009{
1010 struct async_cow *async_cow;
1011 int num_added = 0;
1012 async_cow = container_of(work, struct async_cow, work);
1013
1014 compress_file_range(async_cow->inode, async_cow->locked_page,
1015 async_cow->start, async_cow->end, async_cow,
1016 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001017 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001018 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001019 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001020 }
Chris Mason771ed682008-11-06 22:02:51 -05001021}
1022
1023/*
1024 * work queue call back to submit previously compressed pages
1025 */
1026static noinline void async_cow_submit(struct btrfs_work *work)
1027{
1028 struct async_cow *async_cow;
1029 struct btrfs_root *root;
1030 unsigned long nr_pages;
1031
1032 async_cow = container_of(work, struct async_cow, work);
1033
1034 root = async_cow->root;
1035 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1036 PAGE_CACHE_SHIFT;
1037
Josef Bacik66657b32012-08-01 15:36:24 -04001038 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001039 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001040 waitqueue_active(&root->fs_info->async_submit_wait))
1041 wake_up(&root->fs_info->async_submit_wait);
1042
Chris Masond3977122009-01-05 21:25:51 -05001043 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001044 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001045}
Chris Masonc8b97812008-10-29 14:49:59 -04001046
Chris Mason771ed682008-11-06 22:02:51 -05001047static noinline void async_cow_free(struct btrfs_work *work)
1048{
1049 struct async_cow *async_cow;
1050 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001051 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001052 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001053 kfree(async_cow);
1054}
1055
1056static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1057 u64 start, u64 end, int *page_started,
1058 unsigned long *nr_written)
1059{
1060 struct async_cow *async_cow;
1061 struct btrfs_root *root = BTRFS_I(inode)->root;
1062 unsigned long nr_pages;
1063 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001064 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001065
Chris Masona3429ab2009-10-08 12:30:20 -04001066 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1067 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001068 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001069 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001070 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001071 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001072 async_cow->root = root;
1073 async_cow->locked_page = locked_page;
1074 async_cow->start = start;
1075
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001076 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001077 cur_end = end;
1078 else
1079 cur_end = min(end, start + 512 * 1024 - 1);
1080
1081 async_cow->end = cur_end;
1082 INIT_LIST_HEAD(&async_cow->extents);
1083
1084 async_cow->work.func = async_cow_start;
1085 async_cow->work.ordered_func = async_cow_submit;
1086 async_cow->work.ordered_free = async_cow_free;
1087 async_cow->work.flags = 0;
1088
Chris Mason771ed682008-11-06 22:02:51 -05001089 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1090 PAGE_CACHE_SHIFT;
1091 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1092
1093 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1094 &async_cow->work);
1095
1096 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1097 wait_event(root->fs_info->async_submit_wait,
1098 (atomic_read(&root->fs_info->async_delalloc_pages) <
1099 limit));
1100 }
1101
Chris Masond3977122009-01-05 21:25:51 -05001102 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001103 atomic_read(&root->fs_info->async_delalloc_pages)) {
1104 wait_event(root->fs_info->async_submit_wait,
1105 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1106 0));
1107 }
1108
1109 *nr_written += nr_pages;
1110 start = cur_end + 1;
1111 }
1112 *page_started = 1;
1113 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001114}
1115
Chris Masond3977122009-01-05 21:25:51 -05001116static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001117 u64 bytenr, u64 num_bytes)
1118{
1119 int ret;
1120 struct btrfs_ordered_sum *sums;
1121 LIST_HEAD(list);
1122
Yan Zheng07d400a2009-01-06 11:42:00 -05001123 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001124 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001125 if (ret == 0 && list_empty(&list))
1126 return 0;
1127
1128 while (!list_empty(&list)) {
1129 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1130 list_del(&sums->list);
1131 kfree(sums);
1132 }
1133 return 1;
1134}
1135
Chris Masond352ac62008-09-29 15:18:18 -04001136/*
1137 * when nowcow writeback call back. This checks for snapshots or COW copies
1138 * of the extents that exist in the file, and COWs the file as required.
1139 *
1140 * If no cow copies or snapshots exist, we write directly to the existing
1141 * blocks on disk
1142 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001143static noinline int run_delalloc_nocow(struct inode *inode,
1144 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001145 u64 start, u64 end, int *page_started, int force,
1146 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001147{
Chris Masonbe20aa92007-12-17 20:14:01 -05001148 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001149 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001150 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001151 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001152 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001153 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001154 u64 cow_start;
1155 u64 cur_offset;
1156 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001157 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001158 u64 disk_bytenr;
1159 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001160 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001161 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001162 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001163 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001164 int nocow;
1165 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001166 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001167 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001168
1169 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001170 if (!path) {
1171 extent_clear_unlock_delalloc(inode,
1172 &BTRFS_I(inode)->io_tree,
1173 start, end, locked_page,
1174 EXTENT_CLEAR_UNLOCK_PAGE |
1175 EXTENT_CLEAR_UNLOCK |
1176 EXTENT_CLEAR_DELALLOC |
1177 EXTENT_CLEAR_DIRTY |
1178 EXTENT_SET_WRITEBACK |
1179 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001180 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001181 }
Li Zefan82d59022011-04-20 10:33:24 +08001182
Liu Bo83eea1f2012-07-10 05:28:39 -06001183 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001184
1185 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001186 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001187 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001188 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001189
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001190 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001191 extent_clear_unlock_delalloc(inode,
1192 &BTRFS_I(inode)->io_tree,
1193 start, end, locked_page,
1194 EXTENT_CLEAR_UNLOCK_PAGE |
1195 EXTENT_CLEAR_UNLOCK |
1196 EXTENT_CLEAR_DELALLOC |
1197 EXTENT_CLEAR_DIRTY |
1198 EXTENT_SET_WRITEBACK |
1199 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001200 btrfs_free_path(path);
1201 return PTR_ERR(trans);
1202 }
1203
Josef Bacik74b21072011-04-13 12:02:53 -04001204 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001205
Yan Zheng80ff3852008-10-30 14:20:02 -04001206 cow_start = (u64)-1;
1207 cur_offset = start;
1208 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001209 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001210 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001211 if (ret < 0) {
1212 btrfs_abort_transaction(trans, root, ret);
1213 goto error;
1214 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001215 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1216 leaf = path->nodes[0];
1217 btrfs_item_key_to_cpu(leaf, &found_key,
1218 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001219 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001220 found_key.type == BTRFS_EXTENT_DATA_KEY)
1221 path->slots[0]--;
1222 }
1223 check_prev = 0;
1224next_slot:
1225 leaf = path->nodes[0];
1226 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1227 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001228 if (ret < 0) {
1229 btrfs_abort_transaction(trans, root, ret);
1230 goto error;
1231 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001232 if (ret > 0)
1233 break;
1234 leaf = path->nodes[0];
1235 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001236
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 nocow = 0;
1238 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001239 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001241
Li Zefan33345d012011-04-20 10:31:50 +08001242 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1244 found_key.offset > end)
1245 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001246
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 if (found_key.offset > cur_offset) {
1248 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001249 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001250 goto out_check;
1251 }
Chris Masonc31f8832008-01-08 15:46:31 -05001252
Yan Zheng80ff3852008-10-30 14:20:02 -04001253 fi = btrfs_item_ptr(leaf, path->slots[0],
1254 struct btrfs_file_extent_item);
1255 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001256
Yan Zhengd899e052008-10-30 14:25:28 -04001257 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1258 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001260 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 extent_end = found_key.offset +
1262 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001263 disk_num_bytes =
1264 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 if (extent_end <= start) {
1266 path->slots[0]++;
1267 goto next_slot;
1268 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001269 if (disk_bytenr == 0)
1270 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001271 if (btrfs_file_extent_compression(leaf, fi) ||
1272 btrfs_file_extent_encryption(leaf, fi) ||
1273 btrfs_file_extent_other_encoding(leaf, fi))
1274 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001275 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1276 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001277 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001279 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001280 found_key.offset -
1281 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001282 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001283 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001284 disk_bytenr += cur_offset - found_key.offset;
1285 num_bytes = min(end + 1, extent_end) - cur_offset;
1286 /*
1287 * force cow if csum exists in the range.
1288 * this ensure that csum for a given extent are
1289 * either valid or do not exist.
1290 */
1291 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1292 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 nocow = 1;
1294 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1295 extent_end = found_key.offset +
1296 btrfs_file_extent_inline_len(leaf, fi);
1297 extent_end = ALIGN(extent_end, root->sectorsize);
1298 } else {
1299 BUG_ON(1);
1300 }
1301out_check:
1302 if (extent_end <= start) {
1303 path->slots[0]++;
1304 goto next_slot;
1305 }
1306 if (!nocow) {
1307 if (cow_start == (u64)-1)
1308 cow_start = cur_offset;
1309 cur_offset = extent_end;
1310 if (cur_offset > end)
1311 break;
1312 path->slots[0]++;
1313 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001314 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001315
David Sterbab3b4aa72011-04-21 01:20:15 +02001316 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001318 ret = __cow_file_range(trans, inode, root, locked_page,
1319 cow_start, found_key.offset - 1,
1320 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001321 if (ret) {
1322 btrfs_abort_transaction(trans, root, ret);
1323 goto error;
1324 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001326 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001327
Yan Zhengd899e052008-10-30 14:25:28 -04001328 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1329 struct extent_map *em;
1330 struct extent_map_tree *em_tree;
1331 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001332 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001333 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001334 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001335 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001336 em->len = num_bytes;
1337 em->block_len = num_bytes;
1338 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001339 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001340 em->bdev = root->fs_info->fs_devices->latest_bdev;
1341 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001342 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001343 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001344 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001345 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001346 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001347 if (!ret)
1348 list_move(&em->list,
1349 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001350 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001351 if (ret != -EEXIST) {
1352 free_extent_map(em);
1353 break;
1354 }
1355 btrfs_drop_extent_cache(inode, em->start,
1356 em->start + em->len - 1, 0);
1357 }
1358 type = BTRFS_ORDERED_PREALLOC;
1359 } else {
1360 type = BTRFS_ORDERED_NOCOW;
1361 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001362
1363 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001364 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001365 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001366
Yan, Zhengefa56462010-05-16 10:49:59 -04001367 if (root->root_key.objectid ==
1368 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1369 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1370 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001371 if (ret) {
1372 btrfs_abort_transaction(trans, root, ret);
1373 goto error;
1374 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001375 }
1376
Yan Zhengd899e052008-10-30 14:25:28 -04001377 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001378 cur_offset, cur_offset + num_bytes - 1,
1379 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1380 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1381 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001382 cur_offset = extent_end;
1383 if (cur_offset > end)
1384 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001385 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001386 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001387
Josef Bacik17ca04a2012-05-31 15:58:55 -04001388 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001389 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001390 cur_offset = end;
1391 }
1392
Yan Zheng80ff3852008-10-30 14:20:02 -04001393 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001394 ret = __cow_file_range(trans, inode, root, locked_page,
1395 cow_start, end,
1396 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001397 if (ret) {
1398 btrfs_abort_transaction(trans, root, ret);
1399 goto error;
1400 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001401 }
1402
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403error:
Miao Xiea698d0752012-09-20 01:51:59 -06001404 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001405 if (!ret)
1406 ret = err;
1407
Josef Bacik17ca04a2012-05-31 15:58:55 -04001408 if (ret && cur_offset < end)
1409 extent_clear_unlock_delalloc(inode,
1410 &BTRFS_I(inode)->io_tree,
1411 cur_offset, end, locked_page,
1412 EXTENT_CLEAR_UNLOCK_PAGE |
1413 EXTENT_CLEAR_UNLOCK |
1414 EXTENT_CLEAR_DELALLOC |
1415 EXTENT_CLEAR_DIRTY |
1416 EXTENT_SET_WRITEBACK |
1417 EXTENT_END_WRITEBACK);
1418
Yan Zheng7ea394f2008-08-05 13:05:02 -04001419 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001420 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001421}
1422
Chris Masond352ac62008-09-29 15:18:18 -04001423/*
1424 * extent_io.c call back to do delayed allocation processing
1425 */
Chris Masonc8b97812008-10-29 14:49:59 -04001426static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001427 u64 start, u64 end, int *page_started,
1428 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001429{
Chris Masonbe20aa92007-12-17 20:14:01 -05001430 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001431 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001432
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001433 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001434 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001435 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001436 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001437 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001438 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001439 } else if (!btrfs_test_opt(root, COMPRESS) &&
1440 !(BTRFS_I(inode)->force_compress) &&
1441 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001442 ret = cow_file_range(inode, locked_page, start, end,
1443 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001444 } else {
1445 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1446 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001447 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001448 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001449 }
Chris Masonb888db22007-08-27 16:49:44 -04001450 return ret;
1451}
1452
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001453static void btrfs_split_extent_hook(struct inode *inode,
1454 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001455{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001456 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001457 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001458 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001459
Josef Bacik9e0baf62011-07-15 15:16:44 +00001460 spin_lock(&BTRFS_I(inode)->lock);
1461 BTRFS_I(inode)->outstanding_extents++;
1462 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001463}
1464
1465/*
1466 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1467 * extents so we can keep track of new extents that are just merged onto old
1468 * extents, such as when we are doing sequential writes, so we can properly
1469 * account for the metadata space we'll need.
1470 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001471static void btrfs_merge_extent_hook(struct inode *inode,
1472 struct extent_state *new,
1473 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001474{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001475 /* not delalloc, ignore it */
1476 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001477 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001478
Josef Bacik9e0baf62011-07-15 15:16:44 +00001479 spin_lock(&BTRFS_I(inode)->lock);
1480 BTRFS_I(inode)->outstanding_extents--;
1481 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001482}
1483
Chris Masond352ac62008-09-29 15:18:18 -04001484/*
1485 * extent_io.c set_bit_hook, used to track delayed allocation
1486 * bytes in this file, and to maintain the list of inodes that
1487 * have pending delalloc work to be done.
1488 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001489static void btrfs_set_bit_hook(struct inode *inode,
1490 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001491{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001492
Chris Mason75eff682008-12-15 15:54:40 -05001493 /*
1494 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001495 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001496 * bit, which is only set or cleared with irqs on
1497 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001498 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001499 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001500 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001501 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001502
Josef Bacik9e0baf62011-07-15 15:16:44 +00001503 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001504 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001505 } else {
1506 spin_lock(&BTRFS_I(inode)->lock);
1507 BTRFS_I(inode)->outstanding_extents++;
1508 spin_unlock(&BTRFS_I(inode)->lock);
1509 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001510
Chris Mason75eff682008-12-15 15:54:40 -05001511 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001512 BTRFS_I(inode)->delalloc_bytes += len;
1513 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001514 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001515 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1516 &root->fs_info->delalloc_inodes);
1517 }
Chris Mason75eff682008-12-15 15:54:40 -05001518 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001519 }
Chris Mason291d6732008-01-29 15:55:23 -05001520}
1521
Chris Masond352ac62008-09-29 15:18:18 -04001522/*
1523 * extent_io.c clear_bit_hook, see set_bit_hook for why
1524 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001525static void btrfs_clear_bit_hook(struct inode *inode,
1526 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001527{
Chris Mason75eff682008-12-15 15:54:40 -05001528 /*
1529 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001530 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001531 * bit, which is only set or cleared with irqs on
1532 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001533 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001534 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001535 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001536 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001537
Josef Bacik9e0baf62011-07-15 15:16:44 +00001538 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001539 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001540 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1541 spin_lock(&BTRFS_I(inode)->lock);
1542 BTRFS_I(inode)->outstanding_extents--;
1543 spin_unlock(&BTRFS_I(inode)->lock);
1544 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001545
1546 if (*bits & EXTENT_DO_ACCOUNTING)
1547 btrfs_delalloc_release_metadata(inode, len);
1548
Josef Bacik0cb59c92010-07-02 12:14:14 -04001549 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1550 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001551 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001552
Chris Mason75eff682008-12-15 15:54:40 -05001553 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001554 root->fs_info->delalloc_bytes -= len;
1555 BTRFS_I(inode)->delalloc_bytes -= len;
1556
Josef Bacik0cb59c92010-07-02 12:14:14 -04001557 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001558 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1559 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1560 }
Chris Mason75eff682008-12-15 15:54:40 -05001561 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001562 }
Chris Mason291d6732008-01-29 15:55:23 -05001563}
1564
Chris Masond352ac62008-09-29 15:18:18 -04001565/*
1566 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1567 * we don't create bios that span stripes or chunks
1568 */
Chris Mason239b14b2008-03-24 15:02:07 -04001569int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001570 size_t size, struct bio *bio,
1571 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001572{
1573 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001574 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001575 u64 length = 0;
1576 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001577 int ret;
1578
Chris Mason771ed682008-11-06 22:02:51 -05001579 if (bio_flags & EXTENT_BIO_COMPRESSED)
1580 return 0;
1581
Chris Masonf2d8d742008-04-21 10:03:05 -04001582 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001583 map_length = length;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001584 ret = btrfs_map_block(root->fs_info, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001585 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001586 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001587 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001588 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001589 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001590 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001591}
1592
Chris Masond352ac62008-09-29 15:18:18 -04001593/*
1594 * in order to insert checksums into the metadata in large chunks,
1595 * we wait until bio submission time. All the pages in the bio are
1596 * checksummed and sums are attached onto the ordered extent record.
1597 *
1598 * At IO completion time the cums attached on the ordered extent record
1599 * are inserted into the btree
1600 */
Chris Masond3977122009-01-05 21:25:51 -05001601static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1602 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001603 unsigned long bio_flags,
1604 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001605{
Chris Mason065631f2008-02-20 12:07:25 -05001606 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001607 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001608
Chris Masond20f7042008-12-08 16:58:54 -05001609 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001610 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001611 return 0;
1612}
Chris Masone0156402008-04-16 11:15:20 -04001613
Chris Mason4a69a412008-11-06 22:03:00 -05001614/*
1615 * in order to insert checksums into the metadata in large chunks,
1616 * we wait until bio submission time. All the pages in the bio are
1617 * checksummed and sums are attached onto the ordered extent record.
1618 *
1619 * At IO completion time the cums attached on the ordered extent record
1620 * are inserted into the btree
1621 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001622static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001623 int mirror_num, unsigned long bio_flags,
1624 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001625{
1626 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001627 int ret;
1628
1629 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1630 if (ret)
1631 bio_endio(bio, ret);
1632 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001633}
1634
Chris Masond352ac62008-09-29 15:18:18 -04001635/*
Chris Masoncad321a2008-12-17 14:51:42 -05001636 * extent_io.c submission hook. This does the right thing for csum calculation
1637 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001638 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001639static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001640 int mirror_num, unsigned long bio_flags,
1641 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001642{
1643 struct btrfs_root *root = BTRFS_I(inode)->root;
1644 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001645 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001646 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001647 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001648
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001649 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001650
Liu Bo83eea1f2012-07-10 05:28:39 -06001651 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001652 metadata = 2;
1653
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001654 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001655 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1656 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001657 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001658
Chris Masond20f7042008-12-08 16:58:54 -05001659 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001660 ret = btrfs_submit_compressed_read(inode, bio,
1661 mirror_num,
1662 bio_flags);
1663 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001664 } else if (!skip_sum) {
1665 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1666 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001667 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001668 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001669 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001670 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001671 /* csum items have already been cloned */
1672 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1673 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001674 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001675 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001676 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001677 bio_flags, bio_offset,
1678 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001679 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001680 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001681 } else if (!skip_sum) {
1682 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1683 if (ret)
1684 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001685 }
1686
Chris Mason0b86a832008-03-24 15:01:56 -04001687mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001688 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1689
1690out:
1691 if (ret < 0)
1692 bio_endio(bio, ret);
1693 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001694}
Chris Mason6885f302008-02-20 16:11:05 -05001695
Chris Masond352ac62008-09-29 15:18:18 -04001696/*
1697 * given a list of ordered sums record them in the inode. This happens
1698 * at IO completion time based on sums calculated at bio submission time.
1699 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001700static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001701 struct inode *inode, u64 file_offset,
1702 struct list_head *list)
1703{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001704 struct btrfs_ordered_sum *sum;
1705
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001706 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001707 btrfs_csum_file_blocks(trans,
1708 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001709 }
1710 return 0;
1711}
1712
Josef Bacik2ac55d42010-02-03 19:33:23 +00001713int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1714 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001715{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001716 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001717 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001718 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001719}
1720
Chris Masond352ac62008-09-29 15:18:18 -04001721/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001722struct btrfs_writepage_fixup {
1723 struct page *page;
1724 struct btrfs_work work;
1725};
1726
Christoph Hellwigb2950862008-12-02 09:54:17 -05001727static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001728{
1729 struct btrfs_writepage_fixup *fixup;
1730 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001731 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001732 struct page *page;
1733 struct inode *inode;
1734 u64 page_start;
1735 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001736 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001737
1738 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1739 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001740again:
Chris Mason247e7432008-07-17 12:53:51 -04001741 lock_page(page);
1742 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1743 ClearPageChecked(page);
1744 goto out_page;
1745 }
1746
1747 inode = page->mapping->host;
1748 page_start = page_offset(page);
1749 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1750
Josef Bacik2ac55d42010-02-03 19:33:23 +00001751 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001752 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001753
1754 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001755 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001756 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001757
1758 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1759 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001760 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1761 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001762 unlock_page(page);
1763 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001764 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001765 goto again;
1766 }
Chris Mason247e7432008-07-17 12:53:51 -04001767
Jeff Mahoney87826df2012-02-15 16:23:57 +01001768 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1769 if (ret) {
1770 mapping_set_error(page->mapping, ret);
1771 end_extent_writepage(page, ret, page_start, page_end);
1772 ClearPageChecked(page);
1773 goto out;
1774 }
1775
Josef Bacik2ac55d42010-02-03 19:33:23 +00001776 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001777 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001778 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001779out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001780 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1781 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001782out_page:
1783 unlock_page(page);
1784 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001785 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001786}
1787
1788/*
1789 * There are a few paths in the higher layers of the kernel that directly
1790 * set the page dirty bit without asking the filesystem if it is a
1791 * good idea. This causes problems because we want to make sure COW
1792 * properly happens and the data=ordered rules are followed.
1793 *
Chris Masonc8b97812008-10-29 14:49:59 -04001794 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001795 * hasn't been properly setup for IO. We kick off an async process
1796 * to fix it up. The async helper will wait for ordered extents, set
1797 * the delalloc bit and make it safe to write the page.
1798 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001799static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001800{
1801 struct inode *inode = page->mapping->host;
1802 struct btrfs_writepage_fixup *fixup;
1803 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001804
Chris Mason8b62b722009-09-02 16:53:46 -04001805 /* this page is properly in the ordered list */
1806 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001807 return 0;
1808
1809 if (PageChecked(page))
1810 return -EAGAIN;
1811
1812 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1813 if (!fixup)
1814 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001815
Chris Mason247e7432008-07-17 12:53:51 -04001816 SetPageChecked(page);
1817 page_cache_get(page);
1818 fixup->work.func = btrfs_writepage_fixup_worker;
1819 fixup->page = page;
1820 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001821 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001822}
1823
Yan Zhengd899e052008-10-30 14:25:28 -04001824static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1825 struct inode *inode, u64 file_pos,
1826 u64 disk_bytenr, u64 disk_num_bytes,
1827 u64 num_bytes, u64 ram_bytes,
1828 u8 compression, u8 encryption,
1829 u16 other_encoding, int extent_type)
1830{
1831 struct btrfs_root *root = BTRFS_I(inode)->root;
1832 struct btrfs_file_extent_item *fi;
1833 struct btrfs_path *path;
1834 struct extent_buffer *leaf;
1835 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001836 int ret;
1837
1838 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001839 if (!path)
1840 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001841
Chris Masonb9473432009-03-13 11:00:37 -04001842 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001843
1844 /*
1845 * we may be replacing one extent in the tree with another.
1846 * The new extent is pinned in the extent map, and we don't want
1847 * to drop it from the cache until it is completely in the btree.
1848 *
1849 * So, tell btrfs_drop_extents to leave this extent in the cache.
1850 * the caller is expected to unpin it and allow it to be merged
1851 * with the others.
1852 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001853 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001854 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001855 if (ret)
1856 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001857
Li Zefan33345d012011-04-20 10:31:50 +08001858 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001859 ins.offset = file_pos;
1860 ins.type = BTRFS_EXTENT_DATA_KEY;
1861 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001862 if (ret)
1863 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001864 leaf = path->nodes[0];
1865 fi = btrfs_item_ptr(leaf, path->slots[0],
1866 struct btrfs_file_extent_item);
1867 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1868 btrfs_set_file_extent_type(leaf, fi, extent_type);
1869 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1870 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1871 btrfs_set_file_extent_offset(leaf, fi, 0);
1872 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1873 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1874 btrfs_set_file_extent_compression(leaf, fi, compression);
1875 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1876 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001877
Yan Zhengd899e052008-10-30 14:25:28 -04001878 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001879 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001880
1881 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001882
1883 ins.objectid = disk_bytenr;
1884 ins.offset = disk_num_bytes;
1885 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001886 ret = btrfs_alloc_reserved_file_extent(trans, root,
1887 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001888 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001889out:
Yan Zhengd899e052008-10-30 14:25:28 -04001890 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001891
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001892 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001893}
1894
Chris Mason5d13a982009-03-13 11:41:46 -04001895/*
1896 * helper function for btrfs_finish_ordered_io, this
1897 * just reads in some of the csum leaves to prime them into ram
1898 * before we start the transaction. It limits the amount of btree
1899 * reads required while inside the transaction.
1900 */
Chris Masond352ac62008-09-29 15:18:18 -04001901/* as ordered data IO finishes, this gets called so we can finish
1902 * an ordered extent if the range of bytes in the file it covers are
1903 * fully written.
1904 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001905static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001906{
Josef Bacik5fd02042012-05-02 14:00:54 -04001907 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001908 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001909 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001910 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001911 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001912 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001913 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001914 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001915
Liu Bo83eea1f2012-07-10 05:28:39 -06001916 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001917
Josef Bacik5fd02042012-05-02 14:00:54 -04001918 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1919 ret = -EIO;
1920 goto out;
1921 }
1922
Yan, Zhengc2167752009-11-12 09:34:21 +00001923 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05001925 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1926 if (nolock)
1927 trans = btrfs_join_transaction_nolock(root);
1928 else
1929 trans = btrfs_join_transaction(root);
1930 if (IS_ERR(trans)) {
1931 ret = PTR_ERR(trans);
1932 trans = NULL;
1933 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00001934 }
Josef Bacik6c760c02012-11-09 10:53:21 -05001935 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1936 ret = btrfs_update_inode_fallback(trans, root, inode);
1937 if (ret) /* -ENOMEM or corruption */
1938 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001939 goto out;
1940 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001941
Josef Bacik2ac55d42010-02-03 19:33:23 +00001942 lock_extent_bits(io_tree, ordered_extent->file_offset,
1943 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001944 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001945
Josef Bacik0cb59c92010-07-02 12:14:14 -04001946 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001947 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001948 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001949 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001950 if (IS_ERR(trans)) {
1951 ret = PTR_ERR(trans);
1952 trans = NULL;
1953 goto out_unlock;
1954 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001955 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001956
Chris Masonc8b97812008-10-29 14:49:59 -04001957 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001958 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001959 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001960 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001961 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001962 ordered_extent->file_offset,
1963 ordered_extent->file_offset +
1964 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001965 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001966 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001967 ret = insert_reserved_file_extent(trans, inode,
1968 ordered_extent->file_offset,
1969 ordered_extent->start,
1970 ordered_extent->disk_len,
1971 ordered_extent->len,
1972 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001973 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001974 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04001975 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04001976 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1977 ordered_extent->file_offset, ordered_extent->len,
1978 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001979 if (ret < 0) {
1980 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001981 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001982 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001983
Chris Masone6dcd2d2008-07-17 12:53:50 -04001984 add_pending_csums(trans, inode, ordered_extent->file_offset,
1985 &ordered_extent->list);
1986
Josef Bacik6c760c02012-11-09 10:53:21 -05001987 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1988 ret = btrfs_update_inode_fallback(trans, root, inode);
1989 if (ret) { /* -ENOMEM or corruption */
1990 btrfs_abort_transaction(trans, root, ret);
1991 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04001992 }
1993 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04001994out_unlock:
1995 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1996 ordered_extent->file_offset +
1997 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00001998out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001999 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002000 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002001 if (trans)
2002 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002003
Josef Bacik5fd02042012-05-02 14:00:54 -04002004 if (ret)
2005 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2006 ordered_extent->file_offset +
2007 ordered_extent->len - 1, NULL, GFP_NOFS);
2008
2009 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002010 * This needs to be done to make sure anybody waiting knows we are done
2011 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002012 */
2013 btrfs_remove_ordered_extent(inode, ordered_extent);
2014
Chris Masone6dcd2d2008-07-17 12:53:50 -04002015 /* once for us */
2016 btrfs_put_ordered_extent(ordered_extent);
2017 /* once for the tree */
2018 btrfs_put_ordered_extent(ordered_extent);
2019
Josef Bacik5fd02042012-05-02 14:00:54 -04002020 return ret;
2021}
2022
2023static void finish_ordered_fn(struct btrfs_work *work)
2024{
2025 struct btrfs_ordered_extent *ordered_extent;
2026 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2027 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002028}
2029
Christoph Hellwigb2950862008-12-02 09:54:17 -05002030static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002031 struct extent_state *state, int uptodate)
2032{
Josef Bacik5fd02042012-05-02 14:00:54 -04002033 struct inode *inode = page->mapping->host;
2034 struct btrfs_root *root = BTRFS_I(inode)->root;
2035 struct btrfs_ordered_extent *ordered_extent = NULL;
2036 struct btrfs_workers *workers;
2037
liubo1abe9b82011-03-24 11:18:59 +00002038 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2039
Chris Mason8b62b722009-09-02 16:53:46 -04002040 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002041 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2042 end - start + 1, uptodate))
2043 return 0;
2044
2045 ordered_extent->work.func = finish_ordered_fn;
2046 ordered_extent->work.flags = 0;
2047
Liu Bo83eea1f2012-07-10 05:28:39 -06002048 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002049 workers = &root->fs_info->endio_freespace_worker;
2050 else
2051 workers = &root->fs_info->endio_write_workers;
2052 btrfs_queue_worker(workers, &ordered_extent->work);
2053
2054 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002055}
2056
Chris Masond352ac62008-09-29 15:18:18 -04002057/*
Chris Masond352ac62008-09-29 15:18:18 -04002058 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002059 * if there's a match, we allow the bio to finish. If not, the code in
2060 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002061 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002062static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002063 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002064{
Chris Mason35ebb932007-10-30 16:56:53 -04002065 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04002066 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002067 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002068 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002069 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002070 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002071 struct btrfs_root *root = BTRFS_I(inode)->root;
2072 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002073
Chris Masond20f7042008-12-08 16:58:54 -05002074 if (PageChecked(page)) {
2075 ClearPageChecked(page);
2076 goto good;
2077 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002078
2079 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002080 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002081
Yan Zheng17d217f2008-12-12 10:03:38 -05002082 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002083 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002084 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2085 GFP_NOFS);
2086 return 0;
2087 }
2088
Yanc2e639f2008-02-04 08:57:25 -05002089 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002090 private = state->private;
2091 ret = 0;
2092 } else {
2093 ret = get_state_private(io_tree, start, &private);
2094 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002095 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002096 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002097 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002098
Chris Masonff79f812007-10-15 16:22:25 -04002099 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2100 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002101 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002102 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002103
Cong Wang7ac687d2011-11-25 23:14:28 +08002104 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002105good:
Chris Mason07157aa2007-08-30 08:50:51 -04002106 return 0;
2107
2108zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002109 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002110 "private %llu\n",
2111 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002112 (unsigned long long)start, csum,
2113 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002114 memset(kaddr + offset, 1, end - start + 1);
2115 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002116 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002117 if (private == 0)
2118 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002119 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002120}
Chris Masonb888db22007-08-27 16:49:44 -04002121
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002122struct delayed_iput {
2123 struct list_head list;
2124 struct inode *inode;
2125};
2126
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002127/* JDM: If this is fs-wide, why can't we add a pointer to
2128 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002129void btrfs_add_delayed_iput(struct inode *inode)
2130{
2131 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2132 struct delayed_iput *delayed;
2133
2134 if (atomic_add_unless(&inode->i_count, -1, 1))
2135 return;
2136
2137 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2138 delayed->inode = inode;
2139
2140 spin_lock(&fs_info->delayed_iput_lock);
2141 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2142 spin_unlock(&fs_info->delayed_iput_lock);
2143}
2144
2145void btrfs_run_delayed_iputs(struct btrfs_root *root)
2146{
2147 LIST_HEAD(list);
2148 struct btrfs_fs_info *fs_info = root->fs_info;
2149 struct delayed_iput *delayed;
2150 int empty;
2151
2152 spin_lock(&fs_info->delayed_iput_lock);
2153 empty = list_empty(&fs_info->delayed_iputs);
2154 spin_unlock(&fs_info->delayed_iput_lock);
2155 if (empty)
2156 return;
2157
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002158 spin_lock(&fs_info->delayed_iput_lock);
2159 list_splice_init(&fs_info->delayed_iputs, &list);
2160 spin_unlock(&fs_info->delayed_iput_lock);
2161
2162 while (!list_empty(&list)) {
2163 delayed = list_entry(list.next, struct delayed_iput, list);
2164 list_del(&delayed->list);
2165 iput(delayed->inode);
2166 kfree(delayed);
2167 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002168}
2169
Yan, Zhengd68fc572010-05-16 10:49:58 -04002170enum btrfs_orphan_cleanup_state {
2171 ORPHAN_CLEANUP_STARTED = 1,
2172 ORPHAN_CLEANUP_DONE = 2,
2173};
2174
2175/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002176 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002177 * files in the subvolume, it removes orphan item and frees block_rsv
2178 * structure.
2179 */
2180void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2181 struct btrfs_root *root)
2182{
Josef Bacik90290e12011-12-02 15:44:12 -05002183 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002184 int ret;
2185
Josef Bacik8a35d952012-05-23 14:26:42 -04002186 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002187 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2188 return;
2189
Josef Bacik90290e12011-12-02 15:44:12 -05002190 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002191 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002192 spin_unlock(&root->orphan_lock);
2193 return;
2194 }
2195
2196 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2197 spin_unlock(&root->orphan_lock);
2198 return;
2199 }
2200
2201 block_rsv = root->orphan_block_rsv;
2202 root->orphan_block_rsv = NULL;
2203 spin_unlock(&root->orphan_lock);
2204
Yan, Zhengd68fc572010-05-16 10:49:58 -04002205 if (root->orphan_item_inserted &&
2206 btrfs_root_refs(&root->root_item) > 0) {
2207 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2208 root->root_key.objectid);
2209 BUG_ON(ret);
2210 root->orphan_item_inserted = 0;
2211 }
2212
Josef Bacik90290e12011-12-02 15:44:12 -05002213 if (block_rsv) {
2214 WARN_ON(block_rsv->size > 0);
2215 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002216 }
2217}
2218
2219/*
Josef Bacik7b128762008-07-24 12:17:14 -04002220 * This creates an orphan entry for the given inode in case something goes
2221 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002222 *
2223 * NOTE: caller of this function should reserve 5 units of metadata for
2224 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002225 */
2226int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2227{
2228 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002229 struct btrfs_block_rsv *block_rsv = NULL;
2230 int reserve = 0;
2231 int insert = 0;
2232 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002233
Yan, Zhengd68fc572010-05-16 10:49:58 -04002234 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002235 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002236 if (!block_rsv)
2237 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002238 }
2239
Yan, Zhengd68fc572010-05-16 10:49:58 -04002240 spin_lock(&root->orphan_lock);
2241 if (!root->orphan_block_rsv) {
2242 root->orphan_block_rsv = block_rsv;
2243 } else if (block_rsv) {
2244 btrfs_free_block_rsv(root, block_rsv);
2245 block_rsv = NULL;
2246 }
Josef Bacik7b128762008-07-24 12:17:14 -04002247
Josef Bacik8a35d952012-05-23 14:26:42 -04002248 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2249 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002250#if 0
2251 /*
2252 * For proper ENOSPC handling, we should do orphan
2253 * cleanup when mounting. But this introduces backward
2254 * compatibility issue.
2255 */
2256 if (!xchg(&root->orphan_item_inserted, 1))
2257 insert = 2;
2258 else
2259 insert = 1;
2260#endif
2261 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002262 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002263 }
Josef Bacik7b128762008-07-24 12:17:14 -04002264
Josef Bacik72ac3c02012-05-23 14:13:11 -04002265 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2266 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002267 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002268 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002269
Yan, Zhengd68fc572010-05-16 10:49:58 -04002270 /* grab metadata reservation from transaction handle */
2271 if (reserve) {
2272 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002273 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002274 }
2275
2276 /* insert an orphan item to track this unlinked/truncated file */
2277 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002278 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002279 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002280 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2281 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002282 btrfs_abort_transaction(trans, root, ret);
2283 return ret;
2284 }
2285 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002286 }
2287
2288 /* insert an orphan item to track subvolume contains orphan files */
2289 if (insert >= 2) {
2290 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2291 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002292 if (ret && ret != -EEXIST) {
2293 btrfs_abort_transaction(trans, root, ret);
2294 return ret;
2295 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002296 }
2297 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002298}
2299
2300/*
2301 * We have done the truncate/delete so we can go ahead and remove the orphan
2302 * item for this particular inode.
2303 */
2304int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2305{
2306 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002307 int delete_item = 0;
2308 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002309 int ret = 0;
2310
Yan, Zhengd68fc572010-05-16 10:49:58 -04002311 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002312 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2313 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002314 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002315
Josef Bacik72ac3c02012-05-23 14:13:11 -04002316 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2317 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002318 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002319 spin_unlock(&root->orphan_lock);
2320
2321 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002322 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002323 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002324 }
2325
Josef Bacik8a35d952012-05-23 14:26:42 -04002326 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002327 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002328 atomic_dec(&root->orphan_inodes);
2329 }
Josef Bacik7b128762008-07-24 12:17:14 -04002330
Yan, Zhengd68fc572010-05-16 10:49:58 -04002331 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002332}
2333
2334/*
2335 * this cleans up any orphans that may be left on the list from the last use
2336 * of this root.
2337 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002338int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002339{
2340 struct btrfs_path *path;
2341 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002342 struct btrfs_key key, found_key;
2343 struct btrfs_trans_handle *trans;
2344 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002345 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002346 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2347
Yan, Zhengd68fc572010-05-16 10:49:58 -04002348 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002349 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002350
2351 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002352 if (!path) {
2353 ret = -ENOMEM;
2354 goto out;
2355 }
Josef Bacik7b128762008-07-24 12:17:14 -04002356 path->reada = -1;
2357
2358 key.objectid = BTRFS_ORPHAN_OBJECTID;
2359 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2360 key.offset = (u64)-1;
2361
Josef Bacik7b128762008-07-24 12:17:14 -04002362 while (1) {
2363 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002364 if (ret < 0)
2365 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002366
2367 /*
2368 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002369 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002370 * find the key and see if we have stuff that matches
2371 */
2372 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002373 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002374 if (path->slots[0] == 0)
2375 break;
2376 path->slots[0]--;
2377 }
2378
2379 /* pull out the item */
2380 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2382
2383 /* make sure the item matches what we want */
2384 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2385 break;
2386 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2387 break;
2388
2389 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002390 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002391
2392 /*
2393 * this is where we are basically btrfs_lookup, without the
2394 * crossing root thing. we store the inode number in the
2395 * offset of the orphan item.
2396 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002397
2398 if (found_key.offset == last_objectid) {
2399 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2400 "stopping orphan cleanup\n");
2401 ret = -EINVAL;
2402 goto out;
2403 }
2404
2405 last_objectid = found_key.offset;
2406
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002407 found_key.objectid = found_key.offset;
2408 found_key.type = BTRFS_INODE_ITEM_KEY;
2409 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002410 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002411 ret = PTR_RET(inode);
2412 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002413 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002414
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002415 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2416 struct btrfs_root *dead_root;
2417 struct btrfs_fs_info *fs_info = root->fs_info;
2418 int is_dead_root = 0;
2419
2420 /*
2421 * this is an orphan in the tree root. Currently these
2422 * could come from 2 sources:
2423 * a) a snapshot deletion in progress
2424 * b) a free space cache inode
2425 * We need to distinguish those two, as the snapshot
2426 * orphan must not get deleted.
2427 * find_dead_roots already ran before us, so if this
2428 * is a snapshot deletion, we should find the root
2429 * in the dead_roots list
2430 */
2431 spin_lock(&fs_info->trans_lock);
2432 list_for_each_entry(dead_root, &fs_info->dead_roots,
2433 root_list) {
2434 if (dead_root->root_key.objectid ==
2435 found_key.objectid) {
2436 is_dead_root = 1;
2437 break;
2438 }
2439 }
2440 spin_unlock(&fs_info->trans_lock);
2441 if (is_dead_root) {
2442 /* prevent this orphan from being found again */
2443 key.offset = found_key.objectid - 1;
2444 continue;
2445 }
2446 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002447 /*
2448 * Inode is already gone but the orphan item is still there,
2449 * kill the orphan item.
2450 */
2451 if (ret == -ESTALE) {
2452 trans = btrfs_start_transaction(root, 1);
2453 if (IS_ERR(trans)) {
2454 ret = PTR_ERR(trans);
2455 goto out;
2456 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002457 printk(KERN_ERR "auto deleting %Lu\n",
2458 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002459 ret = btrfs_del_orphan_item(trans, root,
2460 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002461 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002462 btrfs_end_transaction(trans, root);
2463 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002464 }
Josef Bacik7b128762008-07-24 12:17:14 -04002465
Josef Bacik7b128762008-07-24 12:17:14 -04002466 /*
2467 * add this inode to the orphan list so btrfs_orphan_del does
2468 * the proper thing when we hit it
2469 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002470 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2471 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002472
Josef Bacik7b128762008-07-24 12:17:14 -04002473 /* if we have links, this was a truncate, lets do that */
2474 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002475 if (!S_ISREG(inode->i_mode)) {
2476 WARN_ON(1);
2477 iput(inode);
2478 continue;
2479 }
Josef Bacik7b128762008-07-24 12:17:14 -04002480 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05002481
2482 /* 1 for the orphan item deletion. */
2483 trans = btrfs_start_transaction(root, 1);
2484 if (IS_ERR(trans)) {
2485 ret = PTR_ERR(trans);
2486 goto out;
2487 }
2488 ret = btrfs_orphan_add(trans, inode);
2489 btrfs_end_transaction(trans, root);
2490 if (ret)
2491 goto out;
2492
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002493 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002494 } else {
2495 nr_unlink++;
2496 }
2497
2498 /* this will do delete_inode and everything for us */
2499 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002500 if (ret)
2501 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002502 }
Miao Xie3254c872011-11-10 20:45:05 -05002503 /* release the path since we're done with it */
2504 btrfs_release_path(path);
2505
Yan, Zhengd68fc572010-05-16 10:49:58 -04002506 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2507
2508 if (root->orphan_block_rsv)
2509 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2510 (u64)-1);
2511
2512 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002513 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002514 if (!IS_ERR(trans))
2515 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002516 }
Josef Bacik7b128762008-07-24 12:17:14 -04002517
2518 if (nr_unlink)
2519 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2520 if (nr_truncate)
2521 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002522
2523out:
2524 if (ret)
2525 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2526 btrfs_free_path(path);
2527 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002528}
2529
Chris Masond352ac62008-09-29 15:18:18 -04002530/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002531 * very simple check to peek ahead in the leaf looking for xattrs. If we
2532 * don't find any xattrs, we know there can't be any acls.
2533 *
2534 * slot is the slot the inode is in, objectid is the objectid of the inode
2535 */
2536static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2537 int slot, u64 objectid)
2538{
2539 u32 nritems = btrfs_header_nritems(leaf);
2540 struct btrfs_key found_key;
2541 int scanned = 0;
2542
2543 slot++;
2544 while (slot < nritems) {
2545 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2546
2547 /* we found a different objectid, there must not be acls */
2548 if (found_key.objectid != objectid)
2549 return 0;
2550
2551 /* we found an xattr, assume we've got an acl */
2552 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2553 return 1;
2554
2555 /*
2556 * we found a key greater than an xattr key, there can't
2557 * be any acls later on
2558 */
2559 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2560 return 0;
2561
2562 slot++;
2563 scanned++;
2564
2565 /*
2566 * it goes inode, inode backrefs, xattrs, extents,
2567 * so if there are a ton of hard links to an inode there can
2568 * be a lot of backrefs. Don't waste time searching too hard,
2569 * this is just an optimization
2570 */
2571 if (scanned >= 8)
2572 break;
2573 }
2574 /* we hit the end of the leaf before we found an xattr or
2575 * something larger than an xattr. We have to assume the inode
2576 * has acls
2577 */
2578 return 1;
2579}
2580
2581/*
Chris Masond352ac62008-09-29 15:18:18 -04002582 * read an inode from the btree into the in-memory inode
2583 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002584static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002585{
2586 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002587 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002588 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002589 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002590 struct btrfs_root *root = BTRFS_I(inode)->root;
2591 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002592 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002593 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002594 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002595 bool filled = false;
2596
2597 ret = btrfs_fill_inode(inode, &rdev);
2598 if (!ret)
2599 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002600
2601 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002602 if (!path)
2603 goto make_bad;
2604
Josef Bacikd90c7322011-05-17 09:50:54 -04002605 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002606 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002607
Chris Mason39279cc2007-06-12 06:35:45 -04002608 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002609 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002610 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002611
Chris Mason5f39d392007-10-15 16:14:19 -04002612 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002613
2614 if (filled)
2615 goto cache_acl;
2616
Chris Mason5f39d392007-10-15 16:14:19 -04002617 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2618 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002619 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002620 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002621 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2622 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002623 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002624
2625 tspec = btrfs_inode_atime(inode_item);
2626 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2627 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2628
2629 tspec = btrfs_inode_mtime(inode_item);
2630 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2631 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2632
2633 tspec = btrfs_inode_ctime(inode_item);
2634 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2635 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2636
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002637 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002638 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002639 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2640
2641 /*
2642 * If we were modified in the current generation and evicted from memory
2643 * and then re-read we need to do a full sync since we don't have any
2644 * idea about which extents were modified before we were evicted from
2645 * cache.
2646 */
2647 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2648 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2649 &BTRFS_I(inode)->runtime_flags);
2650
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002651 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002652 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002653 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002654 rdev = btrfs_inode_rdev(leaf, inode_item);
2655
Josef Bacikaec74772008-07-24 12:12:38 -04002656 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002657 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002658cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002659 /*
2660 * try to precache a NULL acl entry for files that don't have
2661 * any xattrs or acls
2662 */
Li Zefan33345d012011-04-20 10:31:50 +08002663 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2664 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002665 if (!maybe_acls)
2666 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002667
Chris Mason39279cc2007-06-12 06:35:45 -04002668 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002669
Chris Mason39279cc2007-06-12 06:35:45 -04002670 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002671 case S_IFREG:
2672 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002673 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002674 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002675 inode->i_fop = &btrfs_file_operations;
2676 inode->i_op = &btrfs_file_inode_operations;
2677 break;
2678 case S_IFDIR:
2679 inode->i_fop = &btrfs_dir_file_operations;
2680 if (root == root->fs_info->tree_root)
2681 inode->i_op = &btrfs_dir_ro_inode_operations;
2682 else
2683 inode->i_op = &btrfs_dir_inode_operations;
2684 break;
2685 case S_IFLNK:
2686 inode->i_op = &btrfs_symlink_inode_operations;
2687 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002688 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002689 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002690 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002691 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002692 init_special_inode(inode, inode->i_mode, rdev);
2693 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002694 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002695
2696 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002697 return;
2698
2699make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002700 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002701 make_bad_inode(inode);
2702}
2703
Chris Masond352ac62008-09-29 15:18:18 -04002704/*
2705 * given a leaf and an inode, copy the inode fields into the leaf
2706 */
Chris Masone02119d2008-09-05 16:13:11 -04002707static void fill_inode_item(struct btrfs_trans_handle *trans,
2708 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002709 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002710 struct inode *inode)
2711{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002712 btrfs_set_inode_uid(leaf, item, i_uid_read(inode));
2713 btrfs_set_inode_gid(leaf, item, i_gid_read(inode));
Chris Masondbe674a2008-07-17 12:54:05 -04002714 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002715 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2716 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2717
2718 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2719 inode->i_atime.tv_sec);
2720 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2721 inode->i_atime.tv_nsec);
2722
2723 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2724 inode->i_mtime.tv_sec);
2725 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2726 inode->i_mtime.tv_nsec);
2727
2728 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2729 inode->i_ctime.tv_sec);
2730 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2731 inode->i_ctime.tv_nsec);
2732
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002733 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002734 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002735 btrfs_set_inode_sequence(leaf, item, inode->i_version);
Chris Masone02119d2008-09-05 16:13:11 -04002736 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002737 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002738 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04002739 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002740}
2741
Chris Masond352ac62008-09-29 15:18:18 -04002742/*
2743 * copy everything in the in-memory inode into the btree.
2744 */
Chris Mason21151332011-11-10 20:39:08 -05002745static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002746 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002747{
2748 struct btrfs_inode_item *inode_item;
2749 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002750 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002751 int ret;
2752
2753 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002754 if (!path)
2755 return -ENOMEM;
2756
Chris Masonb9473432009-03-13 11:00:37 -04002757 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002758 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2759 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002760 if (ret) {
2761 if (ret > 0)
2762 ret = -ENOENT;
2763 goto failed;
2764 }
2765
Chris Masonb4ce94d2009-02-04 09:25:08 -05002766 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002767 leaf = path->nodes[0];
2768 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002769 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002770
Chris Masone02119d2008-09-05 16:13:11 -04002771 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002772 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002773 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002774 ret = 0;
2775failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002776 btrfs_free_path(path);
2777 return ret;
2778}
2779
Chris Masond352ac62008-09-29 15:18:18 -04002780/*
Chris Mason21151332011-11-10 20:39:08 -05002781 * copy everything in the in-memory inode into the btree.
2782 */
2783noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2784 struct btrfs_root *root, struct inode *inode)
2785{
2786 int ret;
2787
2788 /*
2789 * If the inode is a free space inode, we can deadlock during commit
2790 * if we put it into the delayed code.
2791 *
2792 * The data relocation inode should also be directly updated
2793 * without delay
2794 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002795 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002796 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002797 btrfs_update_root_times(trans, root);
2798
Chris Mason21151332011-11-10 20:39:08 -05002799 ret = btrfs_delayed_update_inode(trans, root, inode);
2800 if (!ret)
2801 btrfs_set_inode_last_trans(trans, inode);
2802 return ret;
2803 }
2804
2805 return btrfs_update_inode_item(trans, root, inode);
2806}
2807
Josef Bacikbe6aef62012-10-22 15:43:12 -04002808noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2809 struct btrfs_root *root,
2810 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002811{
2812 int ret;
2813
2814 ret = btrfs_update_inode(trans, root, inode);
2815 if (ret == -ENOSPC)
2816 return btrfs_update_inode_item(trans, root, inode);
2817 return ret;
2818}
2819
2820/*
Chris Masond352ac62008-09-29 15:18:18 -04002821 * unlink helper that gets used here in inode.c and in the tree logging
2822 * recovery code. It remove a link in a directory with a given name, and
2823 * also drops the back refs in the inode to the directory
2824 */
Al Viro92986792011-03-04 17:14:37 +00002825static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2826 struct btrfs_root *root,
2827 struct inode *dir, struct inode *inode,
2828 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002829{
2830 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002831 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002832 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002833 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002834 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002835 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002836 u64 ino = btrfs_ino(inode);
2837 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002838
2839 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002840 if (!path) {
2841 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002842 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002843 }
2844
Chris Masonb9473432009-03-13 11:00:37 -04002845 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002846 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002847 name, name_len, -1);
2848 if (IS_ERR(di)) {
2849 ret = PTR_ERR(di);
2850 goto err;
2851 }
2852 if (!di) {
2853 ret = -ENOENT;
2854 goto err;
2855 }
Chris Mason5f39d392007-10-15 16:14:19 -04002856 leaf = path->nodes[0];
2857 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002858 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002859 if (ret)
2860 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002861 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002862
Li Zefan33345d012011-04-20 10:31:50 +08002863 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2864 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002865 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002866 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002867 "inode %llu parent %llu\n", name_len, name,
2868 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002869 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002870 goto err;
2871 }
2872
Miao Xie16cdcec2011-04-22 18:12:22 +08002873 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002874 if (ret) {
2875 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002876 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002877 }
Chris Mason39279cc2007-06-12 06:35:45 -04002878
Chris Masone02119d2008-09-05 16:13:11 -04002879 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002880 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002881 if (ret != 0 && ret != -ENOENT) {
2882 btrfs_abort_transaction(trans, root, ret);
2883 goto err;
2884 }
Chris Masone02119d2008-09-05 16:13:11 -04002885
2886 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2887 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002888 if (ret == -ENOENT)
2889 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002890err:
2891 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002892 if (ret)
2893 goto out;
2894
2895 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002896 inode_inc_iversion(inode);
2897 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002898 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002899 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002900out:
Chris Mason39279cc2007-06-12 06:35:45 -04002901 return ret;
2902}
2903
Al Viro92986792011-03-04 17:14:37 +00002904int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2905 struct btrfs_root *root,
2906 struct inode *dir, struct inode *inode,
2907 const char *name, int name_len)
2908{
2909 int ret;
2910 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2911 if (!ret) {
2912 btrfs_drop_nlink(inode);
2913 ret = btrfs_update_inode(trans, root, inode);
2914 }
2915 return ret;
2916}
2917
2918
Yan, Zhenga22285a2010-05-16 10:48:46 -04002919/* helper to check if there is any shared block in the path */
2920static int check_path_shared(struct btrfs_root *root,
2921 struct btrfs_path *path)
2922{
2923 struct extent_buffer *eb;
2924 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002925 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002926
2927 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002928 int ret;
2929
Yan, Zhenga22285a2010-05-16 10:48:46 -04002930 if (!path->nodes[level])
2931 break;
2932 eb = path->nodes[level];
2933 if (!btrfs_block_can_be_shared(root, eb))
2934 continue;
2935 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2936 &refs, NULL);
2937 if (refs > 1)
2938 return 1;
2939 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002940 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002941}
2942
2943/*
2944 * helper to start transaction for unlink and rmdir.
2945 *
2946 * unlink and rmdir are special in btrfs, they do not always free space.
2947 * so in enospc case, we should make sure they will free space before
2948 * allowing them to use the global metadata reservation.
2949 */
2950static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2951 struct dentry *dentry)
2952{
2953 struct btrfs_trans_handle *trans;
2954 struct btrfs_root *root = BTRFS_I(dir)->root;
2955 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002956 struct btrfs_dir_item *di;
2957 struct inode *inode = dentry->d_inode;
2958 u64 index;
2959 int check_link = 1;
2960 int err = -ENOSPC;
2961 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002962 u64 ino = btrfs_ino(inode);
2963 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002964
Josef Bacike70bea52011-10-11 14:18:24 -04002965 /*
2966 * 1 for the possible orphan item
2967 * 1 for the dir item
2968 * 1 for the dir index
2969 * 1 for the inode ref
2970 * 1 for the inode ref in the tree log
2971 * 2 for the dir entries in the log
2972 * 1 for the inode
2973 */
2974 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002975 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2976 return trans;
2977
Li Zefan33345d012011-04-20 10:31:50 +08002978 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002979 return ERR_PTR(-ENOSPC);
2980
2981 /* check if there is someone else holds reference */
2982 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2983 return ERR_PTR(-ENOSPC);
2984
2985 if (atomic_read(&inode->i_count) > 2)
2986 return ERR_PTR(-ENOSPC);
2987
2988 if (xchg(&root->fs_info->enospc_unlink, 1))
2989 return ERR_PTR(-ENOSPC);
2990
2991 path = btrfs_alloc_path();
2992 if (!path) {
2993 root->fs_info->enospc_unlink = 0;
2994 return ERR_PTR(-ENOMEM);
2995 }
2996
Josef Bacik3880a1b2011-10-14 14:46:51 -04002997 /* 1 for the orphan item */
2998 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002999 if (IS_ERR(trans)) {
3000 btrfs_free_path(path);
3001 root->fs_info->enospc_unlink = 0;
3002 return trans;
3003 }
3004
3005 path->skip_locking = 1;
3006 path->search_commit_root = 1;
3007
3008 ret = btrfs_lookup_inode(trans, root, path,
3009 &BTRFS_I(dir)->location, 0);
3010 if (ret < 0) {
3011 err = ret;
3012 goto out;
3013 }
3014 if (ret == 0) {
3015 if (check_path_shared(root, path))
3016 goto out;
3017 } else {
3018 check_link = 0;
3019 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003020 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003021
3022 ret = btrfs_lookup_inode(trans, root, path,
3023 &BTRFS_I(inode)->location, 0);
3024 if (ret < 0) {
3025 err = ret;
3026 goto out;
3027 }
3028 if (ret == 0) {
3029 if (check_path_shared(root, path))
3030 goto out;
3031 } else {
3032 check_link = 0;
3033 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003034 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003035
3036 if (ret == 0 && S_ISREG(inode->i_mode)) {
3037 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003038 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003039 if (ret < 0) {
3040 err = ret;
3041 goto out;
3042 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003043 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003044 if (check_path_shared(root, path))
3045 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003046 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003047 }
3048
3049 if (!check_link) {
3050 err = 0;
3051 goto out;
3052 }
3053
Li Zefan33345d012011-04-20 10:31:50 +08003054 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003055 dentry->d_name.name, dentry->d_name.len, 0);
3056 if (IS_ERR(di)) {
3057 err = PTR_ERR(di);
3058 goto out;
3059 }
3060 if (di) {
3061 if (check_path_shared(root, path))
3062 goto out;
3063 } else {
3064 err = 0;
3065 goto out;
3066 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003067 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003068
Mark Fashehf1863732012-08-08 11:32:27 -07003069 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3070 dentry->d_name.len, ino, dir_ino, 0,
3071 &index);
3072 if (ret) {
3073 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003074 goto out;
3075 }
Mark Fashehf1863732012-08-08 11:32:27 -07003076
Yan, Zhenga22285a2010-05-16 10:48:46 -04003077 if (check_path_shared(root, path))
3078 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003079
David Sterbab3b4aa72011-04-21 01:20:15 +02003080 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003081
Miao Xie16cdcec2011-04-22 18:12:22 +08003082 /*
3083 * This is a commit root search, if we can lookup inode item and other
3084 * relative items in the commit root, it means the transaction of
3085 * dir/file creation has been committed, and the dir index item that we
3086 * delay to insert has also been inserted into the commit root. So
3087 * we needn't worry about the delayed insertion of the dir index item
3088 * here.
3089 */
Li Zefan33345d012011-04-20 10:31:50 +08003090 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003091 dentry->d_name.name, dentry->d_name.len, 0);
3092 if (IS_ERR(di)) {
3093 err = PTR_ERR(di);
3094 goto out;
3095 }
3096 BUG_ON(ret == -ENOENT);
3097 if (check_path_shared(root, path))
3098 goto out;
3099
3100 err = 0;
3101out:
3102 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003103 /* Migrate the orphan reservation over */
3104 if (!err)
3105 err = btrfs_block_rsv_migrate(trans->block_rsv,
3106 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003107 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003108
Yan, Zhenga22285a2010-05-16 10:48:46 -04003109 if (err) {
3110 btrfs_end_transaction(trans, root);
3111 root->fs_info->enospc_unlink = 0;
3112 return ERR_PTR(err);
3113 }
3114
3115 trans->block_rsv = &root->fs_info->global_block_rsv;
3116 return trans;
3117}
3118
3119static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3120 struct btrfs_root *root)
3121{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003122 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003123 btrfs_block_rsv_release(root, trans->block_rsv,
3124 trans->bytes_reserved);
3125 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003126 BUG_ON(!root->fs_info->enospc_unlink);
3127 root->fs_info->enospc_unlink = 0;
3128 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003129 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003130}
3131
Chris Mason39279cc2007-06-12 06:35:45 -04003132static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3133{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003134 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003135 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003136 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003137 int ret;
3138
Yan, Zhenga22285a2010-05-16 10:48:46 -04003139 trans = __unlink_start_trans(dir, dentry);
3140 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003141 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003142
Chris Mason12fcfd22009-03-24 10:24:20 -04003143 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3144
Chris Masone02119d2008-09-05 16:13:11 -04003145 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3146 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003147 if (ret)
3148 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003149
Yan, Zhenga22285a2010-05-16 10:48:46 -04003150 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003151 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003152 if (ret)
3153 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003154 }
Josef Bacik7b128762008-07-24 12:17:14 -04003155
Tsutomu Itohb5324022011-07-19 07:27:20 +00003156out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003157 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003158 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003159 return ret;
3160}
3161
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003162int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3163 struct btrfs_root *root,
3164 struct inode *dir, u64 objectid,
3165 const char *name, int name_len)
3166{
3167 struct btrfs_path *path;
3168 struct extent_buffer *leaf;
3169 struct btrfs_dir_item *di;
3170 struct btrfs_key key;
3171 u64 index;
3172 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003173 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003174
3175 path = btrfs_alloc_path();
3176 if (!path)
3177 return -ENOMEM;
3178
Li Zefan33345d012011-04-20 10:31:50 +08003179 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003180 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003181 if (IS_ERR_OR_NULL(di)) {
3182 if (!di)
3183 ret = -ENOENT;
3184 else
3185 ret = PTR_ERR(di);
3186 goto out;
3187 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003188
3189 leaf = path->nodes[0];
3190 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3191 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3192 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003193 if (ret) {
3194 btrfs_abort_transaction(trans, root, ret);
3195 goto out;
3196 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003197 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003198
3199 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3200 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003201 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003202 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003203 if (ret != -ENOENT) {
3204 btrfs_abort_transaction(trans, root, ret);
3205 goto out;
3206 }
Li Zefan33345d012011-04-20 10:31:50 +08003207 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003208 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003209 if (IS_ERR_OR_NULL(di)) {
3210 if (!di)
3211 ret = -ENOENT;
3212 else
3213 ret = PTR_ERR(di);
3214 btrfs_abort_transaction(trans, root, ret);
3215 goto out;
3216 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003217
3218 leaf = path->nodes[0];
3219 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003220 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003221 index = key.offset;
3222 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003223 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003224
Miao Xie16cdcec2011-04-22 18:12:22 +08003225 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003226 if (ret) {
3227 btrfs_abort_transaction(trans, root, ret);
3228 goto out;
3229 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003230
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003231 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003232 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003233 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003234 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003235 if (ret)
3236 btrfs_abort_transaction(trans, root, ret);
3237out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003238 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003239 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003240}
3241
Chris Mason39279cc2007-06-12 06:35:45 -04003242static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3243{
3244 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003245 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003246 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003247 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003248
David Sterbab3ae2442012-09-13 16:04:34 -06003249 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003250 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003251 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3252 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003253
Yan, Zhenga22285a2010-05-16 10:48:46 -04003254 trans = __unlink_start_trans(dir, dentry);
3255 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003256 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003257
Li Zefan33345d012011-04-20 10:31:50 +08003258 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003259 err = btrfs_unlink_subvol(trans, root, dir,
3260 BTRFS_I(inode)->location.objectid,
3261 dentry->d_name.name,
3262 dentry->d_name.len);
3263 goto out;
3264 }
3265
Josef Bacik7b128762008-07-24 12:17:14 -04003266 err = btrfs_orphan_add(trans, inode);
3267 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003268 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003269
Chris Mason39279cc2007-06-12 06:35:45 -04003270 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003271 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3272 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003273 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003274 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003275out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003276 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003277 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003278
Chris Mason39279cc2007-06-12 06:35:45 -04003279 return err;
3280}
3281
Chris Mason323ac952008-10-01 19:05:46 -04003282/*
Chris Mason39279cc2007-06-12 06:35:45 -04003283 * this can truncate away extent items, csum items and directory items.
3284 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003285 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003286 *
3287 * csum items that cross the new i_size are truncated to the new size
3288 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003289 *
3290 * min_type is the minimum key type to truncate down to. If set to 0, this
3291 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003292 */
Yan, Zheng80825102009-11-12 09:35:36 +00003293int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3294 struct btrfs_root *root,
3295 struct inode *inode,
3296 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003297{
Chris Mason39279cc2007-06-12 06:35:45 -04003298 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003299 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003300 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003301 struct btrfs_key key;
3302 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003303 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003304 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003305 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003306 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003307 u64 mask = root->sectorsize - 1;
3308 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003309 int found_extent;
3310 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003311 int pending_del_nr = 0;
3312 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003313 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003314 int ret;
3315 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003316 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003317
3318 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003319
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003320 path = btrfs_alloc_path();
3321 if (!path)
3322 return -ENOMEM;
3323 path->reada = -1;
3324
Josef Bacik5dc562c2012-08-17 13:14:17 -04003325 /*
3326 * We want to drop from the next block forward in case this new size is
3327 * not block aligned since we will be keeping the last block of the
3328 * extent just the way it is.
3329 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003330 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003331 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003332
Miao Xie16cdcec2011-04-22 18:12:22 +08003333 /*
3334 * This function is also used to drop the items in the log tree before
3335 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3336 * it is used to drop the loged items. So we shouldn't kill the delayed
3337 * items.
3338 */
3339 if (min_type == 0 && root == BTRFS_I(inode)->root)
3340 btrfs_kill_delayed_inode_items(inode);
3341
Li Zefan33345d012011-04-20 10:31:50 +08003342 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003343 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003344 key.type = (u8)-1;
3345
Chris Mason85e21ba2008-01-29 15:11:36 -05003346search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003347 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003348 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003349 if (ret < 0) {
3350 err = ret;
3351 goto out;
3352 }
Chris Masond3977122009-01-05 21:25:51 -05003353
Chris Mason85e21ba2008-01-29 15:11:36 -05003354 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003355 /* there are no items in the tree for us to truncate, we're
3356 * done
3357 */
Yan, Zheng80825102009-11-12 09:35:36 +00003358 if (path->slots[0] == 0)
3359 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003360 path->slots[0]--;
3361 }
3362
Chris Masond3977122009-01-05 21:25:51 -05003363 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003364 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003365 leaf = path->nodes[0];
3366 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3367 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003368
Li Zefan33345d012011-04-20 10:31:50 +08003369 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003370 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003371
Chris Mason85e21ba2008-01-29 15:11:36 -05003372 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003373 break;
3374
Chris Mason5f39d392007-10-15 16:14:19 -04003375 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003376 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003377 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003378 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003379 extent_type = btrfs_file_extent_type(leaf, fi);
3380 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003381 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003382 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003383 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003384 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003385 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003386 }
Yan008630c2007-11-07 13:31:09 -05003387 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003388 }
Yan, Zheng80825102009-11-12 09:35:36 +00003389 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003390 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003391 } else {
3392 if (item_end < new_size)
3393 break;
3394 if (found_key.offset >= new_size)
3395 del_item = 1;
3396 else
3397 del_item = 0;
3398 }
Chris Mason39279cc2007-06-12 06:35:45 -04003399 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003400 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003401 if (found_type != BTRFS_EXTENT_DATA_KEY)
3402 goto delete;
3403
3404 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003405 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003406 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003407 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003408 u64 orig_num_bytes =
3409 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003410 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003411 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003412 extent_num_bytes = extent_num_bytes &
3413 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003414 btrfs_set_file_extent_num_bytes(leaf, fi,
3415 extent_num_bytes);
3416 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003417 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003418 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003419 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003420 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003421 } else {
Chris Masondb945352007-10-15 16:15:53 -04003422 extent_num_bytes =
3423 btrfs_file_extent_disk_num_bytes(leaf,
3424 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003425 extent_offset = found_key.offset -
3426 btrfs_file_extent_offset(leaf, fi);
3427
Chris Mason39279cc2007-06-12 06:35:45 -04003428 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003429 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003430 if (extent_start != 0) {
3431 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003432 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003433 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003434 }
3435 }
Chris Mason90692182008-02-08 13:49:28 -05003436 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003437 /*
3438 * we can't truncate inline items that have had
3439 * special encodings
3440 */
3441 if (!del_item &&
3442 btrfs_file_extent_compression(leaf, fi) == 0 &&
3443 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3444 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003445 u32 size = new_size - found_key.offset;
3446
3447 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003448 inode_sub_bytes(inode, item_end + 1 -
3449 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003450 }
3451 size =
3452 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003453 btrfs_truncate_item(trans, root, path,
3454 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003455 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003456 inode_sub_bytes(inode, item_end + 1 -
3457 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003458 }
Chris Mason39279cc2007-06-12 06:35:45 -04003459 }
Chris Mason179e29e2007-11-01 11:28:41 -04003460delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003461 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003462 if (!pending_del_nr) {
3463 /* no pending yet, add ourselves */
3464 pending_del_slot = path->slots[0];
3465 pending_del_nr = 1;
3466 } else if (pending_del_nr &&
3467 path->slots[0] + 1 == pending_del_slot) {
3468 /* hop on the pending chunk */
3469 pending_del_nr++;
3470 pending_del_slot = path->slots[0];
3471 } else {
Chris Masond3977122009-01-05 21:25:51 -05003472 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003473 }
Chris Mason39279cc2007-06-12 06:35:45 -04003474 } else {
3475 break;
3476 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003477 if (found_extent && (root->ref_cows ||
3478 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003479 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003480 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003481 extent_num_bytes, 0,
3482 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003483 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003484 BUG_ON(ret);
3485 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003486
Yan, Zheng80825102009-11-12 09:35:36 +00003487 if (found_type == BTRFS_INODE_ITEM_KEY)
3488 break;
3489
3490 if (path->slots[0] == 0 ||
3491 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003492 if (pending_del_nr) {
3493 ret = btrfs_del_items(trans, root, path,
3494 pending_del_slot,
3495 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003496 if (ret) {
3497 btrfs_abort_transaction(trans,
3498 root, ret);
3499 goto error;
3500 }
Yan, Zheng80825102009-11-12 09:35:36 +00003501 pending_del_nr = 0;
3502 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003503 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003504 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003505 } else {
3506 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003507 }
Chris Mason39279cc2007-06-12 06:35:45 -04003508 }
Yan, Zheng80825102009-11-12 09:35:36 +00003509out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003510 if (pending_del_nr) {
3511 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3512 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003513 if (ret)
3514 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003515 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003516error:
Chris Mason39279cc2007-06-12 06:35:45 -04003517 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003518 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003519}
3520
Chris Masona52d9a82007-08-27 16:49:44 -04003521/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003522 * btrfs_truncate_page - read, zero a chunk and write a page
3523 * @inode - inode that we're zeroing
3524 * @from - the offset to start zeroing
3525 * @len - the length to zero, 0 to zero the entire range respective to the
3526 * offset
3527 * @front - zero up to the offset instead of from the offset on
3528 *
3529 * This will find the page for the "from" offset and cow the page and zero the
3530 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003531 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003532int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3533 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003534{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003535 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003536 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003537 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3538 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003539 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003540 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003541 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003542 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3543 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3544 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003545 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003546 int ret = 0;
3547 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003548 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003549
Josef Bacik2aaa6652012-08-29 14:27:18 -04003550 if ((offset & (blocksize - 1)) == 0 &&
3551 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003552 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003553 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003554 if (ret)
3555 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003556
Chris Mason211c17f2008-05-15 09:13:45 -04003557again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003558 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003559 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003560 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00003561 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04003562 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003563 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003564
3565 page_start = page_offset(page);
3566 page_end = page_start + PAGE_CACHE_SIZE - 1;
3567
Chris Masona52d9a82007-08-27 16:49:44 -04003568 if (!PageUptodate(page)) {
3569 ret = btrfs_readpage(NULL, page);
3570 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003571 if (page->mapping != mapping) {
3572 unlock_page(page);
3573 page_cache_release(page);
3574 goto again;
3575 }
Chris Masona52d9a82007-08-27 16:49:44 -04003576 if (!PageUptodate(page)) {
3577 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003578 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003579 }
3580 }
Chris Mason211c17f2008-05-15 09:13:45 -04003581 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003582
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003583 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003584 set_page_extent_mapped(page);
3585
3586 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3587 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003588 unlock_extent_cached(io_tree, page_start, page_end,
3589 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003590 unlock_page(page);
3591 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003592 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003593 btrfs_put_ordered_extent(ordered);
3594 goto again;
3595 }
3596
Josef Bacik2ac55d42010-02-03 19:33:23 +00003597 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003598 EXTENT_DIRTY | EXTENT_DELALLOC |
3599 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003600 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003601
Josef Bacik2ac55d42010-02-03 19:33:23 +00003602 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3603 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003604 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003605 unlock_extent_cached(io_tree, page_start, page_end,
3606 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003607 goto out_unlock;
3608 }
3609
Chris Masone6dcd2d2008-07-17 12:53:50 -04003610 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003611 if (!len)
3612 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003613 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003614 if (front)
3615 memset(kaddr, 0, offset);
3616 else
3617 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003618 flush_dcache_page(page);
3619 kunmap(page);
3620 }
Chris Mason247e7432008-07-17 12:53:51 -04003621 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003622 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003623 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3624 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003625
Chris Mason89642222008-07-24 09:41:53 -04003626out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003627 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003628 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003629 unlock_page(page);
3630 page_cache_release(page);
3631out:
3632 return ret;
3633}
3634
Josef Bacik695a0d02011-03-04 15:46:53 -05003635/*
3636 * This function puts in dummy file extents for the area we're creating a hole
3637 * for. So if we are truncating this file to a larger size we need to insert
3638 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3639 * the range between oldsize and size
3640 */
Josef Bacika41ad392011-01-31 15:30:16 -05003641int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003642{
3643 struct btrfs_trans_handle *trans;
3644 struct btrfs_root *root = BTRFS_I(inode)->root;
3645 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003646 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003647 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003648 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003649 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003650 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003651 u64 block_end = (size + mask) & ~mask;
3652 u64 last_byte;
3653 u64 cur_offset;
3654 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003655 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003656
3657 if (size <= hole_start)
3658 return 0;
3659
Yan Zheng9036c102008-10-30 14:19:41 -04003660 while (1) {
3661 struct btrfs_ordered_extent *ordered;
3662 btrfs_wait_ordered_range(inode, hole_start,
3663 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003664 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003665 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003666 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3667 if (!ordered)
3668 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003669 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3670 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003671 btrfs_put_ordered_extent(ordered);
3672 }
3673
Yan Zheng9036c102008-10-30 14:19:41 -04003674 cur_offset = hole_start;
3675 while (1) {
3676 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3677 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003678 if (IS_ERR(em)) {
3679 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00003680 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003681 break;
3682 }
Yan Zheng9036c102008-10-30 14:19:41 -04003683 last_byte = min(extent_map_end(em), block_end);
3684 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003685 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003686 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003687 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003688
Miao Xie36423202011-12-14 20:12:02 -05003689 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003690 if (IS_ERR(trans)) {
3691 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003692 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003693 }
Yan, Zheng80825102009-11-12 09:35:36 +00003694
Josef Bacik5dc562c2012-08-17 13:14:17 -04003695 err = btrfs_drop_extents(trans, root, inode,
3696 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003697 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003698 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003699 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003700 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003701 break;
Miao Xie5b397372011-09-11 10:52:24 -04003702 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003703
Yan Zheng9036c102008-10-30 14:19:41 -04003704 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003705 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003706 0, hole_size, 0, hole_size,
3707 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003708 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003709 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003710 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003711 break;
Miao Xie5b397372011-09-11 10:52:24 -04003712 }
Yan, Zheng80825102009-11-12 09:35:36 +00003713
Josef Bacik5dc562c2012-08-17 13:14:17 -04003714 btrfs_drop_extent_cache(inode, cur_offset,
3715 cur_offset + hole_size - 1, 0);
3716 hole_em = alloc_extent_map();
3717 if (!hole_em) {
3718 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3719 &BTRFS_I(inode)->runtime_flags);
3720 goto next;
3721 }
3722 hole_em->start = cur_offset;
3723 hole_em->len = hole_size;
3724 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003725
Josef Bacik5dc562c2012-08-17 13:14:17 -04003726 hole_em->block_start = EXTENT_MAP_HOLE;
3727 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05003728 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003729 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3730 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3731 hole_em->generation = trans->transid;
3732
3733 while (1) {
3734 write_lock(&em_tree->lock);
3735 err = add_extent_mapping(em_tree, hole_em);
3736 if (!err)
3737 list_move(&hole_em->list,
3738 &em_tree->modified_extents);
3739 write_unlock(&em_tree->lock);
3740 if (err != -EEXIST)
3741 break;
3742 btrfs_drop_extent_cache(inode, cur_offset,
3743 cur_offset +
3744 hole_size - 1, 0);
3745 }
3746 free_extent_map(hole_em);
3747next:
Miao Xie36423202011-12-14 20:12:02 -05003748 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003749 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003750 }
3751 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003752 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003753 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003754 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003755 break;
3756 }
3757
Yan, Zhenga22285a2010-05-16 10:48:46 -04003758 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003759 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3760 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003761 return err;
3762}
3763
Josef Bacika41ad392011-01-31 15:30:16 -05003764static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003765{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003766 struct btrfs_root *root = BTRFS_I(inode)->root;
3767 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003768 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003769 int ret;
3770
Josef Bacika41ad392011-01-31 15:30:16 -05003771 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003772 return 0;
3773
Josef Bacika41ad392011-01-31 15:30:16 -05003774 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003775 truncate_pagecache(inode, oldsize, newsize);
3776 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003777 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003778 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003779
Miao Xief4a2f4c2011-12-14 20:12:01 -05003780 trans = btrfs_start_transaction(root, 1);
3781 if (IS_ERR(trans))
3782 return PTR_ERR(trans);
3783
3784 i_size_write(inode, newsize);
3785 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3786 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003787 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003788 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003789
Josef Bacika41ad392011-01-31 15:30:16 -05003790 /*
3791 * We're truncating a file that used to have good data down to
3792 * zero. Make sure it gets into the ordered flush list so that
3793 * any new writes get down to disk quickly.
3794 */
3795 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003796 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3797 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003798
Josef Bacikf3fe8202013-01-07 17:03:21 -05003799 /*
3800 * 1 for the orphan item we're going to add
3801 * 1 for the orphan item deletion.
3802 */
3803 trans = btrfs_start_transaction(root, 2);
3804 if (IS_ERR(trans))
3805 return PTR_ERR(trans);
3806
3807 /*
3808 * We need to do this in case we fail at _any_ point during the
3809 * actual truncate. Once we do the truncate_setsize we could
3810 * invalidate pages which forces any outstanding ordered io to
3811 * be instantly completed which will give us extents that need
3812 * to be truncated. If we fail to get an orphan inode down we
3813 * could have left over extents that were never meant to live,
3814 * so we need to garuntee from this point on that everything
3815 * will be consistent.
3816 */
3817 ret = btrfs_orphan_add(trans, inode);
3818 btrfs_end_transaction(trans, root);
3819 if (ret)
3820 return ret;
3821
Josef Bacika41ad392011-01-31 15:30:16 -05003822 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3823 truncate_setsize(inode, newsize);
3824 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003825 if (ret && inode->i_nlink)
3826 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003827 }
3828
Josef Bacika41ad392011-01-31 15:30:16 -05003829 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003830}
3831
Chris Mason39279cc2007-06-12 06:35:45 -04003832static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3833{
3834 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003835 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003836 int err;
3837
Li Zefanb83cc962010-12-20 16:04:08 +08003838 if (btrfs_root_readonly(root))
3839 return -EROFS;
3840
Chris Mason39279cc2007-06-12 06:35:45 -04003841 err = inode_change_ok(inode, attr);
3842 if (err)
3843 return err;
3844
Chris Mason5a3f23d2009-03-31 13:27:11 -04003845 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003846 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003847 if (err)
3848 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003849 }
Yan Zheng9036c102008-10-30 14:19:41 -04003850
Christoph Hellwig10257742010-06-04 11:30:02 +02003851 if (attr->ia_valid) {
3852 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003853 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003854 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003855
Josef Bacik22c44fe2011-11-30 10:45:38 -05003856 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003857 err = btrfs_acl_chmod(inode);
3858 }
3859
Chris Mason39279cc2007-06-12 06:35:45 -04003860 return err;
3861}
Chris Mason61295eb2008-01-14 16:24:38 -05003862
Al Virobd555972010-06-07 11:35:40 -04003863void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003864{
3865 struct btrfs_trans_handle *trans;
3866 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003867 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003868 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003869 int ret;
3870
liubo1abe9b82011-03-24 11:18:59 +00003871 trace_btrfs_inode_evict(inode);
3872
Chris Mason39279cc2007-06-12 06:35:45 -04003873 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003874 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003875 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003876 goto no_delete;
3877
Chris Mason39279cc2007-06-12 06:35:45 -04003878 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003879 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003880 goto no_delete;
3881 }
Al Virobd555972010-06-07 11:35:40 -04003882 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003883 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003884
Yan, Zhengc71bf092009-11-12 09:34:40 +00003885 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003886 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003887 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003888 goto no_delete;
3889 }
3890
Yan, Zheng76dda932009-09-21 16:00:26 -04003891 if (inode->i_nlink > 0) {
3892 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3893 goto no_delete;
3894 }
3895
Miao Xie66d8f3d2012-09-06 04:02:28 -06003896 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003897 if (!rsv) {
3898 btrfs_orphan_del(NULL, inode);
3899 goto no_delete;
3900 }
Josef Bacik4a338542011-08-29 11:01:31 -04003901 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003902 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003903 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003904
Chris Masondbe674a2008-07-17 12:54:05 -04003905 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003906
Josef Bacik4289a662011-08-05 13:22:24 -04003907 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003908 * This is a bit simpler than btrfs_truncate since we've already
3909 * reserved our space for our orphan item in the unlink, so we just
3910 * need to reserve some slack space in case we add bytes and update
3911 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003912 */
Yan, Zheng80825102009-11-12 09:35:36 +00003913 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00003914 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3915 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00003916
Josef Bacik726c35f2011-09-26 15:46:06 -04003917 /*
3918 * Try and steal from the global reserve since we will
3919 * likely not use this space anyway, we want to try as
3920 * hard as possible to get this to work.
3921 */
3922 if (ret)
3923 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3924
Yan, Zhengd68fc572010-05-16 10:49:58 -04003925 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003926 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003927 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003928 btrfs_orphan_del(NULL, inode);
3929 btrfs_free_block_rsv(root, rsv);
3930 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003931 }
3932
Miao Xie08e007d2012-10-16 11:33:38 +00003933 trans = btrfs_start_transaction_lflush(root, 1);
Josef Bacik4289a662011-08-05 13:22:24 -04003934 if (IS_ERR(trans)) {
3935 btrfs_orphan_del(NULL, inode);
3936 btrfs_free_block_rsv(root, rsv);
3937 goto no_delete;
3938 }
3939
3940 trans->block_rsv = rsv;
3941
Yan, Zhengd68fc572010-05-16 10:49:58 -04003942 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04003943 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00003944 break;
3945
Miao Xie8407aa42012-09-07 01:43:32 -06003946 trans->block_rsv = &root->fs_info->trans_block_rsv;
3947 ret = btrfs_update_inode(trans, root, inode);
3948 BUG_ON(ret);
3949
Yan, Zheng80825102009-11-12 09:35:36 +00003950 btrfs_end_transaction(trans, root);
3951 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00003952 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04003953 }
3954
Josef Bacik4289a662011-08-05 13:22:24 -04003955 btrfs_free_block_rsv(root, rsv);
3956
Yan, Zheng80825102009-11-12 09:35:36 +00003957 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003958 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003959 ret = btrfs_orphan_del(trans, inode);
3960 BUG_ON(ret);
3961 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003962
Josef Bacik4289a662011-08-05 13:22:24 -04003963 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003964 if (!(root == root->fs_info->tree_root ||
3965 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003966 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003967
Chris Mason54aa1f42007-06-22 14:16:25 -04003968 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003969 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003970no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02003971 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003972 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003973}
3974
3975/*
3976 * this returns the key found in the dir entry in the location pointer.
3977 * If no dir entries were found, location->objectid is 0.
3978 */
3979static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3980 struct btrfs_key *location)
3981{
3982 const char *name = dentry->d_name.name;
3983 int namelen = dentry->d_name.len;
3984 struct btrfs_dir_item *di;
3985 struct btrfs_path *path;
3986 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003987 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003988
3989 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003990 if (!path)
3991 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003992
Li Zefan33345d012011-04-20 10:31:50 +08003993 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003994 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003995 if (IS_ERR(di))
3996 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003997
David Sterbac7040052011-04-19 18:00:01 +02003998 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003999 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004000
Chris Mason5f39d392007-10-15 16:14:19 -04004001 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004002out:
Chris Mason39279cc2007-06-12 06:35:45 -04004003 btrfs_free_path(path);
4004 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004005out_err:
4006 location->objectid = 0;
4007 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004008}
4009
4010/*
4011 * when we hit a tree root in a directory, the btrfs part of the inode
4012 * needs to be changed to reflect the root directory of the tree root. This
4013 * is kind of like crossing a mount point.
4014 */
4015static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004016 struct inode *dir,
4017 struct dentry *dentry,
4018 struct btrfs_key *location,
4019 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004020{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004021 struct btrfs_path *path;
4022 struct btrfs_root *new_root;
4023 struct btrfs_root_ref *ref;
4024 struct extent_buffer *leaf;
4025 int ret;
4026 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004027
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004028 path = btrfs_alloc_path();
4029 if (!path) {
4030 err = -ENOMEM;
4031 goto out;
4032 }
Chris Mason39279cc2007-06-12 06:35:45 -04004033
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004034 err = -ENOENT;
4035 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4036 BTRFS_I(dir)->root->root_key.objectid,
4037 location->objectid);
4038 if (ret) {
4039 if (ret < 0)
4040 err = ret;
4041 goto out;
4042 }
Chris Mason39279cc2007-06-12 06:35:45 -04004043
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004044 leaf = path->nodes[0];
4045 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004046 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004047 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4048 goto out;
4049
4050 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4051 (unsigned long)(ref + 1),
4052 dentry->d_name.len);
4053 if (ret)
4054 goto out;
4055
David Sterbab3b4aa72011-04-21 01:20:15 +02004056 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004057
4058 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4059 if (IS_ERR(new_root)) {
4060 err = PTR_ERR(new_root);
4061 goto out;
4062 }
4063
4064 if (btrfs_root_refs(&new_root->root_item) == 0) {
4065 err = -ENOENT;
4066 goto out;
4067 }
4068
4069 *sub_root = new_root;
4070 location->objectid = btrfs_root_dirid(&new_root->root_item);
4071 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004072 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004073 err = 0;
4074out:
4075 btrfs_free_path(path);
4076 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004077}
4078
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004079static void inode_tree_add(struct inode *inode)
4080{
4081 struct btrfs_root *root = BTRFS_I(inode)->root;
4082 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004083 struct rb_node **p;
4084 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004085 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004086again:
4087 p = &root->inode_tree.rb_node;
4088 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004089
Al Viro1d3382cb2010-10-23 15:19:20 -04004090 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004091 return;
4092
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004093 spin_lock(&root->inode_lock);
4094 while (*p) {
4095 parent = *p;
4096 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4097
Li Zefan33345d012011-04-20 10:31:50 +08004098 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004099 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004100 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004101 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004102 else {
4103 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004104 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004105 rb_erase(parent, &root->inode_tree);
4106 RB_CLEAR_NODE(parent);
4107 spin_unlock(&root->inode_lock);
4108 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004109 }
4110 }
4111 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4112 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4113 spin_unlock(&root->inode_lock);
4114}
4115
4116static void inode_tree_del(struct inode *inode)
4117{
4118 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004119 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004120
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004121 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004122 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004123 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004124 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004125 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004126 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004127 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004128
Josef Bacik0af3d002010-06-21 14:48:16 -04004129 /*
4130 * Free space cache has inodes in the tree root, but the tree root has a
4131 * root_refs of 0, so this could end up dropping the tree root as a
4132 * snapshot, so we need the extra !root->fs_info->tree_root check to
4133 * make sure we don't drop it.
4134 */
4135 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4136 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004137 synchronize_srcu(&root->fs_info->subvol_srcu);
4138 spin_lock(&root->inode_lock);
4139 empty = RB_EMPTY_ROOT(&root->inode_tree);
4140 spin_unlock(&root->inode_lock);
4141 if (empty)
4142 btrfs_add_dead_root(root);
4143 }
4144}
4145
Jeff Mahoney143bede2012-03-01 14:56:26 +01004146void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004147{
4148 struct rb_node *node;
4149 struct rb_node *prev;
4150 struct btrfs_inode *entry;
4151 struct inode *inode;
4152 u64 objectid = 0;
4153
4154 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4155
4156 spin_lock(&root->inode_lock);
4157again:
4158 node = root->inode_tree.rb_node;
4159 prev = NULL;
4160 while (node) {
4161 prev = node;
4162 entry = rb_entry(node, struct btrfs_inode, rb_node);
4163
Li Zefan33345d012011-04-20 10:31:50 +08004164 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004165 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004166 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004167 node = node->rb_right;
4168 else
4169 break;
4170 }
4171 if (!node) {
4172 while (prev) {
4173 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004174 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004175 node = prev;
4176 break;
4177 }
4178 prev = rb_next(prev);
4179 }
4180 }
4181 while (node) {
4182 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004183 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004184 inode = igrab(&entry->vfs_inode);
4185 if (inode) {
4186 spin_unlock(&root->inode_lock);
4187 if (atomic_read(&inode->i_count) > 1)
4188 d_prune_aliases(inode);
4189 /*
Al Viro45321ac2010-06-07 13:43:19 -04004190 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004191 * the inode cache when its usage count
4192 * hits zero.
4193 */
4194 iput(inode);
4195 cond_resched();
4196 spin_lock(&root->inode_lock);
4197 goto again;
4198 }
4199
4200 if (cond_resched_lock(&root->inode_lock))
4201 goto again;
4202
4203 node = rb_next(node);
4204 }
4205 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004206}
4207
Chris Masone02119d2008-09-05 16:13:11 -04004208static int btrfs_init_locked_inode(struct inode *inode, void *p)
4209{
4210 struct btrfs_iget_args *args = p;
4211 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004212 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004213 return 0;
4214}
4215
4216static int btrfs_find_actor(struct inode *inode, void *opaque)
4217{
4218 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004219 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004220 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004221}
4222
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004223static struct inode *btrfs_iget_locked(struct super_block *s,
4224 u64 objectid,
4225 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004226{
4227 struct inode *inode;
4228 struct btrfs_iget_args args;
4229 args.ino = objectid;
4230 args.root = root;
4231
4232 inode = iget5_locked(s, objectid, btrfs_find_actor,
4233 btrfs_init_locked_inode,
4234 (void *)&args);
4235 return inode;
4236}
4237
Balaji Rao1a54ef82008-07-21 02:01:04 +05304238/* Get an inode object given its location and corresponding root.
4239 * Returns in *is_new if the inode was read from disk
4240 */
4241struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004242 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304243{
4244 struct inode *inode;
4245
4246 inode = btrfs_iget_locked(s, location->objectid, root);
4247 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004248 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304249
4250 if (inode->i_state & I_NEW) {
4251 BTRFS_I(inode)->root = root;
4252 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4253 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004254 if (!is_bad_inode(inode)) {
4255 inode_tree_add(inode);
4256 unlock_new_inode(inode);
4257 if (new)
4258 *new = 1;
4259 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004260 unlock_new_inode(inode);
4261 iput(inode);
4262 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004263 }
4264 }
4265
Balaji Rao1a54ef82008-07-21 02:01:04 +05304266 return inode;
4267}
4268
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004269static struct inode *new_simple_dir(struct super_block *s,
4270 struct btrfs_key *key,
4271 struct btrfs_root *root)
4272{
4273 struct inode *inode = new_inode(s);
4274
4275 if (!inode)
4276 return ERR_PTR(-ENOMEM);
4277
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004278 BTRFS_I(inode)->root = root;
4279 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004280 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004281
4282 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004283 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004284 inode->i_fop = &simple_dir_operations;
4285 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4286 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4287
4288 return inode;
4289}
4290
Chris Mason3de45862008-11-17 21:02:50 -05004291struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004292{
Chris Masond3977122009-01-05 21:25:51 -05004293 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004294 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004295 struct btrfs_root *sub_root = root;
4296 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004297 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004298 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004299
4300 if (dentry->d_name.len > BTRFS_NAME_LEN)
4301 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004302
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004303 if (unlikely(d_need_lookup(dentry))) {
4304 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4305 kfree(dentry->d_fsdata);
4306 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004307 /* This thing is hashed, drop it for now */
4308 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004309 } else {
4310 ret = btrfs_inode_by_name(dir, dentry, &location);
4311 }
Chris Mason5f39d392007-10-15 16:14:19 -04004312
Chris Mason39279cc2007-06-12 06:35:45 -04004313 if (ret < 0)
4314 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004315
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004316 if (location.objectid == 0)
4317 return NULL;
4318
4319 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004320 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004321 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004322 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004323
4324 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4325
Yan, Zheng76dda932009-09-21 16:00:26 -04004326 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004327 ret = fixup_tree_root_location(root, dir, dentry,
4328 &location, &sub_root);
4329 if (ret < 0) {
4330 if (ret != -ENOENT)
4331 inode = ERR_PTR(ret);
4332 else
4333 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4334 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004335 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004336 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004337 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4338
Julia Lawall34d19ba2011-01-24 19:55:19 +00004339 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004340 down_read(&root->fs_info->cleanup_work_sem);
4341 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004342 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004343 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004344 if (ret)
4345 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004346 }
4347
Chris Mason3de45862008-11-17 21:02:50 -05004348 return inode;
4349}
4350
Nick Pigginfe15ce42011-01-07 17:49:23 +11004351static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004352{
4353 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004354 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004355
Li Zefan848cce02012-02-21 17:04:28 +08004356 if (!inode && !IS_ROOT(dentry))
4357 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004358
Li Zefan848cce02012-02-21 17:04:28 +08004359 if (inode) {
4360 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004361 if (btrfs_root_refs(&root->root_item) == 0)
4362 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004363
4364 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4365 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004366 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004367 return 0;
4368}
4369
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004370static void btrfs_dentry_release(struct dentry *dentry)
4371{
4372 if (dentry->d_fsdata)
4373 kfree(dentry->d_fsdata);
4374}
4375
Chris Mason3de45862008-11-17 21:02:50 -05004376static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004377 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004378{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004379 struct dentry *ret;
4380
4381 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4382 if (unlikely(d_need_lookup(dentry))) {
4383 spin_lock(&dentry->d_lock);
4384 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4385 spin_unlock(&dentry->d_lock);
4386 }
4387 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004388}
4389
Miao Xie16cdcec2011-04-22 18:12:22 +08004390unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004391 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4392};
4393
David Woodhousecbdf5a22008-08-06 19:42:33 +01004394static int btrfs_real_readdir(struct file *filp, void *dirent,
4395 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004396{
Chris Mason6da6aba2007-12-18 16:15:09 -05004397 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004398 struct btrfs_root *root = BTRFS_I(inode)->root;
4399 struct btrfs_item *item;
4400 struct btrfs_dir_item *di;
4401 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004402 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004403 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004404 struct list_head ins_list;
4405 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004406 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004407 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004408 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004409 unsigned char d_type;
4410 int over = 0;
4411 u32 di_cur;
4412 u32 di_total;
4413 u32 di_len;
4414 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004415 char tmp_name[32];
4416 char *name_ptr;
4417 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004418 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004419
4420 /* FIXME, use a real flag for deciding about the key type */
4421 if (root->fs_info->tree_root == root)
4422 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004423
Chris Mason39544012007-12-12 14:38:19 -05004424 /* special case for "." */
4425 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004426 over = filldir(dirent, ".", 1,
4427 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004428 if (over)
4429 return 0;
4430 filp->f_pos = 1;
4431 }
Chris Mason39544012007-12-12 14:38:19 -05004432 /* special case for .., just use the back ref */
4433 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004434 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004435 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004436 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004437 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004438 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004439 filp->f_pos = 2;
4440 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004441 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004442 if (!path)
4443 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004444
Josef Bacik026fd312011-05-13 10:32:11 -04004445 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004446
Miao Xie16cdcec2011-04-22 18:12:22 +08004447 if (key_type == BTRFS_DIR_INDEX_KEY) {
4448 INIT_LIST_HEAD(&ins_list);
4449 INIT_LIST_HEAD(&del_list);
4450 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4451 }
4452
Chris Mason39279cc2007-06-12 06:35:45 -04004453 btrfs_set_key_type(&key, key_type);
4454 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004455 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004456
Chris Mason39279cc2007-06-12 06:35:45 -04004457 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4458 if (ret < 0)
4459 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004460
4461 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004462 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004463 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004464 if (slot >= btrfs_header_nritems(leaf)) {
4465 ret = btrfs_next_leaf(root, path);
4466 if (ret < 0)
4467 goto err;
4468 else if (ret > 0)
4469 break;
4470 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004471 }
Chris Mason3de45862008-11-17 21:02:50 -05004472
Chris Mason5f39d392007-10-15 16:14:19 -04004473 item = btrfs_item_nr(leaf, slot);
4474 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4475
4476 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004477 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004478 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004479 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004480 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004481 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004482 if (key_type == BTRFS_DIR_INDEX_KEY &&
4483 btrfs_should_delete_dir_index(&del_list,
4484 found_key.offset))
4485 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004486
4487 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004488 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004489
Chris Mason39279cc2007-06-12 06:35:45 -04004490 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4491 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004492 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004493
4494 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004495 struct btrfs_key location;
4496
Josef Bacik22a94d42011-03-16 16:47:17 -04004497 if (verify_dir_item(root, leaf, di))
4498 break;
4499
Chris Mason5f39d392007-10-15 16:14:19 -04004500 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004501 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004502 name_ptr = tmp_name;
4503 } else {
4504 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004505 if (!name_ptr) {
4506 ret = -ENOMEM;
4507 goto err;
4508 }
Chris Mason5f39d392007-10-15 16:14:19 -04004509 }
4510 read_extent_buffer(leaf, name_ptr,
4511 (unsigned long)(di + 1), name_len);
4512
4513 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4514 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004515
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004516
Chris Mason3de45862008-11-17 21:02:50 -05004517 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004518 * skip it.
4519 *
4520 * In contrast to old kernels, we insert the snapshot's
4521 * dir item and dir index after it has been created, so
4522 * we won't find a reference to our own snapshot. We
4523 * still keep the following code for backward
4524 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004525 */
4526 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4527 location.objectid == root->root_key.objectid) {
4528 over = 0;
4529 goto skip;
4530 }
Chris Mason5f39d392007-10-15 16:14:19 -04004531 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004532 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004533 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004534
Chris Mason3de45862008-11-17 21:02:50 -05004535skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004536 if (name_ptr != tmp_name)
4537 kfree(name_ptr);
4538
Chris Mason39279cc2007-06-12 06:35:45 -04004539 if (over)
4540 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004541 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004542 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004543 di_cur += di_len;
4544 di = (struct btrfs_dir_item *)((char *)di + di_len);
4545 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004546next:
4547 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004548 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004549
Miao Xie16cdcec2011-04-22 18:12:22 +08004550 if (key_type == BTRFS_DIR_INDEX_KEY) {
4551 if (is_curr)
4552 filp->f_pos++;
4553 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4554 &ins_list);
4555 if (ret)
4556 goto nopos;
4557 }
4558
David Woodhouse49593bf2008-08-17 17:08:36 +01004559 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004560 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004561 /*
4562 * 32-bit glibc will use getdents64, but then strtol -
4563 * so the last number we can serve is this.
4564 */
4565 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004566 else
4567 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004568nopos:
4569 ret = 0;
4570err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004571 if (key_type == BTRFS_DIR_INDEX_KEY)
4572 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004573 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004574 return ret;
4575}
4576
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004577int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004578{
4579 struct btrfs_root *root = BTRFS_I(inode)->root;
4580 struct btrfs_trans_handle *trans;
4581 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004582 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004583
Josef Bacik72ac3c02012-05-23 14:13:11 -04004584 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004585 return 0;
4586
Liu Bo83eea1f2012-07-10 05:28:39 -06004587 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004588 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004589
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004590 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004591 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004592 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004593 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004594 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004595 if (IS_ERR(trans))
4596 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004597 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004598 }
4599 return ret;
4600}
4601
4602/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004603 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004604 * inode changes. But, it is most likely to find the inode in cache.
4605 * FIXME, needs more benchmarking...there are no reasons other than performance
4606 * to keep or drop this code.
4607 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004608int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004609{
4610 struct btrfs_root *root = BTRFS_I(inode)->root;
4611 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004612 int ret;
4613
Josef Bacik72ac3c02012-05-23 14:13:11 -04004614 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004615 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004616
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004617 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004618 if (IS_ERR(trans))
4619 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004620
4621 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004622 if (ret && ret == -ENOSPC) {
4623 /* whoops, lets try again with the full transaction */
4624 btrfs_end_transaction(trans, root);
4625 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004626 if (IS_ERR(trans))
4627 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004628
Chris Mason94b60442010-05-26 11:02:00 -04004629 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004630 }
Chris Mason39279cc2007-06-12 06:35:45 -04004631 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004632 if (BTRFS_I(inode)->delayed_node)
4633 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004634
4635 return ret;
4636}
4637
4638/*
4639 * This is a copy of file_update_time. We need this so we can return error on
4640 * ENOSPC for updating the inode in the case of file write and mmap writes.
4641 */
Josef Bacike41f9412012-03-26 09:46:47 -04004642static int btrfs_update_time(struct inode *inode, struct timespec *now,
4643 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004644{
Alexander Block2bc5565282012-06-15 09:49:33 +02004645 struct btrfs_root *root = BTRFS_I(inode)->root;
4646
4647 if (btrfs_root_readonly(root))
4648 return -EROFS;
4649
Josef Bacike41f9412012-03-26 09:46:47 -04004650 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004651 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004652 if (flags & S_CTIME)
4653 inode->i_ctime = *now;
4654 if (flags & S_MTIME)
4655 inode->i_mtime = *now;
4656 if (flags & S_ATIME)
4657 inode->i_atime = *now;
4658 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004659}
4660
Chris Masond352ac62008-09-29 15:18:18 -04004661/*
4662 * find the highest existing sequence number in a directory
4663 * and then set the in-memory index_cnt variable to reflect
4664 * free sequence numbers
4665 */
Josef Bacikaec74772008-07-24 12:12:38 -04004666static int btrfs_set_inode_index_count(struct inode *inode)
4667{
4668 struct btrfs_root *root = BTRFS_I(inode)->root;
4669 struct btrfs_key key, found_key;
4670 struct btrfs_path *path;
4671 struct extent_buffer *leaf;
4672 int ret;
4673
Li Zefan33345d012011-04-20 10:31:50 +08004674 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004675 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4676 key.offset = (u64)-1;
4677
4678 path = btrfs_alloc_path();
4679 if (!path)
4680 return -ENOMEM;
4681
4682 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4683 if (ret < 0)
4684 goto out;
4685 /* FIXME: we should be able to handle this */
4686 if (ret == 0)
4687 goto out;
4688 ret = 0;
4689
4690 /*
4691 * MAGIC NUMBER EXPLANATION:
4692 * since we search a directory based on f_pos we have to start at 2
4693 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4694 * else has to start at 2
4695 */
4696 if (path->slots[0] == 0) {
4697 BTRFS_I(inode)->index_cnt = 2;
4698 goto out;
4699 }
4700
4701 path->slots[0]--;
4702
4703 leaf = path->nodes[0];
4704 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4705
Li Zefan33345d012011-04-20 10:31:50 +08004706 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004707 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4708 BTRFS_I(inode)->index_cnt = 2;
4709 goto out;
4710 }
4711
4712 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4713out:
4714 btrfs_free_path(path);
4715 return ret;
4716}
4717
Chris Masond352ac62008-09-29 15:18:18 -04004718/*
4719 * helper to find a free sequence number in a given directory. This current
4720 * code is very simple, later versions will do smarter things in the btree
4721 */
Chris Mason3de45862008-11-17 21:02:50 -05004722int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004723{
4724 int ret = 0;
4725
4726 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004727 ret = btrfs_inode_delayed_dir_index_count(dir);
4728 if (ret) {
4729 ret = btrfs_set_inode_index_count(dir);
4730 if (ret)
4731 return ret;
4732 }
Josef Bacikaec74772008-07-24 12:12:38 -04004733 }
4734
Chris Mason00e4e6b2008-08-05 11:18:09 -04004735 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004736 BTRFS_I(dir)->index_cnt++;
4737
4738 return ret;
4739}
4740
Chris Mason39279cc2007-06-12 06:35:45 -04004741static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4742 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004743 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004744 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004745 u64 ref_objectid, u64 objectid,
4746 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004747{
4748 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004749 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004750 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004751 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004752 struct btrfs_inode_ref *ref;
4753 struct btrfs_key key[2];
4754 u32 sizes[2];
4755 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004756 int ret;
4757 int owner;
4758
Chris Mason5f39d392007-10-15 16:14:19 -04004759 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004760 if (!path)
4761 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004762
Chris Mason39279cc2007-06-12 06:35:45 -04004763 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004764 if (!inode) {
4765 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004766 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004767 }
Chris Mason39279cc2007-06-12 06:35:45 -04004768
Li Zefan581bb052011-04-20 10:06:11 +08004769 /*
4770 * we have to initialize this early, so we can reclaim the inode
4771 * number if we fail afterwards in this function.
4772 */
4773 inode->i_ino = objectid;
4774
Josef Bacikaec74772008-07-24 12:12:38 -04004775 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004776 trace_btrfs_inode_request(dir);
4777
Chris Mason3de45862008-11-17 21:02:50 -05004778 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004779 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004780 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004781 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004782 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004783 }
Josef Bacikaec74772008-07-24 12:12:38 -04004784 }
4785 /*
4786 * index_cnt is ignored for everything but a dir,
4787 * btrfs_get_inode_index_count has an explanation for the magic
4788 * number
4789 */
4790 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004791 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004792 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004793 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04004794
Josef Bacik5dc562c2012-08-17 13:14:17 -04004795 /*
4796 * We could have gotten an inode number from somebody who was fsynced
4797 * and then removed in this same transaction, so let's just set full
4798 * sync since it will be a full sync anyway and this will blow away the
4799 * old info in the log.
4800 */
4801 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4802
Al Viro569254b2011-07-24 17:08:40 -04004803 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004804 owner = 0;
4805 else
4806 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004807
4808 key[0].objectid = objectid;
4809 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4810 key[0].offset = 0;
4811
Mark Fashehf1863732012-08-08 11:32:27 -07004812 /*
4813 * Start new inodes with an inode_ref. This is slightly more
4814 * efficient for small numbers of hard links since they will
4815 * be packed into one item. Extended refs will kick in if we
4816 * add more hard links than can fit in the ref item.
4817 */
Chris Mason9c583092008-01-29 15:15:18 -05004818 key[1].objectid = objectid;
4819 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4820 key[1].offset = ref_objectid;
4821
4822 sizes[0] = sizeof(struct btrfs_inode_item);
4823 sizes[1] = name_len + sizeof(*ref);
4824
Chris Masonb9473432009-03-13 11:00:37 -04004825 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004826 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4827 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004828 goto fail;
4829
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004830 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004831 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004832 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004833 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4834 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004835 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4836 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004837 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004838
4839 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4840 struct btrfs_inode_ref);
4841 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004842 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004843 ptr = (unsigned long)(ref + 1);
4844 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4845
Chris Mason5f39d392007-10-15 16:14:19 -04004846 btrfs_mark_buffer_dirty(path->nodes[0]);
4847 btrfs_free_path(path);
4848
Chris Mason39279cc2007-06-12 06:35:45 -04004849 location = &BTRFS_I(inode)->location;
4850 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004851 location->offset = 0;
4852 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4853
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004854 btrfs_inherit_iflags(inode, dir);
4855
Al Viro569254b2011-07-24 17:08:40 -04004856 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004857 if (btrfs_test_opt(root, NODATASUM))
4858 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06004859 if (btrfs_test_opt(root, NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004860 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4861 }
4862
Chris Mason39279cc2007-06-12 06:35:45 -04004863 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004864 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004865
4866 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004867 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004868
Alexander Block8ea05e32012-07-25 17:35:53 +02004869 btrfs_update_root_times(trans, root);
4870
Chris Mason39279cc2007-06-12 06:35:45 -04004871 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004872fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004873 if (dir)
4874 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004875 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004876 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004877 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004878}
4879
4880static inline u8 btrfs_inode_type(struct inode *inode)
4881{
4882 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4883}
4884
Chris Masond352ac62008-09-29 15:18:18 -04004885/*
4886 * utility function to add 'inode' into 'parent_inode' with
4887 * a give name and a given sequence number.
4888 * if 'add_backref' is true, also insert a backref from the
4889 * inode to the parent directory.
4890 */
Chris Masone02119d2008-09-05 16:13:11 -04004891int btrfs_add_link(struct btrfs_trans_handle *trans,
4892 struct inode *parent_inode, struct inode *inode,
4893 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004894{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004895 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004896 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004897 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004898 u64 ino = btrfs_ino(inode);
4899 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004900
Li Zefan33345d012011-04-20 10:31:50 +08004901 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004902 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4903 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004904 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004905 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4906 key.offset = 0;
4907 }
Chris Mason39279cc2007-06-12 06:35:45 -04004908
Li Zefan33345d012011-04-20 10:31:50 +08004909 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004910 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4911 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004912 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004913 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004914 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4915 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004916 }
4917
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004918 /* Nothing to clean up yet */
4919 if (ret)
4920 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004921
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004922 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4923 parent_inode, &key,
4924 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05004925 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004926 goto fail_dir_item;
4927 else if (ret) {
4928 btrfs_abort_transaction(trans, root, ret);
4929 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004930 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004931
4932 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4933 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004934 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004935 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4936 ret = btrfs_update_inode(trans, root, parent_inode);
4937 if (ret)
4938 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004939 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004940
4941fail_dir_item:
4942 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4943 u64 local_index;
4944 int err;
4945 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4946 key.objectid, root->root_key.objectid,
4947 parent_ino, &local_index, name, name_len);
4948
4949 } else if (add_backref) {
4950 u64 local_index;
4951 int err;
4952
4953 err = btrfs_del_inode_ref(trans, root, name, name_len,
4954 ino, parent_ino, &local_index);
4955 }
4956 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004957}
4958
4959static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004960 struct inode *dir, struct dentry *dentry,
4961 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004962{
Josef Bacika1b075d2010-11-19 20:36:11 +00004963 int err = btrfs_add_link(trans, dir, inode,
4964 dentry->d_name.name, dentry->d_name.len,
4965 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004966 if (err > 0)
4967 err = -EEXIST;
4968 return err;
4969}
4970
Josef Bacik618e21d2007-07-11 10:18:17 -04004971static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004972 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004973{
4974 struct btrfs_trans_handle *trans;
4975 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004976 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004977 int err;
4978 int drop_inode = 0;
4979 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004980 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004981
4982 if (!new_valid_dev(rdev))
4983 return -EINVAL;
4984
Josef Bacik9ed74f22009-09-11 16:12:44 -04004985 /*
4986 * 2 for inode item and ref
4987 * 2 for dir items
4988 * 1 for xattr if selinux is on
4989 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004990 trans = btrfs_start_transaction(root, 5);
4991 if (IS_ERR(trans))
4992 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004993
Li Zefan581bb052011-04-20 10:06:11 +08004994 err = btrfs_find_free_ino(root, &objectid);
4995 if (err)
4996 goto out_unlock;
4997
Josef Bacikaec74772008-07-24 12:12:38 -04004998 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004999 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005000 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005001 if (IS_ERR(inode)) {
5002 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005003 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005004 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005005
Eric Paris2a7dba32011-02-01 11:05:39 -05005006 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005007 if (err) {
5008 drop_inode = 1;
5009 goto out_unlock;
5010 }
5011
Filipe Brandenburger2794ed02012-11-30 03:40:08 +00005012 err = btrfs_update_inode(trans, root, inode);
5013 if (err) {
5014 drop_inode = 1;
5015 goto out_unlock;
5016 }
5017
Casey Schauflerad19db72011-12-15 10:09:07 -05005018 /*
5019 * If the active LSM wants to access the inode during
5020 * d_instantiate it needs these. Smack checks to see
5021 * if the filesystem supports xattrs by looking at the
5022 * ops vector.
5023 */
5024
5025 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005026 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005027 if (err)
5028 drop_inode = 1;
5029 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005030 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005031 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005032 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005033 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005034out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005035 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005036 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005037 if (drop_inode) {
5038 inode_dec_link_count(inode);
5039 iput(inode);
5040 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005041 return err;
5042}
5043
Chris Mason39279cc2007-06-12 06:35:45 -04005044static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005045 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005046{
5047 struct btrfs_trans_handle *trans;
5048 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005049 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005050 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005051 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005052 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005053 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005054
Josef Bacik9ed74f22009-09-11 16:12:44 -04005055 /*
5056 * 2 for inode item and ref
5057 * 2 for dir items
5058 * 1 for xattr if selinux is on
5059 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005060 trans = btrfs_start_transaction(root, 5);
5061 if (IS_ERR(trans))
5062 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005063
Li Zefan581bb052011-04-20 10:06:11 +08005064 err = btrfs_find_free_ino(root, &objectid);
5065 if (err)
5066 goto out_unlock;
5067
Josef Bacikaec74772008-07-24 12:12:38 -04005068 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005069 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005070 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005071 if (IS_ERR(inode)) {
5072 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005073 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005074 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005075 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005076
Eric Paris2a7dba32011-02-01 11:05:39 -05005077 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005078 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005079 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005080
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005081 err = btrfs_update_inode(trans, root, inode);
5082 if (err)
5083 goto out_unlock;
5084
Casey Schauflerad19db72011-12-15 10:09:07 -05005085 /*
5086 * If the active LSM wants to access the inode during
5087 * d_instantiate it needs these. Smack checks to see
5088 * if the filesystem supports xattrs by looking at the
5089 * ops vector.
5090 */
5091 inode->i_fop = &btrfs_file_operations;
5092 inode->i_op = &btrfs_file_inode_operations;
5093
Josef Bacika1b075d2010-11-19 20:36:11 +00005094 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005095 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005096 goto out_unlock;
5097
5098 inode->i_mapping->a_ops = &btrfs_aops;
5099 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5100 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5101 d_instantiate(dentry, inode);
5102
Chris Mason39279cc2007-06-12 06:35:45 -04005103out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005104 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005105 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005106 inode_dec_link_count(inode);
5107 iput(inode);
5108 }
Liu Bob53d3f52012-11-14 14:34:34 +00005109 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005110 return err;
5111}
5112
5113static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5114 struct dentry *dentry)
5115{
5116 struct btrfs_trans_handle *trans;
5117 struct btrfs_root *root = BTRFS_I(dir)->root;
5118 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005119 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005120 int err;
5121 int drop_inode = 0;
5122
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005123 /* do not allow sys_link's with other subvols of the same device */
5124 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005125 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005126
Mark Fashehf1863732012-08-08 11:32:27 -07005127 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005128 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005129
Chris Mason3de45862008-11-17 21:02:50 -05005130 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005131 if (err)
5132 goto fail;
5133
Yan, Zhenga22285a2010-05-16 10:48:46 -04005134 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005135 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005136 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005137 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005138 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005139 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005140 if (IS_ERR(trans)) {
5141 err = PTR_ERR(trans);
5142 goto fail;
5143 }
Chris Mason5f39d392007-10-15 16:14:19 -04005144
Miao Xie31534952011-04-13 13:19:21 +08005145 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005146 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005147 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005148 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005149 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005150
Josef Bacika1b075d2010-11-19 20:36:11 +00005151 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005152
Yan, Zhenga5719522009-09-24 09:17:31 -04005153 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005154 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005155 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005156 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005157 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005158 if (err)
5159 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005160 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005161 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005162 }
Chris Mason39279cc2007-06-12 06:35:45 -04005163
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005164 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005165fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005166 if (drop_inode) {
5167 inode_dec_link_count(inode);
5168 iput(inode);
5169 }
Liu Bob53d3f52012-11-14 14:34:34 +00005170 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005171 return err;
5172}
5173
Al Viro18bb1db2011-07-26 01:41:39 -04005174static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005175{
Chris Masonb9d86662008-05-02 16:13:49 -04005176 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005177 struct btrfs_trans_handle *trans;
5178 struct btrfs_root *root = BTRFS_I(dir)->root;
5179 int err = 0;
5180 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005181 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005182 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005183
Josef Bacik9ed74f22009-09-11 16:12:44 -04005184 /*
5185 * 2 items for inode and ref
5186 * 2 items for dir items
5187 * 1 for xattr if selinux is on
5188 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005189 trans = btrfs_start_transaction(root, 5);
5190 if (IS_ERR(trans))
5191 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005192
Li Zefan581bb052011-04-20 10:06:11 +08005193 err = btrfs_find_free_ino(root, &objectid);
5194 if (err)
5195 goto out_fail;
5196
Josef Bacikaec74772008-07-24 12:12:38 -04005197 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005198 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005199 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005200 if (IS_ERR(inode)) {
5201 err = PTR_ERR(inode);
5202 goto out_fail;
5203 }
Chris Mason5f39d392007-10-15 16:14:19 -04005204
Chris Mason39279cc2007-06-12 06:35:45 -04005205 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005206
Eric Paris2a7dba32011-02-01 11:05:39 -05005207 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005208 if (err)
5209 goto out_fail;
5210
Chris Mason39279cc2007-06-12 06:35:45 -04005211 inode->i_op = &btrfs_dir_inode_operations;
5212 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005213
Chris Masondbe674a2008-07-17 12:54:05 -04005214 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005215 err = btrfs_update_inode(trans, root, inode);
5216 if (err)
5217 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005218
Josef Bacika1b075d2010-11-19 20:36:11 +00005219 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5220 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005221 if (err)
5222 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005223
Chris Mason39279cc2007-06-12 06:35:45 -04005224 d_instantiate(dentry, inode);
5225 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005226
5227out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005228 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005229 if (drop_on_err)
5230 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005231 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005232 return err;
5233}
5234
Chris Masond352ac62008-09-29 15:18:18 -04005235/* helper for btfs_get_extent. Given an existing extent in the tree,
5236 * and an extent that you want to insert, deal with overlap and insert
5237 * the new extent into the tree.
5238 */
Chris Mason3b951512008-04-17 11:29:12 -04005239static int merge_extent_mapping(struct extent_map_tree *em_tree,
5240 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005241 struct extent_map *em,
5242 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005243{
5244 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005245
Chris Masone6dcd2d2008-07-17 12:53:50 -04005246 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5247 start_diff = map_start - em->start;
5248 em->start = map_start;
5249 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005250 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5251 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005252 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005253 em->block_len -= start_diff;
5254 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005255 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005256}
5257
Chris Masonc8b97812008-10-29 14:49:59 -04005258static noinline int uncompress_inline(struct btrfs_path *path,
5259 struct inode *inode, struct page *page,
5260 size_t pg_offset, u64 extent_offset,
5261 struct btrfs_file_extent_item *item)
5262{
5263 int ret;
5264 struct extent_buffer *leaf = path->nodes[0];
5265 char *tmp;
5266 size_t max_size;
5267 unsigned long inline_size;
5268 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005269 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005270
5271 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005272 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005273 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5274 inline_size = btrfs_file_extent_inline_item_len(leaf,
5275 btrfs_item_nr(leaf, path->slots[0]));
5276 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005277 if (!tmp)
5278 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005279 ptr = btrfs_file_extent_inline_start(item);
5280
5281 read_extent_buffer(leaf, tmp, ptr, inline_size);
5282
Chris Mason5b050f02008-11-11 09:34:41 -05005283 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005284 ret = btrfs_decompress(compress_type, tmp, page,
5285 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005286 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005287 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005288 unsigned long copy_size = min_t(u64,
5289 PAGE_CACHE_SIZE - pg_offset,
5290 max_size - extent_offset);
5291 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005292 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005293 }
5294 kfree(tmp);
5295 return 0;
5296}
5297
Chris Masond352ac62008-09-29 15:18:18 -04005298/*
5299 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005300 * the ugly parts come from merging extents from the disk with the in-ram
5301 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005302 * where the in-ram extents might be locked pending data=ordered completion.
5303 *
5304 * This also copies inline extents directly into the page.
5305 */
Chris Masond3977122009-01-05 21:25:51 -05005306
Chris Masona52d9a82007-08-27 16:49:44 -04005307struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005308 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005309 int create)
5310{
5311 int ret;
5312 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005313 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005314 u64 extent_start = 0;
5315 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005316 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005317 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005318 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005319 struct btrfs_root *root = BTRFS_I(inode)->root;
5320 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005321 struct extent_buffer *leaf;
5322 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005323 struct extent_map *em = NULL;
5324 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005325 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005326 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005327 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005328
Chris Masona52d9a82007-08-27 16:49:44 -04005329again:
Chris Mason890871b2009-09-02 16:24:52 -04005330 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005331 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005332 if (em)
5333 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005334 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005335
Chris Masona52d9a82007-08-27 16:49:44 -04005336 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005337 if (em->start > start || em->start + em->len <= start)
5338 free_extent_map(em);
5339 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005340 free_extent_map(em);
5341 else
5342 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005343 }
David Sterba172ddd62011-04-21 00:48:27 +02005344 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005345 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005346 err = -ENOMEM;
5347 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005348 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005349 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005350 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005351 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005352 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005353 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005354
5355 if (!path) {
5356 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005357 if (!path) {
5358 err = -ENOMEM;
5359 goto out;
5360 }
5361 /*
5362 * Chances are we'll be called again, so go ahead and do
5363 * readahead
5364 */
5365 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005366 }
5367
Chris Mason179e29e2007-11-01 11:28:41 -04005368 ret = btrfs_lookup_file_extent(trans, root, path,
5369 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005370 if (ret < 0) {
5371 err = ret;
5372 goto out;
5373 }
5374
5375 if (ret != 0) {
5376 if (path->slots[0] == 0)
5377 goto not_found;
5378 path->slots[0]--;
5379 }
5380
Chris Mason5f39d392007-10-15 16:14:19 -04005381 leaf = path->nodes[0];
5382 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005383 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005384 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005385 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5386 found_type = btrfs_key_type(&found_key);
5387 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005388 found_type != BTRFS_EXTENT_DATA_KEY) {
5389 goto not_found;
5390 }
5391
Chris Mason5f39d392007-10-15 16:14:19 -04005392 found_type = btrfs_file_extent_type(leaf, item);
5393 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005394 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005395 if (found_type == BTRFS_FILE_EXTENT_REG ||
5396 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005397 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005398 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005399 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5400 size_t size;
5401 size = btrfs_file_extent_inline_len(leaf, item);
5402 extent_end = (extent_start + size + root->sectorsize - 1) &
5403 ~((u64)root->sectorsize - 1);
5404 }
5405
5406 if (start >= extent_end) {
5407 path->slots[0]++;
5408 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5409 ret = btrfs_next_leaf(root, path);
5410 if (ret < 0) {
5411 err = ret;
5412 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005413 }
Yan Zheng9036c102008-10-30 14:19:41 -04005414 if (ret > 0)
5415 goto not_found;
5416 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005417 }
Yan Zheng9036c102008-10-30 14:19:41 -04005418 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5419 if (found_key.objectid != objectid ||
5420 found_key.type != BTRFS_EXTENT_DATA_KEY)
5421 goto not_found;
5422 if (start + len <= found_key.offset)
5423 goto not_found;
5424 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005425 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04005426 em->len = found_key.offset - start;
5427 goto not_found_em;
5428 }
5429
Yan Zhengd899e052008-10-30 14:25:28 -04005430 if (found_type == BTRFS_FILE_EXTENT_REG ||
5431 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005432 em->start = extent_start;
5433 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005434 em->orig_start = extent_start -
5435 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05005436 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5437 item);
Chris Masondb945352007-10-15 16:15:53 -04005438 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5439 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005440 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005441 goto insert;
5442 }
Li Zefan261507a02010-12-17 14:21:50 +08005443 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005444 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005445 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005446 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05005447 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005448 } else {
5449 bytenr += btrfs_file_extent_offset(leaf, item);
5450 em->block_start = bytenr;
5451 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005452 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5453 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005454 }
Chris Masona52d9a82007-08-27 16:49:44 -04005455 goto insert;
5456 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005457 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005458 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005459 size_t size;
5460 size_t extent_offset;
5461 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005462
Chris Masona52d9a82007-08-27 16:49:44 -04005463 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005464 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005465 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005466 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005467 goto out;
5468 }
5469
Yan Zheng9036c102008-10-30 14:19:41 -04005470 size = btrfs_file_extent_inline_len(leaf, item);
5471 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005472 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005473 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005474 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005475 em->len = (copy_size + root->sectorsize - 1) &
5476 ~((u64)root->sectorsize - 1);
Josef Bacikb4939682012-12-03 10:31:19 -05005477 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005478 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08005479 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005480 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005481 em->compress_type = compress_type;
5482 }
Yan689f9342007-10-29 11:41:07 -04005483 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005484 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005485 if (btrfs_file_extent_compression(leaf, item) !=
5486 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005487 ret = uncompress_inline(path, inode, page,
5488 pg_offset,
5489 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005490 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005491 } else {
5492 map = kmap(page);
5493 read_extent_buffer(leaf, map + pg_offset, ptr,
5494 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005495 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5496 memset(map + pg_offset + copy_size, 0,
5497 PAGE_CACHE_SIZE - pg_offset -
5498 copy_size);
5499 }
Chris Masonc8b97812008-10-29 14:49:59 -04005500 kunmap(page);
5501 }
Chris Mason179e29e2007-11-01 11:28:41 -04005502 flush_dcache_page(page);
5503 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005504 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005505 if (!trans) {
5506 kunmap(page);
5507 free_extent_map(em);
5508 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005509
David Sterbab3b4aa72011-04-21 01:20:15 +02005510 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005511 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005512
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005513 if (IS_ERR(trans))
5514 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005515 goto again;
5516 }
Chris Masonc8b97812008-10-29 14:49:59 -04005517 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005518 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005519 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005520 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005521 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005522 }
Chris Masond1310b22008-01-24 16:13:08 -05005523 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005524 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005525 goto insert;
5526 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005527 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005528 }
5529not_found:
5530 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005531 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005532 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005533not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005534 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005535 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005536insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005537 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005538 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005539 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5540 "[%llu %llu]\n", (unsigned long long)em->start,
5541 (unsigned long long)em->len,
5542 (unsigned long long)start,
5543 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005544 err = -EIO;
5545 goto out;
5546 }
Chris Masond1310b22008-01-24 16:13:08 -05005547
5548 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005549 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005550 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005551 /* it is possible that someone inserted the extent into the tree
5552 * while we had the lock dropped. It is also possible that
5553 * an overlapping map exists in the tree
5554 */
Chris Masona52d9a82007-08-27 16:49:44 -04005555 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005556 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005557
5558 ret = 0;
5559
Chris Mason3b951512008-04-17 11:29:12 -04005560 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005561 if (existing && (existing->start > start ||
5562 existing->start + existing->len <= start)) {
5563 free_extent_map(existing);
5564 existing = NULL;
5565 }
Chris Mason3b951512008-04-17 11:29:12 -04005566 if (!existing) {
5567 existing = lookup_extent_mapping(em_tree, em->start,
5568 em->len);
5569 if (existing) {
5570 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005571 em, start,
5572 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005573 free_extent_map(existing);
5574 if (err) {
5575 free_extent_map(em);
5576 em = NULL;
5577 }
5578 } else {
5579 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005580 free_extent_map(em);
5581 em = NULL;
5582 }
5583 } else {
5584 free_extent_map(em);
5585 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005586 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005587 }
Chris Masona52d9a82007-08-27 16:49:44 -04005588 }
Chris Mason890871b2009-09-02 16:24:52 -04005589 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005590out:
liubo1abe9b82011-03-24 11:18:59 +00005591
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005592 if (em)
5593 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005594
Chris Masonf4219502008-07-22 11:18:09 -04005595 if (path)
5596 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005597 if (trans) {
5598 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005599 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005600 err = ret;
5601 }
Chris Masona52d9a82007-08-27 16:49:44 -04005602 if (err) {
5603 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005604 return ERR_PTR(err);
5605 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005606 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005607 return em;
5608}
5609
Chris Masonec29ed52011-02-23 16:23:20 -05005610struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5611 size_t pg_offset, u64 start, u64 len,
5612 int create)
5613{
5614 struct extent_map *em;
5615 struct extent_map *hole_em = NULL;
5616 u64 range_start = start;
5617 u64 end;
5618 u64 found;
5619 u64 found_end;
5620 int err = 0;
5621
5622 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5623 if (IS_ERR(em))
5624 return em;
5625 if (em) {
5626 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00005627 * if our em maps to
5628 * - a hole or
5629 * - a pre-alloc extent,
5630 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05005631 */
Liu Bof9e4fb52013-01-07 10:10:12 +00005632 if (em->block_start != EXTENT_MAP_HOLE &&
5633 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05005634 return em;
5635 else
5636 hole_em = em;
5637 }
5638
5639 /* check to see if we've wrapped (len == -1 or similar) */
5640 end = start + len;
5641 if (end < start)
5642 end = (u64)-1;
5643 else
5644 end -= 1;
5645
5646 em = NULL;
5647
5648 /* ok, we didn't find anything, lets look for delalloc */
5649 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5650 end, len, EXTENT_DELALLOC, 1);
5651 found_end = range_start + found;
5652 if (found_end < range_start)
5653 found_end = (u64)-1;
5654
5655 /*
5656 * we didn't find anything useful, return
5657 * the original results from get_extent()
5658 */
5659 if (range_start > end || found_end <= start) {
5660 em = hole_em;
5661 hole_em = NULL;
5662 goto out;
5663 }
5664
5665 /* adjust the range_start to make sure it doesn't
5666 * go backwards from the start they passed in
5667 */
5668 range_start = max(start,range_start);
5669 found = found_end - range_start;
5670
5671 if (found > 0) {
5672 u64 hole_start = start;
5673 u64 hole_len = len;
5674
David Sterba172ddd62011-04-21 00:48:27 +02005675 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005676 if (!em) {
5677 err = -ENOMEM;
5678 goto out;
5679 }
5680 /*
5681 * when btrfs_get_extent can't find anything it
5682 * returns one huge hole
5683 *
5684 * make sure what it found really fits our range, and
5685 * adjust to make sure it is based on the start from
5686 * the caller
5687 */
5688 if (hole_em) {
5689 u64 calc_end = extent_map_end(hole_em);
5690
5691 if (calc_end <= start || (hole_em->start > end)) {
5692 free_extent_map(hole_em);
5693 hole_em = NULL;
5694 } else {
5695 hole_start = max(hole_em->start, start);
5696 hole_len = calc_end - hole_start;
5697 }
5698 }
5699 em->bdev = NULL;
5700 if (hole_em && range_start > hole_start) {
5701 /* our hole starts before our delalloc, so we
5702 * have to return just the parts of the hole
5703 * that go until the delalloc starts
5704 */
5705 em->len = min(hole_len,
5706 range_start - hole_start);
5707 em->start = hole_start;
5708 em->orig_start = hole_start;
5709 /*
5710 * don't adjust block start at all,
5711 * it is fixed at EXTENT_MAP_HOLE
5712 */
5713 em->block_start = hole_em->block_start;
5714 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00005715 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
5716 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05005717 } else {
5718 em->start = range_start;
5719 em->len = found;
5720 em->orig_start = range_start;
5721 em->block_start = EXTENT_MAP_DELALLOC;
5722 em->block_len = found;
5723 }
5724 } else if (hole_em) {
5725 return hole_em;
5726 }
5727out:
5728
5729 free_extent_map(hole_em);
5730 if (err) {
5731 free_extent_map(em);
5732 return ERR_PTR(err);
5733 }
5734 return em;
5735}
5736
Josef Bacik4b46fce2010-05-23 11:00:55 -04005737static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5738 u64 start, u64 len)
5739{
5740 struct btrfs_root *root = BTRFS_I(inode)->root;
5741 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04005742 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005743 struct btrfs_key ins;
5744 u64 alloc_hint;
5745 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005746
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005747 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005748 if (IS_ERR(trans))
5749 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005750
5751 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5752
5753 alloc_hint = get_extent_allocation_hint(inode, start, len);
5754 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005755 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005756 if (ret) {
5757 em = ERR_PTR(ret);
5758 goto out;
5759 }
5760
Josef Bacik70c8a912012-10-11 16:54:30 -04005761 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
5762 ins.offset, ins.offset, 0);
5763 if (IS_ERR(em))
5764 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005765
5766 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5767 ins.offset, ins.offset, 0);
5768 if (ret) {
5769 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5770 em = ERR_PTR(ret);
5771 }
5772out:
5773 btrfs_end_transaction(trans, root);
5774 return em;
5775}
5776
Chris Mason46bfbb52010-05-26 11:04:10 -04005777/*
5778 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5779 * block must be cow'd
5780 */
5781static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5782 struct inode *inode, u64 offset, u64 len)
5783{
5784 struct btrfs_path *path;
5785 int ret;
5786 struct extent_buffer *leaf;
5787 struct btrfs_root *root = BTRFS_I(inode)->root;
5788 struct btrfs_file_extent_item *fi;
5789 struct btrfs_key key;
5790 u64 disk_bytenr;
5791 u64 backref_offset;
5792 u64 extent_end;
5793 u64 num_bytes;
5794 int slot;
5795 int found_type;
5796
5797 path = btrfs_alloc_path();
5798 if (!path)
5799 return -ENOMEM;
5800
Li Zefan33345d012011-04-20 10:31:50 +08005801 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005802 offset, 0);
5803 if (ret < 0)
5804 goto out;
5805
5806 slot = path->slots[0];
5807 if (ret == 1) {
5808 if (slot == 0) {
5809 /* can't find the item, must cow */
5810 ret = 0;
5811 goto out;
5812 }
5813 slot--;
5814 }
5815 ret = 0;
5816 leaf = path->nodes[0];
5817 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005818 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005819 key.type != BTRFS_EXTENT_DATA_KEY) {
5820 /* not our file or wrong item type, must cow */
5821 goto out;
5822 }
5823
5824 if (key.offset > offset) {
5825 /* Wrong offset, must cow */
5826 goto out;
5827 }
5828
5829 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5830 found_type = btrfs_file_extent_type(leaf, fi);
5831 if (found_type != BTRFS_FILE_EXTENT_REG &&
5832 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5833 /* not a regular extent, must cow */
5834 goto out;
5835 }
5836 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5837 backref_offset = btrfs_file_extent_offset(leaf, fi);
5838
5839 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5840 if (extent_end < offset + len) {
5841 /* extent doesn't include our full range, must cow */
5842 goto out;
5843 }
5844
5845 if (btrfs_extent_readonly(root, disk_bytenr))
5846 goto out;
5847
5848 /*
5849 * look for other files referencing this extent, if we
5850 * find any we must cow
5851 */
Li Zefan33345d012011-04-20 10:31:50 +08005852 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005853 key.offset - backref_offset, disk_bytenr))
5854 goto out;
5855
5856 /*
5857 * adjust disk_bytenr and num_bytes to cover just the bytes
5858 * in this extent we are about to write. If there
5859 * are any csums in that range we have to cow in order
5860 * to keep the csums correct
5861 */
5862 disk_bytenr += backref_offset;
5863 disk_bytenr += offset - key.offset;
5864 num_bytes = min(offset + len, extent_end) - offset;
5865 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5866 goto out;
5867 /*
5868 * all of the above have passed, it is safe to overwrite this extent
5869 * without cow
5870 */
5871 ret = 1;
5872out:
5873 btrfs_free_path(path);
5874 return ret;
5875}
5876
Josef Bacikeb838e72012-07-31 16:28:48 -04005877static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5878 struct extent_state **cached_state, int writing)
5879{
5880 struct btrfs_ordered_extent *ordered;
5881 int ret = 0;
5882
5883 while (1) {
5884 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5885 0, cached_state);
5886 /*
5887 * We're concerned with the entire range that we're going to be
5888 * doing DIO to, so we need to make sure theres no ordered
5889 * extents in this range.
5890 */
5891 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5892 lockend - lockstart + 1);
5893
5894 /*
5895 * We need to make sure there are no buffered pages in this
5896 * range either, we could have raced between the invalidate in
5897 * generic_file_direct_write and locking the extent. The
5898 * invalidate needs to happen so that reads after a write do not
5899 * get stale data.
5900 */
5901 if (!ordered && (!writing ||
5902 !test_range_bit(&BTRFS_I(inode)->io_tree,
5903 lockstart, lockend, EXTENT_UPTODATE, 0,
5904 *cached_state)))
5905 break;
5906
5907 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5908 cached_state, GFP_NOFS);
5909
5910 if (ordered) {
5911 btrfs_start_ordered_extent(inode, ordered, 1);
5912 btrfs_put_ordered_extent(ordered);
5913 } else {
5914 /* Screw you mmap */
5915 ret = filemap_write_and_wait_range(inode->i_mapping,
5916 lockstart,
5917 lockend);
5918 if (ret)
5919 break;
5920
5921 /*
5922 * If we found a page that couldn't be invalidated just
5923 * fall back to buffered.
5924 */
5925 ret = invalidate_inode_pages2_range(inode->i_mapping,
5926 lockstart >> PAGE_CACHE_SHIFT,
5927 lockend >> PAGE_CACHE_SHIFT);
5928 if (ret)
5929 break;
5930 }
5931
5932 cond_resched();
5933 }
5934
5935 return ret;
5936}
5937
Josef Bacik69ffb542012-09-11 15:40:07 -04005938static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5939 u64 len, u64 orig_start,
5940 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05005941 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04005942{
5943 struct extent_map_tree *em_tree;
5944 struct extent_map *em;
5945 struct btrfs_root *root = BTRFS_I(inode)->root;
5946 int ret;
5947
5948 em_tree = &BTRFS_I(inode)->extent_tree;
5949 em = alloc_extent_map();
5950 if (!em)
5951 return ERR_PTR(-ENOMEM);
5952
5953 em->start = start;
5954 em->orig_start = orig_start;
5955 em->len = len;
5956 em->block_len = block_len;
5957 em->block_start = block_start;
5958 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05005959 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005960 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04005961 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5962 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05005963 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04005964
5965 do {
5966 btrfs_drop_extent_cache(inode, em->start,
5967 em->start + em->len - 1, 0);
5968 write_lock(&em_tree->lock);
5969 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04005970 if (!ret)
5971 list_move(&em->list,
5972 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04005973 write_unlock(&em_tree->lock);
5974 } while (ret == -EEXIST);
5975
5976 if (ret) {
5977 free_extent_map(em);
5978 return ERR_PTR(ret);
5979 }
5980
5981 return em;
5982}
5983
5984
Josef Bacik4b46fce2010-05-23 11:00:55 -04005985static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5986 struct buffer_head *bh_result, int create)
5987{
5988 struct extent_map *em;
5989 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04005990 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005991 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04005992 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005993 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005994 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04005995 int unlock_bits = EXTENT_LOCKED;
5996 int ret;
5997
Josef Bacikeb838e72012-07-31 16:28:48 -04005998 if (create) {
5999 ret = btrfs_delalloc_reserve_space(inode, len);
6000 if (ret)
6001 return ret;
6002 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04006003 } else {
6004 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006005 }
6006
Josef Bacikc3298612012-08-03 16:49:19 -04006007 lockstart = start;
6008 lockend = start + len - 1;
6009
Josef Bacikeb838e72012-07-31 16:28:48 -04006010 /*
6011 * If this errors out it's because we couldn't invalidate pagecache for
6012 * this range and we need to fallback to buffered.
6013 */
6014 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6015 return -ENOTBLK;
6016
6017 if (create) {
6018 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6019 lockend, EXTENT_DELALLOC, NULL,
6020 &cached_state, GFP_NOFS);
6021 if (ret)
6022 goto unlock_err;
6023 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006024
6025 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006026 if (IS_ERR(em)) {
6027 ret = PTR_ERR(em);
6028 goto unlock_err;
6029 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006030
6031 /*
6032 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6033 * io. INLINE is special, and we could probably kludge it in here, but
6034 * it's still buffered so for safety lets just fall back to the generic
6035 * buffered path.
6036 *
6037 * For COMPRESSED we _have_ to read the entire extent in so we can
6038 * decompress it, so there will be buffering required no matter what we
6039 * do, so go ahead and fallback to buffered.
6040 *
6041 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6042 * to buffered IO. Don't blame me, this is the price we pay for using
6043 * the generic code.
6044 */
6045 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6046 em->block_start == EXTENT_MAP_INLINE) {
6047 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006048 ret = -ENOTBLK;
6049 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006050 }
6051
6052 /* Just a good old fashioned hole, return */
6053 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6054 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6055 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006056 ret = 0;
6057 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006058 }
6059
6060 /*
6061 * We don't allocate a new extent in the following cases
6062 *
6063 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6064 * existing extent.
6065 * 2) The extent is marked as PREALLOC. We're good to go here and can
6066 * just use the extent.
6067 *
6068 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006069 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006070 len = min(len, em->len - (start - em->start));
6071 lockstart = start + len;
6072 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006073 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006074
6075 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6076 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6077 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006078 int type;
6079 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006080 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006081
6082 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6083 type = BTRFS_ORDERED_PREALLOC;
6084 else
6085 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006086 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006087 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006088
6089 /*
6090 * we're not going to log anything, but we do need
6091 * to make sure the current transaction stays open
6092 * while we look for nocow cross refs
6093 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006094 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006095 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006096 goto must_cow;
6097
6098 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006099 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006100 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006101
6102 if (type == BTRFS_ORDERED_PREALLOC) {
6103 free_extent_map(em);
6104 em = create_pinned_em(inode, start, len,
6105 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006106 block_start, len,
6107 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006108 if (IS_ERR(em)) {
6109 btrfs_end_transaction(trans, root);
6110 goto unlock_err;
6111 }
6112 }
6113
Chris Mason46bfbb52010-05-26 11:04:10 -04006114 ret = btrfs_add_ordered_extent_dio(inode, start,
6115 block_start, len, len, type);
6116 btrfs_end_transaction(trans, root);
6117 if (ret) {
6118 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006119 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006120 }
6121 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006122 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006123 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006124 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006125must_cow:
6126 /*
6127 * this will cow the extent, reset the len in case we changed
6128 * it above
6129 */
6130 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006131 free_extent_map(em);
6132 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006133 if (IS_ERR(em)) {
6134 ret = PTR_ERR(em);
6135 goto unlock_err;
6136 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006137 len = min(len, em->len - (start - em->start));
6138unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006139 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6140 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006141 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006142 bh_result->b_bdev = em->bdev;
6143 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006144 if (create) {
6145 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6146 set_buffer_new(bh_result);
6147
6148 /*
6149 * Need to update the i_size under the extent lock so buffered
6150 * readers will get the updated i_size when we unlock.
6151 */
6152 if (start + len > i_size_read(inode))
6153 i_size_write(inode, start + len);
6154 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006155
Josef Bacikeb838e72012-07-31 16:28:48 -04006156 /*
6157 * In the case of write we need to clear and unlock the entire range,
6158 * in the case of read we need to unlock only the end area that we
6159 * aren't using if there is any left over space.
6160 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006161 if (lockstart < lockend) {
6162 if (create && len < lockend - lockstart) {
6163 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006164 lockstart + len - 1,
6165 unlock_bits | EXTENT_DEFRAG, 1, 0,
Liu Bo24c03fa2012-08-22 20:10:38 -06006166 &cached_state, GFP_NOFS);
6167 /*
6168 * Beside unlock, we also need to cleanup reserved space
6169 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6170 */
6171 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6172 lockstart + len, lockend,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006173 unlock_bits | EXTENT_DO_ACCOUNTING |
6174 EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006175 } else {
6176 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6177 lockend, unlock_bits, 1, 0,
6178 &cached_state, GFP_NOFS);
6179 }
6180 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006181 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006182 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006183
Josef Bacik4b46fce2010-05-23 11:00:55 -04006184 free_extent_map(em);
6185
6186 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006187
6188unlock_err:
6189 if (create)
6190 unlock_bits |= EXTENT_DO_ACCOUNTING;
6191
6192 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6193 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6194 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006195}
6196
6197struct btrfs_dio_private {
6198 struct inode *inode;
6199 u64 logical_offset;
6200 u64 disk_bytenr;
6201 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006202 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006203
6204 /* number of bios pending for this dio */
6205 atomic_t pending_bios;
6206
6207 /* IO errors */
6208 int errors;
6209
6210 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006211};
6212
6213static void btrfs_endio_direct_read(struct bio *bio, int err)
6214{
Miao Xiee65e1532010-11-22 03:04:43 +00006215 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006216 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6217 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006218 struct inode *inode = dip->inode;
6219 struct btrfs_root *root = BTRFS_I(inode)->root;
6220 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006221
6222 start = dip->logical_offset;
6223 do {
6224 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6225 struct page *page = bvec->bv_page;
6226 char *kaddr;
6227 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006228 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006229 unsigned long flags;
6230
Josef Bacikc3298612012-08-03 16:49:19 -04006231 if (get_state_private(&BTRFS_I(inode)->io_tree,
6232 start, &private))
6233 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006234 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006235 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006236 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6237 csum, bvec->bv_len);
6238 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006239 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006240 local_irq_restore(flags);
6241
6242 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006243 if (csum != private) {
6244failed:
Li Zefan33345d012011-04-20 10:31:50 +08006245 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006246 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006247 (unsigned long long)btrfs_ino(inode),
6248 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006249 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006250 err = -EIO;
6251 }
6252 }
6253
6254 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006255 bvec++;
6256 } while (bvec <= bvec_end);
6257
6258 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006259 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006260 bio->bi_private = dip->private;
6261
Josef Bacik4b46fce2010-05-23 11:00:55 -04006262 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006263
6264 /* If we had a csum failure make sure to clear the uptodate flag */
6265 if (err)
6266 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006267 dio_end_io(bio, err);
6268}
6269
6270static void btrfs_endio_direct_write(struct bio *bio, int err)
6271{
6272 struct btrfs_dio_private *dip = bio->bi_private;
6273 struct inode *inode = dip->inode;
6274 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006275 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006276 u64 ordered_offset = dip->logical_offset;
6277 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006278 int ret;
6279
6280 if (err)
6281 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006282again:
6283 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6284 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006285 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006286 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006287 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006288
Josef Bacik5fd02042012-05-02 14:00:54 -04006289 ordered->work.func = finish_ordered_fn;
6290 ordered->work.flags = 0;
6291 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6292 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006293out_test:
6294 /*
6295 * our bio might span multiple ordered extents. If we haven't
6296 * completed the accounting for the whole dio, go back and try again
6297 */
6298 if (ordered_offset < dip->logical_offset + dip->bytes) {
6299 ordered_bytes = dip->logical_offset + dip->bytes -
6300 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006301 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006302 goto again;
6303 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006304out_done:
6305 bio->bi_private = dip->private;
6306
Josef Bacik4b46fce2010-05-23 11:00:55 -04006307 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006308
6309 /* If we had an error make sure to clear the uptodate flag */
6310 if (err)
6311 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006312 dio_end_io(bio, err);
6313}
6314
Chris Masoneaf25d92010-05-25 09:48:28 -04006315static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6316 struct bio *bio, int mirror_num,
6317 unsigned long bio_flags, u64 offset)
6318{
6319 int ret;
6320 struct btrfs_root *root = BTRFS_I(inode)->root;
6321 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006322 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006323 return 0;
6324}
6325
Miao Xiee65e1532010-11-22 03:04:43 +00006326static void btrfs_end_dio_bio(struct bio *bio, int err)
6327{
6328 struct btrfs_dio_private *dip = bio->bi_private;
6329
6330 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006331 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006332 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006333 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006334 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006335 dip->errors = 1;
6336
6337 /*
6338 * before atomic variable goto zero, we must make sure
6339 * dip->errors is perceived to be set.
6340 */
6341 smp_mb__before_atomic_dec();
6342 }
6343
6344 /* if there are more bios still pending for this dio, just exit */
6345 if (!atomic_dec_and_test(&dip->pending_bios))
6346 goto out;
6347
6348 if (dip->errors)
6349 bio_io_error(dip->orig_bio);
6350 else {
6351 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6352 bio_endio(dip->orig_bio, 0);
6353 }
6354out:
6355 bio_put(bio);
6356}
6357
6358static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6359 u64 first_sector, gfp_t gfp_flags)
6360{
6361 int nr_vecs = bio_get_nr_vecs(bdev);
6362 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6363}
6364
6365static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6366 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006367 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006368{
6369 int write = rw & REQ_WRITE;
6370 struct btrfs_root *root = BTRFS_I(inode)->root;
6371 int ret;
6372
Josef Bacikb812ce22012-11-16 13:56:32 -05006373 if (async_submit)
6374 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6375
Miao Xiee65e1532010-11-22 03:04:43 +00006376 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006377
6378 if (!write) {
6379 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6380 if (ret)
6381 goto err;
6382 }
Miao Xiee65e1532010-11-22 03:04:43 +00006383
Josef Bacik1ae39932011-04-06 14:41:34 -04006384 if (skip_sum)
6385 goto map;
6386
6387 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006388 ret = btrfs_wq_submit_bio(root->fs_info,
6389 inode, rw, bio, 0, 0,
6390 file_offset,
6391 __btrfs_submit_bio_start_direct_io,
6392 __btrfs_submit_bio_done);
6393 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006394 } else if (write) {
6395 /*
6396 * If we aren't doing async submit, calculate the csum of the
6397 * bio now.
6398 */
6399 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6400 if (ret)
6401 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006402 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006403 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006404 if (ret)
6405 goto err;
6406 }
Miao Xiee65e1532010-11-22 03:04:43 +00006407
Josef Bacik1ae39932011-04-06 14:41:34 -04006408map:
6409 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006410err:
6411 bio_put(bio);
6412 return ret;
6413}
6414
6415static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6416 int skip_sum)
6417{
6418 struct inode *inode = dip->inode;
6419 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006420 struct bio *bio;
6421 struct bio *orig_bio = dip->orig_bio;
6422 struct bio_vec *bvec = orig_bio->bi_io_vec;
6423 u64 start_sector = orig_bio->bi_sector;
6424 u64 file_offset = dip->logical_offset;
6425 u64 submit_len = 0;
6426 u64 map_length;
6427 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006428 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006429 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006430
Miao Xiee65e1532010-11-22 03:04:43 +00006431 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006432 ret = btrfs_map_block(root->fs_info, READ, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006433 &map_length, NULL, 0);
6434 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006435 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006436 return -EIO;
6437 }
6438
Josef Bacik02f57c72011-04-06 14:25:44 -04006439 if (map_length >= orig_bio->bi_size) {
6440 bio = orig_bio;
6441 goto submit;
6442 }
6443
Josef Bacik1ae39932011-04-06 14:41:34 -04006444 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006445 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6446 if (!bio)
6447 return -ENOMEM;
6448 bio->bi_private = dip;
6449 bio->bi_end_io = btrfs_end_dio_bio;
6450 atomic_inc(&dip->pending_bios);
6451
Miao Xiee65e1532010-11-22 03:04:43 +00006452 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6453 if (unlikely(map_length < submit_len + bvec->bv_len ||
6454 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6455 bvec->bv_offset) < bvec->bv_len)) {
6456 /*
6457 * inc the count before we submit the bio so
6458 * we know the end IO handler won't happen before
6459 * we inc the count. Otherwise, the dip might get freed
6460 * before we're done setting it up
6461 */
6462 atomic_inc(&dip->pending_bios);
6463 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6464 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006465 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006466 if (ret) {
6467 bio_put(bio);
6468 atomic_dec(&dip->pending_bios);
6469 goto out_err;
6470 }
6471
Miao Xiee65e1532010-11-22 03:04:43 +00006472 start_sector += submit_len >> 9;
6473 file_offset += submit_len;
6474
6475 submit_len = 0;
6476 nr_pages = 0;
6477
6478 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6479 start_sector, GFP_NOFS);
6480 if (!bio)
6481 goto out_err;
6482 bio->bi_private = dip;
6483 bio->bi_end_io = btrfs_end_dio_bio;
6484
6485 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006486 ret = btrfs_map_block(root->fs_info, READ,
6487 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006488 &map_length, NULL, 0);
6489 if (ret) {
6490 bio_put(bio);
6491 goto out_err;
6492 }
6493 } else {
6494 submit_len += bvec->bv_len;
6495 nr_pages ++;
6496 bvec++;
6497 }
6498 }
6499
Josef Bacik02f57c72011-04-06 14:25:44 -04006500submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006501 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006502 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006503 if (!ret)
6504 return 0;
6505
6506 bio_put(bio);
6507out_err:
6508 dip->errors = 1;
6509 /*
6510 * before atomic variable goto zero, we must
6511 * make sure dip->errors is perceived to be set.
6512 */
6513 smp_mb__before_atomic_dec();
6514 if (atomic_dec_and_test(&dip->pending_bios))
6515 bio_io_error(dip->orig_bio);
6516
6517 /* bio_end_io() will handle error, so we needn't return it */
6518 return 0;
6519}
6520
Josef Bacik4b46fce2010-05-23 11:00:55 -04006521static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6522 loff_t file_offset)
6523{
6524 struct btrfs_root *root = BTRFS_I(inode)->root;
6525 struct btrfs_dio_private *dip;
6526 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006527 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006528 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006529 int ret = 0;
6530
6531 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6532
6533 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6534 if (!dip) {
6535 ret = -ENOMEM;
6536 goto free_ordered;
6537 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006538
6539 dip->private = bio->bi_private;
6540 dip->inode = inode;
6541 dip->logical_offset = file_offset;
6542
Josef Bacik4b46fce2010-05-23 11:00:55 -04006543 dip->bytes = 0;
6544 do {
6545 dip->bytes += bvec->bv_len;
6546 bvec++;
6547 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6548
Chris Mason46bfbb52010-05-26 11:04:10 -04006549 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006550 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006551 dip->errors = 0;
6552 dip->orig_bio = bio;
6553 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006554
6555 if (write)
6556 bio->bi_end_io = btrfs_endio_direct_write;
6557 else
6558 bio->bi_end_io = btrfs_endio_direct_read;
6559
Miao Xiee65e1532010-11-22 03:04:43 +00006560 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6561 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006562 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006563free_ordered:
6564 /*
6565 * If this is a write, we need to clean up the reserved space and kill
6566 * the ordered extent.
6567 */
6568 if (write) {
6569 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006570 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006571 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6572 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6573 btrfs_free_reserved_extent(root, ordered->start,
6574 ordered->disk_len);
6575 btrfs_put_ordered_extent(ordered);
6576 btrfs_put_ordered_extent(ordered);
6577 }
6578 bio_endio(bio, ret);
6579}
6580
Chris Mason5a5f79b2010-05-26 21:33:37 -04006581static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6582 const struct iovec *iov, loff_t offset,
6583 unsigned long nr_segs)
6584{
6585 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006586 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006587 size_t size;
6588 unsigned long addr;
6589 unsigned blocksize_mask = root->sectorsize - 1;
6590 ssize_t retval = -EINVAL;
6591 loff_t end = offset;
6592
6593 if (offset & blocksize_mask)
6594 goto out;
6595
6596 /* Check the memory alignment. Blocks cannot straddle pages */
6597 for (seg = 0; seg < nr_segs; seg++) {
6598 addr = (unsigned long)iov[seg].iov_base;
6599 size = iov[seg].iov_len;
6600 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006601 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006602 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006603
6604 /* If this is a write we don't need to check anymore */
6605 if (rw & WRITE)
6606 continue;
6607
6608 /*
6609 * Check to make sure we don't have duplicate iov_base's in this
6610 * iovec, if so return EINVAL, otherwise we'll get csum errors
6611 * when reading back.
6612 */
6613 for (i = seg + 1; i < nr_segs; i++) {
6614 if (iov[seg].iov_base == iov[i].iov_base)
6615 goto out;
6616 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006617 }
6618 retval = 0;
6619out:
6620 return retval;
6621}
Josef Bacikeb838e72012-07-31 16:28:48 -04006622
Chris Mason16432982008-04-10 10:23:21 -04006623static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6624 const struct iovec *iov, loff_t offset,
6625 unsigned long nr_segs)
6626{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006627 struct file *file = iocb->ki_filp;
6628 struct inode *inode = file->f_mapping->host;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006629
Chris Mason5a5f79b2010-05-26 21:33:37 -04006630 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006631 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006632 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006633
Josef Bacikeb838e72012-07-31 16:28:48 -04006634 return __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006635 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6636 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6637 btrfs_submit_direct, 0);
Chris Mason16432982008-04-10 10:23:21 -04006638}
6639
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006640#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6641
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006642static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6643 __u64 start, __u64 len)
6644{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006645 int ret;
6646
6647 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6648 if (ret)
6649 return ret;
6650
Chris Masonec29ed52011-02-23 16:23:20 -05006651 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006652}
6653
Chris Mason9ebefb182007-06-15 13:50:00 -04006654int btrfs_readpage(struct file *file, struct page *page)
6655{
Chris Masond1310b22008-01-24 16:13:08 -05006656 struct extent_io_tree *tree;
6657 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006658 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006659}
Chris Mason1832a6d2007-12-21 16:27:21 -05006660
Chris Mason39279cc2007-06-12 06:35:45 -04006661static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6662{
Chris Masond1310b22008-01-24 16:13:08 -05006663 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006664
6665
6666 if (current->flags & PF_MEMALLOC) {
6667 redirty_page_for_writepage(wbc, page);
6668 unlock_page(page);
6669 return 0;
6670 }
Chris Masond1310b22008-01-24 16:13:08 -05006671 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006672 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6673}
Chris Mason39279cc2007-06-12 06:35:45 -04006674
Chris Masonf4219502008-07-22 11:18:09 -04006675int btrfs_writepages(struct address_space *mapping,
6676 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04006677{
Chris Masond1310b22008-01-24 16:13:08 -05006678 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006679
Chris Masond1310b22008-01-24 16:13:08 -05006680 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04006681 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6682}
6683
Chris Mason3ab2fb52007-11-08 10:59:22 -05006684static int
6685btrfs_readpages(struct file *file, struct address_space *mapping,
6686 struct list_head *pages, unsigned nr_pages)
6687{
Chris Masond1310b22008-01-24 16:13:08 -05006688 struct extent_io_tree *tree;
6689 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006690 return extent_readpages(tree, mapping, pages, nr_pages,
6691 btrfs_get_extent);
6692}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006693static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006694{
Chris Masond1310b22008-01-24 16:13:08 -05006695 struct extent_io_tree *tree;
6696 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006697 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006698
Chris Masond1310b22008-01-24 16:13:08 -05006699 tree = &BTRFS_I(page->mapping->host)->io_tree;
6700 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006701 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006702 if (ret == 1) {
6703 ClearPagePrivate(page);
6704 set_page_private(page, 0);
6705 page_cache_release(page);
6706 }
6707 return ret;
6708}
Chris Mason39279cc2007-06-12 06:35:45 -04006709
Chris Masone6dcd2d2008-07-17 12:53:50 -04006710static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6711{
Chris Mason98509cf2008-09-11 15:51:43 -04006712 if (PageWriteback(page) || PageDirty(page))
6713 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006714 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006715}
6716
Chris Masona52d9a82007-08-27 16:49:44 -04006717static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6718{
Josef Bacik5fd02042012-05-02 14:00:54 -04006719 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006720 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006721 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006722 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006723 u64 page_start = page_offset(page);
6724 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006725
Chris Mason8b62b722009-09-02 16:53:46 -04006726 /*
6727 * we have the page locked, so new writeback can't start,
6728 * and the dirty bit won't be cleared while we are here.
6729 *
6730 * Wait for IO on this page so that we can safely clear
6731 * the PagePrivate2 bit and do ordered accounting
6732 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006733 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006734
Josef Bacik5fd02042012-05-02 14:00:54 -04006735 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006736 if (offset) {
6737 btrfs_releasepage(page, GFP_NOFS);
6738 return;
6739 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006740 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Josef Bacik5fd02042012-05-02 14:00:54 -04006741 ordered = btrfs_lookup_ordered_extent(inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006742 page_offset(page));
6743 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006744 /*
6745 * IO on this page will never be started, so we need
6746 * to account for any ordered extents now
6747 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006748 clear_extent_bit(tree, page_start, page_end,
6749 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006750 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6751 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006752 /*
6753 * whoever cleared the private bit is responsible
6754 * for the finish_ordered_io
6755 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006756 if (TestClearPagePrivate2(page) &&
6757 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6758 PAGE_CACHE_SIZE, 1)) {
6759 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006760 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006761 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006762 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006763 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006764 }
6765 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006766 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006767 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6768 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006769 __btrfs_releasepage(page, GFP_NOFS);
6770
Chris Mason4a096752008-07-21 10:29:44 -04006771 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04006772 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04006773 ClearPagePrivate(page);
6774 set_page_private(page, 0);
6775 page_cache_release(page);
6776 }
Chris Mason39279cc2007-06-12 06:35:45 -04006777}
6778
Chris Mason9ebefb182007-06-15 13:50:00 -04006779/*
6780 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6781 * called from a page fault handler when a page is first dirtied. Hence we must
6782 * be careful to check for EOF conditions here. We set the page up correctly
6783 * for a written page which means we get ENOSPC checking when writing into
6784 * holes and correct delalloc and unwritten extent mapping on filesystems that
6785 * support these features.
6786 *
6787 * We are not allowed to take the i_mutex here so we have to play games to
6788 * protect against truncate races as the page could now be beyond EOF. Because
6789 * vmtruncate() writes the inode size before removing pages, once we have the
6790 * page lock we can determine safely if the page is beyond EOF. If it is not
6791 * beyond EOF, then the page is guaranteed safe against truncation until we
6792 * unlock the page.
6793 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006794int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006795{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006796 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006797 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006798 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006799 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6800 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006801 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006802 char *kaddr;
6803 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006804 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006805 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006806 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006807 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006808 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006809
Jan Karab2b5ef52012-06-12 16:20:45 +02006810 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006811 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006812 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006813 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006814 reserved = 1;
6815 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006816 if (ret) {
6817 if (ret == -ENOMEM)
6818 ret = VM_FAULT_OOM;
6819 else /* -ENOSPC, -EIO, etc */
6820 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006821 if (reserved)
6822 goto out;
6823 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006824 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006825
Nick Piggin56a76f82009-03-31 15:23:23 -07006826 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006827again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006828 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006829 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006830 page_start = page_offset(page);
6831 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006832
Chris Mason9ebefb182007-06-15 13:50:00 -04006833 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006834 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006835 /* page got truncated out from underneath us */
6836 goto out_unlock;
6837 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006838 wait_on_page_writeback(page);
6839
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006840 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006841 set_page_extent_mapped(page);
6842
Chris Masoneb84ae02008-07-17 13:53:27 -04006843 /*
6844 * we can't set the delalloc bits if there are pending ordered
6845 * extents. Drop our locks and wait for them to finish
6846 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006847 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6848 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006849 unlock_extent_cached(io_tree, page_start, page_end,
6850 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006851 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006852 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006853 btrfs_put_ordered_extent(ordered);
6854 goto again;
6855 }
6856
Josef Bacikfbf19082009-10-01 17:10:23 -04006857 /*
6858 * XXX - page_mkwrite gets called every time the page is dirtied, even
6859 * if it was already dirty, so for space accounting reasons we need to
6860 * clear any delalloc bits for the range we are fixing to save. There
6861 * is probably a better way to do this, but for now keep consistent with
6862 * prepare_pages in the normal write path.
6863 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006864 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006865 EXTENT_DIRTY | EXTENT_DELALLOC |
6866 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006867 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006868
Josef Bacik2ac55d42010-02-03 19:33:23 +00006869 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6870 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006871 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006872 unlock_extent_cached(io_tree, page_start, page_end,
6873 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006874 ret = VM_FAULT_SIGBUS;
6875 goto out_unlock;
6876 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006877 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006878
6879 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006880 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006881 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006882 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006883 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006884
Chris Masone6dcd2d2008-07-17 12:53:50 -04006885 if (zero_start != PAGE_CACHE_SIZE) {
6886 kaddr = kmap(page);
6887 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6888 flush_dcache_page(page);
6889 kunmap(page);
6890 }
Chris Mason247e7432008-07-17 12:53:51 -04006891 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006892 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006893 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006894
Chris Mason257c62e2009-10-13 13:21:08 -04006895 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6896 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006897 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006898
Josef Bacik2ac55d42010-02-03 19:33:23 +00006899 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006900
6901out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006902 if (!ret) {
6903 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006904 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006905 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006906 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006907out:
Josef Bacikec39e182012-01-12 19:10:12 -05006908 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006909out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006910 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006911 return ret;
6912}
6913
Josef Bacika41ad392011-01-31 15:30:16 -05006914static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006915{
6916 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006917 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006918 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006919 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006920 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04006921 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006922 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006923
Josef Bacik2aaa6652012-08-29 14:27:18 -04006924 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04006925 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006926 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006927
Chris Mason4a096752008-07-21 10:29:44 -04006928 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006929 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006930
Josef Bacikfcb80c22011-05-03 10:40:22 -04006931 /*
6932 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6933 * 3 things going on here
6934 *
6935 * 1) We need to reserve space for our orphan item and the space to
6936 * delete our orphan item. Lord knows we don't want to have a dangling
6937 * orphan item because we didn't reserve space to remove it.
6938 *
6939 * 2) We need to reserve space to update our inode.
6940 *
6941 * 3) We need to have something to cache all the space that is going to
6942 * be free'd up by the truncate operation, but also have some slack
6943 * space reserved in case it uses space during the truncate (thank you
6944 * very much snapshotting).
6945 *
6946 * And we need these to all be seperate. The fact is we can use alot of
6947 * space doing the truncate, and we have no earthly idea how much space
6948 * we will use, so we need the truncate reservation to be seperate so it
6949 * doesn't end up using space reserved for updating the inode or
6950 * removing the orphan item. We also need to be able to stop the
6951 * transaction and start a new one, which means we need to be able to
6952 * update the inode several times, and we have no idea of knowing how
6953 * many times that will be, so we can't just reserve 1 item for the
6954 * entirety of the opration, so that has to be done seperately as well.
6955 * Then there is the orphan item, which does indeed need to be held on
6956 * to for the whole operation, and we need nobody to touch this reserved
6957 * space except the orphan code.
6958 *
6959 * So that leaves us with
6960 *
6961 * 1) root->orphan_block_rsv - for the orphan deletion.
6962 * 2) rsv - for the truncate reservation, which we will steal from the
6963 * transaction reservation.
6964 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6965 * updating the inode.
6966 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06006967 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006968 if (!rsv)
6969 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006970 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006971 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006972
Josef Bacik907cbce2011-08-08 13:46:15 -04006973 /*
Josef Bacik07127182011-08-19 10:29:59 -04006974 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006975 * 1 for updating the inode.
6976 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05006977 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006978 if (IS_ERR(trans)) {
6979 err = PTR_ERR(trans);
6980 goto out;
6981 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006982
Josef Bacik907cbce2011-08-08 13:46:15 -04006983 /* Migrate the slack space for the truncate to our reserve */
6984 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6985 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006986 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006987
Chris Mason5a3f23d2009-03-31 13:27:11 -04006988 /*
6989 * setattr is responsible for setting the ordered_data_close flag,
6990 * but that is only tested during the last file release. That
6991 * could happen well after the next commit, leaving a great big
6992 * window where new writes may get lost if someone chooses to write
6993 * to this file after truncating to zero
6994 *
6995 * The inode doesn't have any dirty data here, and so if we commit
6996 * this is a noop. If someone immediately starts writing to the inode
6997 * it is very likely we'll catch some of their writes in this
6998 * transaction, and the commit will find this file on the ordered
6999 * data list with good things to send down.
7000 *
7001 * This is a best effort solution, there is still a window where
7002 * using truncate to replace the contents of the file will
7003 * end up with a zero length file after a crash.
7004 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007005 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7006 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007007 btrfs_add_ordered_operation(trans, root, inode);
7008
Josef Bacik5dc562c2012-08-17 13:14:17 -04007009 /*
7010 * So if we truncate and then write and fsync we normally would just
7011 * write the extents that changed, which is a problem if we need to
7012 * first truncate that entire inode. So set this flag so we write out
7013 * all of the extents in the inode to the sync log so we're completely
7014 * safe.
7015 */
7016 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007017 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007018
Yan, Zheng80825102009-11-12 09:35:36 +00007019 while (1) {
7020 ret = btrfs_truncate_inode_items(trans, root, inode,
7021 inode->i_size,
7022 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007023 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007024 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007025 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007026 }
Chris Mason39279cc2007-06-12 06:35:45 -04007027
Josef Bacikfcb80c22011-05-03 10:40:22 -04007028 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007029 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007030 if (ret) {
7031 err = ret;
7032 break;
7033 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007034
Yan, Zheng80825102009-11-12 09:35:36 +00007035 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007036 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007037
7038 trans = btrfs_start_transaction(root, 2);
7039 if (IS_ERR(trans)) {
7040 ret = err = PTR_ERR(trans);
7041 trans = NULL;
7042 break;
7043 }
7044
7045 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7046 rsv, min_size);
7047 BUG_ON(ret); /* shouldn't happen */
7048 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007049 }
7050
7051 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007052 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007053 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007054 if (ret)
7055 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007056 }
7057
Chris Mason917c16b2011-11-08 14:49:59 -05007058 if (trans) {
7059 trans->block_rsv = &root->fs_info->trans_block_rsv;
7060 ret = btrfs_update_inode(trans, root, inode);
7061 if (ret && !err)
7062 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007063
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007064 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007065 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007066 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007067
7068out:
7069 btrfs_free_block_rsv(root, rsv);
7070
Josef Bacik3893e332011-01-31 16:03:11 -05007071 if (ret && !err)
7072 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007073
Josef Bacik3893e332011-01-31 16:03:11 -05007074 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007075}
7076
Sven Wegener3b963622008-06-09 21:57:42 -04007077/*
Chris Masond352ac62008-09-29 15:18:18 -04007078 * create a new subvolume directory/inode (helper for the ioctl).
7079 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007080int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007081 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007082{
Chris Mason39279cc2007-06-12 06:35:45 -04007083 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007084 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007085 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007086
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007087 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7088 new_dirid, new_dirid,
7089 S_IFDIR | (~current_umask() & S_IRWXUGO),
7090 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007091 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007092 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007093 inode->i_op = &btrfs_dir_inode_operations;
7094 inode->i_fop = &btrfs_dir_file_operations;
7095
Miklos Szeredibfe86842011-10-28 14:13:29 +02007096 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007097 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007098
Yan, Zheng76dda932009-09-21 16:00:26 -04007099 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007100
Yan, Zheng76dda932009-09-21 16:00:26 -04007101 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007102 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007103}
7104
Chris Mason39279cc2007-06-12 06:35:45 -04007105struct inode *btrfs_alloc_inode(struct super_block *sb)
7106{
7107 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007108 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007109
7110 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7111 if (!ei)
7112 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007113
7114 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007115 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007116 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007117 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007118 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007119 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007120 ei->disk_i_size = 0;
7121 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007122 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007123 ei->index_cnt = (u64)-1;
7124 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007125 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007126
Josef Bacik9e0baf62011-07-15 15:16:44 +00007127 spin_lock_init(&ei->lock);
7128 ei->outstanding_extents = 0;
7129 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007130
Josef Bacik72ac3c02012-05-23 14:13:11 -04007131 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007132 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007133
Miao Xie16cdcec2011-04-22 18:12:22 +08007134 ei->delayed_node = NULL;
7135
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007136 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007137 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007138 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7139 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007140 ei->io_tree.track_uptodate = 1;
7141 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007142 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007143 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007144 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007145 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007146 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007147 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007148 RB_CLEAR_NODE(&ei->rb_node);
7149
7150 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007151}
7152
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007153static void btrfs_i_callback(struct rcu_head *head)
7154{
7155 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007156 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7157}
7158
Chris Mason39279cc2007-06-12 06:35:45 -04007159void btrfs_destroy_inode(struct inode *inode)
7160{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007161 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007162 struct btrfs_root *root = BTRFS_I(inode)->root;
7163
Al Virob3d9b7a2012-06-09 13:51:19 -04007164 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007165 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007166 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7167 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007168 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7169 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007170
Chris Mason5a3f23d2009-03-31 13:27:11 -04007171 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007172 * This can happen where we create an inode, but somebody else also
7173 * created the same inode and we need to destroy the one we already
7174 * created.
7175 */
7176 if (!root)
7177 goto free;
7178
7179 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007180 * Make sure we're properly removed from the ordered operation
7181 * lists.
7182 */
7183 smp_mb();
7184 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7185 spin_lock(&root->fs_info->ordered_extent_lock);
7186 list_del_init(&BTRFS_I(inode)->ordered_operations);
7187 spin_unlock(&root->fs_info->ordered_extent_lock);
7188 }
7189
Josef Bacik8a35d952012-05-23 14:26:42 -04007190 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7191 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007192 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7193 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007194 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007195 }
Josef Bacik7b128762008-07-24 12:17:14 -04007196
Chris Masond3977122009-01-05 21:25:51 -05007197 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007198 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7199 if (!ordered)
7200 break;
7201 else {
Chris Masond3977122009-01-05 21:25:51 -05007202 printk(KERN_ERR "btrfs found ordered "
7203 "extent %llu %llu on inode cleanup\n",
7204 (unsigned long long)ordered->file_offset,
7205 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007206 btrfs_remove_ordered_extent(inode, ordered);
7207 btrfs_put_ordered_extent(ordered);
7208 btrfs_put_ordered_extent(ordered);
7209 }
7210 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007211 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007212 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007213free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007214 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007215 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007216}
7217
Al Viro45321ac2010-06-07 13:43:19 -04007218int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007219{
7220 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007221
Josef Bacik0af3d002010-06-21 14:48:16 -04007222 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007223 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007224 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007225 else
Al Viro45321ac2010-06-07 13:43:19 -04007226 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007227}
7228
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007229static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007230{
7231 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7232
7233 inode_init_once(&ei->vfs_inode);
7234}
7235
7236void btrfs_destroy_cachep(void)
7237{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007238 /*
7239 * Make sure all delayed rcu free inodes are flushed before we
7240 * destroy cache.
7241 */
7242 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007243 if (btrfs_inode_cachep)
7244 kmem_cache_destroy(btrfs_inode_cachep);
7245 if (btrfs_trans_handle_cachep)
7246 kmem_cache_destroy(btrfs_trans_handle_cachep);
7247 if (btrfs_transaction_cachep)
7248 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007249 if (btrfs_path_cachep)
7250 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007251 if (btrfs_free_space_cachep)
7252 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007253 if (btrfs_delalloc_work_cachep)
7254 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007255}
7256
7257int btrfs_init_cachep(void)
7258{
David Sterba837e1972012-09-07 03:00:48 -06007259 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007260 sizeof(struct btrfs_inode), 0,
7261 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007262 if (!btrfs_inode_cachep)
7263 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007264
David Sterba837e1972012-09-07 03:00:48 -06007265 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007266 sizeof(struct btrfs_trans_handle), 0,
7267 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007268 if (!btrfs_trans_handle_cachep)
7269 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007270
David Sterba837e1972012-09-07 03:00:48 -06007271 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007272 sizeof(struct btrfs_transaction), 0,
7273 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007274 if (!btrfs_transaction_cachep)
7275 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007276
David Sterba837e1972012-09-07 03:00:48 -06007277 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007278 sizeof(struct btrfs_path), 0,
7279 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007280 if (!btrfs_path_cachep)
7281 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007282
David Sterba837e1972012-09-07 03:00:48 -06007283 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007284 sizeof(struct btrfs_free_space), 0,
7285 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7286 if (!btrfs_free_space_cachep)
7287 goto fail;
7288
Miao Xie8ccf6f192012-10-25 09:28:04 +00007289 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7290 sizeof(struct btrfs_delalloc_work), 0,
7291 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7292 NULL);
7293 if (!btrfs_delalloc_work_cachep)
7294 goto fail;
7295
Chris Mason39279cc2007-06-12 06:35:45 -04007296 return 0;
7297fail:
7298 btrfs_destroy_cachep();
7299 return -ENOMEM;
7300}
7301
7302static int btrfs_getattr(struct vfsmount *mnt,
7303 struct dentry *dentry, struct kstat *stat)
7304{
7305 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007306 u32 blocksize = inode->i_sb->s_blocksize;
7307
Chris Mason39279cc2007-06-12 06:35:45 -04007308 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007309 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007310 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007311 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7312 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007313 return 0;
7314}
7315
Liu Bo75e7cb72011-03-22 10:12:20 +00007316/*
7317 * If a file is moved, it will inherit the cow and compression flags of the new
7318 * directory.
7319 */
7320static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7321{
7322 struct btrfs_inode *b_dir = BTRFS_I(dir);
7323 struct btrfs_inode *b_inode = BTRFS_I(inode);
7324
7325 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7326 b_inode->flags |= BTRFS_INODE_NODATACOW;
7327 else
7328 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7329
Liu Bobc178232012-06-14 02:23:18 -06007330 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007331 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007332 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7333 } else {
7334 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7335 BTRFS_INODE_NOCOMPRESS);
7336 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007337}
7338
Chris Masond3977122009-01-05 21:25:51 -05007339static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7340 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007341{
7342 struct btrfs_trans_handle *trans;
7343 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007344 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007345 struct inode *new_inode = new_dentry->d_inode;
7346 struct inode *old_inode = old_dentry->d_inode;
7347 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007348 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007349 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007350 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007351 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007352
Li Zefan33345d012011-04-20 10:31:50 +08007353 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007354 return -EPERM;
7355
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007356 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007357 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007358 return -EXDEV;
7359
Li Zefan33345d012011-04-20 10:31:50 +08007360 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7361 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007362 return -ENOTEMPTY;
7363
Chris Mason39279cc2007-06-12 06:35:45 -04007364 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007365 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007366 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007367
7368
7369 /* check for collisions, even if the name isn't there */
7370 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7371 new_dentry->d_name.name,
7372 new_dentry->d_name.len);
7373
7374 if (ret) {
7375 if (ret == -EEXIST) {
7376 /* we shouldn't get
7377 * eexist without a new_inode */
7378 if (!new_inode) {
7379 WARN_ON(1);
7380 return ret;
7381 }
7382 } else {
7383 /* maybe -EOVERFLOW */
7384 return ret;
7385 }
7386 }
7387 ret = 0;
7388
Chris Mason5a3f23d2009-03-31 13:27:11 -04007389 /*
7390 * we're using rename to replace one file with another.
7391 * and the replacement file is large. Start IO on it now so
7392 * we don't add too much work to the end of the transaction
7393 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007394 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007395 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7396 filemap_flush(old_inode->i_mapping);
7397
Yan, Zheng76dda932009-09-21 16:00:26 -04007398 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007399 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007400 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007401 /*
7402 * We want to reserve the absolute worst case amount of items. So if
7403 * both inodes are subvols and we need to unlink them then that would
7404 * require 4 item modifications, but if they are both normal inodes it
7405 * would require 5 item modifications, so we'll assume their normal
7406 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7407 * should cover the worst case number of items we'll modify.
7408 */
7409 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007410 if (IS_ERR(trans)) {
7411 ret = PTR_ERR(trans);
7412 goto out_notrans;
7413 }
Chris Mason5f39d392007-10-15 16:14:19 -04007414
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007415 if (dest != root)
7416 btrfs_record_root_in_trans(trans, dest);
7417
Yan, Zhenga5719522009-09-24 09:17:31 -04007418 ret = btrfs_set_inode_index(new_dir, &index);
7419 if (ret)
7420 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007421
Li Zefan33345d012011-04-20 10:31:50 +08007422 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007423 /* force full log commit if subvolume involved. */
7424 root->fs_info->last_trans_log_full_commit = trans->transid;
7425 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007426 ret = btrfs_insert_inode_ref(trans, dest,
7427 new_dentry->d_name.name,
7428 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007429 old_ino,
7430 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007431 if (ret)
7432 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007433 /*
7434 * this is an ugly little race, but the rename is required
7435 * to make sure that if we crash, the inode is either at the
7436 * old name or the new one. pinning the log transaction lets
7437 * us make sure we don't allow a log commit to come in after
7438 * we unlink the name but before we add the new name back in.
7439 */
7440 btrfs_pin_log_trans(root);
7441 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007442 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007443 * make sure the inode gets flushed if it is replacing
7444 * something.
7445 */
Li Zefan33345d012011-04-20 10:31:50 +08007446 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007447 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007448
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007449 inode_inc_iversion(old_dir);
7450 inode_inc_iversion(new_dir);
7451 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007452 old_dir->i_ctime = old_dir->i_mtime = ctime;
7453 new_dir->i_ctime = new_dir->i_mtime = ctime;
7454 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007455
Chris Mason12fcfd22009-03-24 10:24:20 -04007456 if (old_dentry->d_parent != new_dentry->d_parent)
7457 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7458
Li Zefan33345d012011-04-20 10:31:50 +08007459 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007460 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7461 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7462 old_dentry->d_name.name,
7463 old_dentry->d_name.len);
7464 } else {
Al Viro92986792011-03-04 17:14:37 +00007465 ret = __btrfs_unlink_inode(trans, root, old_dir,
7466 old_dentry->d_inode,
7467 old_dentry->d_name.name,
7468 old_dentry->d_name.len);
7469 if (!ret)
7470 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007471 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007472 if (ret) {
7473 btrfs_abort_transaction(trans, root, ret);
7474 goto out_fail;
7475 }
Chris Mason39279cc2007-06-12 06:35:45 -04007476
7477 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007478 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007479 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007480 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007481 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7482 root_objectid = BTRFS_I(new_inode)->location.objectid;
7483 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7484 root_objectid,
7485 new_dentry->d_name.name,
7486 new_dentry->d_name.len);
7487 BUG_ON(new_inode->i_nlink == 0);
7488 } else {
7489 ret = btrfs_unlink_inode(trans, dest, new_dir,
7490 new_dentry->d_inode,
7491 new_dentry->d_name.name,
7492 new_dentry->d_name.len);
7493 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007494 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007495 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007496 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007497 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007498 if (ret) {
7499 btrfs_abort_transaction(trans, root, ret);
7500 goto out_fail;
7501 }
Chris Mason39279cc2007-06-12 06:35:45 -04007502 }
Josef Bacikaec74772008-07-24 12:12:38 -04007503
Liu Bo75e7cb72011-03-22 10:12:20 +00007504 fixup_inode_flags(new_dir, old_inode);
7505
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007506 ret = btrfs_add_link(trans, new_dir, old_inode,
7507 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007508 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007509 if (ret) {
7510 btrfs_abort_transaction(trans, root, ret);
7511 goto out_fail;
7512 }
Chris Mason39279cc2007-06-12 06:35:45 -04007513
Li Zefan33345d012011-04-20 10:31:50 +08007514 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007515 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007516 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007517 btrfs_end_log_trans(root);
7518 }
Chris Mason39279cc2007-06-12 06:35:45 -04007519out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007520 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007521out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007522 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007523 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007524
Chris Mason39279cc2007-06-12 06:35:45 -04007525 return ret;
7526}
7527
Miao Xie8ccf6f192012-10-25 09:28:04 +00007528static void btrfs_run_delalloc_work(struct btrfs_work *work)
7529{
7530 struct btrfs_delalloc_work *delalloc_work;
7531
7532 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7533 work);
7534 if (delalloc_work->wait)
7535 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7536 else
7537 filemap_flush(delalloc_work->inode->i_mapping);
7538
7539 if (delalloc_work->delay_iput)
7540 btrfs_add_delayed_iput(delalloc_work->inode);
7541 else
7542 iput(delalloc_work->inode);
7543 complete(&delalloc_work->completion);
7544}
7545
7546struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7547 int wait, int delay_iput)
7548{
7549 struct btrfs_delalloc_work *work;
7550
7551 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7552 if (!work)
7553 return NULL;
7554
7555 init_completion(&work->completion);
7556 INIT_LIST_HEAD(&work->list);
7557 work->inode = inode;
7558 work->wait = wait;
7559 work->delay_iput = delay_iput;
7560 work->work.func = btrfs_run_delalloc_work;
7561
7562 return work;
7563}
7564
7565void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7566{
7567 wait_for_completion(&work->completion);
7568 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7569}
7570
Chris Masond352ac62008-09-29 15:18:18 -04007571/*
7572 * some fairly slow code that needs optimization. This walks the list
7573 * of all the inodes with pending delalloc and forces them to disk.
7574 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007575int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007576{
7577 struct list_head *head = &root->fs_info->delalloc_inodes;
7578 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007579 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007580 struct btrfs_delalloc_work *work, *next;
7581 struct list_head works;
7582 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04007583
Yan Zhengc146afa2008-11-12 14:34:12 -05007584 if (root->fs_info->sb->s_flags & MS_RDONLY)
7585 return -EROFS;
7586
Miao Xie8ccf6f192012-10-25 09:28:04 +00007587 INIT_LIST_HEAD(&works);
7588
Chris Mason75eff682008-12-15 15:54:40 -05007589 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007590 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007591 binode = list_entry(head->next, struct btrfs_inode,
7592 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007593 inode = igrab(&binode->vfs_inode);
7594 if (!inode)
7595 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007596 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007597 if (inode) {
Miao Xie8ccf6f192012-10-25 09:28:04 +00007598 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7599 if (!work) {
7600 ret = -ENOMEM;
7601 goto out;
7602 }
7603 list_add_tail(&work->list, &works);
7604 btrfs_queue_worker(&root->fs_info->flush_workers,
7605 &work->work);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007606 }
7607 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007608 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007609 }
Chris Mason75eff682008-12-15 15:54:40 -05007610 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007611
7612 /* the filemap_flush will queue IO into the worker threads, but
7613 * we have to make sure the IO is actually started and that
7614 * ordered extents get created before we return
7615 */
7616 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007617 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007618 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007619 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007620 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7621 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007622 }
7623 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007624out:
7625 list_for_each_entry_safe(work, next, &works, list) {
7626 list_del_init(&work->list);
7627 btrfs_wait_and_free_delalloc_work(work);
7628 }
7629 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04007630}
7631
Chris Mason39279cc2007-06-12 06:35:45 -04007632static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7633 const char *symname)
7634{
7635 struct btrfs_trans_handle *trans;
7636 struct btrfs_root *root = BTRFS_I(dir)->root;
7637 struct btrfs_path *path;
7638 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007639 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007640 int err;
7641 int drop_inode = 0;
7642 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007643 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007644 int name_len;
7645 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007646 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007647 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007648 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04007649
7650 name_len = strlen(symname) + 1;
7651 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7652 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007653
Josef Bacik9ed74f22009-09-11 16:12:44 -04007654 /*
7655 * 2 items for inode item and ref
7656 * 2 items for dir items
7657 * 1 item for xattr if selinux is on
7658 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007659 trans = btrfs_start_transaction(root, 5);
7660 if (IS_ERR(trans))
7661 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007662
Li Zefan581bb052011-04-20 10:06:11 +08007663 err = btrfs_find_free_ino(root, &objectid);
7664 if (err)
7665 goto out_unlock;
7666
Josef Bacikaec74772008-07-24 12:12:38 -04007667 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007668 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007669 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007670 if (IS_ERR(inode)) {
7671 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007672 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007673 }
Chris Mason39279cc2007-06-12 06:35:45 -04007674
Eric Paris2a7dba32011-02-01 11:05:39 -05007675 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007676 if (err) {
7677 drop_inode = 1;
7678 goto out_unlock;
7679 }
7680
Casey Schauflerad19db72011-12-15 10:09:07 -05007681 /*
7682 * If the active LSM wants to access the inode during
7683 * d_instantiate it needs these. Smack checks to see
7684 * if the filesystem supports xattrs by looking at the
7685 * ops vector.
7686 */
7687 inode->i_fop = &btrfs_file_operations;
7688 inode->i_op = &btrfs_file_inode_operations;
7689
Josef Bacika1b075d2010-11-19 20:36:11 +00007690 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007691 if (err)
7692 drop_inode = 1;
7693 else {
7694 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007695 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007696 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007697 }
Chris Mason39279cc2007-06-12 06:35:45 -04007698 if (drop_inode)
7699 goto out_unlock;
7700
7701 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007702 if (!path) {
7703 err = -ENOMEM;
7704 drop_inode = 1;
7705 goto out_unlock;
7706 }
Li Zefan33345d012011-04-20 10:31:50 +08007707 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007708 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007709 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7710 datasize = btrfs_file_extent_calc_inline_size(name_len);
7711 err = btrfs_insert_empty_item(trans, root, path, &key,
7712 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007713 if (err) {
7714 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007715 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007716 goto out_unlock;
7717 }
Chris Mason5f39d392007-10-15 16:14:19 -04007718 leaf = path->nodes[0];
7719 ei = btrfs_item_ptr(leaf, path->slots[0],
7720 struct btrfs_file_extent_item);
7721 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7722 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007723 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007724 btrfs_set_file_extent_encryption(leaf, ei, 0);
7725 btrfs_set_file_extent_compression(leaf, ei, 0);
7726 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7727 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7728
Chris Mason39279cc2007-06-12 06:35:45 -04007729 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007730 write_extent_buffer(leaf, symname, ptr, name_len);
7731 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007732 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007733
Chris Mason39279cc2007-06-12 06:35:45 -04007734 inode->i_op = &btrfs_symlink_inode_operations;
7735 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007736 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007737 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007738 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007739 err = btrfs_update_inode(trans, root, inode);
7740 if (err)
7741 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007742
7743out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007744 if (!err)
7745 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007746 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007747 if (drop_inode) {
7748 inode_dec_link_count(inode);
7749 iput(inode);
7750 }
Liu Bob53d3f52012-11-14 14:34:34 +00007751 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04007752 return err;
7753}
Chris Mason16432982008-04-10 10:23:21 -04007754
Josef Bacik0af3d002010-06-21 14:48:16 -04007755static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7756 u64 start, u64 num_bytes, u64 min_size,
7757 loff_t actual_len, u64 *alloc_hint,
7758 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007759{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007760 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7761 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007762 struct btrfs_root *root = BTRFS_I(inode)->root;
7763 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007764 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007765 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007766 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007767 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007768
Josef Bacik0af3d002010-06-21 14:48:16 -04007769 if (trans)
7770 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007771 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007772 if (own_trans) {
7773 trans = btrfs_start_transaction(root, 3);
7774 if (IS_ERR(trans)) {
7775 ret = PTR_ERR(trans);
7776 break;
7777 }
Yan Zhengd899e052008-10-30 14:25:28 -04007778 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007779
Yan, Zhengefa56462010-05-16 10:49:59 -04007780 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007781 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007782 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007783 if (own_trans)
7784 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007785 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007786 }
7787
Yan Zhengd899e052008-10-30 14:25:28 -04007788 ret = insert_reserved_file_extent(trans, inode,
7789 cur_offset, ins.objectid,
7790 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007791 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007792 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007793 if (ret) {
7794 btrfs_abort_transaction(trans, root, ret);
7795 if (own_trans)
7796 btrfs_end_transaction(trans, root);
7797 break;
7798 }
Chris Masona1ed8352009-09-11 12:27:37 -04007799 btrfs_drop_extent_cache(inode, cur_offset,
7800 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007801
Josef Bacik5dc562c2012-08-17 13:14:17 -04007802 em = alloc_extent_map();
7803 if (!em) {
7804 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7805 &BTRFS_I(inode)->runtime_flags);
7806 goto next;
7807 }
7808
7809 em->start = cur_offset;
7810 em->orig_start = cur_offset;
7811 em->len = ins.offset;
7812 em->block_start = ins.objectid;
7813 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05007814 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007815 em->bdev = root->fs_info->fs_devices->latest_bdev;
7816 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7817 em->generation = trans->transid;
7818
7819 while (1) {
7820 write_lock(&em_tree->lock);
7821 ret = add_extent_mapping(em_tree, em);
7822 if (!ret)
7823 list_move(&em->list,
7824 &em_tree->modified_extents);
7825 write_unlock(&em_tree->lock);
7826 if (ret != -EEXIST)
7827 break;
7828 btrfs_drop_extent_cache(inode, cur_offset,
7829 cur_offset + ins.offset - 1,
7830 0);
7831 }
7832 free_extent_map(em);
7833next:
Yan Zhengd899e052008-10-30 14:25:28 -04007834 num_bytes -= ins.offset;
7835 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007836 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007837
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007838 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007839 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007840 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007841 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007842 (actual_len > inode->i_size) &&
7843 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007844 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007845 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007846 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007847 i_size = cur_offset;
7848 i_size_write(inode, i_size);
7849 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007850 }
7851
Yan Zhengd899e052008-10-30 14:25:28 -04007852 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007853
7854 if (ret) {
7855 btrfs_abort_transaction(trans, root, ret);
7856 if (own_trans)
7857 btrfs_end_transaction(trans, root);
7858 break;
7859 }
Yan Zhengd899e052008-10-30 14:25:28 -04007860
Josef Bacik0af3d002010-06-21 14:48:16 -04007861 if (own_trans)
7862 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007863 }
Yan Zhengd899e052008-10-30 14:25:28 -04007864 return ret;
7865}
7866
Josef Bacik0af3d002010-06-21 14:48:16 -04007867int btrfs_prealloc_file_range(struct inode *inode, int mode,
7868 u64 start, u64 num_bytes, u64 min_size,
7869 loff_t actual_len, u64 *alloc_hint)
7870{
7871 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7872 min_size, actual_len, alloc_hint,
7873 NULL);
7874}
7875
7876int btrfs_prealloc_file_range_trans(struct inode *inode,
7877 struct btrfs_trans_handle *trans, int mode,
7878 u64 start, u64 num_bytes, u64 min_size,
7879 loff_t actual_len, u64 *alloc_hint)
7880{
7881 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7882 min_size, actual_len, alloc_hint, trans);
7883}
7884
Chris Masone6dcd2d2008-07-17 12:53:50 -04007885static int btrfs_set_page_dirty(struct page *page)
7886{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007887 return __set_page_dirty_nobuffers(page);
7888}
7889
Al Viro10556cb22011-06-20 19:28:19 -04007890static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007891{
Li Zefanb83cc962010-12-20 16:04:08 +08007892 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007893 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007894
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007895 if (mask & MAY_WRITE &&
7896 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7897 if (btrfs_root_readonly(root))
7898 return -EROFS;
7899 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7900 return -EACCES;
7901 }
Al Viro2830ba72011-06-20 19:16:29 -04007902 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007903}
Chris Mason39279cc2007-06-12 06:35:45 -04007904
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007905static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007906 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007907 .lookup = btrfs_lookup,
7908 .create = btrfs_create,
7909 .unlink = btrfs_unlink,
7910 .link = btrfs_link,
7911 .mkdir = btrfs_mkdir,
7912 .rmdir = btrfs_rmdir,
7913 .rename = btrfs_rename,
7914 .symlink = btrfs_symlink,
7915 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007916 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007917 .setxattr = btrfs_setxattr,
7918 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007919 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007920 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007921 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007922 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007923};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007924static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007925 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007926 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007927 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007928};
Yan, Zheng76dda932009-09-21 16:00:26 -04007929
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007930static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007931 .llseek = generic_file_llseek,
7932 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007933 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007934 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007935#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007936 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007937#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007938 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007939 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007940};
7941
Chris Masond1310b22008-01-24 16:13:08 -05007942static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007943 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007944 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007945 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007946 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007947 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007948 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007949 .set_bit_hook = btrfs_set_bit_hook,
7950 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007951 .merge_extent_hook = btrfs_merge_extent_hook,
7952 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007953};
7954
Chris Mason35054392009-01-21 13:11:13 -05007955/*
7956 * btrfs doesn't support the bmap operation because swapfiles
7957 * use bmap to make a mapping of extents in the file. They assume
7958 * these extents won't change over the life of the file and they
7959 * use the bmap result to do IO directly to the drive.
7960 *
7961 * the btrfs bmap call would return logical addresses that aren't
7962 * suitable for IO and they also will change frequently as COW
7963 * operations happen. So, swapfile + btrfs == corruption.
7964 *
7965 * For now we're avoiding this by dropping bmap.
7966 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007967static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007968 .readpage = btrfs_readpage,
7969 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04007970 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007971 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007972 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007973 .invalidatepage = btrfs_invalidatepage,
7974 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007975 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007976 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007977};
7978
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007979static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007980 .readpage = btrfs_readpage,
7981 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007982 .invalidatepage = btrfs_invalidatepage,
7983 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007984};
7985
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007986static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007987 .getattr = btrfs_getattr,
7988 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007989 .setxattr = btrfs_setxattr,
7990 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007991 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007992 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007993 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007994 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007995 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007996 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007997};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007998static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007999 .getattr = btrfs_getattr,
8000 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008001 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008002 .setxattr = btrfs_setxattr,
8003 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008004 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008005 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008006 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008007 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008008};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008009static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008010 .readlink = generic_readlink,
8011 .follow_link = page_follow_link_light,
8012 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008013 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008014 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008015 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008016 .setxattr = btrfs_setxattr,
8017 .getxattr = btrfs_getxattr,
8018 .listxattr = btrfs_listxattr,
8019 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008020 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008021 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008022};
Yan, Zheng76dda932009-09-21 16:00:26 -04008023
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008024const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008025 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008026 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008027};