blob: d1e3a2d15f9126ae78b76990bf22ef77bcde9225 [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
Eric Sandeen3972f262013-01-12 02:57:22 +000091static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050092static 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 Masonb888db2b2007-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 Masonb888db2b2007-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;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400703 em->mod_start = em->start;
704 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500705
706 em->block_start = ins.objectid;
707 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500708 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500709 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800710 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500711 set_bit(EXTENT_FLAG_PINNED, &em->flags);
712 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400713 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500714
Chris Masond3977122009-01-05 21:25:51 -0500715 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400716 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500717 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400718 if (!ret)
719 list_move(&em->list,
720 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400721 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500722 if (ret != -EEXIST) {
723 free_extent_map(em);
724 break;
725 }
726 btrfs_drop_extent_cache(inode, async_extent->start,
727 async_extent->start +
728 async_extent->ram_size - 1, 0);
729 }
730
Li Zefan261507a02010-12-17 14:21:50 +0800731 ret = btrfs_add_ordered_extent_compress(inode,
732 async_extent->start,
733 ins.objectid,
734 async_extent->ram_size,
735 ins.offset,
736 BTRFS_ORDERED_COMPRESSED,
737 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100738 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500739
Chris Mason771ed682008-11-06 22:02:51 -0500740 /*
741 * clear dirty, set writeback and unlock the pages.
742 */
743 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400744 &BTRFS_I(inode)->io_tree,
745 async_extent->start,
746 async_extent->start +
747 async_extent->ram_size - 1,
748 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
749 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400750 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400751 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500752
753 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500754 async_extent->start,
755 async_extent->ram_size,
756 ins.objectid,
757 ins.offset, async_extent->pages,
758 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500759
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100760 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500761 alloc_hint = ins.objectid + ins.offset;
762 kfree(async_extent);
763 cond_resched();
764 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100765 ret = 0;
766out:
767 return ret;
768out_free:
769 kfree(async_extent);
770 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500771}
772
Josef Bacik4b46fce2010-05-23 11:00:55 -0400773static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
774 u64 num_bytes)
775{
776 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
777 struct extent_map *em;
778 u64 alloc_hint = 0;
779
780 read_lock(&em_tree->lock);
781 em = search_extent_mapping(em_tree, start, num_bytes);
782 if (em) {
783 /*
784 * if block start isn't an actual block number then find the
785 * first block in this inode and use that as a hint. If that
786 * block is also bogus then just don't worry about it.
787 */
788 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
789 free_extent_map(em);
790 em = search_extent_mapping(em_tree, 0, 0);
791 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
792 alloc_hint = em->block_start;
793 if (em)
794 free_extent_map(em);
795 } else {
796 alloc_hint = em->block_start;
797 free_extent_map(em);
798 }
799 }
800 read_unlock(&em_tree->lock);
801
802 return alloc_hint;
803}
804
Chris Mason771ed682008-11-06 22:02:51 -0500805/*
806 * when extent_io.c finds a delayed allocation range in the file,
807 * the call backs end up in this code. The basic idea is to
808 * allocate extents on disk for the range, and create ordered data structs
809 * in ram to track those extents.
810 *
811 * locked_page is the page that writepage had locked already. We use
812 * it to make sure we don't do extra locks or unlocks.
813 *
814 * *page_started is set to one if we unlock locked_page and do everything
815 * required to start IO on it. It may be clean and already done with
816 * IO when we return.
817 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000818static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
819 struct inode *inode,
820 struct btrfs_root *root,
821 struct page *locked_page,
822 u64 start, u64 end, int *page_started,
823 unsigned long *nr_written,
824 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500825{
Chris Mason771ed682008-11-06 22:02:51 -0500826 u64 alloc_hint = 0;
827 u64 num_bytes;
828 unsigned long ram_size;
829 u64 disk_num_bytes;
830 u64 cur_alloc_size;
831 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500832 struct btrfs_key ins;
833 struct extent_map *em;
834 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
835 int ret = 0;
836
Liu Bo83eea1f2012-07-10 05:28:39 -0600837 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500838
Chris Mason771ed682008-11-06 22:02:51 -0500839 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
840 num_bytes = max(blocksize, num_bytes);
841 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500842
Chris Mason4cb53002011-05-24 15:35:30 -0400843 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400844 if (num_bytes < 64 * 1024 &&
845 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400846 btrfs_add_inode_defrag(trans, inode);
847
Chris Mason771ed682008-11-06 22:02:51 -0500848 if (start == 0) {
849 /* lets try to make an inline extent */
850 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000851 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500852 if (ret == 0) {
853 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400854 &BTRFS_I(inode)->io_tree,
855 start, end, NULL,
856 EXTENT_CLEAR_UNLOCK_PAGE |
857 EXTENT_CLEAR_UNLOCK |
858 EXTENT_CLEAR_DELALLOC |
859 EXTENT_CLEAR_DIRTY |
860 EXTENT_SET_WRITEBACK |
861 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000862
Chris Mason771ed682008-11-06 22:02:51 -0500863 *nr_written = *nr_written +
864 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
865 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500866 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100867 } else if (ret < 0) {
868 btrfs_abort_transaction(trans, root, ret);
869 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500870 }
871 }
Chris Masonc8b97812008-10-29 14:49:59 -0400872
873 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200874 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400875
Josef Bacik4b46fce2010-05-23 11:00:55 -0400876 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400877 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400878
Chris Masond3977122009-01-05 21:25:51 -0500879 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400880 unsigned long op;
881
Josef Bacik287a0ab2010-03-19 18:07:23 +0000882 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400883 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500884 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500885 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100886 if (ret < 0) {
887 btrfs_abort_transaction(trans, root, ret);
888 goto out_unlock;
889 }
Chris Masond3977122009-01-05 21:25:51 -0500890
David Sterba172ddd62011-04-21 00:48:27 +0200891 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100892 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400893 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500894 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500895 ram_size = ins.offset;
896 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400897 em->mod_start = em->start;
898 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400899
Chris Masone6dcd2d2008-07-17 12:53:50 -0400900 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400901 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500902 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400903 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400904 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400905 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400906
Chris Masond3977122009-01-05 21:25:51 -0500907 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400908 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400909 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400910 if (!ret)
911 list_move(&em->list,
912 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400913 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400914 if (ret != -EEXIST) {
915 free_extent_map(em);
916 break;
917 }
918 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400919 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400920 }
921
Chris Mason98d20f62008-04-14 09:46:10 -0400922 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400923 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500924 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100925 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400926
Yan Zheng17d217f2008-12-12 10:03:38 -0500927 if (root->root_key.objectid ==
928 BTRFS_DATA_RELOC_TREE_OBJECTID) {
929 ret = btrfs_reloc_clone_csums(inode, start,
930 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100931 if (ret) {
932 btrfs_abort_transaction(trans, root, ret);
933 goto out_unlock;
934 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500935 }
936
Chris Masond3977122009-01-05 21:25:51 -0500937 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400938 break;
Chris Masond3977122009-01-05 21:25:51 -0500939
Chris Masonc8b97812008-10-29 14:49:59 -0400940 /* we're not doing compressed IO, don't unlock the first
941 * page (which the caller expects to stay locked), don't
942 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400943 *
944 * Do set the Private2 bit so we know this page was properly
945 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400946 */
Chris Masona791e352009-10-08 11:27:10 -0400947 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
948 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
949 EXTENT_SET_PRIVATE2;
950
Chris Masonc8b97812008-10-29 14:49:59 -0400951 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
952 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400953 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400954 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500955 num_bytes -= cur_alloc_size;
956 alloc_hint = ins.objectid + ins.offset;
957 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400958 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100959out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500960 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000961
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100962out_unlock:
963 extent_clear_unlock_delalloc(inode,
964 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400965 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966 EXTENT_CLEAR_UNLOCK_PAGE |
967 EXTENT_CLEAR_UNLOCK |
968 EXTENT_CLEAR_DELALLOC |
969 EXTENT_CLEAR_DIRTY |
970 EXTENT_SET_WRITEBACK |
971 EXTENT_END_WRITEBACK);
972
973 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500974}
Chris Masonc8b97812008-10-29 14:49:59 -0400975
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000976static noinline int cow_file_range(struct inode *inode,
977 struct page *locked_page,
978 u64 start, u64 end, int *page_started,
979 unsigned long *nr_written,
980 int unlock)
981{
982 struct btrfs_trans_handle *trans;
983 struct btrfs_root *root = BTRFS_I(inode)->root;
984 int ret;
985
986 trans = btrfs_join_transaction(root);
987 if (IS_ERR(trans)) {
988 extent_clear_unlock_delalloc(inode,
989 &BTRFS_I(inode)->io_tree,
990 start, end, locked_page,
991 EXTENT_CLEAR_UNLOCK_PAGE |
992 EXTENT_CLEAR_UNLOCK |
993 EXTENT_CLEAR_DELALLOC |
994 EXTENT_CLEAR_DIRTY |
995 EXTENT_SET_WRITEBACK |
996 EXTENT_END_WRITEBACK);
997 return PTR_ERR(trans);
998 }
999 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1000
1001 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1002 page_started, nr_written, unlock);
1003
1004 btrfs_end_transaction(trans, root);
1005
1006 return ret;
1007}
1008
Chris Mason771ed682008-11-06 22:02:51 -05001009/*
1010 * work queue call back to started compression on a file and pages
1011 */
1012static noinline void async_cow_start(struct btrfs_work *work)
1013{
1014 struct async_cow *async_cow;
1015 int num_added = 0;
1016 async_cow = container_of(work, struct async_cow, work);
1017
1018 compress_file_range(async_cow->inode, async_cow->locked_page,
1019 async_cow->start, async_cow->end, async_cow,
1020 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001021 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001022 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001023 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001024 }
Chris Mason771ed682008-11-06 22:02:51 -05001025}
1026
1027/*
1028 * work queue call back to submit previously compressed pages
1029 */
1030static noinline void async_cow_submit(struct btrfs_work *work)
1031{
1032 struct async_cow *async_cow;
1033 struct btrfs_root *root;
1034 unsigned long nr_pages;
1035
1036 async_cow = container_of(work, struct async_cow, work);
1037
1038 root = async_cow->root;
1039 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1040 PAGE_CACHE_SHIFT;
1041
Josef Bacik66657b32012-08-01 15:36:24 -04001042 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001043 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001044 waitqueue_active(&root->fs_info->async_submit_wait))
1045 wake_up(&root->fs_info->async_submit_wait);
1046
Chris Masond3977122009-01-05 21:25:51 -05001047 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001048 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001049}
Chris Masonc8b97812008-10-29 14:49:59 -04001050
Chris Mason771ed682008-11-06 22:02:51 -05001051static noinline void async_cow_free(struct btrfs_work *work)
1052{
1053 struct async_cow *async_cow;
1054 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001055 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001056 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001057 kfree(async_cow);
1058}
1059
1060static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1061 u64 start, u64 end, int *page_started,
1062 unsigned long *nr_written)
1063{
1064 struct async_cow *async_cow;
1065 struct btrfs_root *root = BTRFS_I(inode)->root;
1066 unsigned long nr_pages;
1067 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001068 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001069
Chris Masona3429ab2009-10-08 12:30:20 -04001070 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1071 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001072 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001073 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001074 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001075 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001076 async_cow->root = root;
1077 async_cow->locked_page = locked_page;
1078 async_cow->start = start;
1079
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001080 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001081 cur_end = end;
1082 else
1083 cur_end = min(end, start + 512 * 1024 - 1);
1084
1085 async_cow->end = cur_end;
1086 INIT_LIST_HEAD(&async_cow->extents);
1087
1088 async_cow->work.func = async_cow_start;
1089 async_cow->work.ordered_func = async_cow_submit;
1090 async_cow->work.ordered_free = async_cow_free;
1091 async_cow->work.flags = 0;
1092
Chris Mason771ed682008-11-06 22:02:51 -05001093 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1094 PAGE_CACHE_SHIFT;
1095 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1096
1097 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1098 &async_cow->work);
1099
1100 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1101 wait_event(root->fs_info->async_submit_wait,
1102 (atomic_read(&root->fs_info->async_delalloc_pages) <
1103 limit));
1104 }
1105
Chris Masond3977122009-01-05 21:25:51 -05001106 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001107 atomic_read(&root->fs_info->async_delalloc_pages)) {
1108 wait_event(root->fs_info->async_submit_wait,
1109 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1110 0));
1111 }
1112
1113 *nr_written += nr_pages;
1114 start = cur_end + 1;
1115 }
1116 *page_started = 1;
1117 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001118}
1119
Chris Masond3977122009-01-05 21:25:51 -05001120static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001121 u64 bytenr, u64 num_bytes)
1122{
1123 int ret;
1124 struct btrfs_ordered_sum *sums;
1125 LIST_HEAD(list);
1126
Yan Zheng07d400a2009-01-06 11:42:00 -05001127 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001128 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001129 if (ret == 0 && list_empty(&list))
1130 return 0;
1131
1132 while (!list_empty(&list)) {
1133 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1134 list_del(&sums->list);
1135 kfree(sums);
1136 }
1137 return 1;
1138}
1139
Chris Masond352ac62008-09-29 15:18:18 -04001140/*
1141 * when nowcow writeback call back. This checks for snapshots or COW copies
1142 * of the extents that exist in the file, and COWs the file as required.
1143 *
1144 * If no cow copies or snapshots exist, we write directly to the existing
1145 * blocks on disk
1146 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001147static noinline int run_delalloc_nocow(struct inode *inode,
1148 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001149 u64 start, u64 end, int *page_started, int force,
1150 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001151{
Chris Masonbe20aa92007-12-17 20:14:01 -05001152 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001153 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001154 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001155 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001156 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001157 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001158 u64 cow_start;
1159 u64 cur_offset;
1160 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001161 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001162 u64 disk_bytenr;
1163 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001164 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001165 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001166 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001167 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001168 int nocow;
1169 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001170 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001171 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001172
1173 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001174 if (!path) {
1175 extent_clear_unlock_delalloc(inode,
1176 &BTRFS_I(inode)->io_tree,
1177 start, end, locked_page,
1178 EXTENT_CLEAR_UNLOCK_PAGE |
1179 EXTENT_CLEAR_UNLOCK |
1180 EXTENT_CLEAR_DELALLOC |
1181 EXTENT_CLEAR_DIRTY |
1182 EXTENT_SET_WRITEBACK |
1183 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001184 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001185 }
Li Zefan82d59022011-04-20 10:33:24 +08001186
Liu Bo83eea1f2012-07-10 05:28:39 -06001187 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001188
1189 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001190 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001191 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001192 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001193
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001194 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001195 extent_clear_unlock_delalloc(inode,
1196 &BTRFS_I(inode)->io_tree,
1197 start, end, locked_page,
1198 EXTENT_CLEAR_UNLOCK_PAGE |
1199 EXTENT_CLEAR_UNLOCK |
1200 EXTENT_CLEAR_DELALLOC |
1201 EXTENT_CLEAR_DIRTY |
1202 EXTENT_SET_WRITEBACK |
1203 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001204 btrfs_free_path(path);
1205 return PTR_ERR(trans);
1206 }
1207
Josef Bacik74b21072011-04-13 12:02:53 -04001208 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001209
Yan Zheng80ff3852008-10-30 14:20:02 -04001210 cow_start = (u64)-1;
1211 cur_offset = start;
1212 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001213 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001214 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001215 if (ret < 0) {
1216 btrfs_abort_transaction(trans, root, ret);
1217 goto error;
1218 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1220 leaf = path->nodes[0];
1221 btrfs_item_key_to_cpu(leaf, &found_key,
1222 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001223 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001224 found_key.type == BTRFS_EXTENT_DATA_KEY)
1225 path->slots[0]--;
1226 }
1227 check_prev = 0;
1228next_slot:
1229 leaf = path->nodes[0];
1230 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1231 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001232 if (ret < 0) {
1233 btrfs_abort_transaction(trans, root, ret);
1234 goto error;
1235 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 if (ret > 0)
1237 break;
1238 leaf = path->nodes[0];
1239 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001240
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 nocow = 0;
1242 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001243 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001245
Li Zefan33345d012011-04-20 10:31:50 +08001246 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1248 found_key.offset > end)
1249 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001250
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 if (found_key.offset > cur_offset) {
1252 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001253 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 goto out_check;
1255 }
Chris Masonc31f8832008-01-08 15:46:31 -05001256
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 fi = btrfs_item_ptr(leaf, path->slots[0],
1258 struct btrfs_file_extent_item);
1259 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001260
Yan Zhengd899e052008-10-30 14:25:28 -04001261 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1262 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001264 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 extent_end = found_key.offset +
1266 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001267 disk_num_bytes =
1268 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 if (extent_end <= start) {
1270 path->slots[0]++;
1271 goto next_slot;
1272 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001273 if (disk_bytenr == 0)
1274 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 if (btrfs_file_extent_compression(leaf, fi) ||
1276 btrfs_file_extent_encryption(leaf, fi) ||
1277 btrfs_file_extent_other_encoding(leaf, fi))
1278 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001279 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1280 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001281 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001283 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001284 found_key.offset -
1285 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001286 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001287 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001288 disk_bytenr += cur_offset - found_key.offset;
1289 num_bytes = min(end + 1, extent_end) - cur_offset;
1290 /*
1291 * force cow if csum exists in the range.
1292 * this ensure that csum for a given extent are
1293 * either valid or do not exist.
1294 */
1295 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1296 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 nocow = 1;
1298 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1299 extent_end = found_key.offset +
1300 btrfs_file_extent_inline_len(leaf, fi);
1301 extent_end = ALIGN(extent_end, root->sectorsize);
1302 } else {
1303 BUG_ON(1);
1304 }
1305out_check:
1306 if (extent_end <= start) {
1307 path->slots[0]++;
1308 goto next_slot;
1309 }
1310 if (!nocow) {
1311 if (cow_start == (u64)-1)
1312 cow_start = cur_offset;
1313 cur_offset = extent_end;
1314 if (cur_offset > end)
1315 break;
1316 path->slots[0]++;
1317 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001318 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001319
David Sterbab3b4aa72011-04-21 01:20:15 +02001320 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001322 ret = __cow_file_range(trans, inode, root, locked_page,
1323 cow_start, found_key.offset - 1,
1324 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001325 if (ret) {
1326 btrfs_abort_transaction(trans, root, ret);
1327 goto error;
1328 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001330 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001331
Yan Zhengd899e052008-10-30 14:25:28 -04001332 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1333 struct extent_map *em;
1334 struct extent_map_tree *em_tree;
1335 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001336 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001337 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001338 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001339 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001340 em->len = num_bytes;
1341 em->block_len = num_bytes;
1342 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001343 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001344 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001345 em->mod_start = em->start;
1346 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001347 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001348 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001349 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001350 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001351 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001352 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001353 if (!ret)
1354 list_move(&em->list,
1355 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001356 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001357 if (ret != -EEXIST) {
1358 free_extent_map(em);
1359 break;
1360 }
1361 btrfs_drop_extent_cache(inode, em->start,
1362 em->start + em->len - 1, 0);
1363 }
1364 type = BTRFS_ORDERED_PREALLOC;
1365 } else {
1366 type = BTRFS_ORDERED_NOCOW;
1367 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001368
1369 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001370 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001371 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001372
Yan, Zhengefa56462010-05-16 10:49:59 -04001373 if (root->root_key.objectid ==
1374 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1375 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1376 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001377 if (ret) {
1378 btrfs_abort_transaction(trans, root, ret);
1379 goto error;
1380 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001381 }
1382
Yan Zhengd899e052008-10-30 14:25:28 -04001383 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001384 cur_offset, cur_offset + num_bytes - 1,
1385 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1386 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1387 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001388 cur_offset = extent_end;
1389 if (cur_offset > end)
1390 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001391 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001392 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001393
Josef Bacik17ca04a2012-05-31 15:58:55 -04001394 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001395 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001396 cur_offset = end;
1397 }
1398
Yan Zheng80ff3852008-10-30 14:20:02 -04001399 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001400 ret = __cow_file_range(trans, inode, root, locked_page,
1401 cow_start, end,
1402 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403 if (ret) {
1404 btrfs_abort_transaction(trans, root, ret);
1405 goto error;
1406 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001407 }
1408
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001409error:
Miao Xiea698d0752012-09-20 01:51:59 -06001410 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001411 if (!ret)
1412 ret = err;
1413
Josef Bacik17ca04a2012-05-31 15:58:55 -04001414 if (ret && cur_offset < end)
1415 extent_clear_unlock_delalloc(inode,
1416 &BTRFS_I(inode)->io_tree,
1417 cur_offset, end, locked_page,
1418 EXTENT_CLEAR_UNLOCK_PAGE |
1419 EXTENT_CLEAR_UNLOCK |
1420 EXTENT_CLEAR_DELALLOC |
1421 EXTENT_CLEAR_DIRTY |
1422 EXTENT_SET_WRITEBACK |
1423 EXTENT_END_WRITEBACK);
1424
Yan Zheng7ea394f2008-08-05 13:05:02 -04001425 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001426 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001427}
1428
Chris Masond352ac62008-09-29 15:18:18 -04001429/*
1430 * extent_io.c call back to do delayed allocation processing
1431 */
Chris Masonc8b97812008-10-29 14:49:59 -04001432static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001433 u64 start, u64 end, int *page_started,
1434 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001435{
Chris Masonbe20aa92007-12-17 20:14:01 -05001436 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001437 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001438
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001439 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001440 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001441 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001442 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001443 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001444 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001445 } else if (!btrfs_test_opt(root, COMPRESS) &&
1446 !(BTRFS_I(inode)->force_compress) &&
1447 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001448 ret = cow_file_range(inode, locked_page, start, end,
1449 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001450 } else {
1451 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1452 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001453 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001454 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001455 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001456 return ret;
1457}
1458
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001459static void btrfs_split_extent_hook(struct inode *inode,
1460 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001461{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001462 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001463 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001464 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001465
Josef Bacik9e0baf62011-07-15 15:16:44 +00001466 spin_lock(&BTRFS_I(inode)->lock);
1467 BTRFS_I(inode)->outstanding_extents++;
1468 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001469}
1470
1471/*
1472 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1473 * extents so we can keep track of new extents that are just merged onto old
1474 * extents, such as when we are doing sequential writes, so we can properly
1475 * account for the metadata space we'll need.
1476 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001477static void btrfs_merge_extent_hook(struct inode *inode,
1478 struct extent_state *new,
1479 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001480{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001481 /* not delalloc, ignore it */
1482 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001483 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001484
Josef Bacik9e0baf62011-07-15 15:16:44 +00001485 spin_lock(&BTRFS_I(inode)->lock);
1486 BTRFS_I(inode)->outstanding_extents--;
1487 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001488}
1489
Chris Masond352ac62008-09-29 15:18:18 -04001490/*
1491 * extent_io.c set_bit_hook, used to track delayed allocation
1492 * bytes in this file, and to maintain the list of inodes that
1493 * have pending delalloc work to be done.
1494 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001495static void btrfs_set_bit_hook(struct inode *inode,
1496 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001497{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001498
Chris Mason75eff682008-12-15 15:54:40 -05001499 /*
1500 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001501 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001502 * bit, which is only set or cleared with irqs on
1503 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001504 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001505 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001506 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001507 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001508
Josef Bacik9e0baf62011-07-15 15:16:44 +00001509 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001510 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001511 } else {
1512 spin_lock(&BTRFS_I(inode)->lock);
1513 BTRFS_I(inode)->outstanding_extents++;
1514 spin_unlock(&BTRFS_I(inode)->lock);
1515 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001516
Chris Mason75eff682008-12-15 15:54:40 -05001517 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001518 BTRFS_I(inode)->delalloc_bytes += len;
1519 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001520 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001521 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1522 &root->fs_info->delalloc_inodes);
1523 }
Chris Mason75eff682008-12-15 15:54:40 -05001524 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001525 }
Chris Mason291d6732008-01-29 15:55:23 -05001526}
1527
Chris Masond352ac62008-09-29 15:18:18 -04001528/*
1529 * extent_io.c clear_bit_hook, see set_bit_hook for why
1530 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001531static void btrfs_clear_bit_hook(struct inode *inode,
1532 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001533{
Chris Mason75eff682008-12-15 15:54:40 -05001534 /*
1535 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001536 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001537 * bit, which is only set or cleared with irqs on
1538 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001539 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001540 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001541 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001542 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001543
Josef Bacik9e0baf62011-07-15 15:16:44 +00001544 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001545 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001546 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1547 spin_lock(&BTRFS_I(inode)->lock);
1548 BTRFS_I(inode)->outstanding_extents--;
1549 spin_unlock(&BTRFS_I(inode)->lock);
1550 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001551
1552 if (*bits & EXTENT_DO_ACCOUNTING)
1553 btrfs_delalloc_release_metadata(inode, len);
1554
Josef Bacik0cb59c92010-07-02 12:14:14 -04001555 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1556 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001557 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001558
Chris Mason75eff682008-12-15 15:54:40 -05001559 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001560 root->fs_info->delalloc_bytes -= len;
1561 BTRFS_I(inode)->delalloc_bytes -= len;
1562
Josef Bacik0cb59c92010-07-02 12:14:14 -04001563 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001564 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1565 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1566 }
Chris Mason75eff682008-12-15 15:54:40 -05001567 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001568 }
Chris Mason291d6732008-01-29 15:55:23 -05001569}
1570
Chris Masond352ac62008-09-29 15:18:18 -04001571/*
1572 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1573 * we don't create bios that span stripes or chunks
1574 */
Chris Mason239b14b2008-03-24 15:02:07 -04001575int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001576 size_t size, struct bio *bio,
1577 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001578{
1579 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001580 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001581 u64 length = 0;
1582 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001583 int ret;
1584
Chris Mason771ed682008-11-06 22:02:51 -05001585 if (bio_flags & EXTENT_BIO_COMPRESSED)
1586 return 0;
1587
Chris Masonf2d8d742008-04-21 10:03:05 -04001588 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001589 map_length = length;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001590 ret = btrfs_map_block(root->fs_info, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001591 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001592 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001593 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001594 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001595 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001596 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001597}
1598
Chris Masond352ac62008-09-29 15:18:18 -04001599/*
1600 * in order to insert checksums into the metadata in large chunks,
1601 * we wait until bio submission time. All the pages in the bio are
1602 * checksummed and sums are attached onto the ordered extent record.
1603 *
1604 * At IO completion time the cums attached on the ordered extent record
1605 * are inserted into the btree
1606 */
Chris Masond3977122009-01-05 21:25:51 -05001607static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1608 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001609 unsigned long bio_flags,
1610 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001611{
Chris Mason065631f2008-02-20 12:07:25 -05001612 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001613 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001614
Chris Masond20f7042008-12-08 16:58:54 -05001615 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001616 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001617 return 0;
1618}
Chris Masone0156402008-04-16 11:15:20 -04001619
Chris Mason4a69a412008-11-06 22:03:00 -05001620/*
1621 * in order to insert checksums into the metadata in large chunks,
1622 * we wait until bio submission time. All the pages in the bio are
1623 * checksummed and sums are attached onto the ordered extent record.
1624 *
1625 * At IO completion time the cums attached on the ordered extent record
1626 * are inserted into the btree
1627 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001628static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001629 int mirror_num, unsigned long bio_flags,
1630 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001631{
1632 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001633 int ret;
1634
1635 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1636 if (ret)
1637 bio_endio(bio, ret);
1638 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001639}
1640
Chris Masond352ac62008-09-29 15:18:18 -04001641/*
Chris Masoncad321a2008-12-17 14:51:42 -05001642 * extent_io.c submission hook. This does the right thing for csum calculation
1643 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001644 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001645static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001646 int mirror_num, unsigned long bio_flags,
1647 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001648{
1649 struct btrfs_root *root = BTRFS_I(inode)->root;
1650 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001651 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001652 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001653 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001654
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001655 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001656
Liu Bo83eea1f2012-07-10 05:28:39 -06001657 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001658 metadata = 2;
1659
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001660 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001661 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1662 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001663 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001664
Chris Masond20f7042008-12-08 16:58:54 -05001665 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001666 ret = btrfs_submit_compressed_read(inode, bio,
1667 mirror_num,
1668 bio_flags);
1669 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001670 } else if (!skip_sum) {
1671 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1672 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001673 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001674 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001675 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001676 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001677 /* csum items have already been cloned */
1678 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1679 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001680 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001681 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001682 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001683 bio_flags, bio_offset,
1684 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001685 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001686 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001687 } else if (!skip_sum) {
1688 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1689 if (ret)
1690 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001691 }
1692
Chris Mason0b86a832008-03-24 15:01:56 -04001693mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001694 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1695
1696out:
1697 if (ret < 0)
1698 bio_endio(bio, ret);
1699 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001700}
Chris Mason6885f302008-02-20 16:11:05 -05001701
Chris Masond352ac62008-09-29 15:18:18 -04001702/*
1703 * given a list of ordered sums record them in the inode. This happens
1704 * at IO completion time based on sums calculated at bio submission time.
1705 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001706static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001707 struct inode *inode, u64 file_offset,
1708 struct list_head *list)
1709{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001710 struct btrfs_ordered_sum *sum;
1711
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001712 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001713 btrfs_csum_file_blocks(trans,
1714 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001715 }
1716 return 0;
1717}
1718
Josef Bacik2ac55d42010-02-03 19:33:23 +00001719int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1720 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001721{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001722 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001723 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001724 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001725}
1726
Chris Masond352ac62008-09-29 15:18:18 -04001727/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001728struct btrfs_writepage_fixup {
1729 struct page *page;
1730 struct btrfs_work work;
1731};
1732
Christoph Hellwigb2950862008-12-02 09:54:17 -05001733static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001734{
1735 struct btrfs_writepage_fixup *fixup;
1736 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001737 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001738 struct page *page;
1739 struct inode *inode;
1740 u64 page_start;
1741 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001742 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001743
1744 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1745 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001746again:
Chris Mason247e7432008-07-17 12:53:51 -04001747 lock_page(page);
1748 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1749 ClearPageChecked(page);
1750 goto out_page;
1751 }
1752
1753 inode = page->mapping->host;
1754 page_start = page_offset(page);
1755 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1756
Josef Bacik2ac55d42010-02-03 19:33:23 +00001757 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001758 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001759
1760 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001761 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001762 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001763
1764 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1765 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001766 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1767 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001768 unlock_page(page);
1769 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001770 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001771 goto again;
1772 }
Chris Mason247e7432008-07-17 12:53:51 -04001773
Jeff Mahoney87826df2012-02-15 16:23:57 +01001774 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1775 if (ret) {
1776 mapping_set_error(page->mapping, ret);
1777 end_extent_writepage(page, ret, page_start, page_end);
1778 ClearPageChecked(page);
1779 goto out;
1780 }
1781
Josef Bacik2ac55d42010-02-03 19:33:23 +00001782 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001783 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001784 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001785out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001786 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1787 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001788out_page:
1789 unlock_page(page);
1790 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001791 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001792}
1793
1794/*
1795 * There are a few paths in the higher layers of the kernel that directly
1796 * set the page dirty bit without asking the filesystem if it is a
1797 * good idea. This causes problems because we want to make sure COW
1798 * properly happens and the data=ordered rules are followed.
1799 *
Chris Masonc8b97812008-10-29 14:49:59 -04001800 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001801 * hasn't been properly setup for IO. We kick off an async process
1802 * to fix it up. The async helper will wait for ordered extents, set
1803 * the delalloc bit and make it safe to write the page.
1804 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001805static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001806{
1807 struct inode *inode = page->mapping->host;
1808 struct btrfs_writepage_fixup *fixup;
1809 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001810
Chris Mason8b62b722009-09-02 16:53:46 -04001811 /* this page is properly in the ordered list */
1812 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001813 return 0;
1814
1815 if (PageChecked(page))
1816 return -EAGAIN;
1817
1818 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1819 if (!fixup)
1820 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001821
Chris Mason247e7432008-07-17 12:53:51 -04001822 SetPageChecked(page);
1823 page_cache_get(page);
1824 fixup->work.func = btrfs_writepage_fixup_worker;
1825 fixup->page = page;
1826 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001827 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001828}
1829
Yan Zhengd899e052008-10-30 14:25:28 -04001830static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1831 struct inode *inode, u64 file_pos,
1832 u64 disk_bytenr, u64 disk_num_bytes,
1833 u64 num_bytes, u64 ram_bytes,
1834 u8 compression, u8 encryption,
1835 u16 other_encoding, int extent_type)
1836{
1837 struct btrfs_root *root = BTRFS_I(inode)->root;
1838 struct btrfs_file_extent_item *fi;
1839 struct btrfs_path *path;
1840 struct extent_buffer *leaf;
1841 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001842 int ret;
1843
1844 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001845 if (!path)
1846 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001847
Chris Masonb9473432009-03-13 11:00:37 -04001848 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001849
1850 /*
1851 * we may be replacing one extent in the tree with another.
1852 * The new extent is pinned in the extent map, and we don't want
1853 * to drop it from the cache until it is completely in the btree.
1854 *
1855 * So, tell btrfs_drop_extents to leave this extent in the cache.
1856 * the caller is expected to unpin it and allow it to be merged
1857 * with the others.
1858 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001859 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001860 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001861 if (ret)
1862 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001863
Li Zefan33345d012011-04-20 10:31:50 +08001864 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001865 ins.offset = file_pos;
1866 ins.type = BTRFS_EXTENT_DATA_KEY;
1867 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001868 if (ret)
1869 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001870 leaf = path->nodes[0];
1871 fi = btrfs_item_ptr(leaf, path->slots[0],
1872 struct btrfs_file_extent_item);
1873 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1874 btrfs_set_file_extent_type(leaf, fi, extent_type);
1875 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1876 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1877 btrfs_set_file_extent_offset(leaf, fi, 0);
1878 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1879 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1880 btrfs_set_file_extent_compression(leaf, fi, compression);
1881 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1882 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001883
Yan Zhengd899e052008-10-30 14:25:28 -04001884 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001885 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001886
1887 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001888
1889 ins.objectid = disk_bytenr;
1890 ins.offset = disk_num_bytes;
1891 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001892 ret = btrfs_alloc_reserved_file_extent(trans, root,
1893 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001894 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001895out:
Yan Zhengd899e052008-10-30 14:25:28 -04001896 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001897
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001898 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001899}
1900
Chris Mason5d13a982009-03-13 11:41:46 -04001901/*
1902 * helper function for btrfs_finish_ordered_io, this
1903 * just reads in some of the csum leaves to prime them into ram
1904 * before we start the transaction. It limits the amount of btree
1905 * reads required while inside the transaction.
1906 */
Chris Masond352ac62008-09-29 15:18:18 -04001907/* as ordered data IO finishes, this gets called so we can finish
1908 * an ordered extent if the range of bytes in the file it covers are
1909 * fully written.
1910 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001911static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001912{
Josef Bacik5fd02042012-05-02 14:00:54 -04001913 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001914 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001915 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001916 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001917 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001918 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001919 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001920 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001921
Liu Bo83eea1f2012-07-10 05:28:39 -06001922 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001923
Josef Bacik5fd02042012-05-02 14:00:54 -04001924 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1925 ret = -EIO;
1926 goto out;
1927 }
1928
Yan, Zhengc2167752009-11-12 09:34:21 +00001929 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001930 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05001931 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1932 if (nolock)
1933 trans = btrfs_join_transaction_nolock(root);
1934 else
1935 trans = btrfs_join_transaction(root);
1936 if (IS_ERR(trans)) {
1937 ret = PTR_ERR(trans);
1938 trans = NULL;
1939 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00001940 }
Josef Bacik6c760c02012-11-09 10:53:21 -05001941 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1942 ret = btrfs_update_inode_fallback(trans, root, inode);
1943 if (ret) /* -ENOMEM or corruption */
1944 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001945 goto out;
1946 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001947
Josef Bacik2ac55d42010-02-03 19:33:23 +00001948 lock_extent_bits(io_tree, ordered_extent->file_offset,
1949 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001950 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001951
Josef Bacik0cb59c92010-07-02 12:14:14 -04001952 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001953 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001954 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001955 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001956 if (IS_ERR(trans)) {
1957 ret = PTR_ERR(trans);
1958 trans = NULL;
1959 goto out_unlock;
1960 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001961 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001962
Chris Masonc8b97812008-10-29 14:49:59 -04001963 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001964 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001965 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001966 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001967 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001968 ordered_extent->file_offset,
1969 ordered_extent->file_offset +
1970 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001971 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001972 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001973 ret = insert_reserved_file_extent(trans, inode,
1974 ordered_extent->file_offset,
1975 ordered_extent->start,
1976 ordered_extent->disk_len,
1977 ordered_extent->len,
1978 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001979 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001980 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04001981 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04001982 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1983 ordered_extent->file_offset, ordered_extent->len,
1984 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001985 if (ret < 0) {
1986 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001987 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001988 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001989
Chris Masone6dcd2d2008-07-17 12:53:50 -04001990 add_pending_csums(trans, inode, ordered_extent->file_offset,
1991 &ordered_extent->list);
1992
Josef Bacik6c760c02012-11-09 10:53:21 -05001993 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1994 ret = btrfs_update_inode_fallback(trans, root, inode);
1995 if (ret) { /* -ENOMEM or corruption */
1996 btrfs_abort_transaction(trans, root, ret);
1997 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04001998 }
1999 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002000out_unlock:
2001 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2002 ordered_extent->file_offset +
2003 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002004out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002005 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002006 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002007 if (trans)
2008 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002009
Josef Bacik5fd02042012-05-02 14:00:54 -04002010 if (ret)
2011 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2012 ordered_extent->file_offset +
2013 ordered_extent->len - 1, NULL, GFP_NOFS);
2014
2015 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002016 * This needs to be done to make sure anybody waiting knows we are done
2017 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002018 */
2019 btrfs_remove_ordered_extent(inode, ordered_extent);
2020
Chris Masone6dcd2d2008-07-17 12:53:50 -04002021 /* once for us */
2022 btrfs_put_ordered_extent(ordered_extent);
2023 /* once for the tree */
2024 btrfs_put_ordered_extent(ordered_extent);
2025
Josef Bacik5fd02042012-05-02 14:00:54 -04002026 return ret;
2027}
2028
2029static void finish_ordered_fn(struct btrfs_work *work)
2030{
2031 struct btrfs_ordered_extent *ordered_extent;
2032 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2033 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002034}
2035
Christoph Hellwigb2950862008-12-02 09:54:17 -05002036static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002037 struct extent_state *state, int uptodate)
2038{
Josef Bacik5fd02042012-05-02 14:00:54 -04002039 struct inode *inode = page->mapping->host;
2040 struct btrfs_root *root = BTRFS_I(inode)->root;
2041 struct btrfs_ordered_extent *ordered_extent = NULL;
2042 struct btrfs_workers *workers;
2043
liubo1abe9b82011-03-24 11:18:59 +00002044 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2045
Chris Mason8b62b722009-09-02 16:53:46 -04002046 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002047 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2048 end - start + 1, uptodate))
2049 return 0;
2050
2051 ordered_extent->work.func = finish_ordered_fn;
2052 ordered_extent->work.flags = 0;
2053
Liu Bo83eea1f2012-07-10 05:28:39 -06002054 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002055 workers = &root->fs_info->endio_freespace_worker;
2056 else
2057 workers = &root->fs_info->endio_write_workers;
2058 btrfs_queue_worker(workers, &ordered_extent->work);
2059
2060 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002061}
2062
Chris Masond352ac62008-09-29 15:18:18 -04002063/*
Chris Masond352ac62008-09-29 15:18:18 -04002064 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002065 * if there's a match, we allow the bio to finish. If not, the code in
2066 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002067 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002068static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002069 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002070{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002071 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002072 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002073 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002074 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002075 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002076 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002077 struct btrfs_root *root = BTRFS_I(inode)->root;
2078 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002079
Chris Masond20f7042008-12-08 16:58:54 -05002080 if (PageChecked(page)) {
2081 ClearPageChecked(page);
2082 goto good;
2083 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002084
2085 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002086 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002087
Yan Zheng17d217f2008-12-12 10:03:38 -05002088 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002089 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002090 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2091 GFP_NOFS);
2092 return 0;
2093 }
2094
Yanc2e639f2008-02-04 08:57:25 -05002095 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002096 private = state->private;
2097 ret = 0;
2098 } else {
2099 ret = get_state_private(io_tree, start, &private);
2100 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002101 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002102 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002103 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002104
Chris Masonff79f812007-10-15 16:22:25 -04002105 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2106 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002107 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002108 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002109
Cong Wang7ac687d2011-11-25 23:14:28 +08002110 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002111good:
Chris Mason07157aa2007-08-30 08:50:51 -04002112 return 0;
2113
2114zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002115 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002116 "private %llu\n",
2117 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002118 (unsigned long long)start, csum,
2119 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002120 memset(kaddr + offset, 1, end - start + 1);
2121 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002122 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002123 if (private == 0)
2124 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002125 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002126}
Chris Masonb888db2b2007-08-27 16:49:44 -04002127
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002128struct delayed_iput {
2129 struct list_head list;
2130 struct inode *inode;
2131};
2132
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002133/* JDM: If this is fs-wide, why can't we add a pointer to
2134 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002135void btrfs_add_delayed_iput(struct inode *inode)
2136{
2137 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2138 struct delayed_iput *delayed;
2139
2140 if (atomic_add_unless(&inode->i_count, -1, 1))
2141 return;
2142
2143 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2144 delayed->inode = inode;
2145
2146 spin_lock(&fs_info->delayed_iput_lock);
2147 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2148 spin_unlock(&fs_info->delayed_iput_lock);
2149}
2150
2151void btrfs_run_delayed_iputs(struct btrfs_root *root)
2152{
2153 LIST_HEAD(list);
2154 struct btrfs_fs_info *fs_info = root->fs_info;
2155 struct delayed_iput *delayed;
2156 int empty;
2157
2158 spin_lock(&fs_info->delayed_iput_lock);
2159 empty = list_empty(&fs_info->delayed_iputs);
2160 spin_unlock(&fs_info->delayed_iput_lock);
2161 if (empty)
2162 return;
2163
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002164 spin_lock(&fs_info->delayed_iput_lock);
2165 list_splice_init(&fs_info->delayed_iputs, &list);
2166 spin_unlock(&fs_info->delayed_iput_lock);
2167
2168 while (!list_empty(&list)) {
2169 delayed = list_entry(list.next, struct delayed_iput, list);
2170 list_del(&delayed->list);
2171 iput(delayed->inode);
2172 kfree(delayed);
2173 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002174}
2175
Yan, Zhengd68fc572010-05-16 10:49:58 -04002176enum btrfs_orphan_cleanup_state {
2177 ORPHAN_CLEANUP_STARTED = 1,
2178 ORPHAN_CLEANUP_DONE = 2,
2179};
2180
2181/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002182 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002183 * files in the subvolume, it removes orphan item and frees block_rsv
2184 * structure.
2185 */
2186void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2187 struct btrfs_root *root)
2188{
Josef Bacik90290e12011-12-02 15:44:12 -05002189 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002190 int ret;
2191
Josef Bacik8a35d952012-05-23 14:26:42 -04002192 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002193 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2194 return;
2195
Josef Bacik90290e12011-12-02 15:44:12 -05002196 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002197 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002198 spin_unlock(&root->orphan_lock);
2199 return;
2200 }
2201
2202 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2203 spin_unlock(&root->orphan_lock);
2204 return;
2205 }
2206
2207 block_rsv = root->orphan_block_rsv;
2208 root->orphan_block_rsv = NULL;
2209 spin_unlock(&root->orphan_lock);
2210
Yan, Zhengd68fc572010-05-16 10:49:58 -04002211 if (root->orphan_item_inserted &&
2212 btrfs_root_refs(&root->root_item) > 0) {
2213 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2214 root->root_key.objectid);
2215 BUG_ON(ret);
2216 root->orphan_item_inserted = 0;
2217 }
2218
Josef Bacik90290e12011-12-02 15:44:12 -05002219 if (block_rsv) {
2220 WARN_ON(block_rsv->size > 0);
2221 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002222 }
2223}
2224
2225/*
Josef Bacik7b128762008-07-24 12:17:14 -04002226 * This creates an orphan entry for the given inode in case something goes
2227 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002228 *
2229 * NOTE: caller of this function should reserve 5 units of metadata for
2230 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002231 */
2232int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2233{
2234 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002235 struct btrfs_block_rsv *block_rsv = NULL;
2236 int reserve = 0;
2237 int insert = 0;
2238 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002239
Yan, Zhengd68fc572010-05-16 10:49:58 -04002240 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002241 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002242 if (!block_rsv)
2243 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002244 }
2245
Yan, Zhengd68fc572010-05-16 10:49:58 -04002246 spin_lock(&root->orphan_lock);
2247 if (!root->orphan_block_rsv) {
2248 root->orphan_block_rsv = block_rsv;
2249 } else if (block_rsv) {
2250 btrfs_free_block_rsv(root, block_rsv);
2251 block_rsv = NULL;
2252 }
Josef Bacik7b128762008-07-24 12:17:14 -04002253
Josef Bacik8a35d952012-05-23 14:26:42 -04002254 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2255 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002256#if 0
2257 /*
2258 * For proper ENOSPC handling, we should do orphan
2259 * cleanup when mounting. But this introduces backward
2260 * compatibility issue.
2261 */
2262 if (!xchg(&root->orphan_item_inserted, 1))
2263 insert = 2;
2264 else
2265 insert = 1;
2266#endif
2267 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002268 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002269 }
Josef Bacik7b128762008-07-24 12:17:14 -04002270
Josef Bacik72ac3c02012-05-23 14:13:11 -04002271 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2272 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002273 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002274 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002275
Yan, Zhengd68fc572010-05-16 10:49:58 -04002276 /* grab metadata reservation from transaction handle */
2277 if (reserve) {
2278 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002279 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002280 }
2281
2282 /* insert an orphan item to track this unlinked/truncated file */
2283 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002284 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002285 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002286 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2287 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002288 btrfs_abort_transaction(trans, root, ret);
2289 return ret;
2290 }
2291 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002292 }
2293
2294 /* insert an orphan item to track subvolume contains orphan files */
2295 if (insert >= 2) {
2296 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2297 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002298 if (ret && ret != -EEXIST) {
2299 btrfs_abort_transaction(trans, root, ret);
2300 return ret;
2301 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002302 }
2303 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002304}
2305
2306/*
2307 * We have done the truncate/delete so we can go ahead and remove the orphan
2308 * item for this particular inode.
2309 */
2310int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2311{
2312 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002313 int delete_item = 0;
2314 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002315 int ret = 0;
2316
Yan, Zhengd68fc572010-05-16 10:49:58 -04002317 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002318 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2319 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002320 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002321
Josef Bacik72ac3c02012-05-23 14:13:11 -04002322 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2323 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002324 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002325 spin_unlock(&root->orphan_lock);
2326
2327 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002328 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002329 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002330 }
2331
Josef Bacik8a35d952012-05-23 14:26:42 -04002332 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002333 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002334 atomic_dec(&root->orphan_inodes);
2335 }
Josef Bacik7b128762008-07-24 12:17:14 -04002336
Yan, Zhengd68fc572010-05-16 10:49:58 -04002337 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002338}
2339
2340/*
2341 * this cleans up any orphans that may be left on the list from the last use
2342 * of this root.
2343 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002344int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002345{
2346 struct btrfs_path *path;
2347 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002348 struct btrfs_key key, found_key;
2349 struct btrfs_trans_handle *trans;
2350 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002351 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002352 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2353
Yan, Zhengd68fc572010-05-16 10:49:58 -04002354 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002355 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002356
2357 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002358 if (!path) {
2359 ret = -ENOMEM;
2360 goto out;
2361 }
Josef Bacik7b128762008-07-24 12:17:14 -04002362 path->reada = -1;
2363
2364 key.objectid = BTRFS_ORPHAN_OBJECTID;
2365 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2366 key.offset = (u64)-1;
2367
Josef Bacik7b128762008-07-24 12:17:14 -04002368 while (1) {
2369 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002370 if (ret < 0)
2371 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002372
2373 /*
2374 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002375 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002376 * find the key and see if we have stuff that matches
2377 */
2378 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002379 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002380 if (path->slots[0] == 0)
2381 break;
2382 path->slots[0]--;
2383 }
2384
2385 /* pull out the item */
2386 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002387 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2388
2389 /* make sure the item matches what we want */
2390 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2391 break;
2392 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2393 break;
2394
2395 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002396 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002397
2398 /*
2399 * this is where we are basically btrfs_lookup, without the
2400 * crossing root thing. we store the inode number in the
2401 * offset of the orphan item.
2402 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002403
2404 if (found_key.offset == last_objectid) {
2405 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2406 "stopping orphan cleanup\n");
2407 ret = -EINVAL;
2408 goto out;
2409 }
2410
2411 last_objectid = found_key.offset;
2412
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002413 found_key.objectid = found_key.offset;
2414 found_key.type = BTRFS_INODE_ITEM_KEY;
2415 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002416 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002417 ret = PTR_RET(inode);
2418 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002419 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002420
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002421 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2422 struct btrfs_root *dead_root;
2423 struct btrfs_fs_info *fs_info = root->fs_info;
2424 int is_dead_root = 0;
2425
2426 /*
2427 * this is an orphan in the tree root. Currently these
2428 * could come from 2 sources:
2429 * a) a snapshot deletion in progress
2430 * b) a free space cache inode
2431 * We need to distinguish those two, as the snapshot
2432 * orphan must not get deleted.
2433 * find_dead_roots already ran before us, so if this
2434 * is a snapshot deletion, we should find the root
2435 * in the dead_roots list
2436 */
2437 spin_lock(&fs_info->trans_lock);
2438 list_for_each_entry(dead_root, &fs_info->dead_roots,
2439 root_list) {
2440 if (dead_root->root_key.objectid ==
2441 found_key.objectid) {
2442 is_dead_root = 1;
2443 break;
2444 }
2445 }
2446 spin_unlock(&fs_info->trans_lock);
2447 if (is_dead_root) {
2448 /* prevent this orphan from being found again */
2449 key.offset = found_key.objectid - 1;
2450 continue;
2451 }
2452 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002453 /*
2454 * Inode is already gone but the orphan item is still there,
2455 * kill the orphan item.
2456 */
2457 if (ret == -ESTALE) {
2458 trans = btrfs_start_transaction(root, 1);
2459 if (IS_ERR(trans)) {
2460 ret = PTR_ERR(trans);
2461 goto out;
2462 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002463 printk(KERN_ERR "auto deleting %Lu\n",
2464 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002465 ret = btrfs_del_orphan_item(trans, root,
2466 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002467 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002468 btrfs_end_transaction(trans, root);
2469 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002470 }
Josef Bacik7b128762008-07-24 12:17:14 -04002471
Josef Bacik7b128762008-07-24 12:17:14 -04002472 /*
2473 * add this inode to the orphan list so btrfs_orphan_del does
2474 * the proper thing when we hit it
2475 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002476 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2477 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002478
Josef Bacik7b128762008-07-24 12:17:14 -04002479 /* if we have links, this was a truncate, lets do that */
2480 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002481 if (!S_ISREG(inode->i_mode)) {
2482 WARN_ON(1);
2483 iput(inode);
2484 continue;
2485 }
Josef Bacik7b128762008-07-24 12:17:14 -04002486 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05002487
2488 /* 1 for the orphan item deletion. */
2489 trans = btrfs_start_transaction(root, 1);
2490 if (IS_ERR(trans)) {
2491 ret = PTR_ERR(trans);
2492 goto out;
2493 }
2494 ret = btrfs_orphan_add(trans, inode);
2495 btrfs_end_transaction(trans, root);
2496 if (ret)
2497 goto out;
2498
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002499 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002500 } else {
2501 nr_unlink++;
2502 }
2503
2504 /* this will do delete_inode and everything for us */
2505 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002506 if (ret)
2507 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002508 }
Miao Xie3254c872011-11-10 20:45:05 -05002509 /* release the path since we're done with it */
2510 btrfs_release_path(path);
2511
Yan, Zhengd68fc572010-05-16 10:49:58 -04002512 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2513
2514 if (root->orphan_block_rsv)
2515 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2516 (u64)-1);
2517
2518 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002519 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002520 if (!IS_ERR(trans))
2521 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002522 }
Josef Bacik7b128762008-07-24 12:17:14 -04002523
2524 if (nr_unlink)
2525 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2526 if (nr_truncate)
2527 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002528
2529out:
2530 if (ret)
2531 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2532 btrfs_free_path(path);
2533 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002534}
2535
Chris Masond352ac62008-09-29 15:18:18 -04002536/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002537 * very simple check to peek ahead in the leaf looking for xattrs. If we
2538 * don't find any xattrs, we know there can't be any acls.
2539 *
2540 * slot is the slot the inode is in, objectid is the objectid of the inode
2541 */
2542static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2543 int slot, u64 objectid)
2544{
2545 u32 nritems = btrfs_header_nritems(leaf);
2546 struct btrfs_key found_key;
2547 int scanned = 0;
2548
2549 slot++;
2550 while (slot < nritems) {
2551 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2552
2553 /* we found a different objectid, there must not be acls */
2554 if (found_key.objectid != objectid)
2555 return 0;
2556
2557 /* we found an xattr, assume we've got an acl */
2558 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2559 return 1;
2560
2561 /*
2562 * we found a key greater than an xattr key, there can't
2563 * be any acls later on
2564 */
2565 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2566 return 0;
2567
2568 slot++;
2569 scanned++;
2570
2571 /*
2572 * it goes inode, inode backrefs, xattrs, extents,
2573 * so if there are a ton of hard links to an inode there can
2574 * be a lot of backrefs. Don't waste time searching too hard,
2575 * this is just an optimization
2576 */
2577 if (scanned >= 8)
2578 break;
2579 }
2580 /* we hit the end of the leaf before we found an xattr or
2581 * something larger than an xattr. We have to assume the inode
2582 * has acls
2583 */
2584 return 1;
2585}
2586
2587/*
Chris Masond352ac62008-09-29 15:18:18 -04002588 * read an inode from the btree into the in-memory inode
2589 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002590static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002591{
2592 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002593 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002594 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002595 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002596 struct btrfs_root *root = BTRFS_I(inode)->root;
2597 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002598 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002599 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002600 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002601 bool filled = false;
2602
2603 ret = btrfs_fill_inode(inode, &rdev);
2604 if (!ret)
2605 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002606
2607 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002608 if (!path)
2609 goto make_bad;
2610
Josef Bacikd90c7322011-05-17 09:50:54 -04002611 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002612 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002613
Chris Mason39279cc2007-06-12 06:35:45 -04002614 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002615 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002616 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002617
Chris Mason5f39d392007-10-15 16:14:19 -04002618 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002619
2620 if (filled)
2621 goto cache_acl;
2622
Chris Mason5f39d392007-10-15 16:14:19 -04002623 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2624 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002625 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002626 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002627 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2628 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002629 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002630
2631 tspec = btrfs_inode_atime(inode_item);
2632 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2633 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2634
2635 tspec = btrfs_inode_mtime(inode_item);
2636 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2637 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2638
2639 tspec = btrfs_inode_ctime(inode_item);
2640 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2641 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2642
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002643 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002644 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002645 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2646
2647 /*
2648 * If we were modified in the current generation and evicted from memory
2649 * and then re-read we need to do a full sync since we don't have any
2650 * idea about which extents were modified before we were evicted from
2651 * cache.
2652 */
2653 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2654 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2655 &BTRFS_I(inode)->runtime_flags);
2656
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002657 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002658 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002659 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002660 rdev = btrfs_inode_rdev(leaf, inode_item);
2661
Josef Bacikaec74772008-07-24 12:12:38 -04002662 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002663 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002664cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002665 /*
2666 * try to precache a NULL acl entry for files that don't have
2667 * any xattrs or acls
2668 */
Li Zefan33345d012011-04-20 10:31:50 +08002669 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2670 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002671 if (!maybe_acls)
2672 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002673
Chris Mason39279cc2007-06-12 06:35:45 -04002674 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002675
Chris Mason39279cc2007-06-12 06:35:45 -04002676 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002677 case S_IFREG:
2678 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002679 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002680 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002681 inode->i_fop = &btrfs_file_operations;
2682 inode->i_op = &btrfs_file_inode_operations;
2683 break;
2684 case S_IFDIR:
2685 inode->i_fop = &btrfs_dir_file_operations;
2686 if (root == root->fs_info->tree_root)
2687 inode->i_op = &btrfs_dir_ro_inode_operations;
2688 else
2689 inode->i_op = &btrfs_dir_inode_operations;
2690 break;
2691 case S_IFLNK:
2692 inode->i_op = &btrfs_symlink_inode_operations;
2693 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002694 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002695 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002696 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002697 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002698 init_special_inode(inode, inode->i_mode, rdev);
2699 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002700 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002701
2702 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002703 return;
2704
2705make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002706 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002707 make_bad_inode(inode);
2708}
2709
Chris Masond352ac62008-09-29 15:18:18 -04002710/*
2711 * given a leaf and an inode, copy the inode fields into the leaf
2712 */
Chris Masone02119d2008-09-05 16:13:11 -04002713static void fill_inode_item(struct btrfs_trans_handle *trans,
2714 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002715 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002716 struct inode *inode)
2717{
Liu Bo51fab692012-12-27 09:01:21 +00002718 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002719
Liu Bo51fab692012-12-27 09:01:21 +00002720 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04002721
Liu Bo51fab692012-12-27 09:01:21 +00002722 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
2723 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
2724 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
2725 &token);
2726 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
2727 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002728
Liu Bo51fab692012-12-27 09:01:21 +00002729 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
2730 inode->i_atime.tv_sec, &token);
2731 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
2732 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002733
Liu Bo51fab692012-12-27 09:01:21 +00002734 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
2735 inode->i_mtime.tv_sec, &token);
2736 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
2737 inode->i_mtime.tv_nsec, &token);
2738
2739 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
2740 inode->i_ctime.tv_sec, &token);
2741 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
2742 inode->i_ctime.tv_nsec, &token);
2743
2744 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
2745 &token);
2746 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
2747 &token);
2748 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
2749 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
2750 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
2751 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
2752 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04002753}
2754
Chris Masond352ac62008-09-29 15:18:18 -04002755/*
2756 * copy everything in the in-memory inode into the btree.
2757 */
Chris Mason21151332011-11-10 20:39:08 -05002758static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002759 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002760{
2761 struct btrfs_inode_item *inode_item;
2762 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002763 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002764 int ret;
2765
2766 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002767 if (!path)
2768 return -ENOMEM;
2769
Chris Masonb9473432009-03-13 11:00:37 -04002770 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002771 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2772 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002773 if (ret) {
2774 if (ret > 0)
2775 ret = -ENOENT;
2776 goto failed;
2777 }
2778
Chris Masonb4ce94d2009-02-04 09:25:08 -05002779 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002780 leaf = path->nodes[0];
2781 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002782 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002783
Chris Masone02119d2008-09-05 16:13:11 -04002784 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002785 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002786 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002787 ret = 0;
2788failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002789 btrfs_free_path(path);
2790 return ret;
2791}
2792
Chris Masond352ac62008-09-29 15:18:18 -04002793/*
Chris Mason21151332011-11-10 20:39:08 -05002794 * copy everything in the in-memory inode into the btree.
2795 */
2796noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2797 struct btrfs_root *root, struct inode *inode)
2798{
2799 int ret;
2800
2801 /*
2802 * If the inode is a free space inode, we can deadlock during commit
2803 * if we put it into the delayed code.
2804 *
2805 * The data relocation inode should also be directly updated
2806 * without delay
2807 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002808 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002809 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002810 btrfs_update_root_times(trans, root);
2811
Chris Mason21151332011-11-10 20:39:08 -05002812 ret = btrfs_delayed_update_inode(trans, root, inode);
2813 if (!ret)
2814 btrfs_set_inode_last_trans(trans, inode);
2815 return ret;
2816 }
2817
2818 return btrfs_update_inode_item(trans, root, inode);
2819}
2820
Josef Bacikbe6aef62012-10-22 15:43:12 -04002821noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2822 struct btrfs_root *root,
2823 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002824{
2825 int ret;
2826
2827 ret = btrfs_update_inode(trans, root, inode);
2828 if (ret == -ENOSPC)
2829 return btrfs_update_inode_item(trans, root, inode);
2830 return ret;
2831}
2832
2833/*
Chris Masond352ac62008-09-29 15:18:18 -04002834 * unlink helper that gets used here in inode.c and in the tree logging
2835 * recovery code. It remove a link in a directory with a given name, and
2836 * also drops the back refs in the inode to the directory
2837 */
Al Viro92986792011-03-04 17:14:37 +00002838static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2839 struct btrfs_root *root,
2840 struct inode *dir, struct inode *inode,
2841 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002842{
2843 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002844 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002845 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002846 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002847 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002848 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002849 u64 ino = btrfs_ino(inode);
2850 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002851
2852 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002853 if (!path) {
2854 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002855 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002856 }
2857
Chris Masonb9473432009-03-13 11:00:37 -04002858 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002859 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002860 name, name_len, -1);
2861 if (IS_ERR(di)) {
2862 ret = PTR_ERR(di);
2863 goto err;
2864 }
2865 if (!di) {
2866 ret = -ENOENT;
2867 goto err;
2868 }
Chris Mason5f39d392007-10-15 16:14:19 -04002869 leaf = path->nodes[0];
2870 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002871 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002872 if (ret)
2873 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002874 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002875
Li Zefan33345d012011-04-20 10:31:50 +08002876 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2877 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002878 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002879 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002880 "inode %llu parent %llu\n", name_len, name,
2881 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002882 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002883 goto err;
2884 }
2885
Miao Xie16cdcec2011-04-22 18:12:22 +08002886 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002887 if (ret) {
2888 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002889 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002890 }
Chris Mason39279cc2007-06-12 06:35:45 -04002891
Chris Masone02119d2008-09-05 16:13:11 -04002892 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002893 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002894 if (ret != 0 && ret != -ENOENT) {
2895 btrfs_abort_transaction(trans, root, ret);
2896 goto err;
2897 }
Chris Masone02119d2008-09-05 16:13:11 -04002898
2899 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2900 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002901 if (ret == -ENOENT)
2902 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002903err:
2904 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002905 if (ret)
2906 goto out;
2907
2908 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002909 inode_inc_iversion(inode);
2910 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002911 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002912 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002913out:
Chris Mason39279cc2007-06-12 06:35:45 -04002914 return ret;
2915}
2916
Al Viro92986792011-03-04 17:14:37 +00002917int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2918 struct btrfs_root *root,
2919 struct inode *dir, struct inode *inode,
2920 const char *name, int name_len)
2921{
2922 int ret;
2923 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2924 if (!ret) {
2925 btrfs_drop_nlink(inode);
2926 ret = btrfs_update_inode(trans, root, inode);
2927 }
2928 return ret;
2929}
2930
2931
Yan, Zhenga22285a2010-05-16 10:48:46 -04002932/* helper to check if there is any shared block in the path */
2933static int check_path_shared(struct btrfs_root *root,
2934 struct btrfs_path *path)
2935{
2936 struct extent_buffer *eb;
2937 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002938 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002939
2940 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002941 int ret;
2942
Yan, Zhenga22285a2010-05-16 10:48:46 -04002943 if (!path->nodes[level])
2944 break;
2945 eb = path->nodes[level];
2946 if (!btrfs_block_can_be_shared(root, eb))
2947 continue;
2948 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2949 &refs, NULL);
2950 if (refs > 1)
2951 return 1;
2952 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002953 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002954}
2955
2956/*
2957 * helper to start transaction for unlink and rmdir.
2958 *
2959 * unlink and rmdir are special in btrfs, they do not always free space.
2960 * so in enospc case, we should make sure they will free space before
2961 * allowing them to use the global metadata reservation.
2962 */
2963static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2964 struct dentry *dentry)
2965{
2966 struct btrfs_trans_handle *trans;
2967 struct btrfs_root *root = BTRFS_I(dir)->root;
2968 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002969 struct btrfs_dir_item *di;
2970 struct inode *inode = dentry->d_inode;
2971 u64 index;
2972 int check_link = 1;
2973 int err = -ENOSPC;
2974 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002975 u64 ino = btrfs_ino(inode);
2976 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002977
Josef Bacike70bea52011-10-11 14:18:24 -04002978 /*
2979 * 1 for the possible orphan item
2980 * 1 for the dir item
2981 * 1 for the dir index
2982 * 1 for the inode ref
2983 * 1 for the inode ref in the tree log
2984 * 2 for the dir entries in the log
2985 * 1 for the inode
2986 */
2987 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002988 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2989 return trans;
2990
Li Zefan33345d012011-04-20 10:31:50 +08002991 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002992 return ERR_PTR(-ENOSPC);
2993
2994 /* check if there is someone else holds reference */
2995 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2996 return ERR_PTR(-ENOSPC);
2997
2998 if (atomic_read(&inode->i_count) > 2)
2999 return ERR_PTR(-ENOSPC);
3000
3001 if (xchg(&root->fs_info->enospc_unlink, 1))
3002 return ERR_PTR(-ENOSPC);
3003
3004 path = btrfs_alloc_path();
3005 if (!path) {
3006 root->fs_info->enospc_unlink = 0;
3007 return ERR_PTR(-ENOMEM);
3008 }
3009
Josef Bacik3880a1b2011-10-14 14:46:51 -04003010 /* 1 for the orphan item */
3011 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003012 if (IS_ERR(trans)) {
3013 btrfs_free_path(path);
3014 root->fs_info->enospc_unlink = 0;
3015 return trans;
3016 }
3017
3018 path->skip_locking = 1;
3019 path->search_commit_root = 1;
3020
3021 ret = btrfs_lookup_inode(trans, root, path,
3022 &BTRFS_I(dir)->location, 0);
3023 if (ret < 0) {
3024 err = ret;
3025 goto out;
3026 }
3027 if (ret == 0) {
3028 if (check_path_shared(root, path))
3029 goto out;
3030 } else {
3031 check_link = 0;
3032 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003033 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003034
3035 ret = btrfs_lookup_inode(trans, root, path,
3036 &BTRFS_I(inode)->location, 0);
3037 if (ret < 0) {
3038 err = ret;
3039 goto out;
3040 }
3041 if (ret == 0) {
3042 if (check_path_shared(root, path))
3043 goto out;
3044 } else {
3045 check_link = 0;
3046 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003047 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003048
3049 if (ret == 0 && S_ISREG(inode->i_mode)) {
3050 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003051 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003052 if (ret < 0) {
3053 err = ret;
3054 goto out;
3055 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003056 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003057 if (check_path_shared(root, path))
3058 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003059 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003060 }
3061
3062 if (!check_link) {
3063 err = 0;
3064 goto out;
3065 }
3066
Li Zefan33345d012011-04-20 10:31:50 +08003067 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003068 dentry->d_name.name, dentry->d_name.len, 0);
3069 if (IS_ERR(di)) {
3070 err = PTR_ERR(di);
3071 goto out;
3072 }
3073 if (di) {
3074 if (check_path_shared(root, path))
3075 goto out;
3076 } else {
3077 err = 0;
3078 goto out;
3079 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003080 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003081
Mark Fashehf1863732012-08-08 11:32:27 -07003082 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3083 dentry->d_name.len, ino, dir_ino, 0,
3084 &index);
3085 if (ret) {
3086 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003087 goto out;
3088 }
Mark Fashehf1863732012-08-08 11:32:27 -07003089
Yan, Zhenga22285a2010-05-16 10:48:46 -04003090 if (check_path_shared(root, path))
3091 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003092
David Sterbab3b4aa72011-04-21 01:20:15 +02003093 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003094
Miao Xie16cdcec2011-04-22 18:12:22 +08003095 /*
3096 * This is a commit root search, if we can lookup inode item and other
3097 * relative items in the commit root, it means the transaction of
3098 * dir/file creation has been committed, and the dir index item that we
3099 * delay to insert has also been inserted into the commit root. So
3100 * we needn't worry about the delayed insertion of the dir index item
3101 * here.
3102 */
Li Zefan33345d012011-04-20 10:31:50 +08003103 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003104 dentry->d_name.name, dentry->d_name.len, 0);
3105 if (IS_ERR(di)) {
3106 err = PTR_ERR(di);
3107 goto out;
3108 }
3109 BUG_ON(ret == -ENOENT);
3110 if (check_path_shared(root, path))
3111 goto out;
3112
3113 err = 0;
3114out:
3115 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003116 /* Migrate the orphan reservation over */
3117 if (!err)
3118 err = btrfs_block_rsv_migrate(trans->block_rsv,
3119 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003120 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003121
Yan, Zhenga22285a2010-05-16 10:48:46 -04003122 if (err) {
3123 btrfs_end_transaction(trans, root);
3124 root->fs_info->enospc_unlink = 0;
3125 return ERR_PTR(err);
3126 }
3127
3128 trans->block_rsv = &root->fs_info->global_block_rsv;
3129 return trans;
3130}
3131
3132static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3133 struct btrfs_root *root)
3134{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003135 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003136 btrfs_block_rsv_release(root, trans->block_rsv,
3137 trans->bytes_reserved);
3138 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003139 BUG_ON(!root->fs_info->enospc_unlink);
3140 root->fs_info->enospc_unlink = 0;
3141 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003142 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003143}
3144
Chris Mason39279cc2007-06-12 06:35:45 -04003145static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3146{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003147 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003148 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003149 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003150 int ret;
3151
Yan, Zhenga22285a2010-05-16 10:48:46 -04003152 trans = __unlink_start_trans(dir, dentry);
3153 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003154 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003155
Chris Mason12fcfd22009-03-24 10:24:20 -04003156 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3157
Chris Masone02119d2008-09-05 16:13:11 -04003158 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3159 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003160 if (ret)
3161 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003162
Yan, Zhenga22285a2010-05-16 10:48:46 -04003163 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003164 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003165 if (ret)
3166 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003167 }
Josef Bacik7b128762008-07-24 12:17:14 -04003168
Tsutomu Itohb5324022011-07-19 07:27:20 +00003169out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003170 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003171 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003172 return ret;
3173}
3174
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003175int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3176 struct btrfs_root *root,
3177 struct inode *dir, u64 objectid,
3178 const char *name, int name_len)
3179{
3180 struct btrfs_path *path;
3181 struct extent_buffer *leaf;
3182 struct btrfs_dir_item *di;
3183 struct btrfs_key key;
3184 u64 index;
3185 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003186 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003187
3188 path = btrfs_alloc_path();
3189 if (!path)
3190 return -ENOMEM;
3191
Li Zefan33345d012011-04-20 10:31:50 +08003192 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003193 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003194 if (IS_ERR_OR_NULL(di)) {
3195 if (!di)
3196 ret = -ENOENT;
3197 else
3198 ret = PTR_ERR(di);
3199 goto out;
3200 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003201
3202 leaf = path->nodes[0];
3203 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3204 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3205 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003206 if (ret) {
3207 btrfs_abort_transaction(trans, root, ret);
3208 goto out;
3209 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003210 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003211
3212 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3213 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003214 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003215 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003216 if (ret != -ENOENT) {
3217 btrfs_abort_transaction(trans, root, ret);
3218 goto out;
3219 }
Li Zefan33345d012011-04-20 10:31:50 +08003220 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003221 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003222 if (IS_ERR_OR_NULL(di)) {
3223 if (!di)
3224 ret = -ENOENT;
3225 else
3226 ret = PTR_ERR(di);
3227 btrfs_abort_transaction(trans, root, ret);
3228 goto out;
3229 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003230
3231 leaf = path->nodes[0];
3232 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003233 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003234 index = key.offset;
3235 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003236 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003237
Miao Xie16cdcec2011-04-22 18:12:22 +08003238 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003239 if (ret) {
3240 btrfs_abort_transaction(trans, root, ret);
3241 goto out;
3242 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003243
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003244 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003245 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003246 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003247 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003248 if (ret)
3249 btrfs_abort_transaction(trans, root, ret);
3250out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003251 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003252 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003253}
3254
Chris Mason39279cc2007-06-12 06:35:45 -04003255static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3256{
3257 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003258 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003259 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003260 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003261
David Sterbab3ae2442012-09-13 16:04:34 -06003262 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003263 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003264 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3265 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003266
Yan, Zhenga22285a2010-05-16 10:48:46 -04003267 trans = __unlink_start_trans(dir, dentry);
3268 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003269 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003270
Li Zefan33345d012011-04-20 10:31:50 +08003271 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003272 err = btrfs_unlink_subvol(trans, root, dir,
3273 BTRFS_I(inode)->location.objectid,
3274 dentry->d_name.name,
3275 dentry->d_name.len);
3276 goto out;
3277 }
3278
Josef Bacik7b128762008-07-24 12:17:14 -04003279 err = btrfs_orphan_add(trans, inode);
3280 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003281 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003282
Chris Mason39279cc2007-06-12 06:35:45 -04003283 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003284 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3285 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003286 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003287 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003288out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003289 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003290 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003291
Chris Mason39279cc2007-06-12 06:35:45 -04003292 return err;
3293}
3294
Chris Mason323ac952008-10-01 19:05:46 -04003295/*
Chris Mason39279cc2007-06-12 06:35:45 -04003296 * this can truncate away extent items, csum items and directory items.
3297 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003298 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003299 *
3300 * csum items that cross the new i_size are truncated to the new size
3301 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003302 *
3303 * min_type is the minimum key type to truncate down to. If set to 0, this
3304 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003305 */
Yan, Zheng80825102009-11-12 09:35:36 +00003306int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3307 struct btrfs_root *root,
3308 struct inode *inode,
3309 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003310{
Chris Mason39279cc2007-06-12 06:35:45 -04003311 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003312 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003313 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003314 struct btrfs_key key;
3315 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003316 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003317 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003318 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003319 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003320 u64 mask = root->sectorsize - 1;
3321 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003322 int found_extent;
3323 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003324 int pending_del_nr = 0;
3325 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003326 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003327 int ret;
3328 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003329 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003330
3331 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003332
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003333 path = btrfs_alloc_path();
3334 if (!path)
3335 return -ENOMEM;
3336 path->reada = -1;
3337
Josef Bacik5dc562c2012-08-17 13:14:17 -04003338 /*
3339 * We want to drop from the next block forward in case this new size is
3340 * not block aligned since we will be keeping the last block of the
3341 * extent just the way it is.
3342 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003343 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003344 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003345
Miao Xie16cdcec2011-04-22 18:12:22 +08003346 /*
3347 * This function is also used to drop the items in the log tree before
3348 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3349 * it is used to drop the loged items. So we shouldn't kill the delayed
3350 * items.
3351 */
3352 if (min_type == 0 && root == BTRFS_I(inode)->root)
3353 btrfs_kill_delayed_inode_items(inode);
3354
Li Zefan33345d012011-04-20 10:31:50 +08003355 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003356 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003357 key.type = (u8)-1;
3358
Chris Mason85e21ba2008-01-29 15:11:36 -05003359search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003360 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003361 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003362 if (ret < 0) {
3363 err = ret;
3364 goto out;
3365 }
Chris Masond3977122009-01-05 21:25:51 -05003366
Chris Mason85e21ba2008-01-29 15:11:36 -05003367 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003368 /* there are no items in the tree for us to truncate, we're
3369 * done
3370 */
Yan, Zheng80825102009-11-12 09:35:36 +00003371 if (path->slots[0] == 0)
3372 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003373 path->slots[0]--;
3374 }
3375
Chris Masond3977122009-01-05 21:25:51 -05003376 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003377 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003378 leaf = path->nodes[0];
3379 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3380 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003381
Li Zefan33345d012011-04-20 10:31:50 +08003382 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003383 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003384
Chris Mason85e21ba2008-01-29 15:11:36 -05003385 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003386 break;
3387
Chris Mason5f39d392007-10-15 16:14:19 -04003388 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003389 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003390 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003391 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003392 extent_type = btrfs_file_extent_type(leaf, fi);
3393 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003394 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003395 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003396 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003397 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003398 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003399 }
Yan008630c2007-11-07 13:31:09 -05003400 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003401 }
Yan, Zheng80825102009-11-12 09:35:36 +00003402 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003403 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003404 } else {
3405 if (item_end < new_size)
3406 break;
3407 if (found_key.offset >= new_size)
3408 del_item = 1;
3409 else
3410 del_item = 0;
3411 }
Chris Mason39279cc2007-06-12 06:35:45 -04003412 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003413 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003414 if (found_type != BTRFS_EXTENT_DATA_KEY)
3415 goto delete;
3416
3417 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003418 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003419 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003420 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003421 u64 orig_num_bytes =
3422 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003423 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003424 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003425 extent_num_bytes = extent_num_bytes &
3426 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003427 btrfs_set_file_extent_num_bytes(leaf, fi,
3428 extent_num_bytes);
3429 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003430 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003431 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003432 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003433 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003434 } else {
Chris Masondb945352007-10-15 16:15:53 -04003435 extent_num_bytes =
3436 btrfs_file_extent_disk_num_bytes(leaf,
3437 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003438 extent_offset = found_key.offset -
3439 btrfs_file_extent_offset(leaf, fi);
3440
Chris Mason39279cc2007-06-12 06:35:45 -04003441 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003442 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003443 if (extent_start != 0) {
3444 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003445 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003446 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003447 }
3448 }
Chris Mason90692182008-02-08 13:49:28 -05003449 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003450 /*
3451 * we can't truncate inline items that have had
3452 * special encodings
3453 */
3454 if (!del_item &&
3455 btrfs_file_extent_compression(leaf, fi) == 0 &&
3456 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3457 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003458 u32 size = new_size - found_key.offset;
3459
3460 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003461 inode_sub_bytes(inode, item_end + 1 -
3462 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003463 }
3464 size =
3465 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003466 btrfs_truncate_item(trans, root, path,
3467 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003468 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003469 inode_sub_bytes(inode, item_end + 1 -
3470 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003471 }
Chris Mason39279cc2007-06-12 06:35:45 -04003472 }
Chris Mason179e29e2007-11-01 11:28:41 -04003473delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003474 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003475 if (!pending_del_nr) {
3476 /* no pending yet, add ourselves */
3477 pending_del_slot = path->slots[0];
3478 pending_del_nr = 1;
3479 } else if (pending_del_nr &&
3480 path->slots[0] + 1 == pending_del_slot) {
3481 /* hop on the pending chunk */
3482 pending_del_nr++;
3483 pending_del_slot = path->slots[0];
3484 } else {
Chris Masond3977122009-01-05 21:25:51 -05003485 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003486 }
Chris Mason39279cc2007-06-12 06:35:45 -04003487 } else {
3488 break;
3489 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003490 if (found_extent && (root->ref_cows ||
3491 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003492 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003493 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003494 extent_num_bytes, 0,
3495 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003496 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003497 BUG_ON(ret);
3498 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003499
Yan, Zheng80825102009-11-12 09:35:36 +00003500 if (found_type == BTRFS_INODE_ITEM_KEY)
3501 break;
3502
3503 if (path->slots[0] == 0 ||
3504 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003505 if (pending_del_nr) {
3506 ret = btrfs_del_items(trans, root, path,
3507 pending_del_slot,
3508 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003509 if (ret) {
3510 btrfs_abort_transaction(trans,
3511 root, ret);
3512 goto error;
3513 }
Yan, Zheng80825102009-11-12 09:35:36 +00003514 pending_del_nr = 0;
3515 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003516 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003517 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003518 } else {
3519 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003520 }
Chris Mason39279cc2007-06-12 06:35:45 -04003521 }
Yan, Zheng80825102009-11-12 09:35:36 +00003522out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003523 if (pending_del_nr) {
3524 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3525 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003526 if (ret)
3527 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003528 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003529error:
Chris Mason39279cc2007-06-12 06:35:45 -04003530 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003531 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003532}
3533
Chris Masona52d9a82007-08-27 16:49:44 -04003534/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003535 * btrfs_truncate_page - read, zero a chunk and write a page
3536 * @inode - inode that we're zeroing
3537 * @from - the offset to start zeroing
3538 * @len - the length to zero, 0 to zero the entire range respective to the
3539 * offset
3540 * @front - zero up to the offset instead of from the offset on
3541 *
3542 * This will find the page for the "from" offset and cow the page and zero the
3543 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003544 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003545int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3546 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003547{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003548 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003549 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003550 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3551 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003552 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003553 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003554 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003555 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3556 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3557 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003558 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003559 int ret = 0;
3560 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003561 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003562
Josef Bacik2aaa6652012-08-29 14:27:18 -04003563 if ((offset & (blocksize - 1)) == 0 &&
3564 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003565 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003566 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003567 if (ret)
3568 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003569
Chris Mason211c17f2008-05-15 09:13:45 -04003570again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003571 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003572 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003573 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00003574 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04003575 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003576 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003577
3578 page_start = page_offset(page);
3579 page_end = page_start + PAGE_CACHE_SIZE - 1;
3580
Chris Masona52d9a82007-08-27 16:49:44 -04003581 if (!PageUptodate(page)) {
3582 ret = btrfs_readpage(NULL, page);
3583 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003584 if (page->mapping != mapping) {
3585 unlock_page(page);
3586 page_cache_release(page);
3587 goto again;
3588 }
Chris Masona52d9a82007-08-27 16:49:44 -04003589 if (!PageUptodate(page)) {
3590 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003591 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003592 }
3593 }
Chris Mason211c17f2008-05-15 09:13:45 -04003594 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003595
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003596 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003597 set_page_extent_mapped(page);
3598
3599 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3600 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003601 unlock_extent_cached(io_tree, page_start, page_end,
3602 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003603 unlock_page(page);
3604 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003605 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003606 btrfs_put_ordered_extent(ordered);
3607 goto again;
3608 }
3609
Josef Bacik2ac55d42010-02-03 19:33:23 +00003610 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003611 EXTENT_DIRTY | EXTENT_DELALLOC |
3612 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003613 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003614
Josef Bacik2ac55d42010-02-03 19:33:23 +00003615 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3616 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003617 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003618 unlock_extent_cached(io_tree, page_start, page_end,
3619 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003620 goto out_unlock;
3621 }
3622
Chris Masone6dcd2d2008-07-17 12:53:50 -04003623 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003624 if (!len)
3625 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003626 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003627 if (front)
3628 memset(kaddr, 0, offset);
3629 else
3630 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003631 flush_dcache_page(page);
3632 kunmap(page);
3633 }
Chris Mason247e7432008-07-17 12:53:51 -04003634 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003635 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003636 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3637 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003638
Chris Mason89642222008-07-24 09:41:53 -04003639out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003640 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003641 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003642 unlock_page(page);
3643 page_cache_release(page);
3644out:
3645 return ret;
3646}
3647
Josef Bacik695a0d02011-03-04 15:46:53 -05003648/*
3649 * This function puts in dummy file extents for the area we're creating a hole
3650 * for. So if we are truncating this file to a larger size we need to insert
3651 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3652 * the range between oldsize and size
3653 */
Josef Bacika41ad392011-01-31 15:30:16 -05003654int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003655{
3656 struct btrfs_trans_handle *trans;
3657 struct btrfs_root *root = BTRFS_I(inode)->root;
3658 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003659 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003660 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003661 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003662 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003663 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003664 u64 block_end = (size + mask) & ~mask;
3665 u64 last_byte;
3666 u64 cur_offset;
3667 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003668 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003669
3670 if (size <= hole_start)
3671 return 0;
3672
Yan Zheng9036c102008-10-30 14:19:41 -04003673 while (1) {
3674 struct btrfs_ordered_extent *ordered;
3675 btrfs_wait_ordered_range(inode, hole_start,
3676 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003677 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003678 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003679 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3680 if (!ordered)
3681 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003682 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3683 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003684 btrfs_put_ordered_extent(ordered);
3685 }
3686
Yan Zheng9036c102008-10-30 14:19:41 -04003687 cur_offset = hole_start;
3688 while (1) {
3689 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3690 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003691 if (IS_ERR(em)) {
3692 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00003693 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003694 break;
3695 }
Yan Zheng9036c102008-10-30 14:19:41 -04003696 last_byte = min(extent_map_end(em), block_end);
3697 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003698 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003699 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003700 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003701
Miao Xie36423202011-12-14 20:12:02 -05003702 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003703 if (IS_ERR(trans)) {
3704 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003705 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003706 }
Yan, Zheng80825102009-11-12 09:35:36 +00003707
Josef Bacik5dc562c2012-08-17 13:14:17 -04003708 err = btrfs_drop_extents(trans, root, inode,
3709 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003710 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003711 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003712 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003713 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003714 break;
Miao Xie5b397372011-09-11 10:52:24 -04003715 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003716
Yan Zheng9036c102008-10-30 14:19:41 -04003717 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003718 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003719 0, hole_size, 0, hole_size,
3720 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003721 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003722 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003723 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003724 break;
Miao Xie5b397372011-09-11 10:52:24 -04003725 }
Yan, Zheng80825102009-11-12 09:35:36 +00003726
Josef Bacik5dc562c2012-08-17 13:14:17 -04003727 btrfs_drop_extent_cache(inode, cur_offset,
3728 cur_offset + hole_size - 1, 0);
3729 hole_em = alloc_extent_map();
3730 if (!hole_em) {
3731 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3732 &BTRFS_I(inode)->runtime_flags);
3733 goto next;
3734 }
3735 hole_em->start = cur_offset;
3736 hole_em->len = hole_size;
3737 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003738
Josef Bacik5dc562c2012-08-17 13:14:17 -04003739 hole_em->block_start = EXTENT_MAP_HOLE;
3740 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05003741 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003742 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3743 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3744 hole_em->generation = trans->transid;
3745
3746 while (1) {
3747 write_lock(&em_tree->lock);
3748 err = add_extent_mapping(em_tree, hole_em);
3749 if (!err)
3750 list_move(&hole_em->list,
3751 &em_tree->modified_extents);
3752 write_unlock(&em_tree->lock);
3753 if (err != -EEXIST)
3754 break;
3755 btrfs_drop_extent_cache(inode, cur_offset,
3756 cur_offset +
3757 hole_size - 1, 0);
3758 }
3759 free_extent_map(hole_em);
3760next:
Miao Xie36423202011-12-14 20:12:02 -05003761 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003762 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003763 }
3764 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003765 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003766 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003767 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003768 break;
3769 }
3770
Yan, Zhenga22285a2010-05-16 10:48:46 -04003771 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003772 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3773 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003774 return err;
3775}
3776
Eric Sandeen3972f262013-01-12 02:57:22 +00003777static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00003778{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003779 struct btrfs_root *root = BTRFS_I(inode)->root;
3780 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003781 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00003782 loff_t newsize = attr->ia_size;
3783 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00003784 int ret;
3785
Josef Bacika41ad392011-01-31 15:30:16 -05003786 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003787 return 0;
3788
Eric Sandeen3972f262013-01-12 02:57:22 +00003789 /*
3790 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3791 * special case where we need to update the times despite not having
3792 * these flags set. For all other operations the VFS set these flags
3793 * explicitly if it wants a timestamp update.
3794 */
3795 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
3796 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
3797
Josef Bacika41ad392011-01-31 15:30:16 -05003798 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003799 truncate_pagecache(inode, oldsize, newsize);
3800 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003801 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003802 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003803
Miao Xief4a2f4c2011-12-14 20:12:01 -05003804 trans = btrfs_start_transaction(root, 1);
3805 if (IS_ERR(trans))
3806 return PTR_ERR(trans);
3807
3808 i_size_write(inode, newsize);
3809 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3810 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003811 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003812 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003813
Josef Bacika41ad392011-01-31 15:30:16 -05003814 /*
3815 * We're truncating a file that used to have good data down to
3816 * zero. Make sure it gets into the ordered flush list so that
3817 * any new writes get down to disk quickly.
3818 */
3819 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003820 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3821 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003822
Josef Bacikf3fe8202013-01-07 17:03:21 -05003823 /*
3824 * 1 for the orphan item we're going to add
3825 * 1 for the orphan item deletion.
3826 */
3827 trans = btrfs_start_transaction(root, 2);
3828 if (IS_ERR(trans))
3829 return PTR_ERR(trans);
3830
3831 /*
3832 * We need to do this in case we fail at _any_ point during the
3833 * actual truncate. Once we do the truncate_setsize we could
3834 * invalidate pages which forces any outstanding ordered io to
3835 * be instantly completed which will give us extents that need
3836 * to be truncated. If we fail to get an orphan inode down we
3837 * could have left over extents that were never meant to live,
3838 * so we need to garuntee from this point on that everything
3839 * will be consistent.
3840 */
3841 ret = btrfs_orphan_add(trans, inode);
3842 btrfs_end_transaction(trans, root);
3843 if (ret)
3844 return ret;
3845
Josef Bacika41ad392011-01-31 15:30:16 -05003846 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3847 truncate_setsize(inode, newsize);
3848 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003849 if (ret && inode->i_nlink)
3850 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003851 }
3852
Josef Bacika41ad392011-01-31 15:30:16 -05003853 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003854}
3855
Chris Mason39279cc2007-06-12 06:35:45 -04003856static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3857{
3858 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003859 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003860 int err;
3861
Li Zefanb83cc962010-12-20 16:04:08 +08003862 if (btrfs_root_readonly(root))
3863 return -EROFS;
3864
Chris Mason39279cc2007-06-12 06:35:45 -04003865 err = inode_change_ok(inode, attr);
3866 if (err)
3867 return err;
3868
Chris Mason5a3f23d2009-03-31 13:27:11 -04003869 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00003870 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00003871 if (err)
3872 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003873 }
Yan Zheng9036c102008-10-30 14:19:41 -04003874
Christoph Hellwig10257742010-06-04 11:30:02 +02003875 if (attr->ia_valid) {
3876 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003877 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003878 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003879
Josef Bacik22c44fe2011-11-30 10:45:38 -05003880 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003881 err = btrfs_acl_chmod(inode);
3882 }
3883
Chris Mason39279cc2007-06-12 06:35:45 -04003884 return err;
3885}
Chris Mason61295eb2008-01-14 16:24:38 -05003886
Al Virobd555972010-06-07 11:35:40 -04003887void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003888{
3889 struct btrfs_trans_handle *trans;
3890 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003891 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003892 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003893 int ret;
3894
liubo1abe9b82011-03-24 11:18:59 +00003895 trace_btrfs_inode_evict(inode);
3896
Chris Mason39279cc2007-06-12 06:35:45 -04003897 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003898 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003899 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003900 goto no_delete;
3901
Chris Mason39279cc2007-06-12 06:35:45 -04003902 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003903 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003904 goto no_delete;
3905 }
Al Virobd555972010-06-07 11:35:40 -04003906 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003907 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003908
Yan, Zhengc71bf092009-11-12 09:34:40 +00003909 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003910 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003911 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003912 goto no_delete;
3913 }
3914
Yan, Zheng76dda932009-09-21 16:00:26 -04003915 if (inode->i_nlink > 0) {
3916 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3917 goto no_delete;
3918 }
3919
Miao Xie0e8c36a2012-12-19 06:59:51 +00003920 ret = btrfs_commit_inode_delayed_inode(inode);
3921 if (ret) {
3922 btrfs_orphan_del(NULL, inode);
3923 goto no_delete;
3924 }
3925
Miao Xie66d8f3d2012-09-06 04:02:28 -06003926 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003927 if (!rsv) {
3928 btrfs_orphan_del(NULL, inode);
3929 goto no_delete;
3930 }
Josef Bacik4a338542011-08-29 11:01:31 -04003931 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003932 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003933 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003934
Chris Masondbe674a2008-07-17 12:54:05 -04003935 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003936
Josef Bacik4289a662011-08-05 13:22:24 -04003937 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003938 * This is a bit simpler than btrfs_truncate since we've already
3939 * reserved our space for our orphan item in the unlink, so we just
3940 * need to reserve some slack space in case we add bytes and update
3941 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003942 */
Yan, Zheng80825102009-11-12 09:35:36 +00003943 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00003944 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3945 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00003946
Josef Bacik726c35f2011-09-26 15:46:06 -04003947 /*
3948 * Try and steal from the global reserve since we will
3949 * likely not use this space anyway, we want to try as
3950 * hard as possible to get this to work.
3951 */
3952 if (ret)
3953 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3954
Yan, Zhengd68fc572010-05-16 10:49:58 -04003955 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003956 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003957 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003958 btrfs_orphan_del(NULL, inode);
3959 btrfs_free_block_rsv(root, rsv);
3960 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003961 }
3962
Miao Xie0e8c36a2012-12-19 06:59:51 +00003963 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04003964 if (IS_ERR(trans)) {
3965 btrfs_orphan_del(NULL, inode);
3966 btrfs_free_block_rsv(root, rsv);
3967 goto no_delete;
3968 }
3969
3970 trans->block_rsv = rsv;
3971
Yan, Zhengd68fc572010-05-16 10:49:58 -04003972 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04003973 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00003974 break;
3975
Miao Xie8407aa42012-09-07 01:43:32 -06003976 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003977 btrfs_end_transaction(trans, root);
3978 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00003979 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04003980 }
3981
Josef Bacik4289a662011-08-05 13:22:24 -04003982 btrfs_free_block_rsv(root, rsv);
3983
Yan, Zheng80825102009-11-12 09:35:36 +00003984 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003985 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003986 ret = btrfs_orphan_del(trans, inode);
3987 BUG_ON(ret);
3988 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003989
Josef Bacik4289a662011-08-05 13:22:24 -04003990 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003991 if (!(root == root->fs_info->tree_root ||
3992 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003993 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003994
Chris Mason54aa1f42007-06-22 14:16:25 -04003995 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003996 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003997no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02003998 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003999 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004000}
4001
4002/*
4003 * this returns the key found in the dir entry in the location pointer.
4004 * If no dir entries were found, location->objectid is 0.
4005 */
4006static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4007 struct btrfs_key *location)
4008{
4009 const char *name = dentry->d_name.name;
4010 int namelen = dentry->d_name.len;
4011 struct btrfs_dir_item *di;
4012 struct btrfs_path *path;
4013 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004014 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004015
4016 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004017 if (!path)
4018 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004019
Li Zefan33345d012011-04-20 10:31:50 +08004020 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004021 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004022 if (IS_ERR(di))
4023 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004024
David Sterbac7040052011-04-19 18:00:01 +02004025 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004026 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004027
Chris Mason5f39d392007-10-15 16:14:19 -04004028 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004029out:
Chris Mason39279cc2007-06-12 06:35:45 -04004030 btrfs_free_path(path);
4031 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004032out_err:
4033 location->objectid = 0;
4034 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004035}
4036
4037/*
4038 * when we hit a tree root in a directory, the btrfs part of the inode
4039 * needs to be changed to reflect the root directory of the tree root. This
4040 * is kind of like crossing a mount point.
4041 */
4042static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004043 struct inode *dir,
4044 struct dentry *dentry,
4045 struct btrfs_key *location,
4046 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004047{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004048 struct btrfs_path *path;
4049 struct btrfs_root *new_root;
4050 struct btrfs_root_ref *ref;
4051 struct extent_buffer *leaf;
4052 int ret;
4053 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004054
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004055 path = btrfs_alloc_path();
4056 if (!path) {
4057 err = -ENOMEM;
4058 goto out;
4059 }
Chris Mason39279cc2007-06-12 06:35:45 -04004060
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004061 err = -ENOENT;
4062 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4063 BTRFS_I(dir)->root->root_key.objectid,
4064 location->objectid);
4065 if (ret) {
4066 if (ret < 0)
4067 err = ret;
4068 goto out;
4069 }
Chris Mason39279cc2007-06-12 06:35:45 -04004070
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004071 leaf = path->nodes[0];
4072 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004073 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004074 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4075 goto out;
4076
4077 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4078 (unsigned long)(ref + 1),
4079 dentry->d_name.len);
4080 if (ret)
4081 goto out;
4082
David Sterbab3b4aa72011-04-21 01:20:15 +02004083 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004084
4085 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4086 if (IS_ERR(new_root)) {
4087 err = PTR_ERR(new_root);
4088 goto out;
4089 }
4090
4091 if (btrfs_root_refs(&new_root->root_item) == 0) {
4092 err = -ENOENT;
4093 goto out;
4094 }
4095
4096 *sub_root = new_root;
4097 location->objectid = btrfs_root_dirid(&new_root->root_item);
4098 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004099 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004100 err = 0;
4101out:
4102 btrfs_free_path(path);
4103 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004104}
4105
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004106static void inode_tree_add(struct inode *inode)
4107{
4108 struct btrfs_root *root = BTRFS_I(inode)->root;
4109 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004110 struct rb_node **p;
4111 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004112 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004113again:
4114 p = &root->inode_tree.rb_node;
4115 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004116
Al Viro1d3382cb2010-10-23 15:19:20 -04004117 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004118 return;
4119
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004120 spin_lock(&root->inode_lock);
4121 while (*p) {
4122 parent = *p;
4123 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4124
Li Zefan33345d012011-04-20 10:31:50 +08004125 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004126 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004127 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004128 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004129 else {
4130 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004131 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004132 rb_erase(parent, &root->inode_tree);
4133 RB_CLEAR_NODE(parent);
4134 spin_unlock(&root->inode_lock);
4135 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004136 }
4137 }
4138 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4139 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4140 spin_unlock(&root->inode_lock);
4141}
4142
4143static void inode_tree_del(struct inode *inode)
4144{
4145 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004146 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004147
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004148 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004149 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004150 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004151 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004152 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004153 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004154 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004155
Josef Bacik0af3d002010-06-21 14:48:16 -04004156 /*
4157 * Free space cache has inodes in the tree root, but the tree root has a
4158 * root_refs of 0, so this could end up dropping the tree root as a
4159 * snapshot, so we need the extra !root->fs_info->tree_root check to
4160 * make sure we don't drop it.
4161 */
4162 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4163 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004164 synchronize_srcu(&root->fs_info->subvol_srcu);
4165 spin_lock(&root->inode_lock);
4166 empty = RB_EMPTY_ROOT(&root->inode_tree);
4167 spin_unlock(&root->inode_lock);
4168 if (empty)
4169 btrfs_add_dead_root(root);
4170 }
4171}
4172
Jeff Mahoney143bede2012-03-01 14:56:26 +01004173void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004174{
4175 struct rb_node *node;
4176 struct rb_node *prev;
4177 struct btrfs_inode *entry;
4178 struct inode *inode;
4179 u64 objectid = 0;
4180
4181 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4182
4183 spin_lock(&root->inode_lock);
4184again:
4185 node = root->inode_tree.rb_node;
4186 prev = NULL;
4187 while (node) {
4188 prev = node;
4189 entry = rb_entry(node, struct btrfs_inode, rb_node);
4190
Li Zefan33345d012011-04-20 10:31:50 +08004191 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004192 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004193 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004194 node = node->rb_right;
4195 else
4196 break;
4197 }
4198 if (!node) {
4199 while (prev) {
4200 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004201 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004202 node = prev;
4203 break;
4204 }
4205 prev = rb_next(prev);
4206 }
4207 }
4208 while (node) {
4209 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004210 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004211 inode = igrab(&entry->vfs_inode);
4212 if (inode) {
4213 spin_unlock(&root->inode_lock);
4214 if (atomic_read(&inode->i_count) > 1)
4215 d_prune_aliases(inode);
4216 /*
Al Viro45321ac2010-06-07 13:43:19 -04004217 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004218 * the inode cache when its usage count
4219 * hits zero.
4220 */
4221 iput(inode);
4222 cond_resched();
4223 spin_lock(&root->inode_lock);
4224 goto again;
4225 }
4226
4227 if (cond_resched_lock(&root->inode_lock))
4228 goto again;
4229
4230 node = rb_next(node);
4231 }
4232 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004233}
4234
Chris Masone02119d2008-09-05 16:13:11 -04004235static int btrfs_init_locked_inode(struct inode *inode, void *p)
4236{
4237 struct btrfs_iget_args *args = p;
4238 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004239 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004240 return 0;
4241}
4242
4243static int btrfs_find_actor(struct inode *inode, void *opaque)
4244{
4245 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004246 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004247 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004248}
4249
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004250static struct inode *btrfs_iget_locked(struct super_block *s,
4251 u64 objectid,
4252 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004253{
4254 struct inode *inode;
4255 struct btrfs_iget_args args;
4256 args.ino = objectid;
4257 args.root = root;
4258
4259 inode = iget5_locked(s, objectid, btrfs_find_actor,
4260 btrfs_init_locked_inode,
4261 (void *)&args);
4262 return inode;
4263}
4264
Balaji Rao1a54ef82008-07-21 02:01:04 +05304265/* Get an inode object given its location and corresponding root.
4266 * Returns in *is_new if the inode was read from disk
4267 */
4268struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004269 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304270{
4271 struct inode *inode;
4272
4273 inode = btrfs_iget_locked(s, location->objectid, root);
4274 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004275 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304276
4277 if (inode->i_state & I_NEW) {
4278 BTRFS_I(inode)->root = root;
4279 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4280 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004281 if (!is_bad_inode(inode)) {
4282 inode_tree_add(inode);
4283 unlock_new_inode(inode);
4284 if (new)
4285 *new = 1;
4286 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004287 unlock_new_inode(inode);
4288 iput(inode);
4289 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004290 }
4291 }
4292
Balaji Rao1a54ef82008-07-21 02:01:04 +05304293 return inode;
4294}
4295
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004296static struct inode *new_simple_dir(struct super_block *s,
4297 struct btrfs_key *key,
4298 struct btrfs_root *root)
4299{
4300 struct inode *inode = new_inode(s);
4301
4302 if (!inode)
4303 return ERR_PTR(-ENOMEM);
4304
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004305 BTRFS_I(inode)->root = root;
4306 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004307 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004308
4309 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004310 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004311 inode->i_fop = &simple_dir_operations;
4312 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4313 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4314
4315 return inode;
4316}
4317
Chris Mason3de45862008-11-17 21:02:50 -05004318struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004319{
Chris Masond3977122009-01-05 21:25:51 -05004320 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004321 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004322 struct btrfs_root *sub_root = root;
4323 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004324 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004325 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004326
4327 if (dentry->d_name.len > BTRFS_NAME_LEN)
4328 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004329
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004330 if (unlikely(d_need_lookup(dentry))) {
4331 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4332 kfree(dentry->d_fsdata);
4333 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004334 /* This thing is hashed, drop it for now */
4335 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004336 } else {
4337 ret = btrfs_inode_by_name(dir, dentry, &location);
4338 }
Chris Mason5f39d392007-10-15 16:14:19 -04004339
Chris Mason39279cc2007-06-12 06:35:45 -04004340 if (ret < 0)
4341 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004342
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004343 if (location.objectid == 0)
4344 return NULL;
4345
4346 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004347 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004348 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004349 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004350
4351 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4352
Yan, Zheng76dda932009-09-21 16:00:26 -04004353 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004354 ret = fixup_tree_root_location(root, dir, dentry,
4355 &location, &sub_root);
4356 if (ret < 0) {
4357 if (ret != -ENOENT)
4358 inode = ERR_PTR(ret);
4359 else
4360 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4361 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004362 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004363 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004364 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4365
Julia Lawall34d19ba2011-01-24 19:55:19 +00004366 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004367 down_read(&root->fs_info->cleanup_work_sem);
4368 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004369 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004370 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004371 if (ret)
4372 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004373 }
4374
Chris Mason3de45862008-11-17 21:02:50 -05004375 return inode;
4376}
4377
Nick Pigginfe15ce42011-01-07 17:49:23 +11004378static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004379{
4380 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004381 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004382
Li Zefan848cce02012-02-21 17:04:28 +08004383 if (!inode && !IS_ROOT(dentry))
4384 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004385
Li Zefan848cce02012-02-21 17:04:28 +08004386 if (inode) {
4387 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004388 if (btrfs_root_refs(&root->root_item) == 0)
4389 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004390
4391 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4392 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004393 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004394 return 0;
4395}
4396
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004397static void btrfs_dentry_release(struct dentry *dentry)
4398{
4399 if (dentry->d_fsdata)
4400 kfree(dentry->d_fsdata);
4401}
4402
Chris Mason3de45862008-11-17 21:02:50 -05004403static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004404 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004405{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004406 struct dentry *ret;
4407
4408 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4409 if (unlikely(d_need_lookup(dentry))) {
4410 spin_lock(&dentry->d_lock);
4411 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4412 spin_unlock(&dentry->d_lock);
4413 }
4414 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004415}
4416
Miao Xie16cdcec2011-04-22 18:12:22 +08004417unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004418 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4419};
4420
David Woodhousecbdf5a22008-08-06 19:42:33 +01004421static int btrfs_real_readdir(struct file *filp, void *dirent,
4422 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004423{
Chris Mason6da6aba2007-12-18 16:15:09 -05004424 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004425 struct btrfs_root *root = BTRFS_I(inode)->root;
4426 struct btrfs_item *item;
4427 struct btrfs_dir_item *di;
4428 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004429 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004430 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004431 struct list_head ins_list;
4432 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004433 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004434 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004435 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004436 unsigned char d_type;
4437 int over = 0;
4438 u32 di_cur;
4439 u32 di_total;
4440 u32 di_len;
4441 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004442 char tmp_name[32];
4443 char *name_ptr;
4444 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004445 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004446
4447 /* FIXME, use a real flag for deciding about the key type */
4448 if (root->fs_info->tree_root == root)
4449 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004450
Chris Mason39544012007-12-12 14:38:19 -05004451 /* special case for "." */
4452 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004453 over = filldir(dirent, ".", 1,
4454 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004455 if (over)
4456 return 0;
4457 filp->f_pos = 1;
4458 }
Chris Mason39544012007-12-12 14:38:19 -05004459 /* special case for .., just use the back ref */
4460 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004461 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004462 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004463 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004464 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004465 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004466 filp->f_pos = 2;
4467 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004468 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004469 if (!path)
4470 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004471
Josef Bacik026fd312011-05-13 10:32:11 -04004472 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004473
Miao Xie16cdcec2011-04-22 18:12:22 +08004474 if (key_type == BTRFS_DIR_INDEX_KEY) {
4475 INIT_LIST_HEAD(&ins_list);
4476 INIT_LIST_HEAD(&del_list);
4477 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4478 }
4479
Chris Mason39279cc2007-06-12 06:35:45 -04004480 btrfs_set_key_type(&key, key_type);
4481 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004482 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004483
Chris Mason39279cc2007-06-12 06:35:45 -04004484 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4485 if (ret < 0)
4486 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004487
4488 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004489 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004490 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004491 if (slot >= btrfs_header_nritems(leaf)) {
4492 ret = btrfs_next_leaf(root, path);
4493 if (ret < 0)
4494 goto err;
4495 else if (ret > 0)
4496 break;
4497 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004498 }
Chris Mason3de45862008-11-17 21:02:50 -05004499
Chris Mason5f39d392007-10-15 16:14:19 -04004500 item = btrfs_item_nr(leaf, slot);
4501 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4502
4503 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004504 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004505 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004506 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004507 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004508 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004509 if (key_type == BTRFS_DIR_INDEX_KEY &&
4510 btrfs_should_delete_dir_index(&del_list,
4511 found_key.offset))
4512 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004513
4514 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004515 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004516
Chris Mason39279cc2007-06-12 06:35:45 -04004517 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4518 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004519 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004520
4521 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004522 struct btrfs_key location;
4523
Josef Bacik22a94d42011-03-16 16:47:17 -04004524 if (verify_dir_item(root, leaf, di))
4525 break;
4526
Chris Mason5f39d392007-10-15 16:14:19 -04004527 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004528 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004529 name_ptr = tmp_name;
4530 } else {
4531 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004532 if (!name_ptr) {
4533 ret = -ENOMEM;
4534 goto err;
4535 }
Chris Mason5f39d392007-10-15 16:14:19 -04004536 }
4537 read_extent_buffer(leaf, name_ptr,
4538 (unsigned long)(di + 1), name_len);
4539
4540 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4541 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004542
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004543
Chris Mason3de45862008-11-17 21:02:50 -05004544 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004545 * skip it.
4546 *
4547 * In contrast to old kernels, we insert the snapshot's
4548 * dir item and dir index after it has been created, so
4549 * we won't find a reference to our own snapshot. We
4550 * still keep the following code for backward
4551 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004552 */
4553 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4554 location.objectid == root->root_key.objectid) {
4555 over = 0;
4556 goto skip;
4557 }
Chris Mason5f39d392007-10-15 16:14:19 -04004558 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004559 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004560 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004561
Chris Mason3de45862008-11-17 21:02:50 -05004562skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004563 if (name_ptr != tmp_name)
4564 kfree(name_ptr);
4565
Chris Mason39279cc2007-06-12 06:35:45 -04004566 if (over)
4567 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004568 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004569 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004570 di_cur += di_len;
4571 di = (struct btrfs_dir_item *)((char *)di + di_len);
4572 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004573next:
4574 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004575 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004576
Miao Xie16cdcec2011-04-22 18:12:22 +08004577 if (key_type == BTRFS_DIR_INDEX_KEY) {
4578 if (is_curr)
4579 filp->f_pos++;
4580 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4581 &ins_list);
4582 if (ret)
4583 goto nopos;
4584 }
4585
David Woodhouse49593bf2008-08-17 17:08:36 +01004586 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004587 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004588 /*
4589 * 32-bit glibc will use getdents64, but then strtol -
4590 * so the last number we can serve is this.
4591 */
4592 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004593 else
4594 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004595nopos:
4596 ret = 0;
4597err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004598 if (key_type == BTRFS_DIR_INDEX_KEY)
4599 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004600 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004601 return ret;
4602}
4603
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004604int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004605{
4606 struct btrfs_root *root = BTRFS_I(inode)->root;
4607 struct btrfs_trans_handle *trans;
4608 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004609 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004610
Josef Bacik72ac3c02012-05-23 14:13:11 -04004611 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004612 return 0;
4613
Liu Bo83eea1f2012-07-10 05:28:39 -06004614 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004615 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004616
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004617 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004618 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004619 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004620 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004621 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004622 if (IS_ERR(trans))
4623 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004624 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004625 }
4626 return ret;
4627}
4628
4629/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004630 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004631 * inode changes. But, it is most likely to find the inode in cache.
4632 * FIXME, needs more benchmarking...there are no reasons other than performance
4633 * to keep or drop this code.
4634 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004635int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004636{
4637 struct btrfs_root *root = BTRFS_I(inode)->root;
4638 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004639 int ret;
4640
Josef Bacik72ac3c02012-05-23 14:13:11 -04004641 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004642 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004643
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004644 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004645 if (IS_ERR(trans))
4646 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004647
4648 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004649 if (ret && ret == -ENOSPC) {
4650 /* whoops, lets try again with the full transaction */
4651 btrfs_end_transaction(trans, root);
4652 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004653 if (IS_ERR(trans))
4654 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004655
Chris Mason94b60442010-05-26 11:02:00 -04004656 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004657 }
Chris Mason39279cc2007-06-12 06:35:45 -04004658 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004659 if (BTRFS_I(inode)->delayed_node)
4660 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004661
4662 return ret;
4663}
4664
4665/*
4666 * This is a copy of file_update_time. We need this so we can return error on
4667 * ENOSPC for updating the inode in the case of file write and mmap writes.
4668 */
Josef Bacike41f9412012-03-26 09:46:47 -04004669static int btrfs_update_time(struct inode *inode, struct timespec *now,
4670 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004671{
Alexander Block2bc5565282012-06-15 09:49:33 +02004672 struct btrfs_root *root = BTRFS_I(inode)->root;
4673
4674 if (btrfs_root_readonly(root))
4675 return -EROFS;
4676
Josef Bacike41f9412012-03-26 09:46:47 -04004677 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004678 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004679 if (flags & S_CTIME)
4680 inode->i_ctime = *now;
4681 if (flags & S_MTIME)
4682 inode->i_mtime = *now;
4683 if (flags & S_ATIME)
4684 inode->i_atime = *now;
4685 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004686}
4687
Chris Masond352ac62008-09-29 15:18:18 -04004688/*
4689 * find the highest existing sequence number in a directory
4690 * and then set the in-memory index_cnt variable to reflect
4691 * free sequence numbers
4692 */
Josef Bacikaec74772008-07-24 12:12:38 -04004693static int btrfs_set_inode_index_count(struct inode *inode)
4694{
4695 struct btrfs_root *root = BTRFS_I(inode)->root;
4696 struct btrfs_key key, found_key;
4697 struct btrfs_path *path;
4698 struct extent_buffer *leaf;
4699 int ret;
4700
Li Zefan33345d012011-04-20 10:31:50 +08004701 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004702 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4703 key.offset = (u64)-1;
4704
4705 path = btrfs_alloc_path();
4706 if (!path)
4707 return -ENOMEM;
4708
4709 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4710 if (ret < 0)
4711 goto out;
4712 /* FIXME: we should be able to handle this */
4713 if (ret == 0)
4714 goto out;
4715 ret = 0;
4716
4717 /*
4718 * MAGIC NUMBER EXPLANATION:
4719 * since we search a directory based on f_pos we have to start at 2
4720 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4721 * else has to start at 2
4722 */
4723 if (path->slots[0] == 0) {
4724 BTRFS_I(inode)->index_cnt = 2;
4725 goto out;
4726 }
4727
4728 path->slots[0]--;
4729
4730 leaf = path->nodes[0];
4731 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4732
Li Zefan33345d012011-04-20 10:31:50 +08004733 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004734 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4735 BTRFS_I(inode)->index_cnt = 2;
4736 goto out;
4737 }
4738
4739 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4740out:
4741 btrfs_free_path(path);
4742 return ret;
4743}
4744
Chris Masond352ac62008-09-29 15:18:18 -04004745/*
4746 * helper to find a free sequence number in a given directory. This current
4747 * code is very simple, later versions will do smarter things in the btree
4748 */
Chris Mason3de45862008-11-17 21:02:50 -05004749int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004750{
4751 int ret = 0;
4752
4753 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004754 ret = btrfs_inode_delayed_dir_index_count(dir);
4755 if (ret) {
4756 ret = btrfs_set_inode_index_count(dir);
4757 if (ret)
4758 return ret;
4759 }
Josef Bacikaec74772008-07-24 12:12:38 -04004760 }
4761
Chris Mason00e4e6b2008-08-05 11:18:09 -04004762 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004763 BTRFS_I(dir)->index_cnt++;
4764
4765 return ret;
4766}
4767
Chris Mason39279cc2007-06-12 06:35:45 -04004768static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4769 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004770 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004771 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004772 u64 ref_objectid, u64 objectid,
4773 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004774{
4775 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004776 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004777 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004778 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004779 struct btrfs_inode_ref *ref;
4780 struct btrfs_key key[2];
4781 u32 sizes[2];
4782 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004783 int ret;
4784 int owner;
4785
Chris Mason5f39d392007-10-15 16:14:19 -04004786 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004787 if (!path)
4788 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004789
Chris Mason39279cc2007-06-12 06:35:45 -04004790 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004791 if (!inode) {
4792 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004793 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004794 }
Chris Mason39279cc2007-06-12 06:35:45 -04004795
Li Zefan581bb052011-04-20 10:06:11 +08004796 /*
4797 * we have to initialize this early, so we can reclaim the inode
4798 * number if we fail afterwards in this function.
4799 */
4800 inode->i_ino = objectid;
4801
Josef Bacikaec74772008-07-24 12:12:38 -04004802 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004803 trace_btrfs_inode_request(dir);
4804
Chris Mason3de45862008-11-17 21:02:50 -05004805 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004806 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004807 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004808 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004809 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004810 }
Josef Bacikaec74772008-07-24 12:12:38 -04004811 }
4812 /*
4813 * index_cnt is ignored for everything but a dir,
4814 * btrfs_get_inode_index_count has an explanation for the magic
4815 * number
4816 */
4817 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004818 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004819 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004820 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04004821
Josef Bacik5dc562c2012-08-17 13:14:17 -04004822 /*
4823 * We could have gotten an inode number from somebody who was fsynced
4824 * and then removed in this same transaction, so let's just set full
4825 * sync since it will be a full sync anyway and this will blow away the
4826 * old info in the log.
4827 */
4828 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4829
Al Viro569254b2011-07-24 17:08:40 -04004830 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004831 owner = 0;
4832 else
4833 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004834
4835 key[0].objectid = objectid;
4836 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4837 key[0].offset = 0;
4838
Mark Fashehf1863732012-08-08 11:32:27 -07004839 /*
4840 * Start new inodes with an inode_ref. This is slightly more
4841 * efficient for small numbers of hard links since they will
4842 * be packed into one item. Extended refs will kick in if we
4843 * add more hard links than can fit in the ref item.
4844 */
Chris Mason9c583092008-01-29 15:15:18 -05004845 key[1].objectid = objectid;
4846 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4847 key[1].offset = ref_objectid;
4848
4849 sizes[0] = sizeof(struct btrfs_inode_item);
4850 sizes[1] = name_len + sizeof(*ref);
4851
Chris Masonb9473432009-03-13 11:00:37 -04004852 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004853 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4854 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004855 goto fail;
4856
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004857 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004858 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004859 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004860 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4861 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004862 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4863 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004864 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004865
4866 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4867 struct btrfs_inode_ref);
4868 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004869 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004870 ptr = (unsigned long)(ref + 1);
4871 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4872
Chris Mason5f39d392007-10-15 16:14:19 -04004873 btrfs_mark_buffer_dirty(path->nodes[0]);
4874 btrfs_free_path(path);
4875
Chris Mason39279cc2007-06-12 06:35:45 -04004876 location = &BTRFS_I(inode)->location;
4877 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004878 location->offset = 0;
4879 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4880
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004881 btrfs_inherit_iflags(inode, dir);
4882
Al Viro569254b2011-07-24 17:08:40 -04004883 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004884 if (btrfs_test_opt(root, NODATASUM))
4885 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06004886 if (btrfs_test_opt(root, NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004887 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4888 }
4889
Chris Mason39279cc2007-06-12 06:35:45 -04004890 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004891 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004892
4893 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004894 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004895
Alexander Block8ea05e32012-07-25 17:35:53 +02004896 btrfs_update_root_times(trans, root);
4897
Chris Mason39279cc2007-06-12 06:35:45 -04004898 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004899fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004900 if (dir)
4901 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004902 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004903 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004904 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004905}
4906
4907static inline u8 btrfs_inode_type(struct inode *inode)
4908{
4909 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4910}
4911
Chris Masond352ac62008-09-29 15:18:18 -04004912/*
4913 * utility function to add 'inode' into 'parent_inode' with
4914 * a give name and a given sequence number.
4915 * if 'add_backref' is true, also insert a backref from the
4916 * inode to the parent directory.
4917 */
Chris Masone02119d2008-09-05 16:13:11 -04004918int btrfs_add_link(struct btrfs_trans_handle *trans,
4919 struct inode *parent_inode, struct inode *inode,
4920 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004921{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004922 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004923 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004924 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004925 u64 ino = btrfs_ino(inode);
4926 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004927
Li Zefan33345d012011-04-20 10:31:50 +08004928 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004929 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4930 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004931 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004932 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4933 key.offset = 0;
4934 }
Chris Mason39279cc2007-06-12 06:35:45 -04004935
Li Zefan33345d012011-04-20 10:31:50 +08004936 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004937 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4938 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004939 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004940 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004941 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4942 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004943 }
4944
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004945 /* Nothing to clean up yet */
4946 if (ret)
4947 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004948
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004949 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4950 parent_inode, &key,
4951 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05004952 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004953 goto fail_dir_item;
4954 else if (ret) {
4955 btrfs_abort_transaction(trans, root, ret);
4956 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004957 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004958
4959 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4960 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004961 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004962 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4963 ret = btrfs_update_inode(trans, root, parent_inode);
4964 if (ret)
4965 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004966 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004967
4968fail_dir_item:
4969 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4970 u64 local_index;
4971 int err;
4972 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4973 key.objectid, root->root_key.objectid,
4974 parent_ino, &local_index, name, name_len);
4975
4976 } else if (add_backref) {
4977 u64 local_index;
4978 int err;
4979
4980 err = btrfs_del_inode_ref(trans, root, name, name_len,
4981 ino, parent_ino, &local_index);
4982 }
4983 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004984}
4985
4986static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004987 struct inode *dir, struct dentry *dentry,
4988 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004989{
Josef Bacika1b075d2010-11-19 20:36:11 +00004990 int err = btrfs_add_link(trans, dir, inode,
4991 dentry->d_name.name, dentry->d_name.len,
4992 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004993 if (err > 0)
4994 err = -EEXIST;
4995 return err;
4996}
4997
Josef Bacik618e21d2007-07-11 10:18:17 -04004998static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004999 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005000{
5001 struct btrfs_trans_handle *trans;
5002 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005003 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005004 int err;
5005 int drop_inode = 0;
5006 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005007 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005008
5009 if (!new_valid_dev(rdev))
5010 return -EINVAL;
5011
Josef Bacik9ed74f22009-09-11 16:12:44 -04005012 /*
5013 * 2 for inode item and ref
5014 * 2 for dir items
5015 * 1 for xattr if selinux is on
5016 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005017 trans = btrfs_start_transaction(root, 5);
5018 if (IS_ERR(trans))
5019 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005020
Li Zefan581bb052011-04-20 10:06:11 +08005021 err = btrfs_find_free_ino(root, &objectid);
5022 if (err)
5023 goto out_unlock;
5024
Josef Bacikaec74772008-07-24 12:12:38 -04005025 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005026 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005027 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005028 if (IS_ERR(inode)) {
5029 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005030 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005031 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005032
Eric Paris2a7dba32011-02-01 11:05:39 -05005033 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005034 if (err) {
5035 drop_inode = 1;
5036 goto out_unlock;
5037 }
5038
Filipe Brandenburger2794ed02012-11-30 03:40:08 +00005039 err = btrfs_update_inode(trans, root, inode);
5040 if (err) {
5041 drop_inode = 1;
5042 goto out_unlock;
5043 }
5044
Casey Schauflerad19db72011-12-15 10:09:07 -05005045 /*
5046 * If the active LSM wants to access the inode during
5047 * d_instantiate it needs these. Smack checks to see
5048 * if the filesystem supports xattrs by looking at the
5049 * ops vector.
5050 */
5051
5052 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005053 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005054 if (err)
5055 drop_inode = 1;
5056 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005057 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005058 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005059 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005060 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005061out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005062 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005063 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005064 if (drop_inode) {
5065 inode_dec_link_count(inode);
5066 iput(inode);
5067 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005068 return err;
5069}
5070
Chris Mason39279cc2007-06-12 06:35:45 -04005071static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005072 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005073{
5074 struct btrfs_trans_handle *trans;
5075 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005076 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005077 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005078 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005079 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005080 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005081
Josef Bacik9ed74f22009-09-11 16:12:44 -04005082 /*
5083 * 2 for inode item and ref
5084 * 2 for dir items
5085 * 1 for xattr if selinux is on
5086 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005087 trans = btrfs_start_transaction(root, 5);
5088 if (IS_ERR(trans))
5089 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005090
Li Zefan581bb052011-04-20 10:06:11 +08005091 err = btrfs_find_free_ino(root, &objectid);
5092 if (err)
5093 goto out_unlock;
5094
Josef Bacikaec74772008-07-24 12:12:38 -04005095 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005096 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005097 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005098 if (IS_ERR(inode)) {
5099 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005100 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005101 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005102 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005103
Eric Paris2a7dba32011-02-01 11:05:39 -05005104 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005105 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005106 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005107
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005108 err = btrfs_update_inode(trans, root, inode);
5109 if (err)
5110 goto out_unlock;
5111
Casey Schauflerad19db72011-12-15 10:09:07 -05005112 /*
5113 * If the active LSM wants to access the inode during
5114 * d_instantiate it needs these. Smack checks to see
5115 * if the filesystem supports xattrs by looking at the
5116 * ops vector.
5117 */
5118 inode->i_fop = &btrfs_file_operations;
5119 inode->i_op = &btrfs_file_inode_operations;
5120
Josef Bacika1b075d2010-11-19 20:36:11 +00005121 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005122 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005123 goto out_unlock;
5124
5125 inode->i_mapping->a_ops = &btrfs_aops;
5126 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5127 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5128 d_instantiate(dentry, inode);
5129
Chris Mason39279cc2007-06-12 06:35:45 -04005130out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005131 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005132 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005133 inode_dec_link_count(inode);
5134 iput(inode);
5135 }
Liu Bob53d3f52012-11-14 14:34:34 +00005136 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005137 return err;
5138}
5139
5140static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5141 struct dentry *dentry)
5142{
5143 struct btrfs_trans_handle *trans;
5144 struct btrfs_root *root = BTRFS_I(dir)->root;
5145 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005146 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005147 int err;
5148 int drop_inode = 0;
5149
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005150 /* do not allow sys_link's with other subvols of the same device */
5151 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005152 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005153
Mark Fashehf1863732012-08-08 11:32:27 -07005154 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005155 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005156
Chris Mason3de45862008-11-17 21:02:50 -05005157 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005158 if (err)
5159 goto fail;
5160
Yan, Zhenga22285a2010-05-16 10:48:46 -04005161 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005162 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005163 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005164 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005165 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005166 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005167 if (IS_ERR(trans)) {
5168 err = PTR_ERR(trans);
5169 goto fail;
5170 }
Chris Mason5f39d392007-10-15 16:14:19 -04005171
Miao Xie31534952011-04-13 13:19:21 +08005172 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005173 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005174 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005175 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005176 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005177
Josef Bacika1b075d2010-11-19 20:36:11 +00005178 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005179
Yan, Zhenga5719522009-09-24 09:17:31 -04005180 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005181 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005182 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005183 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005184 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005185 if (err)
5186 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005187 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005188 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005189 }
Chris Mason39279cc2007-06-12 06:35:45 -04005190
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005191 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005192fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005193 if (drop_inode) {
5194 inode_dec_link_count(inode);
5195 iput(inode);
5196 }
Liu Bob53d3f52012-11-14 14:34:34 +00005197 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005198 return err;
5199}
5200
Al Viro18bb1db2011-07-26 01:41:39 -04005201static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005202{
Chris Masonb9d86662008-05-02 16:13:49 -04005203 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005204 struct btrfs_trans_handle *trans;
5205 struct btrfs_root *root = BTRFS_I(dir)->root;
5206 int err = 0;
5207 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005208 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005209 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005210
Josef Bacik9ed74f22009-09-11 16:12:44 -04005211 /*
5212 * 2 items for inode and ref
5213 * 2 items for dir items
5214 * 1 for xattr if selinux is on
5215 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005216 trans = btrfs_start_transaction(root, 5);
5217 if (IS_ERR(trans))
5218 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005219
Li Zefan581bb052011-04-20 10:06:11 +08005220 err = btrfs_find_free_ino(root, &objectid);
5221 if (err)
5222 goto out_fail;
5223
Josef Bacikaec74772008-07-24 12:12:38 -04005224 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005225 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005226 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005227 if (IS_ERR(inode)) {
5228 err = PTR_ERR(inode);
5229 goto out_fail;
5230 }
Chris Mason5f39d392007-10-15 16:14:19 -04005231
Chris Mason39279cc2007-06-12 06:35:45 -04005232 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005233
Eric Paris2a7dba32011-02-01 11:05:39 -05005234 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005235 if (err)
5236 goto out_fail;
5237
Chris Mason39279cc2007-06-12 06:35:45 -04005238 inode->i_op = &btrfs_dir_inode_operations;
5239 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005240
Chris Masondbe674a2008-07-17 12:54:05 -04005241 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005242 err = btrfs_update_inode(trans, root, inode);
5243 if (err)
5244 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005245
Josef Bacika1b075d2010-11-19 20:36:11 +00005246 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5247 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005248 if (err)
5249 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005250
Chris Mason39279cc2007-06-12 06:35:45 -04005251 d_instantiate(dentry, inode);
5252 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005253
5254out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005255 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005256 if (drop_on_err)
5257 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005258 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005259 return err;
5260}
5261
Chris Masond352ac62008-09-29 15:18:18 -04005262/* helper for btfs_get_extent. Given an existing extent in the tree,
5263 * and an extent that you want to insert, deal with overlap and insert
5264 * the new extent into the tree.
5265 */
Chris Mason3b951512008-04-17 11:29:12 -04005266static int merge_extent_mapping(struct extent_map_tree *em_tree,
5267 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005268 struct extent_map *em,
5269 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005270{
5271 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005272
Chris Masone6dcd2d2008-07-17 12:53:50 -04005273 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5274 start_diff = map_start - em->start;
5275 em->start = map_start;
5276 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005277 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5278 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005279 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005280 em->block_len -= start_diff;
5281 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005282 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005283}
5284
Chris Masonc8b97812008-10-29 14:49:59 -04005285static noinline int uncompress_inline(struct btrfs_path *path,
5286 struct inode *inode, struct page *page,
5287 size_t pg_offset, u64 extent_offset,
5288 struct btrfs_file_extent_item *item)
5289{
5290 int ret;
5291 struct extent_buffer *leaf = path->nodes[0];
5292 char *tmp;
5293 size_t max_size;
5294 unsigned long inline_size;
5295 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005296 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005297
5298 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005299 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005300 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5301 inline_size = btrfs_file_extent_inline_item_len(leaf,
5302 btrfs_item_nr(leaf, path->slots[0]));
5303 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005304 if (!tmp)
5305 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005306 ptr = btrfs_file_extent_inline_start(item);
5307
5308 read_extent_buffer(leaf, tmp, ptr, inline_size);
5309
Chris Mason5b050f02008-11-11 09:34:41 -05005310 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005311 ret = btrfs_decompress(compress_type, tmp, page,
5312 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005313 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005314 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005315 unsigned long copy_size = min_t(u64,
5316 PAGE_CACHE_SIZE - pg_offset,
5317 max_size - extent_offset);
5318 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005319 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005320 }
5321 kfree(tmp);
5322 return 0;
5323}
5324
Chris Masond352ac62008-09-29 15:18:18 -04005325/*
5326 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005327 * the ugly parts come from merging extents from the disk with the in-ram
5328 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005329 * where the in-ram extents might be locked pending data=ordered completion.
5330 *
5331 * This also copies inline extents directly into the page.
5332 */
Chris Masond3977122009-01-05 21:25:51 -05005333
Chris Masona52d9a82007-08-27 16:49:44 -04005334struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005335 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005336 int create)
5337{
5338 int ret;
5339 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005340 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005341 u64 extent_start = 0;
5342 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005343 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005344 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005345 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005346 struct btrfs_root *root = BTRFS_I(inode)->root;
5347 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005348 struct extent_buffer *leaf;
5349 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005350 struct extent_map *em = NULL;
5351 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005352 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005353 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005354 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005355
Chris Masona52d9a82007-08-27 16:49:44 -04005356again:
Chris Mason890871b2009-09-02 16:24:52 -04005357 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005358 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005359 if (em)
5360 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005361 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005362
Chris Masona52d9a82007-08-27 16:49:44 -04005363 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005364 if (em->start > start || em->start + em->len <= start)
5365 free_extent_map(em);
5366 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005367 free_extent_map(em);
5368 else
5369 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005370 }
David Sterba172ddd62011-04-21 00:48:27 +02005371 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005372 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005373 err = -ENOMEM;
5374 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005375 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005376 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005377 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005378 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005379 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005380 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005381
5382 if (!path) {
5383 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005384 if (!path) {
5385 err = -ENOMEM;
5386 goto out;
5387 }
5388 /*
5389 * Chances are we'll be called again, so go ahead and do
5390 * readahead
5391 */
5392 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005393 }
5394
Chris Mason179e29e2007-11-01 11:28:41 -04005395 ret = btrfs_lookup_file_extent(trans, root, path,
5396 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005397 if (ret < 0) {
5398 err = ret;
5399 goto out;
5400 }
5401
5402 if (ret != 0) {
5403 if (path->slots[0] == 0)
5404 goto not_found;
5405 path->slots[0]--;
5406 }
5407
Chris Mason5f39d392007-10-15 16:14:19 -04005408 leaf = path->nodes[0];
5409 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005410 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005411 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005412 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5413 found_type = btrfs_key_type(&found_key);
5414 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005415 found_type != BTRFS_EXTENT_DATA_KEY) {
5416 goto not_found;
5417 }
5418
Chris Mason5f39d392007-10-15 16:14:19 -04005419 found_type = btrfs_file_extent_type(leaf, item);
5420 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005421 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005422 if (found_type == BTRFS_FILE_EXTENT_REG ||
5423 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005424 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005425 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005426 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5427 size_t size;
5428 size = btrfs_file_extent_inline_len(leaf, item);
5429 extent_end = (extent_start + size + root->sectorsize - 1) &
5430 ~((u64)root->sectorsize - 1);
5431 }
5432
5433 if (start >= extent_end) {
5434 path->slots[0]++;
5435 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5436 ret = btrfs_next_leaf(root, path);
5437 if (ret < 0) {
5438 err = ret;
5439 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005440 }
Yan Zheng9036c102008-10-30 14:19:41 -04005441 if (ret > 0)
5442 goto not_found;
5443 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005444 }
Yan Zheng9036c102008-10-30 14:19:41 -04005445 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5446 if (found_key.objectid != objectid ||
5447 found_key.type != BTRFS_EXTENT_DATA_KEY)
5448 goto not_found;
5449 if (start + len <= found_key.offset)
5450 goto not_found;
5451 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005452 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04005453 em->len = found_key.offset - start;
5454 goto not_found_em;
5455 }
5456
Yan Zhengd899e052008-10-30 14:25:28 -04005457 if (found_type == BTRFS_FILE_EXTENT_REG ||
5458 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005459 em->start = extent_start;
5460 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005461 em->orig_start = extent_start -
5462 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05005463 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5464 item);
Chris Masondb945352007-10-15 16:15:53 -04005465 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5466 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005467 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005468 goto insert;
5469 }
Li Zefan261507a02010-12-17 14:21:50 +08005470 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005471 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005472 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005473 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05005474 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005475 } else {
5476 bytenr += btrfs_file_extent_offset(leaf, item);
5477 em->block_start = bytenr;
5478 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005479 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5480 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005481 }
Chris Masona52d9a82007-08-27 16:49:44 -04005482 goto insert;
5483 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005484 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005485 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005486 size_t size;
5487 size_t extent_offset;
5488 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005489
Chris Masona52d9a82007-08-27 16:49:44 -04005490 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005491 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005492 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005493 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005494 goto out;
5495 }
5496
Yan Zheng9036c102008-10-30 14:19:41 -04005497 size = btrfs_file_extent_inline_len(leaf, item);
5498 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005499 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005500 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005501 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005502 em->len = (copy_size + root->sectorsize - 1) &
5503 ~((u64)root->sectorsize - 1);
Josef Bacikb4939682012-12-03 10:31:19 -05005504 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005505 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08005506 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005507 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005508 em->compress_type = compress_type;
5509 }
Yan689f9342007-10-29 11:41:07 -04005510 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005511 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005512 if (btrfs_file_extent_compression(leaf, item) !=
5513 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005514 ret = uncompress_inline(path, inode, page,
5515 pg_offset,
5516 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005517 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005518 } else {
5519 map = kmap(page);
5520 read_extent_buffer(leaf, map + pg_offset, ptr,
5521 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005522 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5523 memset(map + pg_offset + copy_size, 0,
5524 PAGE_CACHE_SIZE - pg_offset -
5525 copy_size);
5526 }
Chris Masonc8b97812008-10-29 14:49:59 -04005527 kunmap(page);
5528 }
Chris Mason179e29e2007-11-01 11:28:41 -04005529 flush_dcache_page(page);
5530 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005531 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005532 if (!trans) {
5533 kunmap(page);
5534 free_extent_map(em);
5535 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005536
David Sterbab3b4aa72011-04-21 01:20:15 +02005537 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005538 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005539
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005540 if (IS_ERR(trans))
5541 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005542 goto again;
5543 }
Chris Masonc8b97812008-10-29 14:49:59 -04005544 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005545 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005546 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005547 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005548 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005549 }
Chris Masond1310b22008-01-24 16:13:08 -05005550 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005551 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005552 goto insert;
5553 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005554 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005555 }
5556not_found:
5557 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005558 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005559 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005560not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005561 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005562 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005563insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005564 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005565 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005566 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5567 "[%llu %llu]\n", (unsigned long long)em->start,
5568 (unsigned long long)em->len,
5569 (unsigned long long)start,
5570 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005571 err = -EIO;
5572 goto out;
5573 }
Chris Masond1310b22008-01-24 16:13:08 -05005574
5575 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005576 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005577 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005578 /* it is possible that someone inserted the extent into the tree
5579 * while we had the lock dropped. It is also possible that
5580 * an overlapping map exists in the tree
5581 */
Chris Masona52d9a82007-08-27 16:49:44 -04005582 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005583 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005584
5585 ret = 0;
5586
Chris Mason3b951512008-04-17 11:29:12 -04005587 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005588 if (existing && (existing->start > start ||
5589 existing->start + existing->len <= start)) {
5590 free_extent_map(existing);
5591 existing = NULL;
5592 }
Chris Mason3b951512008-04-17 11:29:12 -04005593 if (!existing) {
5594 existing = lookup_extent_mapping(em_tree, em->start,
5595 em->len);
5596 if (existing) {
5597 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005598 em, start,
5599 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005600 free_extent_map(existing);
5601 if (err) {
5602 free_extent_map(em);
5603 em = NULL;
5604 }
5605 } else {
5606 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005607 free_extent_map(em);
5608 em = NULL;
5609 }
5610 } else {
5611 free_extent_map(em);
5612 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005613 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005614 }
Chris Masona52d9a82007-08-27 16:49:44 -04005615 }
Chris Mason890871b2009-09-02 16:24:52 -04005616 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005617out:
liubo1abe9b82011-03-24 11:18:59 +00005618
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005619 if (em)
5620 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005621
Chris Masonf4219502008-07-22 11:18:09 -04005622 if (path)
5623 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005624 if (trans) {
5625 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005626 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005627 err = ret;
5628 }
Chris Masona52d9a82007-08-27 16:49:44 -04005629 if (err) {
5630 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005631 return ERR_PTR(err);
5632 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005633 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005634 return em;
5635}
5636
Chris Masonec29ed52011-02-23 16:23:20 -05005637struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5638 size_t pg_offset, u64 start, u64 len,
5639 int create)
5640{
5641 struct extent_map *em;
5642 struct extent_map *hole_em = NULL;
5643 u64 range_start = start;
5644 u64 end;
5645 u64 found;
5646 u64 found_end;
5647 int err = 0;
5648
5649 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5650 if (IS_ERR(em))
5651 return em;
5652 if (em) {
5653 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00005654 * if our em maps to
5655 * - a hole or
5656 * - a pre-alloc extent,
5657 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05005658 */
Liu Bof9e4fb52013-01-07 10:10:12 +00005659 if (em->block_start != EXTENT_MAP_HOLE &&
5660 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05005661 return em;
5662 else
5663 hole_em = em;
5664 }
5665
5666 /* check to see if we've wrapped (len == -1 or similar) */
5667 end = start + len;
5668 if (end < start)
5669 end = (u64)-1;
5670 else
5671 end -= 1;
5672
5673 em = NULL;
5674
5675 /* ok, we didn't find anything, lets look for delalloc */
5676 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5677 end, len, EXTENT_DELALLOC, 1);
5678 found_end = range_start + found;
5679 if (found_end < range_start)
5680 found_end = (u64)-1;
5681
5682 /*
5683 * we didn't find anything useful, return
5684 * the original results from get_extent()
5685 */
5686 if (range_start > end || found_end <= start) {
5687 em = hole_em;
5688 hole_em = NULL;
5689 goto out;
5690 }
5691
5692 /* adjust the range_start to make sure it doesn't
5693 * go backwards from the start they passed in
5694 */
5695 range_start = max(start,range_start);
5696 found = found_end - range_start;
5697
5698 if (found > 0) {
5699 u64 hole_start = start;
5700 u64 hole_len = len;
5701
David Sterba172ddd62011-04-21 00:48:27 +02005702 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005703 if (!em) {
5704 err = -ENOMEM;
5705 goto out;
5706 }
5707 /*
5708 * when btrfs_get_extent can't find anything it
5709 * returns one huge hole
5710 *
5711 * make sure what it found really fits our range, and
5712 * adjust to make sure it is based on the start from
5713 * the caller
5714 */
5715 if (hole_em) {
5716 u64 calc_end = extent_map_end(hole_em);
5717
5718 if (calc_end <= start || (hole_em->start > end)) {
5719 free_extent_map(hole_em);
5720 hole_em = NULL;
5721 } else {
5722 hole_start = max(hole_em->start, start);
5723 hole_len = calc_end - hole_start;
5724 }
5725 }
5726 em->bdev = NULL;
5727 if (hole_em && range_start > hole_start) {
5728 /* our hole starts before our delalloc, so we
5729 * have to return just the parts of the hole
5730 * that go until the delalloc starts
5731 */
5732 em->len = min(hole_len,
5733 range_start - hole_start);
5734 em->start = hole_start;
5735 em->orig_start = hole_start;
5736 /*
5737 * don't adjust block start at all,
5738 * it is fixed at EXTENT_MAP_HOLE
5739 */
5740 em->block_start = hole_em->block_start;
5741 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00005742 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
5743 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05005744 } else {
5745 em->start = range_start;
5746 em->len = found;
5747 em->orig_start = range_start;
5748 em->block_start = EXTENT_MAP_DELALLOC;
5749 em->block_len = found;
5750 }
5751 } else if (hole_em) {
5752 return hole_em;
5753 }
5754out:
5755
5756 free_extent_map(hole_em);
5757 if (err) {
5758 free_extent_map(em);
5759 return ERR_PTR(err);
5760 }
5761 return em;
5762}
5763
Josef Bacik4b46fce2010-05-23 11:00:55 -04005764static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5765 u64 start, u64 len)
5766{
5767 struct btrfs_root *root = BTRFS_I(inode)->root;
5768 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04005769 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005770 struct btrfs_key ins;
5771 u64 alloc_hint;
5772 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005773
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005774 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005775 if (IS_ERR(trans))
5776 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005777
5778 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5779
5780 alloc_hint = get_extent_allocation_hint(inode, start, len);
5781 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005782 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005783 if (ret) {
5784 em = ERR_PTR(ret);
5785 goto out;
5786 }
5787
Josef Bacik70c8a912012-10-11 16:54:30 -04005788 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
5789 ins.offset, ins.offset, 0);
5790 if (IS_ERR(em))
5791 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005792
5793 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5794 ins.offset, ins.offset, 0);
5795 if (ret) {
5796 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5797 em = ERR_PTR(ret);
5798 }
5799out:
5800 btrfs_end_transaction(trans, root);
5801 return em;
5802}
5803
Chris Mason46bfbb52010-05-26 11:04:10 -04005804/*
5805 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5806 * block must be cow'd
5807 */
5808static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5809 struct inode *inode, u64 offset, u64 len)
5810{
5811 struct btrfs_path *path;
5812 int ret;
5813 struct extent_buffer *leaf;
5814 struct btrfs_root *root = BTRFS_I(inode)->root;
5815 struct btrfs_file_extent_item *fi;
5816 struct btrfs_key key;
5817 u64 disk_bytenr;
5818 u64 backref_offset;
5819 u64 extent_end;
5820 u64 num_bytes;
5821 int slot;
5822 int found_type;
5823
5824 path = btrfs_alloc_path();
5825 if (!path)
5826 return -ENOMEM;
5827
Li Zefan33345d012011-04-20 10:31:50 +08005828 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005829 offset, 0);
5830 if (ret < 0)
5831 goto out;
5832
5833 slot = path->slots[0];
5834 if (ret == 1) {
5835 if (slot == 0) {
5836 /* can't find the item, must cow */
5837 ret = 0;
5838 goto out;
5839 }
5840 slot--;
5841 }
5842 ret = 0;
5843 leaf = path->nodes[0];
5844 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005845 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005846 key.type != BTRFS_EXTENT_DATA_KEY) {
5847 /* not our file or wrong item type, must cow */
5848 goto out;
5849 }
5850
5851 if (key.offset > offset) {
5852 /* Wrong offset, must cow */
5853 goto out;
5854 }
5855
5856 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5857 found_type = btrfs_file_extent_type(leaf, fi);
5858 if (found_type != BTRFS_FILE_EXTENT_REG &&
5859 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5860 /* not a regular extent, must cow */
5861 goto out;
5862 }
5863 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5864 backref_offset = btrfs_file_extent_offset(leaf, fi);
5865
5866 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5867 if (extent_end < offset + len) {
5868 /* extent doesn't include our full range, must cow */
5869 goto out;
5870 }
5871
5872 if (btrfs_extent_readonly(root, disk_bytenr))
5873 goto out;
5874
5875 /*
5876 * look for other files referencing this extent, if we
5877 * find any we must cow
5878 */
Li Zefan33345d012011-04-20 10:31:50 +08005879 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005880 key.offset - backref_offset, disk_bytenr))
5881 goto out;
5882
5883 /*
5884 * adjust disk_bytenr and num_bytes to cover just the bytes
5885 * in this extent we are about to write. If there
5886 * are any csums in that range we have to cow in order
5887 * to keep the csums correct
5888 */
5889 disk_bytenr += backref_offset;
5890 disk_bytenr += offset - key.offset;
5891 num_bytes = min(offset + len, extent_end) - offset;
5892 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5893 goto out;
5894 /*
5895 * all of the above have passed, it is safe to overwrite this extent
5896 * without cow
5897 */
5898 ret = 1;
5899out:
5900 btrfs_free_path(path);
5901 return ret;
5902}
5903
Josef Bacikeb838e72012-07-31 16:28:48 -04005904static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5905 struct extent_state **cached_state, int writing)
5906{
5907 struct btrfs_ordered_extent *ordered;
5908 int ret = 0;
5909
5910 while (1) {
5911 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5912 0, cached_state);
5913 /*
5914 * We're concerned with the entire range that we're going to be
5915 * doing DIO to, so we need to make sure theres no ordered
5916 * extents in this range.
5917 */
5918 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5919 lockend - lockstart + 1);
5920
5921 /*
5922 * We need to make sure there are no buffered pages in this
5923 * range either, we could have raced between the invalidate in
5924 * generic_file_direct_write and locking the extent. The
5925 * invalidate needs to happen so that reads after a write do not
5926 * get stale data.
5927 */
5928 if (!ordered && (!writing ||
5929 !test_range_bit(&BTRFS_I(inode)->io_tree,
5930 lockstart, lockend, EXTENT_UPTODATE, 0,
5931 *cached_state)))
5932 break;
5933
5934 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5935 cached_state, GFP_NOFS);
5936
5937 if (ordered) {
5938 btrfs_start_ordered_extent(inode, ordered, 1);
5939 btrfs_put_ordered_extent(ordered);
5940 } else {
5941 /* Screw you mmap */
5942 ret = filemap_write_and_wait_range(inode->i_mapping,
5943 lockstart,
5944 lockend);
5945 if (ret)
5946 break;
5947
5948 /*
5949 * If we found a page that couldn't be invalidated just
5950 * fall back to buffered.
5951 */
5952 ret = invalidate_inode_pages2_range(inode->i_mapping,
5953 lockstart >> PAGE_CACHE_SHIFT,
5954 lockend >> PAGE_CACHE_SHIFT);
5955 if (ret)
5956 break;
5957 }
5958
5959 cond_resched();
5960 }
5961
5962 return ret;
5963}
5964
Josef Bacik69ffb542012-09-11 15:40:07 -04005965static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5966 u64 len, u64 orig_start,
5967 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05005968 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04005969{
5970 struct extent_map_tree *em_tree;
5971 struct extent_map *em;
5972 struct btrfs_root *root = BTRFS_I(inode)->root;
5973 int ret;
5974
5975 em_tree = &BTRFS_I(inode)->extent_tree;
5976 em = alloc_extent_map();
5977 if (!em)
5978 return ERR_PTR(-ENOMEM);
5979
5980 em->start = start;
5981 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04005982 em->mod_start = start;
5983 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04005984 em->len = len;
5985 em->block_len = block_len;
5986 em->block_start = block_start;
5987 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05005988 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005989 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04005990 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5991 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05005992 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04005993
5994 do {
5995 btrfs_drop_extent_cache(inode, em->start,
5996 em->start + em->len - 1, 0);
5997 write_lock(&em_tree->lock);
5998 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04005999 if (!ret)
6000 list_move(&em->list,
6001 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006002 write_unlock(&em_tree->lock);
6003 } while (ret == -EEXIST);
6004
6005 if (ret) {
6006 free_extent_map(em);
6007 return ERR_PTR(ret);
6008 }
6009
6010 return em;
6011}
6012
6013
Josef Bacik4b46fce2010-05-23 11:00:55 -04006014static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6015 struct buffer_head *bh_result, int create)
6016{
6017 struct extent_map *em;
6018 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006019 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006020 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006021 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006022 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006023 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006024 int unlock_bits = EXTENT_LOCKED;
6025 int ret;
6026
Josef Bacikeb838e72012-07-31 16:28:48 -04006027 if (create) {
6028 ret = btrfs_delalloc_reserve_space(inode, len);
6029 if (ret)
6030 return ret;
6031 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04006032 } else {
6033 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006034 }
6035
Josef Bacikc3298612012-08-03 16:49:19 -04006036 lockstart = start;
6037 lockend = start + len - 1;
6038
Josef Bacikeb838e72012-07-31 16:28:48 -04006039 /*
6040 * If this errors out it's because we couldn't invalidate pagecache for
6041 * this range and we need to fallback to buffered.
6042 */
6043 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6044 return -ENOTBLK;
6045
6046 if (create) {
6047 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6048 lockend, EXTENT_DELALLOC, NULL,
6049 &cached_state, GFP_NOFS);
6050 if (ret)
6051 goto unlock_err;
6052 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006053
6054 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006055 if (IS_ERR(em)) {
6056 ret = PTR_ERR(em);
6057 goto unlock_err;
6058 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006059
6060 /*
6061 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6062 * io. INLINE is special, and we could probably kludge it in here, but
6063 * it's still buffered so for safety lets just fall back to the generic
6064 * buffered path.
6065 *
6066 * For COMPRESSED we _have_ to read the entire extent in so we can
6067 * decompress it, so there will be buffering required no matter what we
6068 * do, so go ahead and fallback to buffered.
6069 *
6070 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6071 * to buffered IO. Don't blame me, this is the price we pay for using
6072 * the generic code.
6073 */
6074 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6075 em->block_start == EXTENT_MAP_INLINE) {
6076 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006077 ret = -ENOTBLK;
6078 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006079 }
6080
6081 /* Just a good old fashioned hole, return */
6082 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6083 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6084 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006085 ret = 0;
6086 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006087 }
6088
6089 /*
6090 * We don't allocate a new extent in the following cases
6091 *
6092 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6093 * existing extent.
6094 * 2) The extent is marked as PREALLOC. We're good to go here and can
6095 * just use the extent.
6096 *
6097 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006098 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006099 len = min(len, em->len - (start - em->start));
6100 lockstart = start + len;
6101 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006102 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006103
6104 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6105 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6106 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006107 int type;
6108 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006109 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006110
6111 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6112 type = BTRFS_ORDERED_PREALLOC;
6113 else
6114 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006115 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006116 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006117
6118 /*
6119 * we're not going to log anything, but we do need
6120 * to make sure the current transaction stays open
6121 * while we look for nocow cross refs
6122 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006123 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006124 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006125 goto must_cow;
6126
6127 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006128 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006129 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006130
6131 if (type == BTRFS_ORDERED_PREALLOC) {
6132 free_extent_map(em);
6133 em = create_pinned_em(inode, start, len,
6134 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006135 block_start, len,
6136 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006137 if (IS_ERR(em)) {
6138 btrfs_end_transaction(trans, root);
6139 goto unlock_err;
6140 }
6141 }
6142
Chris Mason46bfbb52010-05-26 11:04:10 -04006143 ret = btrfs_add_ordered_extent_dio(inode, start,
6144 block_start, len, len, type);
6145 btrfs_end_transaction(trans, root);
6146 if (ret) {
6147 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006148 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006149 }
6150 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006151 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006152 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006153 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006154must_cow:
6155 /*
6156 * this will cow the extent, reset the len in case we changed
6157 * it above
6158 */
6159 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006160 free_extent_map(em);
6161 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006162 if (IS_ERR(em)) {
6163 ret = PTR_ERR(em);
6164 goto unlock_err;
6165 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006166 len = min(len, em->len - (start - em->start));
6167unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006168 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6169 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006170 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006171 bh_result->b_bdev = em->bdev;
6172 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006173 if (create) {
6174 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6175 set_buffer_new(bh_result);
6176
6177 /*
6178 * Need to update the i_size under the extent lock so buffered
6179 * readers will get the updated i_size when we unlock.
6180 */
6181 if (start + len > i_size_read(inode))
6182 i_size_write(inode, start + len);
6183 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006184
Josef Bacikeb838e72012-07-31 16:28:48 -04006185 /*
6186 * In the case of write we need to clear and unlock the entire range,
6187 * in the case of read we need to unlock only the end area that we
6188 * aren't using if there is any left over space.
6189 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006190 if (lockstart < lockend) {
6191 if (create && len < lockend - lockstart) {
6192 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006193 lockstart + len - 1,
6194 unlock_bits | EXTENT_DEFRAG, 1, 0,
Liu Bo24c03fa2012-08-22 20:10:38 -06006195 &cached_state, GFP_NOFS);
6196 /*
6197 * Beside unlock, we also need to cleanup reserved space
6198 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6199 */
6200 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6201 lockstart + len, lockend,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006202 unlock_bits | EXTENT_DO_ACCOUNTING |
6203 EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006204 } else {
6205 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6206 lockend, unlock_bits, 1, 0,
6207 &cached_state, GFP_NOFS);
6208 }
6209 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006210 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006211 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006212
Josef Bacik4b46fce2010-05-23 11:00:55 -04006213 free_extent_map(em);
6214
6215 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006216
6217unlock_err:
6218 if (create)
6219 unlock_bits |= EXTENT_DO_ACCOUNTING;
6220
6221 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6222 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6223 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006224}
6225
6226struct btrfs_dio_private {
6227 struct inode *inode;
6228 u64 logical_offset;
6229 u64 disk_bytenr;
6230 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006231 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006232
6233 /* number of bios pending for this dio */
6234 atomic_t pending_bios;
6235
6236 /* IO errors */
6237 int errors;
6238
6239 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006240};
6241
6242static void btrfs_endio_direct_read(struct bio *bio, int err)
6243{
Miao Xiee65e1532010-11-22 03:04:43 +00006244 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006245 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6246 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006247 struct inode *inode = dip->inode;
6248 struct btrfs_root *root = BTRFS_I(inode)->root;
6249 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006250
6251 start = dip->logical_offset;
6252 do {
6253 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6254 struct page *page = bvec->bv_page;
6255 char *kaddr;
6256 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006257 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006258 unsigned long flags;
6259
Josef Bacikc3298612012-08-03 16:49:19 -04006260 if (get_state_private(&BTRFS_I(inode)->io_tree,
6261 start, &private))
6262 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006263 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006264 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006265 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6266 csum, bvec->bv_len);
6267 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006268 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006269 local_irq_restore(flags);
6270
6271 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006272 if (csum != private) {
6273failed:
Li Zefan33345d012011-04-20 10:31:50 +08006274 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006275 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006276 (unsigned long long)btrfs_ino(inode),
6277 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006278 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006279 err = -EIO;
6280 }
6281 }
6282
6283 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006284 bvec++;
6285 } while (bvec <= bvec_end);
6286
6287 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006288 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006289 bio->bi_private = dip->private;
6290
Josef Bacik4b46fce2010-05-23 11:00:55 -04006291 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006292
6293 /* If we had a csum failure make sure to clear the uptodate flag */
6294 if (err)
6295 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006296 dio_end_io(bio, err);
6297}
6298
6299static void btrfs_endio_direct_write(struct bio *bio, int err)
6300{
6301 struct btrfs_dio_private *dip = bio->bi_private;
6302 struct inode *inode = dip->inode;
6303 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006304 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006305 u64 ordered_offset = dip->logical_offset;
6306 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006307 int ret;
6308
6309 if (err)
6310 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006311again:
6312 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6313 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006314 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006315 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006316 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006317
Josef Bacik5fd02042012-05-02 14:00:54 -04006318 ordered->work.func = finish_ordered_fn;
6319 ordered->work.flags = 0;
6320 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6321 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006322out_test:
6323 /*
6324 * our bio might span multiple ordered extents. If we haven't
6325 * completed the accounting for the whole dio, go back and try again
6326 */
6327 if (ordered_offset < dip->logical_offset + dip->bytes) {
6328 ordered_bytes = dip->logical_offset + dip->bytes -
6329 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006330 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006331 goto again;
6332 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006333out_done:
6334 bio->bi_private = dip->private;
6335
Josef Bacik4b46fce2010-05-23 11:00:55 -04006336 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006337
6338 /* If we had an error make sure to clear the uptodate flag */
6339 if (err)
6340 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006341 dio_end_io(bio, err);
6342}
6343
Chris Masoneaf25d92010-05-25 09:48:28 -04006344static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6345 struct bio *bio, int mirror_num,
6346 unsigned long bio_flags, u64 offset)
6347{
6348 int ret;
6349 struct btrfs_root *root = BTRFS_I(inode)->root;
6350 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006351 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006352 return 0;
6353}
6354
Miao Xiee65e1532010-11-22 03:04:43 +00006355static void btrfs_end_dio_bio(struct bio *bio, int err)
6356{
6357 struct btrfs_dio_private *dip = bio->bi_private;
6358
6359 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006360 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006361 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006362 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006363 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006364 dip->errors = 1;
6365
6366 /*
6367 * before atomic variable goto zero, we must make sure
6368 * dip->errors is perceived to be set.
6369 */
6370 smp_mb__before_atomic_dec();
6371 }
6372
6373 /* if there are more bios still pending for this dio, just exit */
6374 if (!atomic_dec_and_test(&dip->pending_bios))
6375 goto out;
6376
6377 if (dip->errors)
6378 bio_io_error(dip->orig_bio);
6379 else {
6380 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6381 bio_endio(dip->orig_bio, 0);
6382 }
6383out:
6384 bio_put(bio);
6385}
6386
6387static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6388 u64 first_sector, gfp_t gfp_flags)
6389{
6390 int nr_vecs = bio_get_nr_vecs(bdev);
6391 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6392}
6393
6394static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6395 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006396 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006397{
6398 int write = rw & REQ_WRITE;
6399 struct btrfs_root *root = BTRFS_I(inode)->root;
6400 int ret;
6401
Josef Bacikb812ce22012-11-16 13:56:32 -05006402 if (async_submit)
6403 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6404
Miao Xiee65e1532010-11-22 03:04:43 +00006405 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006406
6407 if (!write) {
6408 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6409 if (ret)
6410 goto err;
6411 }
Miao Xiee65e1532010-11-22 03:04:43 +00006412
Josef Bacik1ae39932011-04-06 14:41:34 -04006413 if (skip_sum)
6414 goto map;
6415
6416 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006417 ret = btrfs_wq_submit_bio(root->fs_info,
6418 inode, rw, bio, 0, 0,
6419 file_offset,
6420 __btrfs_submit_bio_start_direct_io,
6421 __btrfs_submit_bio_done);
6422 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006423 } else if (write) {
6424 /*
6425 * If we aren't doing async submit, calculate the csum of the
6426 * bio now.
6427 */
6428 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6429 if (ret)
6430 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006431 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006432 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006433 if (ret)
6434 goto err;
6435 }
Miao Xiee65e1532010-11-22 03:04:43 +00006436
Josef Bacik1ae39932011-04-06 14:41:34 -04006437map:
6438 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006439err:
6440 bio_put(bio);
6441 return ret;
6442}
6443
6444static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6445 int skip_sum)
6446{
6447 struct inode *inode = dip->inode;
6448 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006449 struct bio *bio;
6450 struct bio *orig_bio = dip->orig_bio;
6451 struct bio_vec *bvec = orig_bio->bi_io_vec;
6452 u64 start_sector = orig_bio->bi_sector;
6453 u64 file_offset = dip->logical_offset;
6454 u64 submit_len = 0;
6455 u64 map_length;
6456 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006457 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006458 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006459
Miao Xiee65e1532010-11-22 03:04:43 +00006460 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006461 ret = btrfs_map_block(root->fs_info, READ, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006462 &map_length, NULL, 0);
6463 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006464 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006465 return -EIO;
6466 }
6467
Josef Bacik02f57c72011-04-06 14:25:44 -04006468 if (map_length >= orig_bio->bi_size) {
6469 bio = orig_bio;
6470 goto submit;
6471 }
6472
Josef Bacik1ae39932011-04-06 14:41:34 -04006473 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006474 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6475 if (!bio)
6476 return -ENOMEM;
6477 bio->bi_private = dip;
6478 bio->bi_end_io = btrfs_end_dio_bio;
6479 atomic_inc(&dip->pending_bios);
6480
Miao Xiee65e1532010-11-22 03:04:43 +00006481 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6482 if (unlikely(map_length < submit_len + bvec->bv_len ||
6483 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6484 bvec->bv_offset) < bvec->bv_len)) {
6485 /*
6486 * inc the count before we submit the bio so
6487 * we know the end IO handler won't happen before
6488 * we inc the count. Otherwise, the dip might get freed
6489 * before we're done setting it up
6490 */
6491 atomic_inc(&dip->pending_bios);
6492 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6493 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006494 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006495 if (ret) {
6496 bio_put(bio);
6497 atomic_dec(&dip->pending_bios);
6498 goto out_err;
6499 }
6500
Miao Xiee65e1532010-11-22 03:04:43 +00006501 start_sector += submit_len >> 9;
6502 file_offset += submit_len;
6503
6504 submit_len = 0;
6505 nr_pages = 0;
6506
6507 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6508 start_sector, GFP_NOFS);
6509 if (!bio)
6510 goto out_err;
6511 bio->bi_private = dip;
6512 bio->bi_end_io = btrfs_end_dio_bio;
6513
6514 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006515 ret = btrfs_map_block(root->fs_info, READ,
6516 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006517 &map_length, NULL, 0);
6518 if (ret) {
6519 bio_put(bio);
6520 goto out_err;
6521 }
6522 } else {
6523 submit_len += bvec->bv_len;
6524 nr_pages ++;
6525 bvec++;
6526 }
6527 }
6528
Josef Bacik02f57c72011-04-06 14:25:44 -04006529submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006530 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006531 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006532 if (!ret)
6533 return 0;
6534
6535 bio_put(bio);
6536out_err:
6537 dip->errors = 1;
6538 /*
6539 * before atomic variable goto zero, we must
6540 * make sure dip->errors is perceived to be set.
6541 */
6542 smp_mb__before_atomic_dec();
6543 if (atomic_dec_and_test(&dip->pending_bios))
6544 bio_io_error(dip->orig_bio);
6545
6546 /* bio_end_io() will handle error, so we needn't return it */
6547 return 0;
6548}
6549
Josef Bacik4b46fce2010-05-23 11:00:55 -04006550static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6551 loff_t file_offset)
6552{
6553 struct btrfs_root *root = BTRFS_I(inode)->root;
6554 struct btrfs_dio_private *dip;
6555 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006556 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006557 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006558 int ret = 0;
6559
6560 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6561
6562 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6563 if (!dip) {
6564 ret = -ENOMEM;
6565 goto free_ordered;
6566 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006567
6568 dip->private = bio->bi_private;
6569 dip->inode = inode;
6570 dip->logical_offset = file_offset;
6571
Josef Bacik4b46fce2010-05-23 11:00:55 -04006572 dip->bytes = 0;
6573 do {
6574 dip->bytes += bvec->bv_len;
6575 bvec++;
6576 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6577
Chris Mason46bfbb52010-05-26 11:04:10 -04006578 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006579 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006580 dip->errors = 0;
6581 dip->orig_bio = bio;
6582 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006583
6584 if (write)
6585 bio->bi_end_io = btrfs_endio_direct_write;
6586 else
6587 bio->bi_end_io = btrfs_endio_direct_read;
6588
Miao Xiee65e1532010-11-22 03:04:43 +00006589 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6590 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006591 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006592free_ordered:
6593 /*
6594 * If this is a write, we need to clean up the reserved space and kill
6595 * the ordered extent.
6596 */
6597 if (write) {
6598 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006599 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006600 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6601 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6602 btrfs_free_reserved_extent(root, ordered->start,
6603 ordered->disk_len);
6604 btrfs_put_ordered_extent(ordered);
6605 btrfs_put_ordered_extent(ordered);
6606 }
6607 bio_endio(bio, ret);
6608}
6609
Chris Mason5a5f79b2010-05-26 21:33:37 -04006610static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6611 const struct iovec *iov, loff_t offset,
6612 unsigned long nr_segs)
6613{
6614 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006615 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006616 size_t size;
6617 unsigned long addr;
6618 unsigned blocksize_mask = root->sectorsize - 1;
6619 ssize_t retval = -EINVAL;
6620 loff_t end = offset;
6621
6622 if (offset & blocksize_mask)
6623 goto out;
6624
6625 /* Check the memory alignment. Blocks cannot straddle pages */
6626 for (seg = 0; seg < nr_segs; seg++) {
6627 addr = (unsigned long)iov[seg].iov_base;
6628 size = iov[seg].iov_len;
6629 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006630 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006631 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006632
6633 /* If this is a write we don't need to check anymore */
6634 if (rw & WRITE)
6635 continue;
6636
6637 /*
6638 * Check to make sure we don't have duplicate iov_base's in this
6639 * iovec, if so return EINVAL, otherwise we'll get csum errors
6640 * when reading back.
6641 */
6642 for (i = seg + 1; i < nr_segs; i++) {
6643 if (iov[seg].iov_base == iov[i].iov_base)
6644 goto out;
6645 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006646 }
6647 retval = 0;
6648out:
6649 return retval;
6650}
Josef Bacikeb838e72012-07-31 16:28:48 -04006651
Chris Mason16432982008-04-10 10:23:21 -04006652static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6653 const struct iovec *iov, loff_t offset,
6654 unsigned long nr_segs)
6655{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006656 struct file *file = iocb->ki_filp;
6657 struct inode *inode = file->f_mapping->host;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006658
Chris Mason5a5f79b2010-05-26 21:33:37 -04006659 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006660 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006661 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006662
Josef Bacikeb838e72012-07-31 16:28:48 -04006663 return __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006664 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6665 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6666 btrfs_submit_direct, 0);
Chris Mason16432982008-04-10 10:23:21 -04006667}
6668
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006669#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6670
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006671static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6672 __u64 start, __u64 len)
6673{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006674 int ret;
6675
6676 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6677 if (ret)
6678 return ret;
6679
Chris Masonec29ed52011-02-23 16:23:20 -05006680 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006681}
6682
Chris Mason9ebefb182007-06-15 13:50:00 -04006683int btrfs_readpage(struct file *file, struct page *page)
6684{
Chris Masond1310b22008-01-24 16:13:08 -05006685 struct extent_io_tree *tree;
6686 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006687 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006688}
Chris Mason1832a6d2007-12-21 16:27:21 -05006689
Chris Mason39279cc2007-06-12 06:35:45 -04006690static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6691{
Chris Masond1310b22008-01-24 16:13:08 -05006692 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04006693
6694
6695 if (current->flags & PF_MEMALLOC) {
6696 redirty_page_for_writepage(wbc, page);
6697 unlock_page(page);
6698 return 0;
6699 }
Chris Masond1310b22008-01-24 16:13:08 -05006700 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006701 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6702}
Chris Mason39279cc2007-06-12 06:35:45 -04006703
Chris Masonf4219502008-07-22 11:18:09 -04006704int btrfs_writepages(struct address_space *mapping,
6705 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04006706{
Chris Masond1310b22008-01-24 16:13:08 -05006707 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006708
Chris Masond1310b22008-01-24 16:13:08 -05006709 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04006710 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6711}
6712
Chris Mason3ab2fb52007-11-08 10:59:22 -05006713static int
6714btrfs_readpages(struct file *file, struct address_space *mapping,
6715 struct list_head *pages, unsigned nr_pages)
6716{
Chris Masond1310b22008-01-24 16:13:08 -05006717 struct extent_io_tree *tree;
6718 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006719 return extent_readpages(tree, mapping, pages, nr_pages,
6720 btrfs_get_extent);
6721}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006722static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006723{
Chris Masond1310b22008-01-24 16:13:08 -05006724 struct extent_io_tree *tree;
6725 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006726 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006727
Chris Masond1310b22008-01-24 16:13:08 -05006728 tree = &BTRFS_I(page->mapping->host)->io_tree;
6729 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006730 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006731 if (ret == 1) {
6732 ClearPagePrivate(page);
6733 set_page_private(page, 0);
6734 page_cache_release(page);
6735 }
6736 return ret;
6737}
Chris Mason39279cc2007-06-12 06:35:45 -04006738
Chris Masone6dcd2d2008-07-17 12:53:50 -04006739static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6740{
Chris Mason98509cf2008-09-11 15:51:43 -04006741 if (PageWriteback(page) || PageDirty(page))
6742 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006743 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006744}
6745
Chris Masona52d9a82007-08-27 16:49:44 -04006746static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6747{
Josef Bacik5fd02042012-05-02 14:00:54 -04006748 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006749 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006750 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006751 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006752 u64 page_start = page_offset(page);
6753 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006754
Chris Mason8b62b722009-09-02 16:53:46 -04006755 /*
6756 * we have the page locked, so new writeback can't start,
6757 * and the dirty bit won't be cleared while we are here.
6758 *
6759 * Wait for IO on this page so that we can safely clear
6760 * the PagePrivate2 bit and do ordered accounting
6761 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006762 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006763
Josef Bacik5fd02042012-05-02 14:00:54 -04006764 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006765 if (offset) {
6766 btrfs_releasepage(page, GFP_NOFS);
6767 return;
6768 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006769 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00006770 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04006771 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006772 /*
6773 * IO on this page will never be started, so we need
6774 * to account for any ordered extents now
6775 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006776 clear_extent_bit(tree, page_start, page_end,
6777 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006778 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6779 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006780 /*
6781 * whoever cleared the private bit is responsible
6782 * for the finish_ordered_io
6783 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006784 if (TestClearPagePrivate2(page) &&
6785 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6786 PAGE_CACHE_SIZE, 1)) {
6787 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006788 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006789 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006790 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006791 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006792 }
6793 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006794 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006795 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6796 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006797 __btrfs_releasepage(page, GFP_NOFS);
6798
Chris Mason4a096752008-07-21 10:29:44 -04006799 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006800 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006801 ClearPagePrivate(page);
6802 set_page_private(page, 0);
6803 page_cache_release(page);
6804 }
Chris Mason39279cc2007-06-12 06:35:45 -04006805}
6806
Chris Mason9ebefb182007-06-15 13:50:00 -04006807/*
6808 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6809 * called from a page fault handler when a page is first dirtied. Hence we must
6810 * be careful to check for EOF conditions here. We set the page up correctly
6811 * for a written page which means we get ENOSPC checking when writing into
6812 * holes and correct delalloc and unwritten extent mapping on filesystems that
6813 * support these features.
6814 *
6815 * We are not allowed to take the i_mutex here so we have to play games to
6816 * protect against truncate races as the page could now be beyond EOF. Because
6817 * vmtruncate() writes the inode size before removing pages, once we have the
6818 * page lock we can determine safely if the page is beyond EOF. If it is not
6819 * beyond EOF, then the page is guaranteed safe against truncation until we
6820 * unlock the page.
6821 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006822int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006823{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006824 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006825 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006826 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006827 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6828 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006829 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006830 char *kaddr;
6831 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006832 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006833 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006834 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006835 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006836 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006837
Jan Karab2b5ef52012-06-12 16:20:45 +02006838 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006839 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006840 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006841 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006842 reserved = 1;
6843 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006844 if (ret) {
6845 if (ret == -ENOMEM)
6846 ret = VM_FAULT_OOM;
6847 else /* -ENOSPC, -EIO, etc */
6848 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006849 if (reserved)
6850 goto out;
6851 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006852 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006853
Nick Piggin56a76f82009-03-31 15:23:23 -07006854 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006855again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006856 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006857 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006858 page_start = page_offset(page);
6859 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006860
Chris Mason9ebefb182007-06-15 13:50:00 -04006861 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006862 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006863 /* page got truncated out from underneath us */
6864 goto out_unlock;
6865 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006866 wait_on_page_writeback(page);
6867
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006868 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006869 set_page_extent_mapped(page);
6870
Chris Masoneb84ae02008-07-17 13:53:27 -04006871 /*
6872 * we can't set the delalloc bits if there are pending ordered
6873 * extents. Drop our locks and wait for them to finish
6874 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006875 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6876 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006877 unlock_extent_cached(io_tree, page_start, page_end,
6878 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006879 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006880 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006881 btrfs_put_ordered_extent(ordered);
6882 goto again;
6883 }
6884
Josef Bacikfbf19082009-10-01 17:10:23 -04006885 /*
6886 * XXX - page_mkwrite gets called every time the page is dirtied, even
6887 * if it was already dirty, so for space accounting reasons we need to
6888 * clear any delalloc bits for the range we are fixing to save. There
6889 * is probably a better way to do this, but for now keep consistent with
6890 * prepare_pages in the normal write path.
6891 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006892 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006893 EXTENT_DIRTY | EXTENT_DELALLOC |
6894 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006895 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006896
Josef Bacik2ac55d42010-02-03 19:33:23 +00006897 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6898 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006899 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006900 unlock_extent_cached(io_tree, page_start, page_end,
6901 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006902 ret = VM_FAULT_SIGBUS;
6903 goto out_unlock;
6904 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006905 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006906
6907 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006908 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006909 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006910 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006911 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006912
Chris Masone6dcd2d2008-07-17 12:53:50 -04006913 if (zero_start != PAGE_CACHE_SIZE) {
6914 kaddr = kmap(page);
6915 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6916 flush_dcache_page(page);
6917 kunmap(page);
6918 }
Chris Mason247e7432008-07-17 12:53:51 -04006919 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006920 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006921 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006922
Chris Mason257c62e2009-10-13 13:21:08 -04006923 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6924 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006925 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006926
Josef Bacik2ac55d42010-02-03 19:33:23 +00006927 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006928
6929out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006930 if (!ret) {
6931 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006932 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006933 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006934 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006935out:
Josef Bacikec39e182012-01-12 19:10:12 -05006936 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006937out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006938 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006939 return ret;
6940}
6941
Josef Bacika41ad392011-01-31 15:30:16 -05006942static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006943{
6944 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006945 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006946 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006947 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006948 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04006949 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006950 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006951
Josef Bacik2aaa6652012-08-29 14:27:18 -04006952 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04006953 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006954 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006955
Chris Mason4a096752008-07-21 10:29:44 -04006956 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006957 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006958
Josef Bacikfcb80c22011-05-03 10:40:22 -04006959 /*
6960 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6961 * 3 things going on here
6962 *
6963 * 1) We need to reserve space for our orphan item and the space to
6964 * delete our orphan item. Lord knows we don't want to have a dangling
6965 * orphan item because we didn't reserve space to remove it.
6966 *
6967 * 2) We need to reserve space to update our inode.
6968 *
6969 * 3) We need to have something to cache all the space that is going to
6970 * be free'd up by the truncate operation, but also have some slack
6971 * space reserved in case it uses space during the truncate (thank you
6972 * very much snapshotting).
6973 *
6974 * And we need these to all be seperate. The fact is we can use alot of
6975 * space doing the truncate, and we have no earthly idea how much space
6976 * we will use, so we need the truncate reservation to be seperate so it
6977 * doesn't end up using space reserved for updating the inode or
6978 * removing the orphan item. We also need to be able to stop the
6979 * transaction and start a new one, which means we need to be able to
6980 * update the inode several times, and we have no idea of knowing how
6981 * many times that will be, so we can't just reserve 1 item for the
6982 * entirety of the opration, so that has to be done seperately as well.
6983 * Then there is the orphan item, which does indeed need to be held on
6984 * to for the whole operation, and we need nobody to touch this reserved
6985 * space except the orphan code.
6986 *
6987 * So that leaves us with
6988 *
6989 * 1) root->orphan_block_rsv - for the orphan deletion.
6990 * 2) rsv - for the truncate reservation, which we will steal from the
6991 * transaction reservation.
6992 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6993 * updating the inode.
6994 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06006995 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006996 if (!rsv)
6997 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006998 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006999 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007000
Josef Bacik907cbce2011-08-08 13:46:15 -04007001 /*
Josef Bacik07127182011-08-19 10:29:59 -04007002 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007003 * 1 for updating the inode.
7004 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007005 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007006 if (IS_ERR(trans)) {
7007 err = PTR_ERR(trans);
7008 goto out;
7009 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007010
Josef Bacik907cbce2011-08-08 13:46:15 -04007011 /* Migrate the slack space for the truncate to our reserve */
7012 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7013 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007014 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007015
Chris Mason5a3f23d2009-03-31 13:27:11 -04007016 /*
7017 * setattr is responsible for setting the ordered_data_close flag,
7018 * but that is only tested during the last file release. That
7019 * could happen well after the next commit, leaving a great big
7020 * window where new writes may get lost if someone chooses to write
7021 * to this file after truncating to zero
7022 *
7023 * The inode doesn't have any dirty data here, and so if we commit
7024 * this is a noop. If someone immediately starts writing to the inode
7025 * it is very likely we'll catch some of their writes in this
7026 * transaction, and the commit will find this file on the ordered
7027 * data list with good things to send down.
7028 *
7029 * This is a best effort solution, there is still a window where
7030 * using truncate to replace the contents of the file will
7031 * end up with a zero length file after a crash.
7032 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007033 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7034 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007035 btrfs_add_ordered_operation(trans, root, inode);
7036
Josef Bacik5dc562c2012-08-17 13:14:17 -04007037 /*
7038 * So if we truncate and then write and fsync we normally would just
7039 * write the extents that changed, which is a problem if we need to
7040 * first truncate that entire inode. So set this flag so we write out
7041 * all of the extents in the inode to the sync log so we're completely
7042 * safe.
7043 */
7044 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007045 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007046
Yan, Zheng80825102009-11-12 09:35:36 +00007047 while (1) {
7048 ret = btrfs_truncate_inode_items(trans, root, inode,
7049 inode->i_size,
7050 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007051 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007052 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007053 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007054 }
Chris Mason39279cc2007-06-12 06:35:45 -04007055
Josef Bacikfcb80c22011-05-03 10:40:22 -04007056 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007057 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007058 if (ret) {
7059 err = ret;
7060 break;
7061 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007062
Yan, Zheng80825102009-11-12 09:35:36 +00007063 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007064 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007065
7066 trans = btrfs_start_transaction(root, 2);
7067 if (IS_ERR(trans)) {
7068 ret = err = PTR_ERR(trans);
7069 trans = NULL;
7070 break;
7071 }
7072
7073 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7074 rsv, min_size);
7075 BUG_ON(ret); /* shouldn't happen */
7076 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007077 }
7078
7079 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007080 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007081 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007082 if (ret)
7083 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007084 }
7085
Chris Mason917c16b2011-11-08 14:49:59 -05007086 if (trans) {
7087 trans->block_rsv = &root->fs_info->trans_block_rsv;
7088 ret = btrfs_update_inode(trans, root, inode);
7089 if (ret && !err)
7090 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007091
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007092 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007093 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007094 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007095
7096out:
7097 btrfs_free_block_rsv(root, rsv);
7098
Josef Bacik3893e332011-01-31 16:03:11 -05007099 if (ret && !err)
7100 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007101
Josef Bacik3893e332011-01-31 16:03:11 -05007102 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007103}
7104
Sven Wegener3b963622008-06-09 21:57:42 -04007105/*
Chris Masond352ac62008-09-29 15:18:18 -04007106 * create a new subvolume directory/inode (helper for the ioctl).
7107 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007108int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007109 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007110{
Chris Mason39279cc2007-06-12 06:35:45 -04007111 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007112 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007113 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007114
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007115 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7116 new_dirid, new_dirid,
7117 S_IFDIR | (~current_umask() & S_IRWXUGO),
7118 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007119 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007120 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007121 inode->i_op = &btrfs_dir_inode_operations;
7122 inode->i_fop = &btrfs_dir_file_operations;
7123
Miklos Szeredibfe86842011-10-28 14:13:29 +02007124 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007125 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007126
Yan, Zheng76dda932009-09-21 16:00:26 -04007127 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007128
Yan, Zheng76dda932009-09-21 16:00:26 -04007129 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007130 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007131}
7132
Chris Mason39279cc2007-06-12 06:35:45 -04007133struct inode *btrfs_alloc_inode(struct super_block *sb)
7134{
7135 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007136 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007137
7138 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7139 if (!ei)
7140 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007141
7142 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007143 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007144 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007145 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007146 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007147 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007148 ei->disk_i_size = 0;
7149 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007150 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007151 ei->index_cnt = (u64)-1;
7152 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007153 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007154
Josef Bacik9e0baf62011-07-15 15:16:44 +00007155 spin_lock_init(&ei->lock);
7156 ei->outstanding_extents = 0;
7157 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007158
Josef Bacik72ac3c02012-05-23 14:13:11 -04007159 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007160 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007161
Miao Xie16cdcec2011-04-22 18:12:22 +08007162 ei->delayed_node = NULL;
7163
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007164 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007165 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007166 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7167 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007168 ei->io_tree.track_uptodate = 1;
7169 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007170 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007171 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007172 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007173 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007174 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007175 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007176 RB_CLEAR_NODE(&ei->rb_node);
7177
7178 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007179}
7180
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007181static void btrfs_i_callback(struct rcu_head *head)
7182{
7183 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007184 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7185}
7186
Chris Mason39279cc2007-06-12 06:35:45 -04007187void btrfs_destroy_inode(struct inode *inode)
7188{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007189 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007190 struct btrfs_root *root = BTRFS_I(inode)->root;
7191
Al Virob3d9b7a2012-06-09 13:51:19 -04007192 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007193 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007194 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7195 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007196 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7197 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007198
Chris Mason5a3f23d2009-03-31 13:27:11 -04007199 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007200 * This can happen where we create an inode, but somebody else also
7201 * created the same inode and we need to destroy the one we already
7202 * created.
7203 */
7204 if (!root)
7205 goto free;
7206
7207 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007208 * Make sure we're properly removed from the ordered operation
7209 * lists.
7210 */
7211 smp_mb();
7212 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7213 spin_lock(&root->fs_info->ordered_extent_lock);
7214 list_del_init(&BTRFS_I(inode)->ordered_operations);
7215 spin_unlock(&root->fs_info->ordered_extent_lock);
7216 }
7217
Josef Bacik8a35d952012-05-23 14:26:42 -04007218 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7219 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007220 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7221 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007222 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007223 }
Josef Bacik7b128762008-07-24 12:17:14 -04007224
Chris Masond3977122009-01-05 21:25:51 -05007225 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007226 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7227 if (!ordered)
7228 break;
7229 else {
Chris Masond3977122009-01-05 21:25:51 -05007230 printk(KERN_ERR "btrfs found ordered "
7231 "extent %llu %llu on inode cleanup\n",
7232 (unsigned long long)ordered->file_offset,
7233 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007234 btrfs_remove_ordered_extent(inode, ordered);
7235 btrfs_put_ordered_extent(ordered);
7236 btrfs_put_ordered_extent(ordered);
7237 }
7238 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007239 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007240 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007241free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007242 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007243 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007244}
7245
Al Viro45321ac2010-06-07 13:43:19 -04007246int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007247{
7248 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007249
Josef Bacik0af3d002010-06-21 14:48:16 -04007250 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007251 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007252 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007253 else
Al Viro45321ac2010-06-07 13:43:19 -04007254 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007255}
7256
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007257static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007258{
7259 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7260
7261 inode_init_once(&ei->vfs_inode);
7262}
7263
7264void btrfs_destroy_cachep(void)
7265{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007266 /*
7267 * Make sure all delayed rcu free inodes are flushed before we
7268 * destroy cache.
7269 */
7270 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007271 if (btrfs_inode_cachep)
7272 kmem_cache_destroy(btrfs_inode_cachep);
7273 if (btrfs_trans_handle_cachep)
7274 kmem_cache_destroy(btrfs_trans_handle_cachep);
7275 if (btrfs_transaction_cachep)
7276 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007277 if (btrfs_path_cachep)
7278 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007279 if (btrfs_free_space_cachep)
7280 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007281 if (btrfs_delalloc_work_cachep)
7282 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007283}
7284
7285int btrfs_init_cachep(void)
7286{
David Sterba837e1972012-09-07 03:00:48 -06007287 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007288 sizeof(struct btrfs_inode), 0,
7289 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007290 if (!btrfs_inode_cachep)
7291 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007292
David Sterba837e1972012-09-07 03:00:48 -06007293 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007294 sizeof(struct btrfs_trans_handle), 0,
7295 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007296 if (!btrfs_trans_handle_cachep)
7297 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007298
David Sterba837e1972012-09-07 03:00:48 -06007299 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007300 sizeof(struct btrfs_transaction), 0,
7301 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007302 if (!btrfs_transaction_cachep)
7303 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007304
David Sterba837e1972012-09-07 03:00:48 -06007305 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007306 sizeof(struct btrfs_path), 0,
7307 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007308 if (!btrfs_path_cachep)
7309 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007310
David Sterba837e1972012-09-07 03:00:48 -06007311 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007312 sizeof(struct btrfs_free_space), 0,
7313 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7314 if (!btrfs_free_space_cachep)
7315 goto fail;
7316
Miao Xie8ccf6f192012-10-25 09:28:04 +00007317 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7318 sizeof(struct btrfs_delalloc_work), 0,
7319 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7320 NULL);
7321 if (!btrfs_delalloc_work_cachep)
7322 goto fail;
7323
Chris Mason39279cc2007-06-12 06:35:45 -04007324 return 0;
7325fail:
7326 btrfs_destroy_cachep();
7327 return -ENOMEM;
7328}
7329
7330static int btrfs_getattr(struct vfsmount *mnt,
7331 struct dentry *dentry, struct kstat *stat)
7332{
7333 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007334 u32 blocksize = inode->i_sb->s_blocksize;
7335
Chris Mason39279cc2007-06-12 06:35:45 -04007336 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007337 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007338 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007339 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7340 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007341 return 0;
7342}
7343
Liu Bo75e7cb72011-03-22 10:12:20 +00007344/*
7345 * If a file is moved, it will inherit the cow and compression flags of the new
7346 * directory.
7347 */
7348static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7349{
7350 struct btrfs_inode *b_dir = BTRFS_I(dir);
7351 struct btrfs_inode *b_inode = BTRFS_I(inode);
7352
7353 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7354 b_inode->flags |= BTRFS_INODE_NODATACOW;
7355 else
7356 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7357
Liu Bobc178232012-06-14 02:23:18 -06007358 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007359 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007360 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7361 } else {
7362 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7363 BTRFS_INODE_NOCOMPRESS);
7364 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007365}
7366
Chris Masond3977122009-01-05 21:25:51 -05007367static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7368 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007369{
7370 struct btrfs_trans_handle *trans;
7371 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007372 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007373 struct inode *new_inode = new_dentry->d_inode;
7374 struct inode *old_inode = old_dentry->d_inode;
7375 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007376 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007377 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007378 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007379 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007380
Li Zefan33345d012011-04-20 10:31:50 +08007381 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007382 return -EPERM;
7383
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007384 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007385 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007386 return -EXDEV;
7387
Li Zefan33345d012011-04-20 10:31:50 +08007388 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7389 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007390 return -ENOTEMPTY;
7391
Chris Mason39279cc2007-06-12 06:35:45 -04007392 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007393 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007394 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007395
7396
7397 /* check for collisions, even if the name isn't there */
7398 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7399 new_dentry->d_name.name,
7400 new_dentry->d_name.len);
7401
7402 if (ret) {
7403 if (ret == -EEXIST) {
7404 /* we shouldn't get
7405 * eexist without a new_inode */
7406 if (!new_inode) {
7407 WARN_ON(1);
7408 return ret;
7409 }
7410 } else {
7411 /* maybe -EOVERFLOW */
7412 return ret;
7413 }
7414 }
7415 ret = 0;
7416
Chris Mason5a3f23d2009-03-31 13:27:11 -04007417 /*
7418 * we're using rename to replace one file with another.
7419 * and the replacement file is large. Start IO on it now so
7420 * we don't add too much work to the end of the transaction
7421 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007422 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007423 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7424 filemap_flush(old_inode->i_mapping);
7425
Yan, Zheng76dda932009-09-21 16:00:26 -04007426 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007427 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007428 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007429 /*
7430 * We want to reserve the absolute worst case amount of items. So if
7431 * both inodes are subvols and we need to unlink them then that would
7432 * require 4 item modifications, but if they are both normal inodes it
7433 * would require 5 item modifications, so we'll assume their normal
7434 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7435 * should cover the worst case number of items we'll modify.
7436 */
7437 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007438 if (IS_ERR(trans)) {
7439 ret = PTR_ERR(trans);
7440 goto out_notrans;
7441 }
Chris Mason5f39d392007-10-15 16:14:19 -04007442
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007443 if (dest != root)
7444 btrfs_record_root_in_trans(trans, dest);
7445
Yan, Zhenga5719522009-09-24 09:17:31 -04007446 ret = btrfs_set_inode_index(new_dir, &index);
7447 if (ret)
7448 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007449
Li Zefan33345d012011-04-20 10:31:50 +08007450 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007451 /* force full log commit if subvolume involved. */
7452 root->fs_info->last_trans_log_full_commit = trans->transid;
7453 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007454 ret = btrfs_insert_inode_ref(trans, dest,
7455 new_dentry->d_name.name,
7456 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007457 old_ino,
7458 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007459 if (ret)
7460 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007461 /*
7462 * this is an ugly little race, but the rename is required
7463 * to make sure that if we crash, the inode is either at the
7464 * old name or the new one. pinning the log transaction lets
7465 * us make sure we don't allow a log commit to come in after
7466 * we unlink the name but before we add the new name back in.
7467 */
7468 btrfs_pin_log_trans(root);
7469 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007470 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007471 * make sure the inode gets flushed if it is replacing
7472 * something.
7473 */
Li Zefan33345d012011-04-20 10:31:50 +08007474 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007475 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007476
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007477 inode_inc_iversion(old_dir);
7478 inode_inc_iversion(new_dir);
7479 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007480 old_dir->i_ctime = old_dir->i_mtime = ctime;
7481 new_dir->i_ctime = new_dir->i_mtime = ctime;
7482 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007483
Chris Mason12fcfd22009-03-24 10:24:20 -04007484 if (old_dentry->d_parent != new_dentry->d_parent)
7485 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7486
Li Zefan33345d012011-04-20 10:31:50 +08007487 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007488 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7489 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7490 old_dentry->d_name.name,
7491 old_dentry->d_name.len);
7492 } else {
Al Viro92986792011-03-04 17:14:37 +00007493 ret = __btrfs_unlink_inode(trans, root, old_dir,
7494 old_dentry->d_inode,
7495 old_dentry->d_name.name,
7496 old_dentry->d_name.len);
7497 if (!ret)
7498 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007499 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007500 if (ret) {
7501 btrfs_abort_transaction(trans, root, ret);
7502 goto out_fail;
7503 }
Chris Mason39279cc2007-06-12 06:35:45 -04007504
7505 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007506 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007507 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007508 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007509 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7510 root_objectid = BTRFS_I(new_inode)->location.objectid;
7511 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7512 root_objectid,
7513 new_dentry->d_name.name,
7514 new_dentry->d_name.len);
7515 BUG_ON(new_inode->i_nlink == 0);
7516 } else {
7517 ret = btrfs_unlink_inode(trans, dest, new_dir,
7518 new_dentry->d_inode,
7519 new_dentry->d_name.name,
7520 new_dentry->d_name.len);
7521 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007522 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007523 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007524 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007525 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007526 if (ret) {
7527 btrfs_abort_transaction(trans, root, ret);
7528 goto out_fail;
7529 }
Chris Mason39279cc2007-06-12 06:35:45 -04007530 }
Josef Bacikaec74772008-07-24 12:12:38 -04007531
Liu Bo75e7cb72011-03-22 10:12:20 +00007532 fixup_inode_flags(new_dir, old_inode);
7533
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007534 ret = btrfs_add_link(trans, new_dir, old_inode,
7535 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007536 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007537 if (ret) {
7538 btrfs_abort_transaction(trans, root, ret);
7539 goto out_fail;
7540 }
Chris Mason39279cc2007-06-12 06:35:45 -04007541
Li Zefan33345d012011-04-20 10:31:50 +08007542 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007543 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007544 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007545 btrfs_end_log_trans(root);
7546 }
Chris Mason39279cc2007-06-12 06:35:45 -04007547out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007548 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007549out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007550 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007551 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007552
Chris Mason39279cc2007-06-12 06:35:45 -04007553 return ret;
7554}
7555
Miao Xie8ccf6f192012-10-25 09:28:04 +00007556static void btrfs_run_delalloc_work(struct btrfs_work *work)
7557{
7558 struct btrfs_delalloc_work *delalloc_work;
7559
7560 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7561 work);
7562 if (delalloc_work->wait)
7563 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7564 else
7565 filemap_flush(delalloc_work->inode->i_mapping);
7566
7567 if (delalloc_work->delay_iput)
7568 btrfs_add_delayed_iput(delalloc_work->inode);
7569 else
7570 iput(delalloc_work->inode);
7571 complete(&delalloc_work->completion);
7572}
7573
7574struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7575 int wait, int delay_iput)
7576{
7577 struct btrfs_delalloc_work *work;
7578
7579 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7580 if (!work)
7581 return NULL;
7582
7583 init_completion(&work->completion);
7584 INIT_LIST_HEAD(&work->list);
7585 work->inode = inode;
7586 work->wait = wait;
7587 work->delay_iput = delay_iput;
7588 work->work.func = btrfs_run_delalloc_work;
7589
7590 return work;
7591}
7592
7593void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7594{
7595 wait_for_completion(&work->completion);
7596 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7597}
7598
Chris Masond352ac62008-09-29 15:18:18 -04007599/*
7600 * some fairly slow code that needs optimization. This walks the list
7601 * of all the inodes with pending delalloc and forces them to disk.
7602 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007603int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007604{
Chris Masonea8c2812008-08-04 23:17:27 -04007605 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007606 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007607 struct btrfs_delalloc_work *work, *next;
7608 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00007609 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007610 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04007611
Yan Zhengc146afa2008-11-12 14:34:12 -05007612 if (root->fs_info->sb->s_flags & MS_RDONLY)
7613 return -EROFS;
7614
Miao Xie8ccf6f192012-10-25 09:28:04 +00007615 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007616 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00007617
Chris Mason75eff682008-12-15 15:54:40 -05007618 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007619 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
7620 while (!list_empty(&splice)) {
7621 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04007622 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007623
7624 list_del_init(&binode->delalloc_inodes);
7625
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007626 inode = igrab(&binode->vfs_inode);
7627 if (!inode)
Miao Xie1eafa6c2013-01-22 10:49:00 +00007628 continue;
7629
7630 list_add_tail(&binode->delalloc_inodes,
7631 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007632 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007633
7634 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7635 if (unlikely(!work)) {
7636 ret = -ENOMEM;
7637 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007638 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007639 list_add_tail(&work->list, &works);
7640 btrfs_queue_worker(&root->fs_info->flush_workers,
7641 &work->work);
7642
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007643 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007644 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007645 }
Chris Mason75eff682008-12-15 15:54:40 -05007646 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007647
Miao Xie1eafa6c2013-01-22 10:49:00 +00007648 list_for_each_entry_safe(work, next, &works, list) {
7649 list_del_init(&work->list);
7650 btrfs_wait_and_free_delalloc_work(work);
7651 }
7652
Chris Mason8c8bee12008-09-29 11:19:10 -04007653 /* the filemap_flush will queue IO into the worker threads, but
7654 * we have to make sure the IO is actually started and that
7655 * ordered extents get created before we return
7656 */
7657 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007658 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007659 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007660 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007661 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7662 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007663 }
7664 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007665 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007666out:
7667 list_for_each_entry_safe(work, next, &works, list) {
7668 list_del_init(&work->list);
7669 btrfs_wait_and_free_delalloc_work(work);
7670 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007671
7672 if (!list_empty_careful(&splice)) {
7673 spin_lock(&root->fs_info->delalloc_lock);
7674 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
7675 spin_unlock(&root->fs_info->delalloc_lock);
7676 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00007677 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04007678}
7679
Chris Mason39279cc2007-06-12 06:35:45 -04007680static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7681 const char *symname)
7682{
7683 struct btrfs_trans_handle *trans;
7684 struct btrfs_root *root = BTRFS_I(dir)->root;
7685 struct btrfs_path *path;
7686 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007687 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007688 int err;
7689 int drop_inode = 0;
7690 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007691 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007692 int name_len;
7693 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007694 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007695 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007696 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04007697
7698 name_len = strlen(symname) + 1;
7699 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7700 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007701
Josef Bacik9ed74f22009-09-11 16:12:44 -04007702 /*
7703 * 2 items for inode item and ref
7704 * 2 items for dir items
7705 * 1 item for xattr if selinux is on
7706 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007707 trans = btrfs_start_transaction(root, 5);
7708 if (IS_ERR(trans))
7709 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007710
Li Zefan581bb052011-04-20 10:06:11 +08007711 err = btrfs_find_free_ino(root, &objectid);
7712 if (err)
7713 goto out_unlock;
7714
Josef Bacikaec74772008-07-24 12:12:38 -04007715 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007716 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007717 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007718 if (IS_ERR(inode)) {
7719 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007720 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007721 }
Chris Mason39279cc2007-06-12 06:35:45 -04007722
Eric Paris2a7dba32011-02-01 11:05:39 -05007723 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007724 if (err) {
7725 drop_inode = 1;
7726 goto out_unlock;
7727 }
7728
Casey Schauflerad19db72011-12-15 10:09:07 -05007729 /*
7730 * If the active LSM wants to access the inode during
7731 * d_instantiate it needs these. Smack checks to see
7732 * if the filesystem supports xattrs by looking at the
7733 * ops vector.
7734 */
7735 inode->i_fop = &btrfs_file_operations;
7736 inode->i_op = &btrfs_file_inode_operations;
7737
Josef Bacika1b075d2010-11-19 20:36:11 +00007738 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007739 if (err)
7740 drop_inode = 1;
7741 else {
7742 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007743 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007744 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007745 }
Chris Mason39279cc2007-06-12 06:35:45 -04007746 if (drop_inode)
7747 goto out_unlock;
7748
7749 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007750 if (!path) {
7751 err = -ENOMEM;
7752 drop_inode = 1;
7753 goto out_unlock;
7754 }
Li Zefan33345d012011-04-20 10:31:50 +08007755 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007756 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007757 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7758 datasize = btrfs_file_extent_calc_inline_size(name_len);
7759 err = btrfs_insert_empty_item(trans, root, path, &key,
7760 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007761 if (err) {
7762 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007763 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007764 goto out_unlock;
7765 }
Chris Mason5f39d392007-10-15 16:14:19 -04007766 leaf = path->nodes[0];
7767 ei = btrfs_item_ptr(leaf, path->slots[0],
7768 struct btrfs_file_extent_item);
7769 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7770 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007771 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007772 btrfs_set_file_extent_encryption(leaf, ei, 0);
7773 btrfs_set_file_extent_compression(leaf, ei, 0);
7774 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7775 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7776
Chris Mason39279cc2007-06-12 06:35:45 -04007777 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007778 write_extent_buffer(leaf, symname, ptr, name_len);
7779 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007780 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007781
Chris Mason39279cc2007-06-12 06:35:45 -04007782 inode->i_op = &btrfs_symlink_inode_operations;
7783 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007784 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007785 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007786 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007787 err = btrfs_update_inode(trans, root, inode);
7788 if (err)
7789 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007790
7791out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007792 if (!err)
7793 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007794 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007795 if (drop_inode) {
7796 inode_dec_link_count(inode);
7797 iput(inode);
7798 }
Liu Bob53d3f52012-11-14 14:34:34 +00007799 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04007800 return err;
7801}
Chris Mason16432982008-04-10 10:23:21 -04007802
Josef Bacik0af3d002010-06-21 14:48:16 -04007803static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7804 u64 start, u64 num_bytes, u64 min_size,
7805 loff_t actual_len, u64 *alloc_hint,
7806 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007807{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007808 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7809 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007810 struct btrfs_root *root = BTRFS_I(inode)->root;
7811 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007812 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007813 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007814 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007815 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007816
Josef Bacik0af3d002010-06-21 14:48:16 -04007817 if (trans)
7818 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007819 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007820 if (own_trans) {
7821 trans = btrfs_start_transaction(root, 3);
7822 if (IS_ERR(trans)) {
7823 ret = PTR_ERR(trans);
7824 break;
7825 }
Yan Zhengd899e052008-10-30 14:25:28 -04007826 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007827
Yan, Zhengefa56462010-05-16 10:49:59 -04007828 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007829 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007830 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007831 if (own_trans)
7832 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007833 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007834 }
7835
Yan Zhengd899e052008-10-30 14:25:28 -04007836 ret = insert_reserved_file_extent(trans, inode,
7837 cur_offset, ins.objectid,
7838 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007839 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007840 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007841 if (ret) {
7842 btrfs_abort_transaction(trans, root, ret);
7843 if (own_trans)
7844 btrfs_end_transaction(trans, root);
7845 break;
7846 }
Chris Masona1ed8352009-09-11 12:27:37 -04007847 btrfs_drop_extent_cache(inode, cur_offset,
7848 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007849
Josef Bacik5dc562c2012-08-17 13:14:17 -04007850 em = alloc_extent_map();
7851 if (!em) {
7852 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7853 &BTRFS_I(inode)->runtime_flags);
7854 goto next;
7855 }
7856
7857 em->start = cur_offset;
7858 em->orig_start = cur_offset;
7859 em->len = ins.offset;
7860 em->block_start = ins.objectid;
7861 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05007862 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007863 em->bdev = root->fs_info->fs_devices->latest_bdev;
7864 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7865 em->generation = trans->transid;
7866
7867 while (1) {
7868 write_lock(&em_tree->lock);
7869 ret = add_extent_mapping(em_tree, em);
7870 if (!ret)
7871 list_move(&em->list,
7872 &em_tree->modified_extents);
7873 write_unlock(&em_tree->lock);
7874 if (ret != -EEXIST)
7875 break;
7876 btrfs_drop_extent_cache(inode, cur_offset,
7877 cur_offset + ins.offset - 1,
7878 0);
7879 }
7880 free_extent_map(em);
7881next:
Yan Zhengd899e052008-10-30 14:25:28 -04007882 num_bytes -= ins.offset;
7883 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007884 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007885
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007886 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007887 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007888 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007889 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007890 (actual_len > inode->i_size) &&
7891 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007892 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007893 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007894 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007895 i_size = cur_offset;
7896 i_size_write(inode, i_size);
7897 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007898 }
7899
Yan Zhengd899e052008-10-30 14:25:28 -04007900 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007901
7902 if (ret) {
7903 btrfs_abort_transaction(trans, root, ret);
7904 if (own_trans)
7905 btrfs_end_transaction(trans, root);
7906 break;
7907 }
Yan Zhengd899e052008-10-30 14:25:28 -04007908
Josef Bacik0af3d002010-06-21 14:48:16 -04007909 if (own_trans)
7910 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007911 }
Yan Zhengd899e052008-10-30 14:25:28 -04007912 return ret;
7913}
7914
Josef Bacik0af3d002010-06-21 14:48:16 -04007915int btrfs_prealloc_file_range(struct inode *inode, int mode,
7916 u64 start, u64 num_bytes, u64 min_size,
7917 loff_t actual_len, u64 *alloc_hint)
7918{
7919 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7920 min_size, actual_len, alloc_hint,
7921 NULL);
7922}
7923
7924int btrfs_prealloc_file_range_trans(struct inode *inode,
7925 struct btrfs_trans_handle *trans, int mode,
7926 u64 start, u64 num_bytes, u64 min_size,
7927 loff_t actual_len, u64 *alloc_hint)
7928{
7929 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7930 min_size, actual_len, alloc_hint, trans);
7931}
7932
Chris Masone6dcd2d2008-07-17 12:53:50 -04007933static int btrfs_set_page_dirty(struct page *page)
7934{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007935 return __set_page_dirty_nobuffers(page);
7936}
7937
Al Viro10556cb2011-06-20 19:28:19 -04007938static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007939{
Li Zefanb83cc962010-12-20 16:04:08 +08007940 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007941 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007942
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007943 if (mask & MAY_WRITE &&
7944 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7945 if (btrfs_root_readonly(root))
7946 return -EROFS;
7947 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7948 return -EACCES;
7949 }
Al Viro2830ba72011-06-20 19:16:29 -04007950 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007951}
Chris Mason39279cc2007-06-12 06:35:45 -04007952
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007953static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007954 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007955 .lookup = btrfs_lookup,
7956 .create = btrfs_create,
7957 .unlink = btrfs_unlink,
7958 .link = btrfs_link,
7959 .mkdir = btrfs_mkdir,
7960 .rmdir = btrfs_rmdir,
7961 .rename = btrfs_rename,
7962 .symlink = btrfs_symlink,
7963 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007964 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007965 .setxattr = btrfs_setxattr,
7966 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007967 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007968 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007969 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007970 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007971};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007972static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007973 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007974 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007975 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007976};
Yan, Zheng76dda932009-09-21 16:00:26 -04007977
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007978static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007979 .llseek = generic_file_llseek,
7980 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007981 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007982 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007983#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007984 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007985#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007986 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007987 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007988};
7989
Chris Masond1310b22008-01-24 16:13:08 -05007990static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007991 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007992 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007993 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007994 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007995 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007996 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007997 .set_bit_hook = btrfs_set_bit_hook,
7998 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007999 .merge_extent_hook = btrfs_merge_extent_hook,
8000 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008001};
8002
Chris Mason35054392009-01-21 13:11:13 -05008003/*
8004 * btrfs doesn't support the bmap operation because swapfiles
8005 * use bmap to make a mapping of extents in the file. They assume
8006 * these extents won't change over the life of the file and they
8007 * use the bmap result to do IO directly to the drive.
8008 *
8009 * the btrfs bmap call would return logical addresses that aren't
8010 * suitable for IO and they also will change frequently as COW
8011 * operations happen. So, swapfile + btrfs == corruption.
8012 *
8013 * For now we're avoiding this by dropping bmap.
8014 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008015static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008016 .readpage = btrfs_readpage,
8017 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04008018 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008019 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008020 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008021 .invalidatepage = btrfs_invalidatepage,
8022 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008023 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008024 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008025};
8026
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008027static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008028 .readpage = btrfs_readpage,
8029 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008030 .invalidatepage = btrfs_invalidatepage,
8031 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008032};
8033
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008034static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008035 .getattr = btrfs_getattr,
8036 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008037 .setxattr = btrfs_setxattr,
8038 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008039 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008040 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008041 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008042 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008043 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008044 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008045};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008046static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008047 .getattr = btrfs_getattr,
8048 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008049 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008050 .setxattr = btrfs_setxattr,
8051 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008052 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008053 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008054 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008055 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008056};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008057static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008058 .readlink = generic_readlink,
8059 .follow_link = page_follow_link_light,
8060 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008061 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008062 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008063 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008064 .setxattr = btrfs_setxattr,
8065 .getxattr = btrfs_getxattr,
8066 .listxattr = btrfs_listxattr,
8067 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008068 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008069 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008070};
Yan, Zheng76dda932009-09-21 16:00:26 -04008071
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008072const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008073 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008074 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008075};