blob: 02d946a61dddb712f23a50c8e634b6e68f5ea5e1 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050042#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040043#include "ctree.h"
44#include "disk-io.h"
45#include "transaction.h"
46#include "btrfs_inode.h"
47#include "ioctl.h"
48#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040049#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040050#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040051#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020052#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040053#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050054#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050055#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080056#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040057
58struct btrfs_iget_args {
59 u64 ino;
60 struct btrfs_root *root;
61};
62
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070063static const struct inode_operations btrfs_dir_inode_operations;
64static const struct inode_operations btrfs_symlink_inode_operations;
65static const struct inode_operations btrfs_dir_ro_inode_operations;
66static const struct inode_operations btrfs_special_inode_operations;
67static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070068static const struct address_space_operations btrfs_aops;
69static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070070static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050071static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000074static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075struct kmem_cache *btrfs_trans_handle_cachep;
76struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050078struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
80#define S_SHIFT 12
81static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
82 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
83 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
84 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
85 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
86 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
87 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
88 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
89};
90
Josef Bacika41ad392011-01-31 15:30:16 -050091static int btrfs_setsize(struct inode *inode, loff_t newsize);
92static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040093static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050094static noinline int cow_file_range(struct inode *inode,
95 struct page *locked_page,
96 u64 start, u64 end, int *page_started,
97 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -040098static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
99 u64 len, u64 orig_start,
100 u64 block_start, u64 block_len,
101 u64 orig_block_len, int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400102
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000103static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500104 struct inode *inode, struct inode *dir,
105 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500106{
107 int err;
108
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000109 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500110 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500111 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500112 return err;
113}
114
Chris Masond352ac62008-09-29 15:18:18 -0400115/*
Chris Masonc8b97812008-10-29 14:49:59 -0400116 * this does all the hard work for inserting an inline extent into
117 * the btree. The caller should have done a btrfs_drop_extents so that
118 * no overlapping inline items exist in the btree
119 */
Chris Masond3977122009-01-05 21:25:51 -0500120static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400121 struct btrfs_root *root, struct inode *inode,
122 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000123 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400124 struct page **compressed_pages)
125{
126 struct btrfs_key key;
127 struct btrfs_path *path;
128 struct extent_buffer *leaf;
129 struct page *page = NULL;
130 char *kaddr;
131 unsigned long ptr;
132 struct btrfs_file_extent_item *ei;
133 int err = 0;
134 int ret;
135 size_t cur_size = size;
136 size_t datasize;
137 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400138
Li Zefanfe3f5662011-03-28 08:30:38 +0000139 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400140 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400141
Chris Masond3977122009-01-05 21:25:51 -0500142 path = btrfs_alloc_path();
143 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400144 return -ENOMEM;
145
Chris Masonb9473432009-03-13 11:00:37 -0400146 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Li Zefan33345d012011-04-20 10:31:50 +0800148 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400149 key.offset = start;
150 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400151 datasize = btrfs_file_extent_calc_inline_size(cur_size);
152
153 inode_add_bytes(inode, size);
154 ret = btrfs_insert_empty_item(trans, root, path, &key,
155 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400156 if (ret) {
157 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400158 goto fail;
159 }
160 leaf = path->nodes[0];
161 ei = btrfs_item_ptr(leaf, path->slots[0],
162 struct btrfs_file_extent_item);
163 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
164 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
165 btrfs_set_file_extent_encryption(leaf, ei, 0);
166 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
167 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
168 ptr = btrfs_file_extent_inline_start(ei);
169
Li Zefan261507a02010-12-17 14:21:50 +0800170 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct page *cpage;
172 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500173 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400174 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500175 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400176 PAGE_CACHE_SIZE);
177
Cong Wang7ac687d2011-11-25 23:14:28 +0800178 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400179 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800180 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400181
182 i++;
183 ptr += cur_size;
184 compressed_size -= cur_size;
185 }
186 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800187 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400188 } else {
189 page = find_get_page(inode->i_mapping,
190 start >> PAGE_CACHE_SHIFT);
191 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800192 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 offset = start & (PAGE_CACHE_SIZE - 1);
194 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800195 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400196 page_cache_release(page);
197 }
198 btrfs_mark_buffer_dirty(leaf);
199 btrfs_free_path(path);
200
Yan, Zhengc2167752009-11-12 09:34:21 +0000201 /*
202 * we're an inline extent, so nobody can
203 * extend the file past i_size without locking
204 * a page we already have locked.
205 *
206 * We must do any isize and inode updates
207 * before we unlock the pages. Otherwise we
208 * could end up racing with unlink.
209 */
Chris Masonc8b97812008-10-29 14:49:59 -0400210 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100211 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000212
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100213 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400214fail:
215 btrfs_free_path(path);
216 return err;
217}
218
219
220/*
221 * conditionally insert an inline extent into the file. This
222 * does the checks required to make sure the data is small enough
223 * to fit as an inline extent.
224 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400225static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400226 struct btrfs_root *root,
227 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000228 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400229 struct page **compressed_pages)
230{
231 u64 isize = i_size_read(inode);
232 u64 actual_end = min(end + 1, isize);
233 u64 inline_len = actual_end - start;
234 u64 aligned_end = (end + root->sectorsize - 1) &
235 ~((u64)root->sectorsize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 u64 data_len = inline_len;
237 int ret;
238
239 if (compressed_size)
240 data_len = compressed_size;
241
242 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400243 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400244 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
245 (!compressed_size &&
246 (actual_end & (root->sectorsize - 1)) == 0) ||
247 end + 1 < isize ||
248 data_len > root->fs_info->max_inline) {
249 return 1;
250 }
251
Josef Bacik26714852012-08-29 12:24:27 -0400252 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100253 if (ret)
254 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400255
256 if (isize > actual_end)
257 inline_len = min_t(u64, isize, actual_end);
258 ret = insert_inline_extent(trans, root, inode, start,
259 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000260 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400261 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100262 btrfs_abort_transaction(trans, root, ret);
263 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400264 } else if (ret == -ENOSPC) {
265 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100266 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400267
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400268 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400269 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400270 return 0;
271}
272
Chris Mason771ed682008-11-06 22:02:51 -0500273struct async_extent {
274 u64 start;
275 u64 ram_size;
276 u64 compressed_size;
277 struct page **pages;
278 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800279 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500280 struct list_head list;
281};
282
283struct async_cow {
284 struct inode *inode;
285 struct btrfs_root *root;
286 struct page *locked_page;
287 u64 start;
288 u64 end;
289 struct list_head extents;
290 struct btrfs_work work;
291};
292
293static noinline int add_async_extent(struct async_cow *cow,
294 u64 start, u64 ram_size,
295 u64 compressed_size,
296 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800297 unsigned long nr_pages,
298 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500299{
300 struct async_extent *async_extent;
301
302 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100303 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500304 async_extent->start = start;
305 async_extent->ram_size = ram_size;
306 async_extent->compressed_size = compressed_size;
307 async_extent->pages = pages;
308 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800309 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500310 list_add_tail(&async_extent->list, &cow->extents);
311 return 0;
312}
313
Chris Masonc8b97812008-10-29 14:49:59 -0400314/*
Chris Mason771ed682008-11-06 22:02:51 -0500315 * we create compressed extents in two phases. The first
316 * phase compresses a range of pages that have already been
317 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400318 *
Chris Mason771ed682008-11-06 22:02:51 -0500319 * This is done inside an ordered work queue, and the compression
320 * is spread across many cpus. The actual IO submission is step
321 * two, and the ordered work queue takes care of making sure that
322 * happens in the same order things were put onto the queue by
323 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400324 *
Chris Mason771ed682008-11-06 22:02:51 -0500325 * If this code finds it can't get good compression, it puts an
326 * entry onto the work queue to write the uncompressed bytes. This
327 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300328 * are written in the same order that the flusher thread sent them
329 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400330 */
Chris Mason771ed682008-11-06 22:02:51 -0500331static noinline int compress_file_range(struct inode *inode,
332 struct page *locked_page,
333 u64 start, u64 end,
334 struct async_cow *async_cow,
335 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400336{
337 struct btrfs_root *root = BTRFS_I(inode)->root;
338 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400339 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400340 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400341 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500342 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400343 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400344 struct page **pages = NULL;
345 unsigned long nr_pages;
346 unsigned long nr_pages_ret = 0;
347 unsigned long total_compressed = 0;
348 unsigned long total_in = 0;
349 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500350 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400351 int i;
352 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800353 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400354
Liu Bo4cb13e52012-03-29 09:57:45 -0400355 /* if this is a small write inside eof, kick off a defrag */
356 if ((end - start + 1) < 16 * 1024 &&
357 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400358 btrfs_add_inode_defrag(NULL, inode);
359
Chris Mason42dc7ba2008-12-15 11:44:56 -0500360 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400361again:
362 will_compress = 0;
363 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
364 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
365
Chris Masonf03d9302009-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 Masonf03d9302009-02-04 09:31:06 -0500544cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500545 /*
546 * No compression, but we still need to write the pages in
547 * the file we've been given so far. redirty the locked
548 * page if it corresponds to our extent and set things up
549 * for the async work queue to run cow_file_range to do
550 * the normal delalloc dance
551 */
552 if (page_offset(locked_page) >= start &&
553 page_offset(locked_page) <= end) {
554 __set_page_dirty_nobuffers(locked_page);
555 /* unlocked later on in the async handlers */
556 }
Li Zefan261507a02010-12-17 14:21:50 +0800557 add_async_extent(async_cow, start, end - start + 1,
558 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500559 *num_added += 1;
560 }
561
562out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100563 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500564
565free_pages_out:
566 for (i = 0; i < nr_pages_ret; i++) {
567 WARN_ON(pages[i]->mapping);
568 page_cache_release(pages[i]);
569 }
Chris Masond3977122009-01-05 21:25:51 -0500570 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500571
572 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100573
574cleanup_and_out:
575 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
576 start, end, NULL,
577 EXTENT_CLEAR_UNLOCK_PAGE |
578 EXTENT_CLEAR_DIRTY |
579 EXTENT_CLEAR_DELALLOC |
580 EXTENT_SET_WRITEBACK |
581 EXTENT_END_WRITEBACK);
582 if (!trans || IS_ERR(trans))
583 btrfs_error(root->fs_info, ret, "Failed to join transaction");
584 else
585 btrfs_abort_transaction(trans, root, ret);
586 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500587}
588
589/*
590 * phase two of compressed writeback. This is the ordered portion
591 * of the code, which only gets called in the order the work was
592 * queued. We walk all the async extents created by compress_file_range
593 * and send them down to the disk.
594 */
595static noinline int submit_compressed_extents(struct inode *inode,
596 struct async_cow *async_cow)
597{
598 struct async_extent *async_extent;
599 u64 alloc_hint = 0;
600 struct btrfs_trans_handle *trans;
601 struct btrfs_key ins;
602 struct extent_map *em;
603 struct btrfs_root *root = BTRFS_I(inode)->root;
604 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
605 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500606 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500607
608 if (list_empty(&async_cow->extents))
609 return 0;
610
Chris Mason771ed682008-11-06 22:02:51 -0500611
Chris Masond3977122009-01-05 21:25:51 -0500612 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500613 async_extent = list_entry(async_cow->extents.next,
614 struct async_extent, list);
615 list_del(&async_extent->list);
616
617 io_tree = &BTRFS_I(inode)->io_tree;
618
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500619retry:
Chris Mason771ed682008-11-06 22:02:51 -0500620 /* did the compression code fall back to uncompressed IO? */
621 if (!async_extent->pages) {
622 int page_started = 0;
623 unsigned long nr_written = 0;
624
625 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000626 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100627 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500628
629 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500630 ret = cow_file_range(inode, async_cow->locked_page,
631 async_extent->start,
632 async_extent->start +
633 async_extent->ram_size - 1,
634 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500635
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100636 /* JDM XXX */
637
Chris Mason771ed682008-11-06 22:02:51 -0500638 /*
639 * if page_started, cow_file_range inserted an
640 * inline extent and took care of all the unlocking
641 * and IO for us. Otherwise, we need to submit
642 * all those pages down to the drive.
643 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500644 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500645 extent_write_locked_range(io_tree,
646 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500647 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500648 async_extent->ram_size - 1,
649 btrfs_get_extent,
650 WB_SYNC_ALL);
651 kfree(async_extent);
652 cond_resched();
653 continue;
654 }
655
656 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100657 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500658
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400659 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100660 if (IS_ERR(trans)) {
661 ret = PTR_ERR(trans);
662 } else {
663 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
664 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500665 async_extent->compressed_size,
666 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500667 0, alloc_hint, &ins, 1);
Miao Xie962197b2012-09-11 23:27:35 -0600668 if (ret && ret != -ENOSPC)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100669 btrfs_abort_transaction(trans, root, ret);
670 btrfs_end_transaction(trans, root);
671 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000672
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500673 if (ret) {
674 int i;
675 for (i = 0; i < async_extent->nr_pages; i++) {
676 WARN_ON(async_extent->pages[i]->mapping);
677 page_cache_release(async_extent->pages[i]);
678 }
679 kfree(async_extent->pages);
680 async_extent->nr_pages = 0;
681 async_extent->pages = NULL;
682 unlock_extent(io_tree, async_extent->start,
683 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100684 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100685 if (ret == -ENOSPC)
686 goto retry;
687 goto out_free; /* JDM: Requeue? */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500688 }
689
Yan, Zhengc2167752009-11-12 09:34:21 +0000690 /*
691 * here we're doing allocation and writeback of the
692 * compressed pages
693 */
694 btrfs_drop_extent_cache(inode, async_extent->start,
695 async_extent->start +
696 async_extent->ram_size - 1, 0);
697
David Sterba172ddd62011-04-21 00:48:27 +0200698 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100699 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500700 em->start = async_extent->start;
701 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500702 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500703
704 em->block_start = ins.objectid;
705 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500706 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500707 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800708 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500709 set_bit(EXTENT_FLAG_PINNED, &em->flags);
710 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400711 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500712
Chris Masond3977122009-01-05 21:25:51 -0500713 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400714 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500715 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400716 if (!ret)
717 list_move(&em->list,
718 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400719 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500720 if (ret != -EEXIST) {
721 free_extent_map(em);
722 break;
723 }
724 btrfs_drop_extent_cache(inode, async_extent->start,
725 async_extent->start +
726 async_extent->ram_size - 1, 0);
727 }
728
Li Zefan261507a02010-12-17 14:21:50 +0800729 ret = btrfs_add_ordered_extent_compress(inode,
730 async_extent->start,
731 ins.objectid,
732 async_extent->ram_size,
733 ins.offset,
734 BTRFS_ORDERED_COMPRESSED,
735 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100736 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500737
Chris Mason771ed682008-11-06 22:02:51 -0500738 /*
739 * clear dirty, set writeback and unlock the pages.
740 */
741 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400742 &BTRFS_I(inode)->io_tree,
743 async_extent->start,
744 async_extent->start +
745 async_extent->ram_size - 1,
746 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
747 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400748 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400749 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500750
751 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500752 async_extent->start,
753 async_extent->ram_size,
754 ins.objectid,
755 ins.offset, async_extent->pages,
756 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500757
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100758 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500759 alloc_hint = ins.objectid + ins.offset;
760 kfree(async_extent);
761 cond_resched();
762 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100763 ret = 0;
764out:
765 return ret;
766out_free:
767 kfree(async_extent);
768 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500769}
770
Josef Bacik4b46fce2010-05-23 11:00:55 -0400771static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
772 u64 num_bytes)
773{
774 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
775 struct extent_map *em;
776 u64 alloc_hint = 0;
777
778 read_lock(&em_tree->lock);
779 em = search_extent_mapping(em_tree, start, num_bytes);
780 if (em) {
781 /*
782 * if block start isn't an actual block number then find the
783 * first block in this inode and use that as a hint. If that
784 * block is also bogus then just don't worry about it.
785 */
786 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
787 free_extent_map(em);
788 em = search_extent_mapping(em_tree, 0, 0);
789 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
790 alloc_hint = em->block_start;
791 if (em)
792 free_extent_map(em);
793 } else {
794 alloc_hint = em->block_start;
795 free_extent_map(em);
796 }
797 }
798 read_unlock(&em_tree->lock);
799
800 return alloc_hint;
801}
802
Chris Mason771ed682008-11-06 22:02:51 -0500803/*
804 * when extent_io.c finds a delayed allocation range in the file,
805 * the call backs end up in this code. The basic idea is to
806 * allocate extents on disk for the range, and create ordered data structs
807 * in ram to track those extents.
808 *
809 * locked_page is the page that writepage had locked already. We use
810 * it to make sure we don't do extra locks or unlocks.
811 *
812 * *page_started is set to one if we unlock locked_page and do everything
813 * required to start IO on it. It may be clean and already done with
814 * IO when we return.
815 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000816static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
817 struct inode *inode,
818 struct btrfs_root *root,
819 struct page *locked_page,
820 u64 start, u64 end, int *page_started,
821 unsigned long *nr_written,
822 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500823{
Chris Mason771ed682008-11-06 22:02:51 -0500824 u64 alloc_hint = 0;
825 u64 num_bytes;
826 unsigned long ram_size;
827 u64 disk_num_bytes;
828 u64 cur_alloc_size;
829 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500830 struct btrfs_key ins;
831 struct extent_map *em;
832 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
833 int ret = 0;
834
Liu Bo83eea1f2012-07-10 05:28:39 -0600835 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500836
Chris Mason771ed682008-11-06 22:02:51 -0500837 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
838 num_bytes = max(blocksize, num_bytes);
839 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500840
Chris Mason4cb53002011-05-24 15:35:30 -0400841 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400842 if (num_bytes < 64 * 1024 &&
843 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400844 btrfs_add_inode_defrag(trans, inode);
845
Chris Mason771ed682008-11-06 22:02:51 -0500846 if (start == 0) {
847 /* lets try to make an inline extent */
848 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000849 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500850 if (ret == 0) {
851 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400852 &BTRFS_I(inode)->io_tree,
853 start, end, NULL,
854 EXTENT_CLEAR_UNLOCK_PAGE |
855 EXTENT_CLEAR_UNLOCK |
856 EXTENT_CLEAR_DELALLOC |
857 EXTENT_CLEAR_DIRTY |
858 EXTENT_SET_WRITEBACK |
859 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000860
Chris Mason771ed682008-11-06 22:02:51 -0500861 *nr_written = *nr_written +
862 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
863 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500864 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100865 } else if (ret < 0) {
866 btrfs_abort_transaction(trans, root, ret);
867 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500868 }
869 }
Chris Masonc8b97812008-10-29 14:49:59 -0400870
871 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200872 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400873
Josef Bacik4b46fce2010-05-23 11:00:55 -0400874 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400875 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400876
Chris Masond3977122009-01-05 21:25:51 -0500877 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400878 unsigned long op;
879
Josef Bacik287a0ab2010-03-19 18:07:23 +0000880 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400881 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500882 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500883 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100884 if (ret < 0) {
885 btrfs_abort_transaction(trans, root, ret);
886 goto out_unlock;
887 }
Chris Masond3977122009-01-05 21:25:51 -0500888
David Sterba172ddd62011-04-21 00:48:27 +0200889 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400891 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500892 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500893 ram_size = ins.offset;
894 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400895
Chris Masone6dcd2d2008-07-17 12:53:50 -0400896 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400897 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500898 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400899 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400900 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400901 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400902
Chris Masond3977122009-01-05 21:25:51 -0500903 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400904 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400905 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400906 if (!ret)
907 list_move(&em->list,
908 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400909 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400910 if (ret != -EEXIST) {
911 free_extent_map(em);
912 break;
913 }
914 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400915 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400916 }
917
Chris Mason98d20f62008-04-14 09:46:10 -0400918 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400919 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500920 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100921 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400922
Yan Zheng17d217f2008-12-12 10:03:38 -0500923 if (root->root_key.objectid ==
924 BTRFS_DATA_RELOC_TREE_OBJECTID) {
925 ret = btrfs_reloc_clone_csums(inode, start,
926 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100927 if (ret) {
928 btrfs_abort_transaction(trans, root, ret);
929 goto out_unlock;
930 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500931 }
932
Chris Masond3977122009-01-05 21:25:51 -0500933 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400934 break;
Chris Masond3977122009-01-05 21:25:51 -0500935
Chris Masonc8b97812008-10-29 14:49:59 -0400936 /* we're not doing compressed IO, don't unlock the first
937 * page (which the caller expects to stay locked), don't
938 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400939 *
940 * Do set the Private2 bit so we know this page was properly
941 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400942 */
Chris Masona791e352009-10-08 11:27:10 -0400943 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
944 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
945 EXTENT_SET_PRIVATE2;
946
Chris Masonc8b97812008-10-29 14:49:59 -0400947 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
948 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400949 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400950 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500951 num_bytes -= cur_alloc_size;
952 alloc_hint = ins.objectid + ins.offset;
953 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400954 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100955out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500956 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000957
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100958out_unlock:
959 extent_clear_unlock_delalloc(inode,
960 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400961 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100962 EXTENT_CLEAR_UNLOCK_PAGE |
963 EXTENT_CLEAR_UNLOCK |
964 EXTENT_CLEAR_DELALLOC |
965 EXTENT_CLEAR_DIRTY |
966 EXTENT_SET_WRITEBACK |
967 EXTENT_END_WRITEBACK);
968
969 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500970}
Chris Masonc8b97812008-10-29 14:49:59 -0400971
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000972static noinline int cow_file_range(struct inode *inode,
973 struct page *locked_page,
974 u64 start, u64 end, int *page_started,
975 unsigned long *nr_written,
976 int unlock)
977{
978 struct btrfs_trans_handle *trans;
979 struct btrfs_root *root = BTRFS_I(inode)->root;
980 int ret;
981
982 trans = btrfs_join_transaction(root);
983 if (IS_ERR(trans)) {
984 extent_clear_unlock_delalloc(inode,
985 &BTRFS_I(inode)->io_tree,
986 start, end, locked_page,
987 EXTENT_CLEAR_UNLOCK_PAGE |
988 EXTENT_CLEAR_UNLOCK |
989 EXTENT_CLEAR_DELALLOC |
990 EXTENT_CLEAR_DIRTY |
991 EXTENT_SET_WRITEBACK |
992 EXTENT_END_WRITEBACK);
993 return PTR_ERR(trans);
994 }
995 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
996
997 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
998 page_started, nr_written, unlock);
999
1000 btrfs_end_transaction(trans, root);
1001
1002 return ret;
1003}
1004
Chris Mason771ed682008-11-06 22:02:51 -05001005/*
1006 * work queue call back to started compression on a file and pages
1007 */
1008static noinline void async_cow_start(struct btrfs_work *work)
1009{
1010 struct async_cow *async_cow;
1011 int num_added = 0;
1012 async_cow = container_of(work, struct async_cow, work);
1013
1014 compress_file_range(async_cow->inode, async_cow->locked_page,
1015 async_cow->start, async_cow->end, async_cow,
1016 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001017 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001018 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001019 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001020 }
Chris Mason771ed682008-11-06 22:02:51 -05001021}
1022
1023/*
1024 * work queue call back to submit previously compressed pages
1025 */
1026static noinline void async_cow_submit(struct btrfs_work *work)
1027{
1028 struct async_cow *async_cow;
1029 struct btrfs_root *root;
1030 unsigned long nr_pages;
1031
1032 async_cow = container_of(work, struct async_cow, work);
1033
1034 root = async_cow->root;
1035 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1036 PAGE_CACHE_SHIFT;
1037
Josef Bacik66657b32012-08-01 15:36:24 -04001038 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001039 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001040 waitqueue_active(&root->fs_info->async_submit_wait))
1041 wake_up(&root->fs_info->async_submit_wait);
1042
Chris Masond3977122009-01-05 21:25:51 -05001043 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001044 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001045}
Chris Masonc8b97812008-10-29 14:49:59 -04001046
Chris Mason771ed682008-11-06 22:02:51 -05001047static noinline void async_cow_free(struct btrfs_work *work)
1048{
1049 struct async_cow *async_cow;
1050 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001051 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001052 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001053 kfree(async_cow);
1054}
1055
1056static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1057 u64 start, u64 end, int *page_started,
1058 unsigned long *nr_written)
1059{
1060 struct async_cow *async_cow;
1061 struct btrfs_root *root = BTRFS_I(inode)->root;
1062 unsigned long nr_pages;
1063 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001064 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001065
Chris Masona3429ab2009-10-08 12:30:20 -04001066 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1067 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001068 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001069 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001070 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001071 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001072 async_cow->root = root;
1073 async_cow->locked_page = locked_page;
1074 async_cow->start = start;
1075
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001076 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001077 cur_end = end;
1078 else
1079 cur_end = min(end, start + 512 * 1024 - 1);
1080
1081 async_cow->end = cur_end;
1082 INIT_LIST_HEAD(&async_cow->extents);
1083
1084 async_cow->work.func = async_cow_start;
1085 async_cow->work.ordered_func = async_cow_submit;
1086 async_cow->work.ordered_free = async_cow_free;
1087 async_cow->work.flags = 0;
1088
Chris Mason771ed682008-11-06 22:02:51 -05001089 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1090 PAGE_CACHE_SHIFT;
1091 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1092
1093 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1094 &async_cow->work);
1095
1096 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1097 wait_event(root->fs_info->async_submit_wait,
1098 (atomic_read(&root->fs_info->async_delalloc_pages) <
1099 limit));
1100 }
1101
Chris Masond3977122009-01-05 21:25:51 -05001102 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001103 atomic_read(&root->fs_info->async_delalloc_pages)) {
1104 wait_event(root->fs_info->async_submit_wait,
1105 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1106 0));
1107 }
1108
1109 *nr_written += nr_pages;
1110 start = cur_end + 1;
1111 }
1112 *page_started = 1;
1113 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001114}
1115
Chris Masond3977122009-01-05 21:25:51 -05001116static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001117 u64 bytenr, u64 num_bytes)
1118{
1119 int ret;
1120 struct btrfs_ordered_sum *sums;
1121 LIST_HEAD(list);
1122
Yan Zheng07d400a2009-01-06 11:42:00 -05001123 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001124 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001125 if (ret == 0 && list_empty(&list))
1126 return 0;
1127
1128 while (!list_empty(&list)) {
1129 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1130 list_del(&sums->list);
1131 kfree(sums);
1132 }
1133 return 1;
1134}
1135
Chris Masond352ac62008-09-29 15:18:18 -04001136/*
1137 * when nowcow writeback call back. This checks for snapshots or COW copies
1138 * of the extents that exist in the file, and COWs the file as required.
1139 *
1140 * If no cow copies or snapshots exist, we write directly to the existing
1141 * blocks on disk
1142 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001143static noinline int run_delalloc_nocow(struct inode *inode,
1144 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001145 u64 start, u64 end, int *page_started, int force,
1146 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001147{
Chris Masonbe20aa92007-12-17 20:14:01 -05001148 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001149 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001150 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001151 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001152 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001153 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001154 u64 cow_start;
1155 u64 cur_offset;
1156 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001157 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001158 u64 disk_bytenr;
1159 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001160 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001161 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001162 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001163 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001164 int nocow;
1165 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001166 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001167 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001168
1169 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001170 if (!path) {
1171 extent_clear_unlock_delalloc(inode,
1172 &BTRFS_I(inode)->io_tree,
1173 start, end, locked_page,
1174 EXTENT_CLEAR_UNLOCK_PAGE |
1175 EXTENT_CLEAR_UNLOCK |
1176 EXTENT_CLEAR_DELALLOC |
1177 EXTENT_CLEAR_DIRTY |
1178 EXTENT_SET_WRITEBACK |
1179 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001180 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001181 }
Li Zefan82d59022011-04-20 10:33:24 +08001182
Liu Bo83eea1f2012-07-10 05:28:39 -06001183 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001184
1185 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001186 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001187 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001188 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001189
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001190 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001191 extent_clear_unlock_delalloc(inode,
1192 &BTRFS_I(inode)->io_tree,
1193 start, end, locked_page,
1194 EXTENT_CLEAR_UNLOCK_PAGE |
1195 EXTENT_CLEAR_UNLOCK |
1196 EXTENT_CLEAR_DELALLOC |
1197 EXTENT_CLEAR_DIRTY |
1198 EXTENT_SET_WRITEBACK |
1199 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001200 btrfs_free_path(path);
1201 return PTR_ERR(trans);
1202 }
1203
Josef Bacik74b21072011-04-13 12:02:53 -04001204 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001205
Yan Zheng80ff3852008-10-30 14:20:02 -04001206 cow_start = (u64)-1;
1207 cur_offset = start;
1208 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001209 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001210 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001211 if (ret < 0) {
1212 btrfs_abort_transaction(trans, root, ret);
1213 goto error;
1214 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001215 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1216 leaf = path->nodes[0];
1217 btrfs_item_key_to_cpu(leaf, &found_key,
1218 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001219 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001220 found_key.type == BTRFS_EXTENT_DATA_KEY)
1221 path->slots[0]--;
1222 }
1223 check_prev = 0;
1224next_slot:
1225 leaf = path->nodes[0];
1226 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1227 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001228 if (ret < 0) {
1229 btrfs_abort_transaction(trans, root, ret);
1230 goto error;
1231 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001232 if (ret > 0)
1233 break;
1234 leaf = path->nodes[0];
1235 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001236
Yan Zheng80ff3852008-10-30 14:20:02 -04001237 nocow = 0;
1238 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001239 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001241
Li Zefan33345d012011-04-20 10:31:50 +08001242 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1244 found_key.offset > end)
1245 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001246
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 if (found_key.offset > cur_offset) {
1248 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001249 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001250 goto out_check;
1251 }
Chris Masonc31f8832008-01-08 15:46:31 -05001252
Yan Zheng80ff3852008-10-30 14:20:02 -04001253 fi = btrfs_item_ptr(leaf, path->slots[0],
1254 struct btrfs_file_extent_item);
1255 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001256
Yan Zhengd899e052008-10-30 14:25:28 -04001257 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1258 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001260 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 extent_end = found_key.offset +
1262 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001263 disk_num_bytes =
1264 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 if (extent_end <= start) {
1266 path->slots[0]++;
1267 goto next_slot;
1268 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001269 if (disk_bytenr == 0)
1270 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001271 if (btrfs_file_extent_compression(leaf, fi) ||
1272 btrfs_file_extent_encryption(leaf, fi) ||
1273 btrfs_file_extent_other_encoding(leaf, fi))
1274 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001275 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1276 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001277 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001279 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001280 found_key.offset -
1281 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001282 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001283 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001284 disk_bytenr += cur_offset - found_key.offset;
1285 num_bytes = min(end + 1, extent_end) - cur_offset;
1286 /*
1287 * force cow if csum exists in the range.
1288 * this ensure that csum for a given extent are
1289 * either valid or do not exist.
1290 */
1291 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1292 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 nocow = 1;
1294 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1295 extent_end = found_key.offset +
1296 btrfs_file_extent_inline_len(leaf, fi);
1297 extent_end = ALIGN(extent_end, root->sectorsize);
1298 } else {
1299 BUG_ON(1);
1300 }
1301out_check:
1302 if (extent_end <= start) {
1303 path->slots[0]++;
1304 goto next_slot;
1305 }
1306 if (!nocow) {
1307 if (cow_start == (u64)-1)
1308 cow_start = cur_offset;
1309 cur_offset = extent_end;
1310 if (cur_offset > end)
1311 break;
1312 path->slots[0]++;
1313 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001314 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001315
David Sterbab3b4aa72011-04-21 01:20:15 +02001316 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001318 ret = __cow_file_range(trans, inode, root, locked_page,
1319 cow_start, found_key.offset - 1,
1320 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001321 if (ret) {
1322 btrfs_abort_transaction(trans, root, ret);
1323 goto error;
1324 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001326 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001327
Yan Zhengd899e052008-10-30 14:25:28 -04001328 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1329 struct extent_map *em;
1330 struct extent_map_tree *em_tree;
1331 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001332 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001333 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001334 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001335 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001336 em->len = num_bytes;
1337 em->block_len = num_bytes;
1338 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001339 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001340 em->bdev = root->fs_info->fs_devices->latest_bdev;
1341 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001342 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001343 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001344 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001345 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001346 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001347 if (!ret)
1348 list_move(&em->list,
1349 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001350 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001351 if (ret != -EEXIST) {
1352 free_extent_map(em);
1353 break;
1354 }
1355 btrfs_drop_extent_cache(inode, em->start,
1356 em->start + em->len - 1, 0);
1357 }
1358 type = BTRFS_ORDERED_PREALLOC;
1359 } else {
1360 type = BTRFS_ORDERED_NOCOW;
1361 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001362
1363 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001364 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001365 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001366
Yan, Zhengefa56462010-05-16 10:49:59 -04001367 if (root->root_key.objectid ==
1368 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1369 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1370 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001371 if (ret) {
1372 btrfs_abort_transaction(trans, root, ret);
1373 goto error;
1374 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001375 }
1376
Yan Zhengd899e052008-10-30 14:25:28 -04001377 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001378 cur_offset, cur_offset + num_bytes - 1,
1379 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1380 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1381 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001382 cur_offset = extent_end;
1383 if (cur_offset > end)
1384 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001385 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001386 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001387
Josef Bacik17ca04a2012-05-31 15:58:55 -04001388 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001389 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001390 cur_offset = end;
1391 }
1392
Yan Zheng80ff3852008-10-30 14:20:02 -04001393 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001394 ret = __cow_file_range(trans, inode, root, locked_page,
1395 cow_start, end,
1396 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001397 if (ret) {
1398 btrfs_abort_transaction(trans, root, ret);
1399 goto error;
1400 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001401 }
1402
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403error:
Miao Xiea698d0752012-09-20 01:51:59 -06001404 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001405 if (!ret)
1406 ret = err;
1407
Josef Bacik17ca04a2012-05-31 15:58:55 -04001408 if (ret && cur_offset < end)
1409 extent_clear_unlock_delalloc(inode,
1410 &BTRFS_I(inode)->io_tree,
1411 cur_offset, end, locked_page,
1412 EXTENT_CLEAR_UNLOCK_PAGE |
1413 EXTENT_CLEAR_UNLOCK |
1414 EXTENT_CLEAR_DELALLOC |
1415 EXTENT_CLEAR_DIRTY |
1416 EXTENT_SET_WRITEBACK |
1417 EXTENT_END_WRITEBACK);
1418
Yan Zheng7ea394f2008-08-05 13:05:02 -04001419 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001420 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001421}
1422
Chris Masond352ac62008-09-29 15:18:18 -04001423/*
1424 * extent_io.c call back to do delayed allocation processing
1425 */
Chris Masonc8b97812008-10-29 14:49:59 -04001426static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001427 u64 start, u64 end, int *page_started,
1428 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001429{
Chris Masonbe20aa92007-12-17 20:14:01 -05001430 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001431 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001432
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001433 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001434 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001435 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001436 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001437 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001438 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001439 } else if (!btrfs_test_opt(root, COMPRESS) &&
1440 !(BTRFS_I(inode)->force_compress) &&
1441 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001442 ret = cow_file_range(inode, locked_page, start, end,
1443 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001444 } else {
1445 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1446 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001447 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001448 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001449 }
Chris Masonb888db22007-08-27 16:49:44 -04001450 return ret;
1451}
1452
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001453static void btrfs_split_extent_hook(struct inode *inode,
1454 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001455{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001456 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001457 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001458 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001459
Josef Bacik9e0baf62011-07-15 15:16:44 +00001460 spin_lock(&BTRFS_I(inode)->lock);
1461 BTRFS_I(inode)->outstanding_extents++;
1462 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001463}
1464
1465/*
1466 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1467 * extents so we can keep track of new extents that are just merged onto old
1468 * extents, such as when we are doing sequential writes, so we can properly
1469 * account for the metadata space we'll need.
1470 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001471static void btrfs_merge_extent_hook(struct inode *inode,
1472 struct extent_state *new,
1473 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001474{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001475 /* not delalloc, ignore it */
1476 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001477 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001478
Josef Bacik9e0baf62011-07-15 15:16:44 +00001479 spin_lock(&BTRFS_I(inode)->lock);
1480 BTRFS_I(inode)->outstanding_extents--;
1481 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001482}
1483
Chris Masond352ac62008-09-29 15:18:18 -04001484/*
1485 * extent_io.c set_bit_hook, used to track delayed allocation
1486 * bytes in this file, and to maintain the list of inodes that
1487 * have pending delalloc work to be done.
1488 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001489static void btrfs_set_bit_hook(struct inode *inode,
1490 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001491{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001492
Chris Mason75eff682008-12-15 15:54:40 -05001493 /*
1494 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001495 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001496 * bit, which is only set or cleared with irqs on
1497 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001498 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001499 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001500 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001501 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001502
Josef Bacik9e0baf62011-07-15 15:16:44 +00001503 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001504 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001505 } else {
1506 spin_lock(&BTRFS_I(inode)->lock);
1507 BTRFS_I(inode)->outstanding_extents++;
1508 spin_unlock(&BTRFS_I(inode)->lock);
1509 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001510
Chris Mason75eff682008-12-15 15:54:40 -05001511 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001512 BTRFS_I(inode)->delalloc_bytes += len;
1513 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001514 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001515 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1516 &root->fs_info->delalloc_inodes);
1517 }
Chris Mason75eff682008-12-15 15:54:40 -05001518 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001519 }
Chris Mason291d6732008-01-29 15:55:23 -05001520}
1521
Chris Masond352ac62008-09-29 15:18:18 -04001522/*
1523 * extent_io.c clear_bit_hook, see set_bit_hook for why
1524 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001525static void btrfs_clear_bit_hook(struct inode *inode,
1526 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001527{
Chris Mason75eff682008-12-15 15:54:40 -05001528 /*
1529 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001530 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001531 * bit, which is only set or cleared with irqs on
1532 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001533 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001534 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001535 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001536 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001537
Josef Bacik9e0baf62011-07-15 15:16:44 +00001538 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001539 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001540 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1541 spin_lock(&BTRFS_I(inode)->lock);
1542 BTRFS_I(inode)->outstanding_extents--;
1543 spin_unlock(&BTRFS_I(inode)->lock);
1544 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001545
1546 if (*bits & EXTENT_DO_ACCOUNTING)
1547 btrfs_delalloc_release_metadata(inode, len);
1548
Josef Bacik0cb59c92010-07-02 12:14:14 -04001549 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1550 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001551 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001552
Chris Mason75eff682008-12-15 15:54:40 -05001553 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001554 root->fs_info->delalloc_bytes -= len;
1555 BTRFS_I(inode)->delalloc_bytes -= len;
1556
Josef Bacik0cb59c92010-07-02 12:14:14 -04001557 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001558 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1559 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1560 }
Chris Mason75eff682008-12-15 15:54:40 -05001561 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001562 }
Chris Mason291d6732008-01-29 15:55:23 -05001563}
1564
Chris Masond352ac62008-09-29 15:18:18 -04001565/*
1566 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1567 * we don't create bios that span stripes or chunks
1568 */
Chris Mason239b14b2008-03-24 15:02:07 -04001569int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001570 size_t size, struct bio *bio,
1571 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001572{
1573 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001574 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001575 u64 length = 0;
1576 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001577 int ret;
1578
Chris Mason771ed682008-11-06 22:02:51 -05001579 if (bio_flags & EXTENT_BIO_COMPRESSED)
1580 return 0;
1581
Chris Masonf2d8d742008-04-21 10:03:05 -04001582 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001583 map_length = length;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001584 ret = btrfs_map_block(root->fs_info, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001585 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001586 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001587 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001588 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001589 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001590 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001591}
1592
Chris Masond352ac62008-09-29 15:18:18 -04001593/*
1594 * in order to insert checksums into the metadata in large chunks,
1595 * we wait until bio submission time. All the pages in the bio are
1596 * checksummed and sums are attached onto the ordered extent record.
1597 *
1598 * At IO completion time the cums attached on the ordered extent record
1599 * are inserted into the btree
1600 */
Chris Masond3977122009-01-05 21:25:51 -05001601static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1602 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001603 unsigned long bio_flags,
1604 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001605{
Chris Mason065631f2008-02-20 12:07:25 -05001606 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001607 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001608
Chris Masond20f7042008-12-08 16:58:54 -05001609 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001610 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001611 return 0;
1612}
Chris Masone0156402008-04-16 11:15:20 -04001613
Chris Mason4a69a412008-11-06 22:03:00 -05001614/*
1615 * in order to insert checksums into the metadata in large chunks,
1616 * we wait until bio submission time. All the pages in the bio are
1617 * checksummed and sums are attached onto the ordered extent record.
1618 *
1619 * At IO completion time the cums attached on the ordered extent record
1620 * are inserted into the btree
1621 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001622static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001623 int mirror_num, unsigned long bio_flags,
1624 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001625{
1626 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001627 int ret;
1628
1629 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1630 if (ret)
1631 bio_endio(bio, ret);
1632 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001633}
1634
Chris Masond352ac62008-09-29 15:18:18 -04001635/*
Chris Masoncad321a2008-12-17 14:51:42 -05001636 * extent_io.c submission hook. This does the right thing for csum calculation
1637 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001638 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001639static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001640 int mirror_num, unsigned long bio_flags,
1641 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001642{
1643 struct btrfs_root *root = BTRFS_I(inode)->root;
1644 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001645 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001646 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001647 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001648
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001649 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001650
Liu Bo83eea1f2012-07-10 05:28:39 -06001651 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001652 metadata = 2;
1653
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001654 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001655 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1656 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001657 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001658
Chris Masond20f7042008-12-08 16:58:54 -05001659 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001660 ret = btrfs_submit_compressed_read(inode, bio,
1661 mirror_num,
1662 bio_flags);
1663 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001664 } else if (!skip_sum) {
1665 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1666 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001667 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001668 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001669 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001670 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001671 /* csum items have already been cloned */
1672 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1673 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001674 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001675 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001676 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001677 bio_flags, bio_offset,
1678 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001679 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001680 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001681 } else if (!skip_sum) {
1682 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1683 if (ret)
1684 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001685 }
1686
Chris Mason0b86a832008-03-24 15:01:56 -04001687mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001688 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1689
1690out:
1691 if (ret < 0)
1692 bio_endio(bio, ret);
1693 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001694}
Chris Mason6885f302008-02-20 16:11:05 -05001695
Chris Masond352ac62008-09-29 15:18:18 -04001696/*
1697 * given a list of ordered sums record them in the inode. This happens
1698 * at IO completion time based on sums calculated at bio submission time.
1699 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001700static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001701 struct inode *inode, u64 file_offset,
1702 struct list_head *list)
1703{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001704 struct btrfs_ordered_sum *sum;
1705
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001706 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001707 btrfs_csum_file_blocks(trans,
1708 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001709 }
1710 return 0;
1711}
1712
Josef Bacik2ac55d42010-02-03 19:33:23 +00001713int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1714 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001715{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001716 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001717 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001718 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001719}
1720
Chris Masond352ac62008-09-29 15:18:18 -04001721/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001722struct btrfs_writepage_fixup {
1723 struct page *page;
1724 struct btrfs_work work;
1725};
1726
Christoph Hellwigb2950862008-12-02 09:54:17 -05001727static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001728{
1729 struct btrfs_writepage_fixup *fixup;
1730 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001731 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001732 struct page *page;
1733 struct inode *inode;
1734 u64 page_start;
1735 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001736 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001737
1738 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1739 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001740again:
Chris Mason247e7432008-07-17 12:53:51 -04001741 lock_page(page);
1742 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1743 ClearPageChecked(page);
1744 goto out_page;
1745 }
1746
1747 inode = page->mapping->host;
1748 page_start = page_offset(page);
1749 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1750
Josef Bacik2ac55d42010-02-03 19:33:23 +00001751 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001752 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001753
1754 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001755 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001756 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001757
1758 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1759 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001760 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1761 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001762 unlock_page(page);
1763 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001764 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001765 goto again;
1766 }
Chris Mason247e7432008-07-17 12:53:51 -04001767
Jeff Mahoney87826df2012-02-15 16:23:57 +01001768 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1769 if (ret) {
1770 mapping_set_error(page->mapping, ret);
1771 end_extent_writepage(page, ret, page_start, page_end);
1772 ClearPageChecked(page);
1773 goto out;
1774 }
1775
Josef Bacik2ac55d42010-02-03 19:33:23 +00001776 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001777 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001778 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001779out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001780 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1781 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001782out_page:
1783 unlock_page(page);
1784 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001785 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001786}
1787
1788/*
1789 * There are a few paths in the higher layers of the kernel that directly
1790 * set the page dirty bit without asking the filesystem if it is a
1791 * good idea. This causes problems because we want to make sure COW
1792 * properly happens and the data=ordered rules are followed.
1793 *
Chris Masonc8b97812008-10-29 14:49:59 -04001794 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001795 * hasn't been properly setup for IO. We kick off an async process
1796 * to fix it up. The async helper will wait for ordered extents, set
1797 * the delalloc bit and make it safe to write the page.
1798 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001799static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001800{
1801 struct inode *inode = page->mapping->host;
1802 struct btrfs_writepage_fixup *fixup;
1803 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001804
Chris Mason8b62b722009-09-02 16:53:46 -04001805 /* this page is properly in the ordered list */
1806 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001807 return 0;
1808
1809 if (PageChecked(page))
1810 return -EAGAIN;
1811
1812 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1813 if (!fixup)
1814 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001815
Chris Mason247e7432008-07-17 12:53:51 -04001816 SetPageChecked(page);
1817 page_cache_get(page);
1818 fixup->work.func = btrfs_writepage_fixup_worker;
1819 fixup->page = page;
1820 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001821 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001822}
1823
Yan Zhengd899e052008-10-30 14:25:28 -04001824static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1825 struct inode *inode, u64 file_pos,
1826 u64 disk_bytenr, u64 disk_num_bytes,
1827 u64 num_bytes, u64 ram_bytes,
1828 u8 compression, u8 encryption,
1829 u16 other_encoding, int extent_type)
1830{
1831 struct btrfs_root *root = BTRFS_I(inode)->root;
1832 struct btrfs_file_extent_item *fi;
1833 struct btrfs_path *path;
1834 struct extent_buffer *leaf;
1835 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001836 int ret;
1837
1838 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001839 if (!path)
1840 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001841
Chris Masonb9473432009-03-13 11:00:37 -04001842 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001843
1844 /*
1845 * we may be replacing one extent in the tree with another.
1846 * The new extent is pinned in the extent map, and we don't want
1847 * to drop it from the cache until it is completely in the btree.
1848 *
1849 * So, tell btrfs_drop_extents to leave this extent in the cache.
1850 * the caller is expected to unpin it and allow it to be merged
1851 * with the others.
1852 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001853 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001854 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001855 if (ret)
1856 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001857
Li Zefan33345d012011-04-20 10:31:50 +08001858 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001859 ins.offset = file_pos;
1860 ins.type = BTRFS_EXTENT_DATA_KEY;
1861 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001862 if (ret)
1863 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001864 leaf = path->nodes[0];
1865 fi = btrfs_item_ptr(leaf, path->slots[0],
1866 struct btrfs_file_extent_item);
1867 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1868 btrfs_set_file_extent_type(leaf, fi, extent_type);
1869 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1870 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1871 btrfs_set_file_extent_offset(leaf, fi, 0);
1872 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1873 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1874 btrfs_set_file_extent_compression(leaf, fi, compression);
1875 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1876 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001877
Yan Zhengd899e052008-10-30 14:25:28 -04001878 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001879 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001880
1881 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001882
1883 ins.objectid = disk_bytenr;
1884 ins.offset = disk_num_bytes;
1885 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001886 ret = btrfs_alloc_reserved_file_extent(trans, root,
1887 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001888 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001889out:
Yan Zhengd899e052008-10-30 14:25:28 -04001890 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001891
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001892 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001893}
1894
Chris Mason5d13a982009-03-13 11:41:46 -04001895/*
1896 * helper function for btrfs_finish_ordered_io, this
1897 * just reads in some of the csum leaves to prime them into ram
1898 * before we start the transaction. It limits the amount of btree
1899 * reads required while inside the transaction.
1900 */
Chris Masond352ac62008-09-29 15:18:18 -04001901/* as ordered data IO finishes, this gets called so we can finish
1902 * an ordered extent if the range of bytes in the file it covers are
1903 * fully written.
1904 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001905static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001906{
Josef Bacik5fd02042012-05-02 14:00:54 -04001907 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001908 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001909 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001910 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001911 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001912 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001913 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001914 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001915
Liu Bo83eea1f2012-07-10 05:28:39 -06001916 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001917
Josef Bacik5fd02042012-05-02 14:00:54 -04001918 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1919 ret = -EIO;
1920 goto out;
1921 }
1922
Yan, Zhengc2167752009-11-12 09:34:21 +00001923 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05001925 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1926 if (nolock)
1927 trans = btrfs_join_transaction_nolock(root);
1928 else
1929 trans = btrfs_join_transaction(root);
1930 if (IS_ERR(trans)) {
1931 ret = PTR_ERR(trans);
1932 trans = NULL;
1933 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00001934 }
Josef Bacik6c760c02012-11-09 10:53:21 -05001935 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1936 ret = btrfs_update_inode_fallback(trans, root, inode);
1937 if (ret) /* -ENOMEM or corruption */
1938 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001939 goto out;
1940 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001941
Josef Bacik2ac55d42010-02-03 19:33:23 +00001942 lock_extent_bits(io_tree, ordered_extent->file_offset,
1943 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001944 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001945
Josef Bacik0cb59c92010-07-02 12:14:14 -04001946 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001947 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001948 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001949 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001950 if (IS_ERR(trans)) {
1951 ret = PTR_ERR(trans);
1952 trans = NULL;
1953 goto out_unlock;
1954 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001955 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001956
Chris Masonc8b97812008-10-29 14:49:59 -04001957 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001958 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001959 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001960 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001961 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001962 ordered_extent->file_offset,
1963 ordered_extent->file_offset +
1964 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001965 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001966 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001967 ret = insert_reserved_file_extent(trans, inode,
1968 ordered_extent->file_offset,
1969 ordered_extent->start,
1970 ordered_extent->disk_len,
1971 ordered_extent->len,
1972 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001973 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001974 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04001975 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04001976 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1977 ordered_extent->file_offset, ordered_extent->len,
1978 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001979 if (ret < 0) {
1980 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001981 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001982 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001983
Chris Masone6dcd2d2008-07-17 12:53:50 -04001984 add_pending_csums(trans, inode, ordered_extent->file_offset,
1985 &ordered_extent->list);
1986
Josef Bacik6c760c02012-11-09 10:53:21 -05001987 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1988 ret = btrfs_update_inode_fallback(trans, root, inode);
1989 if (ret) { /* -ENOMEM or corruption */
1990 btrfs_abort_transaction(trans, root, ret);
1991 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04001992 }
1993 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04001994out_unlock:
1995 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1996 ordered_extent->file_offset +
1997 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00001998out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001999 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002000 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002001 if (trans)
2002 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002003
Josef Bacik5fd02042012-05-02 14:00:54 -04002004 if (ret)
2005 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2006 ordered_extent->file_offset +
2007 ordered_extent->len - 1, NULL, GFP_NOFS);
2008
2009 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002010 * This needs to be done to make sure anybody waiting knows we are done
2011 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002012 */
2013 btrfs_remove_ordered_extent(inode, ordered_extent);
2014
Chris Masone6dcd2d2008-07-17 12:53:50 -04002015 /* once for us */
2016 btrfs_put_ordered_extent(ordered_extent);
2017 /* once for the tree */
2018 btrfs_put_ordered_extent(ordered_extent);
2019
Josef Bacik5fd02042012-05-02 14:00:54 -04002020 return ret;
2021}
2022
2023static void finish_ordered_fn(struct btrfs_work *work)
2024{
2025 struct btrfs_ordered_extent *ordered_extent;
2026 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2027 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002028}
2029
Christoph Hellwigb2950862008-12-02 09:54:17 -05002030static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002031 struct extent_state *state, int uptodate)
2032{
Josef Bacik5fd02042012-05-02 14:00:54 -04002033 struct inode *inode = page->mapping->host;
2034 struct btrfs_root *root = BTRFS_I(inode)->root;
2035 struct btrfs_ordered_extent *ordered_extent = NULL;
2036 struct btrfs_workers *workers;
2037
liubo1abe9b82011-03-24 11:18:59 +00002038 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2039
Chris Mason8b62b722009-09-02 16:53:46 -04002040 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002041 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2042 end - start + 1, uptodate))
2043 return 0;
2044
2045 ordered_extent->work.func = finish_ordered_fn;
2046 ordered_extent->work.flags = 0;
2047
Liu Bo83eea1f2012-07-10 05:28:39 -06002048 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002049 workers = &root->fs_info->endio_freespace_worker;
2050 else
2051 workers = &root->fs_info->endio_write_workers;
2052 btrfs_queue_worker(workers, &ordered_extent->work);
2053
2054 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002055}
2056
Chris Masond352ac62008-09-29 15:18:18 -04002057/*
Chris Masond352ac62008-09-29 15:18:18 -04002058 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002059 * if there's a match, we allow the bio to finish. If not, the code in
2060 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002061 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002062static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002063 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002064{
Chris Mason35ebb932007-10-30 16:56:53 -04002065 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04002066 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002067 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002068 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002069 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002070 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002071 struct btrfs_root *root = BTRFS_I(inode)->root;
2072 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002073
Chris Masond20f7042008-12-08 16:58:54 -05002074 if (PageChecked(page)) {
2075 ClearPageChecked(page);
2076 goto good;
2077 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002078
2079 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002080 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002081
Yan Zheng17d217f2008-12-12 10:03:38 -05002082 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002083 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002084 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2085 GFP_NOFS);
2086 return 0;
2087 }
2088
Yanc2e639f2008-02-04 08:57:25 -05002089 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002090 private = state->private;
2091 ret = 0;
2092 } else {
2093 ret = get_state_private(io_tree, start, &private);
2094 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002095 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002096 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002097 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002098
Chris Masonff79f812007-10-15 16:22:25 -04002099 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2100 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002101 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002102 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002103
Cong Wang7ac687d2011-11-25 23:14:28 +08002104 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002105good:
Chris Mason07157aa2007-08-30 08:50:51 -04002106 return 0;
2107
2108zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002109 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002110 "private %llu\n",
2111 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002112 (unsigned long long)start, csum,
2113 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002114 memset(kaddr + offset, 1, end - start + 1);
2115 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002116 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002117 if (private == 0)
2118 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002119 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002120}
Chris Masonb888db22007-08-27 16:49:44 -04002121
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002122struct delayed_iput {
2123 struct list_head list;
2124 struct inode *inode;
2125};
2126
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002127/* JDM: If this is fs-wide, why can't we add a pointer to
2128 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002129void btrfs_add_delayed_iput(struct inode *inode)
2130{
2131 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2132 struct delayed_iput *delayed;
2133
2134 if (atomic_add_unless(&inode->i_count, -1, 1))
2135 return;
2136
2137 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2138 delayed->inode = inode;
2139
2140 spin_lock(&fs_info->delayed_iput_lock);
2141 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2142 spin_unlock(&fs_info->delayed_iput_lock);
2143}
2144
2145void btrfs_run_delayed_iputs(struct btrfs_root *root)
2146{
2147 LIST_HEAD(list);
2148 struct btrfs_fs_info *fs_info = root->fs_info;
2149 struct delayed_iput *delayed;
2150 int empty;
2151
2152 spin_lock(&fs_info->delayed_iput_lock);
2153 empty = list_empty(&fs_info->delayed_iputs);
2154 spin_unlock(&fs_info->delayed_iput_lock);
2155 if (empty)
2156 return;
2157
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002158 spin_lock(&fs_info->delayed_iput_lock);
2159 list_splice_init(&fs_info->delayed_iputs, &list);
2160 spin_unlock(&fs_info->delayed_iput_lock);
2161
2162 while (!list_empty(&list)) {
2163 delayed = list_entry(list.next, struct delayed_iput, list);
2164 list_del(&delayed->list);
2165 iput(delayed->inode);
2166 kfree(delayed);
2167 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002168}
2169
Yan, Zhengd68fc572010-05-16 10:49:58 -04002170enum btrfs_orphan_cleanup_state {
2171 ORPHAN_CLEANUP_STARTED = 1,
2172 ORPHAN_CLEANUP_DONE = 2,
2173};
2174
2175/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002176 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002177 * files in the subvolume, it removes orphan item and frees block_rsv
2178 * structure.
2179 */
2180void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2181 struct btrfs_root *root)
2182{
Josef Bacik90290e12011-12-02 15:44:12 -05002183 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002184 int ret;
2185
Josef Bacik8a35d952012-05-23 14:26:42 -04002186 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002187 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2188 return;
2189
Josef Bacik90290e12011-12-02 15:44:12 -05002190 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002191 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002192 spin_unlock(&root->orphan_lock);
2193 return;
2194 }
2195
2196 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2197 spin_unlock(&root->orphan_lock);
2198 return;
2199 }
2200
2201 block_rsv = root->orphan_block_rsv;
2202 root->orphan_block_rsv = NULL;
2203 spin_unlock(&root->orphan_lock);
2204
Yan, Zhengd68fc572010-05-16 10:49:58 -04002205 if (root->orphan_item_inserted &&
2206 btrfs_root_refs(&root->root_item) > 0) {
2207 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2208 root->root_key.objectid);
2209 BUG_ON(ret);
2210 root->orphan_item_inserted = 0;
2211 }
2212
Josef Bacik90290e12011-12-02 15:44:12 -05002213 if (block_rsv) {
2214 WARN_ON(block_rsv->size > 0);
2215 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002216 }
2217}
2218
2219/*
Josef Bacik7b128762008-07-24 12:17:14 -04002220 * This creates an orphan entry for the given inode in case something goes
2221 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002222 *
2223 * NOTE: caller of this function should reserve 5 units of metadata for
2224 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002225 */
2226int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2227{
2228 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002229 struct btrfs_block_rsv *block_rsv = NULL;
2230 int reserve = 0;
2231 int insert = 0;
2232 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002233
Yan, Zhengd68fc572010-05-16 10:49:58 -04002234 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002235 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002236 if (!block_rsv)
2237 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002238 }
2239
Yan, Zhengd68fc572010-05-16 10:49:58 -04002240 spin_lock(&root->orphan_lock);
2241 if (!root->orphan_block_rsv) {
2242 root->orphan_block_rsv = block_rsv;
2243 } else if (block_rsv) {
2244 btrfs_free_block_rsv(root, block_rsv);
2245 block_rsv = NULL;
2246 }
Josef Bacik7b128762008-07-24 12:17:14 -04002247
Josef Bacik8a35d952012-05-23 14:26:42 -04002248 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2249 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002250#if 0
2251 /*
2252 * For proper ENOSPC handling, we should do orphan
2253 * cleanup when mounting. But this introduces backward
2254 * compatibility issue.
2255 */
2256 if (!xchg(&root->orphan_item_inserted, 1))
2257 insert = 2;
2258 else
2259 insert = 1;
2260#endif
2261 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002262 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002263 }
Josef Bacik7b128762008-07-24 12:17:14 -04002264
Josef Bacik72ac3c02012-05-23 14:13:11 -04002265 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2266 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002267 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002268 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002269
Yan, Zhengd68fc572010-05-16 10:49:58 -04002270 /* grab metadata reservation from transaction handle */
2271 if (reserve) {
2272 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002273 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002274 }
2275
2276 /* insert an orphan item to track this unlinked/truncated file */
2277 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002278 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002279 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002280 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2281 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002282 btrfs_abort_transaction(trans, root, ret);
2283 return ret;
2284 }
2285 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002286 }
2287
2288 /* insert an orphan item to track subvolume contains orphan files */
2289 if (insert >= 2) {
2290 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2291 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002292 if (ret && ret != -EEXIST) {
2293 btrfs_abort_transaction(trans, root, ret);
2294 return ret;
2295 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002296 }
2297 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002298}
2299
2300/*
2301 * We have done the truncate/delete so we can go ahead and remove the orphan
2302 * item for this particular inode.
2303 */
2304int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2305{
2306 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002307 int delete_item = 0;
2308 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002309 int ret = 0;
2310
Yan, Zhengd68fc572010-05-16 10:49:58 -04002311 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002312 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2313 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002314 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002315
Josef Bacik72ac3c02012-05-23 14:13:11 -04002316 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2317 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002318 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002319 spin_unlock(&root->orphan_lock);
2320
2321 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002322 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002323 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002324 }
2325
Josef Bacik8a35d952012-05-23 14:26:42 -04002326 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002327 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002328 atomic_dec(&root->orphan_inodes);
2329 }
Josef Bacik7b128762008-07-24 12:17:14 -04002330
Yan, Zhengd68fc572010-05-16 10:49:58 -04002331 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002332}
2333
2334/*
2335 * this cleans up any orphans that may be left on the list from the last use
2336 * of this root.
2337 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002338int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002339{
2340 struct btrfs_path *path;
2341 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002342 struct btrfs_key key, found_key;
2343 struct btrfs_trans_handle *trans;
2344 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002345 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002346 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2347
Yan, Zhengd68fc572010-05-16 10:49:58 -04002348 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002349 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002350
2351 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002352 if (!path) {
2353 ret = -ENOMEM;
2354 goto out;
2355 }
Josef Bacik7b128762008-07-24 12:17:14 -04002356 path->reada = -1;
2357
2358 key.objectid = BTRFS_ORPHAN_OBJECTID;
2359 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2360 key.offset = (u64)-1;
2361
Josef Bacik7b128762008-07-24 12:17:14 -04002362 while (1) {
2363 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002364 if (ret < 0)
2365 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002366
2367 /*
2368 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002369 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002370 * find the key and see if we have stuff that matches
2371 */
2372 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002373 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002374 if (path->slots[0] == 0)
2375 break;
2376 path->slots[0]--;
2377 }
2378
2379 /* pull out the item */
2380 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2382
2383 /* make sure the item matches what we want */
2384 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2385 break;
2386 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2387 break;
2388
2389 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002390 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002391
2392 /*
2393 * this is where we are basically btrfs_lookup, without the
2394 * crossing root thing. we store the inode number in the
2395 * offset of the orphan item.
2396 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002397
2398 if (found_key.offset == last_objectid) {
2399 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2400 "stopping orphan cleanup\n");
2401 ret = -EINVAL;
2402 goto out;
2403 }
2404
2405 last_objectid = found_key.offset;
2406
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002407 found_key.objectid = found_key.offset;
2408 found_key.type = BTRFS_INODE_ITEM_KEY;
2409 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002410 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002411 ret = PTR_RET(inode);
2412 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002413 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002414
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002415 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2416 struct btrfs_root *dead_root;
2417 struct btrfs_fs_info *fs_info = root->fs_info;
2418 int is_dead_root = 0;
2419
2420 /*
2421 * this is an orphan in the tree root. Currently these
2422 * could come from 2 sources:
2423 * a) a snapshot deletion in progress
2424 * b) a free space cache inode
2425 * We need to distinguish those two, as the snapshot
2426 * orphan must not get deleted.
2427 * find_dead_roots already ran before us, so if this
2428 * is a snapshot deletion, we should find the root
2429 * in the dead_roots list
2430 */
2431 spin_lock(&fs_info->trans_lock);
2432 list_for_each_entry(dead_root, &fs_info->dead_roots,
2433 root_list) {
2434 if (dead_root->root_key.objectid ==
2435 found_key.objectid) {
2436 is_dead_root = 1;
2437 break;
2438 }
2439 }
2440 spin_unlock(&fs_info->trans_lock);
2441 if (is_dead_root) {
2442 /* prevent this orphan from being found again */
2443 key.offset = found_key.objectid - 1;
2444 continue;
2445 }
2446 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002447 /*
2448 * Inode is already gone but the orphan item is still there,
2449 * kill the orphan item.
2450 */
2451 if (ret == -ESTALE) {
2452 trans = btrfs_start_transaction(root, 1);
2453 if (IS_ERR(trans)) {
2454 ret = PTR_ERR(trans);
2455 goto out;
2456 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002457 printk(KERN_ERR "auto deleting %Lu\n",
2458 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002459 ret = btrfs_del_orphan_item(trans, root,
2460 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002461 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002462 btrfs_end_transaction(trans, root);
2463 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002464 }
Josef Bacik7b128762008-07-24 12:17:14 -04002465
Josef Bacik7b128762008-07-24 12:17:14 -04002466 /*
2467 * add this inode to the orphan list so btrfs_orphan_del does
2468 * the proper thing when we hit it
2469 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002470 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2471 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002472
Josef Bacik7b128762008-07-24 12:17:14 -04002473 /* if we have links, this was a truncate, lets do that */
2474 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002475 if (!S_ISREG(inode->i_mode)) {
2476 WARN_ON(1);
2477 iput(inode);
2478 continue;
2479 }
Josef Bacik7b128762008-07-24 12:17:14 -04002480 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002481 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002482 } else {
2483 nr_unlink++;
2484 }
2485
2486 /* this will do delete_inode and everything for us */
2487 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002488 if (ret)
2489 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002490 }
Miao Xie3254c872011-11-10 20:45:05 -05002491 /* release the path since we're done with it */
2492 btrfs_release_path(path);
2493
Yan, Zhengd68fc572010-05-16 10:49:58 -04002494 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2495
2496 if (root->orphan_block_rsv)
2497 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2498 (u64)-1);
2499
2500 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002501 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002502 if (!IS_ERR(trans))
2503 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002504 }
Josef Bacik7b128762008-07-24 12:17:14 -04002505
2506 if (nr_unlink)
2507 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2508 if (nr_truncate)
2509 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002510
2511out:
2512 if (ret)
2513 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2514 btrfs_free_path(path);
2515 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002516}
2517
Chris Masond352ac62008-09-29 15:18:18 -04002518/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002519 * very simple check to peek ahead in the leaf looking for xattrs. If we
2520 * don't find any xattrs, we know there can't be any acls.
2521 *
2522 * slot is the slot the inode is in, objectid is the objectid of the inode
2523 */
2524static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2525 int slot, u64 objectid)
2526{
2527 u32 nritems = btrfs_header_nritems(leaf);
2528 struct btrfs_key found_key;
2529 int scanned = 0;
2530
2531 slot++;
2532 while (slot < nritems) {
2533 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2534
2535 /* we found a different objectid, there must not be acls */
2536 if (found_key.objectid != objectid)
2537 return 0;
2538
2539 /* we found an xattr, assume we've got an acl */
2540 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2541 return 1;
2542
2543 /*
2544 * we found a key greater than an xattr key, there can't
2545 * be any acls later on
2546 */
2547 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2548 return 0;
2549
2550 slot++;
2551 scanned++;
2552
2553 /*
2554 * it goes inode, inode backrefs, xattrs, extents,
2555 * so if there are a ton of hard links to an inode there can
2556 * be a lot of backrefs. Don't waste time searching too hard,
2557 * this is just an optimization
2558 */
2559 if (scanned >= 8)
2560 break;
2561 }
2562 /* we hit the end of the leaf before we found an xattr or
2563 * something larger than an xattr. We have to assume the inode
2564 * has acls
2565 */
2566 return 1;
2567}
2568
2569/*
Chris Masond352ac62008-09-29 15:18:18 -04002570 * read an inode from the btree into the in-memory inode
2571 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002572static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002573{
2574 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002575 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002576 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002577 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002578 struct btrfs_root *root = BTRFS_I(inode)->root;
2579 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002580 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002581 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002582 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002583 bool filled = false;
2584
2585 ret = btrfs_fill_inode(inode, &rdev);
2586 if (!ret)
2587 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002588
2589 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002590 if (!path)
2591 goto make_bad;
2592
Josef Bacikd90c7322011-05-17 09:50:54 -04002593 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002594 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002595
Chris Mason39279cc2007-06-12 06:35:45 -04002596 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002597 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002598 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002599
Chris Mason5f39d392007-10-15 16:14:19 -04002600 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002601
2602 if (filled)
2603 goto cache_acl;
2604
Chris Mason5f39d392007-10-15 16:14:19 -04002605 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2606 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002607 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002608 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002609 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2610 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002611 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002612
2613 tspec = btrfs_inode_atime(inode_item);
2614 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2615 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2616
2617 tspec = btrfs_inode_mtime(inode_item);
2618 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2619 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2620
2621 tspec = btrfs_inode_ctime(inode_item);
2622 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2623 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2624
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002625 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002626 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002627 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2628
2629 /*
2630 * If we were modified in the current generation and evicted from memory
2631 * and then re-read we need to do a full sync since we don't have any
2632 * idea about which extents were modified before we were evicted from
2633 * cache.
2634 */
2635 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2636 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2637 &BTRFS_I(inode)->runtime_flags);
2638
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002639 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002640 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002641 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002642 rdev = btrfs_inode_rdev(leaf, inode_item);
2643
Josef Bacikaec74772008-07-24 12:12:38 -04002644 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002645 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002646cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002647 /*
2648 * try to precache a NULL acl entry for files that don't have
2649 * any xattrs or acls
2650 */
Li Zefan33345d012011-04-20 10:31:50 +08002651 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2652 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002653 if (!maybe_acls)
2654 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002655
Chris Mason39279cc2007-06-12 06:35:45 -04002656 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002657
Chris Mason39279cc2007-06-12 06:35:45 -04002658 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002659 case S_IFREG:
2660 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002661 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002662 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002663 inode->i_fop = &btrfs_file_operations;
2664 inode->i_op = &btrfs_file_inode_operations;
2665 break;
2666 case S_IFDIR:
2667 inode->i_fop = &btrfs_dir_file_operations;
2668 if (root == root->fs_info->tree_root)
2669 inode->i_op = &btrfs_dir_ro_inode_operations;
2670 else
2671 inode->i_op = &btrfs_dir_inode_operations;
2672 break;
2673 case S_IFLNK:
2674 inode->i_op = &btrfs_symlink_inode_operations;
2675 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002676 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002677 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002678 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002679 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002680 init_special_inode(inode, inode->i_mode, rdev);
2681 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002682 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002683
2684 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002685 return;
2686
2687make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002688 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002689 make_bad_inode(inode);
2690}
2691
Chris Masond352ac62008-09-29 15:18:18 -04002692/*
2693 * given a leaf and an inode, copy the inode fields into the leaf
2694 */
Chris Masone02119d2008-09-05 16:13:11 -04002695static void fill_inode_item(struct btrfs_trans_handle *trans,
2696 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002697 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002698 struct inode *inode)
2699{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002700 btrfs_set_inode_uid(leaf, item, i_uid_read(inode));
2701 btrfs_set_inode_gid(leaf, item, i_gid_read(inode));
Chris Masondbe674a2008-07-17 12:54:05 -04002702 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002703 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2704 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2705
2706 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2707 inode->i_atime.tv_sec);
2708 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2709 inode->i_atime.tv_nsec);
2710
2711 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2712 inode->i_mtime.tv_sec);
2713 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2714 inode->i_mtime.tv_nsec);
2715
2716 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2717 inode->i_ctime.tv_sec);
2718 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2719 inode->i_ctime.tv_nsec);
2720
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002721 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002722 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002723 btrfs_set_inode_sequence(leaf, item, inode->i_version);
Chris Masone02119d2008-09-05 16:13:11 -04002724 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002725 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002726 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f12011-05-11 15:26:06 -04002727 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002728}
2729
Chris Masond352ac62008-09-29 15:18:18 -04002730/*
2731 * copy everything in the in-memory inode into the btree.
2732 */
Chris Mason21151332011-11-10 20:39:08 -05002733static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002734 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002735{
2736 struct btrfs_inode_item *inode_item;
2737 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002738 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002739 int ret;
2740
2741 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002742 if (!path)
2743 return -ENOMEM;
2744
Chris Masonb9473432009-03-13 11:00:37 -04002745 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002746 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2747 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002748 if (ret) {
2749 if (ret > 0)
2750 ret = -ENOENT;
2751 goto failed;
2752 }
2753
Chris Masonb4ce94d2009-02-04 09:25:08 -05002754 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002755 leaf = path->nodes[0];
2756 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002757 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002758
Chris Masone02119d2008-09-05 16:13:11 -04002759 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002760 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002761 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002762 ret = 0;
2763failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002764 btrfs_free_path(path);
2765 return ret;
2766}
2767
Chris Masond352ac62008-09-29 15:18:18 -04002768/*
Chris Mason21151332011-11-10 20:39:08 -05002769 * copy everything in the in-memory inode into the btree.
2770 */
2771noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2772 struct btrfs_root *root, struct inode *inode)
2773{
2774 int ret;
2775
2776 /*
2777 * If the inode is a free space inode, we can deadlock during commit
2778 * if we put it into the delayed code.
2779 *
2780 * The data relocation inode should also be directly updated
2781 * without delay
2782 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002783 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002784 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002785 btrfs_update_root_times(trans, root);
2786
Chris Mason21151332011-11-10 20:39:08 -05002787 ret = btrfs_delayed_update_inode(trans, root, inode);
2788 if (!ret)
2789 btrfs_set_inode_last_trans(trans, inode);
2790 return ret;
2791 }
2792
2793 return btrfs_update_inode_item(trans, root, inode);
2794}
2795
Josef Bacikbe6aef62012-10-22 15:43:12 -04002796noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2797 struct btrfs_root *root,
2798 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002799{
2800 int ret;
2801
2802 ret = btrfs_update_inode(trans, root, inode);
2803 if (ret == -ENOSPC)
2804 return btrfs_update_inode_item(trans, root, inode);
2805 return ret;
2806}
2807
2808/*
Chris Masond352ac62008-09-29 15:18:18 -04002809 * unlink helper that gets used here in inode.c and in the tree logging
2810 * recovery code. It remove a link in a directory with a given name, and
2811 * also drops the back refs in the inode to the directory
2812 */
Al Viro92986792011-03-04 17:14:37 +00002813static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2814 struct btrfs_root *root,
2815 struct inode *dir, struct inode *inode,
2816 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002817{
2818 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002819 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002820 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002821 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002822 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002823 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002824 u64 ino = btrfs_ino(inode);
2825 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002826
2827 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002828 if (!path) {
2829 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002830 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002831 }
2832
Chris Masonb9473432009-03-13 11:00:37 -04002833 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002834 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002835 name, name_len, -1);
2836 if (IS_ERR(di)) {
2837 ret = PTR_ERR(di);
2838 goto err;
2839 }
2840 if (!di) {
2841 ret = -ENOENT;
2842 goto err;
2843 }
Chris Mason5f39d392007-10-15 16:14:19 -04002844 leaf = path->nodes[0];
2845 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002846 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002847 if (ret)
2848 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002849 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002850
Li Zefan33345d012011-04-20 10:31:50 +08002851 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2852 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002853 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002854 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002855 "inode %llu parent %llu\n", name_len, name,
2856 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002857 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002858 goto err;
2859 }
2860
Miao Xie16cdcec2011-04-22 18:12:22 +08002861 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002862 if (ret) {
2863 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002864 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002865 }
Chris Mason39279cc2007-06-12 06:35:45 -04002866
Chris Masone02119d2008-09-05 16:13:11 -04002867 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002868 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002869 if (ret != 0 && ret != -ENOENT) {
2870 btrfs_abort_transaction(trans, root, ret);
2871 goto err;
2872 }
Chris Masone02119d2008-09-05 16:13:11 -04002873
2874 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2875 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002876 if (ret == -ENOENT)
2877 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002878err:
2879 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002880 if (ret)
2881 goto out;
2882
2883 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002884 inode_inc_iversion(inode);
2885 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002886 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002887 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002888out:
Chris Mason39279cc2007-06-12 06:35:45 -04002889 return ret;
2890}
2891
Al Viro92986792011-03-04 17:14:37 +00002892int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2893 struct btrfs_root *root,
2894 struct inode *dir, struct inode *inode,
2895 const char *name, int name_len)
2896{
2897 int ret;
2898 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2899 if (!ret) {
2900 btrfs_drop_nlink(inode);
2901 ret = btrfs_update_inode(trans, root, inode);
2902 }
2903 return ret;
2904}
2905
2906
Yan, Zhenga22285a2010-05-16 10:48:46 -04002907/* helper to check if there is any shared block in the path */
2908static int check_path_shared(struct btrfs_root *root,
2909 struct btrfs_path *path)
2910{
2911 struct extent_buffer *eb;
2912 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002913 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002914
2915 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002916 int ret;
2917
Yan, Zhenga22285a2010-05-16 10:48:46 -04002918 if (!path->nodes[level])
2919 break;
2920 eb = path->nodes[level];
2921 if (!btrfs_block_can_be_shared(root, eb))
2922 continue;
2923 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2924 &refs, NULL);
2925 if (refs > 1)
2926 return 1;
2927 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002928 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002929}
2930
2931/*
2932 * helper to start transaction for unlink and rmdir.
2933 *
2934 * unlink and rmdir are special in btrfs, they do not always free space.
2935 * so in enospc case, we should make sure they will free space before
2936 * allowing them to use the global metadata reservation.
2937 */
2938static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2939 struct dentry *dentry)
2940{
2941 struct btrfs_trans_handle *trans;
2942 struct btrfs_root *root = BTRFS_I(dir)->root;
2943 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002944 struct btrfs_dir_item *di;
2945 struct inode *inode = dentry->d_inode;
2946 u64 index;
2947 int check_link = 1;
2948 int err = -ENOSPC;
2949 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002950 u64 ino = btrfs_ino(inode);
2951 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002952
Josef Bacike70bea52011-10-11 14:18:24 -04002953 /*
2954 * 1 for the possible orphan item
2955 * 1 for the dir item
2956 * 1 for the dir index
2957 * 1 for the inode ref
2958 * 1 for the inode ref in the tree log
2959 * 2 for the dir entries in the log
2960 * 1 for the inode
2961 */
2962 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002963 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2964 return trans;
2965
Li Zefan33345d012011-04-20 10:31:50 +08002966 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002967 return ERR_PTR(-ENOSPC);
2968
2969 /* check if there is someone else holds reference */
2970 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2971 return ERR_PTR(-ENOSPC);
2972
2973 if (atomic_read(&inode->i_count) > 2)
2974 return ERR_PTR(-ENOSPC);
2975
2976 if (xchg(&root->fs_info->enospc_unlink, 1))
2977 return ERR_PTR(-ENOSPC);
2978
2979 path = btrfs_alloc_path();
2980 if (!path) {
2981 root->fs_info->enospc_unlink = 0;
2982 return ERR_PTR(-ENOMEM);
2983 }
2984
Josef Bacik3880a1b2011-10-14 14:46:51 -04002985 /* 1 for the orphan item */
2986 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002987 if (IS_ERR(trans)) {
2988 btrfs_free_path(path);
2989 root->fs_info->enospc_unlink = 0;
2990 return trans;
2991 }
2992
2993 path->skip_locking = 1;
2994 path->search_commit_root = 1;
2995
2996 ret = btrfs_lookup_inode(trans, root, path,
2997 &BTRFS_I(dir)->location, 0);
2998 if (ret < 0) {
2999 err = ret;
3000 goto out;
3001 }
3002 if (ret == 0) {
3003 if (check_path_shared(root, path))
3004 goto out;
3005 } else {
3006 check_link = 0;
3007 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003008 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003009
3010 ret = btrfs_lookup_inode(trans, root, path,
3011 &BTRFS_I(inode)->location, 0);
3012 if (ret < 0) {
3013 err = ret;
3014 goto out;
3015 }
3016 if (ret == 0) {
3017 if (check_path_shared(root, path))
3018 goto out;
3019 } else {
3020 check_link = 0;
3021 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003022 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003023
3024 if (ret == 0 && S_ISREG(inode->i_mode)) {
3025 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003026 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003027 if (ret < 0) {
3028 err = ret;
3029 goto out;
3030 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003031 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003032 if (check_path_shared(root, path))
3033 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003034 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003035 }
3036
3037 if (!check_link) {
3038 err = 0;
3039 goto out;
3040 }
3041
Li Zefan33345d012011-04-20 10:31:50 +08003042 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003043 dentry->d_name.name, dentry->d_name.len, 0);
3044 if (IS_ERR(di)) {
3045 err = PTR_ERR(di);
3046 goto out;
3047 }
3048 if (di) {
3049 if (check_path_shared(root, path))
3050 goto out;
3051 } else {
3052 err = 0;
3053 goto out;
3054 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003055 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003056
Mark Fashehf1863732012-08-08 11:32:27 -07003057 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3058 dentry->d_name.len, ino, dir_ino, 0,
3059 &index);
3060 if (ret) {
3061 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003062 goto out;
3063 }
Mark Fashehf1863732012-08-08 11:32:27 -07003064
Yan, Zhenga22285a2010-05-16 10:48:46 -04003065 if (check_path_shared(root, path))
3066 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003067
David Sterbab3b4aa72011-04-21 01:20:15 +02003068 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003069
Miao Xie16cdcec2011-04-22 18:12:22 +08003070 /*
3071 * This is a commit root search, if we can lookup inode item and other
3072 * relative items in the commit root, it means the transaction of
3073 * dir/file creation has been committed, and the dir index item that we
3074 * delay to insert has also been inserted into the commit root. So
3075 * we needn't worry about the delayed insertion of the dir index item
3076 * here.
3077 */
Li Zefan33345d012011-04-20 10:31:50 +08003078 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003079 dentry->d_name.name, dentry->d_name.len, 0);
3080 if (IS_ERR(di)) {
3081 err = PTR_ERR(di);
3082 goto out;
3083 }
3084 BUG_ON(ret == -ENOENT);
3085 if (check_path_shared(root, path))
3086 goto out;
3087
3088 err = 0;
3089out:
3090 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003091 /* Migrate the orphan reservation over */
3092 if (!err)
3093 err = btrfs_block_rsv_migrate(trans->block_rsv,
3094 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003095 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003096
Yan, Zhenga22285a2010-05-16 10:48:46 -04003097 if (err) {
3098 btrfs_end_transaction(trans, root);
3099 root->fs_info->enospc_unlink = 0;
3100 return ERR_PTR(err);
3101 }
3102
3103 trans->block_rsv = &root->fs_info->global_block_rsv;
3104 return trans;
3105}
3106
3107static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3108 struct btrfs_root *root)
3109{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003110 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003111 btrfs_block_rsv_release(root, trans->block_rsv,
3112 trans->bytes_reserved);
3113 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003114 BUG_ON(!root->fs_info->enospc_unlink);
3115 root->fs_info->enospc_unlink = 0;
3116 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003117 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003118}
3119
Chris Mason39279cc2007-06-12 06:35:45 -04003120static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3121{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003122 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003123 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003124 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003125 int ret;
3126
Yan, Zhenga22285a2010-05-16 10:48:46 -04003127 trans = __unlink_start_trans(dir, dentry);
3128 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003129 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003130
Chris Mason12fcfd22009-03-24 10:24:20 -04003131 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3132
Chris Masone02119d2008-09-05 16:13:11 -04003133 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3134 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003135 if (ret)
3136 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003137
Yan, Zhenga22285a2010-05-16 10:48:46 -04003138 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003139 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003140 if (ret)
3141 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003142 }
Josef Bacik7b128762008-07-24 12:17:14 -04003143
Tsutomu Itohb5324022011-07-19 07:27:20 +00003144out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003145 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003146 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003147 return ret;
3148}
3149
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003150int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3151 struct btrfs_root *root,
3152 struct inode *dir, u64 objectid,
3153 const char *name, int name_len)
3154{
3155 struct btrfs_path *path;
3156 struct extent_buffer *leaf;
3157 struct btrfs_dir_item *di;
3158 struct btrfs_key key;
3159 u64 index;
3160 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003161 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003162
3163 path = btrfs_alloc_path();
3164 if (!path)
3165 return -ENOMEM;
3166
Li Zefan33345d012011-04-20 10:31:50 +08003167 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003168 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003169 if (IS_ERR_OR_NULL(di)) {
3170 if (!di)
3171 ret = -ENOENT;
3172 else
3173 ret = PTR_ERR(di);
3174 goto out;
3175 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003176
3177 leaf = path->nodes[0];
3178 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3179 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3180 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003181 if (ret) {
3182 btrfs_abort_transaction(trans, root, ret);
3183 goto out;
3184 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003185 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003186
3187 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3188 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003189 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003190 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003191 if (ret != -ENOENT) {
3192 btrfs_abort_transaction(trans, root, ret);
3193 goto out;
3194 }
Li Zefan33345d012011-04-20 10:31:50 +08003195 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003196 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003197 if (IS_ERR_OR_NULL(di)) {
3198 if (!di)
3199 ret = -ENOENT;
3200 else
3201 ret = PTR_ERR(di);
3202 btrfs_abort_transaction(trans, root, ret);
3203 goto out;
3204 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003205
3206 leaf = path->nodes[0];
3207 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003208 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003209 index = key.offset;
3210 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003211 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003212
Miao Xie16cdcec2011-04-22 18:12:22 +08003213 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003214 if (ret) {
3215 btrfs_abort_transaction(trans, root, ret);
3216 goto out;
3217 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003218
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003219 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003220 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003221 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003222 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003223 if (ret)
3224 btrfs_abort_transaction(trans, root, ret);
3225out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003226 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003227 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003228}
3229
Chris Mason39279cc2007-06-12 06:35:45 -04003230static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3231{
3232 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003233 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003234 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003235 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003236
David Sterbab3ae2442012-09-13 16:04:34 -06003237 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003238 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003239 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3240 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003241
Yan, Zhenga22285a2010-05-16 10:48:46 -04003242 trans = __unlink_start_trans(dir, dentry);
3243 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003244 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003245
Li Zefan33345d012011-04-20 10:31:50 +08003246 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003247 err = btrfs_unlink_subvol(trans, root, dir,
3248 BTRFS_I(inode)->location.objectid,
3249 dentry->d_name.name,
3250 dentry->d_name.len);
3251 goto out;
3252 }
3253
Josef Bacik7b128762008-07-24 12:17:14 -04003254 err = btrfs_orphan_add(trans, inode);
3255 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003256 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003257
Chris Mason39279cc2007-06-12 06:35:45 -04003258 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003259 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3260 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003261 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003262 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003263out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003264 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003265 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003266
Chris Mason39279cc2007-06-12 06:35:45 -04003267 return err;
3268}
3269
Chris Mason323ac952008-10-01 19:05:46 -04003270/*
Chris Mason39279cc2007-06-12 06:35:45 -04003271 * this can truncate away extent items, csum items and directory items.
3272 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003273 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003274 *
3275 * csum items that cross the new i_size are truncated to the new size
3276 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003277 *
3278 * min_type is the minimum key type to truncate down to. If set to 0, this
3279 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003280 */
Yan, Zheng80825102009-11-12 09:35:36 +00003281int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3282 struct btrfs_root *root,
3283 struct inode *inode,
3284 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003285{
Chris Mason39279cc2007-06-12 06:35:45 -04003286 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003287 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003288 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003289 struct btrfs_key key;
3290 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003291 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003292 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003293 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003294 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003295 u64 mask = root->sectorsize - 1;
3296 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003297 int found_extent;
3298 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003299 int pending_del_nr = 0;
3300 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003301 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003302 int ret;
3303 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003304 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003305
3306 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003307
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003308 path = btrfs_alloc_path();
3309 if (!path)
3310 return -ENOMEM;
3311 path->reada = -1;
3312
Josef Bacik5dc562c2012-08-17 13:14:17 -04003313 /*
3314 * We want to drop from the next block forward in case this new size is
3315 * not block aligned since we will be keeping the last block of the
3316 * extent just the way it is.
3317 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003318 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003319 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003320
Miao Xie16cdcec2011-04-22 18:12:22 +08003321 /*
3322 * This function is also used to drop the items in the log tree before
3323 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3324 * it is used to drop the loged items. So we shouldn't kill the delayed
3325 * items.
3326 */
3327 if (min_type == 0 && root == BTRFS_I(inode)->root)
3328 btrfs_kill_delayed_inode_items(inode);
3329
Li Zefan33345d012011-04-20 10:31:50 +08003330 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003331 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003332 key.type = (u8)-1;
3333
Chris Mason85e21ba2008-01-29 15:11:36 -05003334search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003335 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003336 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003337 if (ret < 0) {
3338 err = ret;
3339 goto out;
3340 }
Chris Masond3977122009-01-05 21:25:51 -05003341
Chris Mason85e21ba2008-01-29 15:11:36 -05003342 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003343 /* there are no items in the tree for us to truncate, we're
3344 * done
3345 */
Yan, Zheng80825102009-11-12 09:35:36 +00003346 if (path->slots[0] == 0)
3347 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003348 path->slots[0]--;
3349 }
3350
Chris Masond3977122009-01-05 21:25:51 -05003351 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003352 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003353 leaf = path->nodes[0];
3354 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3355 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003356
Li Zefan33345d012011-04-20 10:31:50 +08003357 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003358 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003359
Chris Mason85e21ba2008-01-29 15:11:36 -05003360 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003361 break;
3362
Chris Mason5f39d392007-10-15 16:14:19 -04003363 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003364 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003365 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003366 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003367 extent_type = btrfs_file_extent_type(leaf, fi);
3368 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003369 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003370 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003371 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003372 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003373 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003374 }
Yan008630c2007-11-07 13:31:09 -05003375 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003376 }
Yan, Zheng80825102009-11-12 09:35:36 +00003377 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003378 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003379 } else {
3380 if (item_end < new_size)
3381 break;
3382 if (found_key.offset >= new_size)
3383 del_item = 1;
3384 else
3385 del_item = 0;
3386 }
Chris Mason39279cc2007-06-12 06:35:45 -04003387 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003388 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003389 if (found_type != BTRFS_EXTENT_DATA_KEY)
3390 goto delete;
3391
3392 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003393 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003394 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003395 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003396 u64 orig_num_bytes =
3397 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003398 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003399 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003400 extent_num_bytes = extent_num_bytes &
3401 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003402 btrfs_set_file_extent_num_bytes(leaf, fi,
3403 extent_num_bytes);
3404 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003405 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003406 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003407 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003408 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003409 } else {
Chris Masondb945352007-10-15 16:15:53 -04003410 extent_num_bytes =
3411 btrfs_file_extent_disk_num_bytes(leaf,
3412 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003413 extent_offset = found_key.offset -
3414 btrfs_file_extent_offset(leaf, fi);
3415
Chris Mason39279cc2007-06-12 06:35:45 -04003416 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003417 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003418 if (extent_start != 0) {
3419 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003420 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003421 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003422 }
3423 }
Chris Mason90692182008-02-08 13:49:28 -05003424 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003425 /*
3426 * we can't truncate inline items that have had
3427 * special encodings
3428 */
3429 if (!del_item &&
3430 btrfs_file_extent_compression(leaf, fi) == 0 &&
3431 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3432 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003433 u32 size = new_size - found_key.offset;
3434
3435 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003436 inode_sub_bytes(inode, item_end + 1 -
3437 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003438 }
3439 size =
3440 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003441 btrfs_truncate_item(trans, root, path,
3442 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003443 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003444 inode_sub_bytes(inode, item_end + 1 -
3445 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003446 }
Chris Mason39279cc2007-06-12 06:35:45 -04003447 }
Chris Mason179e29e2007-11-01 11:28:41 -04003448delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003449 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003450 if (!pending_del_nr) {
3451 /* no pending yet, add ourselves */
3452 pending_del_slot = path->slots[0];
3453 pending_del_nr = 1;
3454 } else if (pending_del_nr &&
3455 path->slots[0] + 1 == pending_del_slot) {
3456 /* hop on the pending chunk */
3457 pending_del_nr++;
3458 pending_del_slot = path->slots[0];
3459 } else {
Chris Masond3977122009-01-05 21:25:51 -05003460 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003461 }
Chris Mason39279cc2007-06-12 06:35:45 -04003462 } else {
3463 break;
3464 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003465 if (found_extent && (root->ref_cows ||
3466 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003467 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003468 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003469 extent_num_bytes, 0,
3470 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003471 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003472 BUG_ON(ret);
3473 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003474
Yan, Zheng80825102009-11-12 09:35:36 +00003475 if (found_type == BTRFS_INODE_ITEM_KEY)
3476 break;
3477
3478 if (path->slots[0] == 0 ||
3479 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003480 if (pending_del_nr) {
3481 ret = btrfs_del_items(trans, root, path,
3482 pending_del_slot,
3483 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003484 if (ret) {
3485 btrfs_abort_transaction(trans,
3486 root, ret);
3487 goto error;
3488 }
Yan, Zheng80825102009-11-12 09:35:36 +00003489 pending_del_nr = 0;
3490 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003491 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003492 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003493 } else {
3494 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003495 }
Chris Mason39279cc2007-06-12 06:35:45 -04003496 }
Yan, Zheng80825102009-11-12 09:35:36 +00003497out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003498 if (pending_del_nr) {
3499 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3500 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003501 if (ret)
3502 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003503 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003504error:
Chris Mason39279cc2007-06-12 06:35:45 -04003505 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003506 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003507}
3508
Chris Masona52d9a82007-08-27 16:49:44 -04003509/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003510 * btrfs_truncate_page - read, zero a chunk and write a page
3511 * @inode - inode that we're zeroing
3512 * @from - the offset to start zeroing
3513 * @len - the length to zero, 0 to zero the entire range respective to the
3514 * offset
3515 * @front - zero up to the offset instead of from the offset on
3516 *
3517 * This will find the page for the "from" offset and cow the page and zero the
3518 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003519 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003520int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3521 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003522{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003523 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003524 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003525 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3526 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003527 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003528 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003529 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003530 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3531 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3532 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003533 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003534 int ret = 0;
3535 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003536 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003537
Josef Bacik2aaa6652012-08-29 14:27:18 -04003538 if ((offset & (blocksize - 1)) == 0 &&
3539 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003540 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003541 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003542 if (ret)
3543 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003544
Chris Mason211c17f2008-05-15 09:13:45 -04003545again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003546 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003547 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003548 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00003549 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04003550 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003551 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003552
3553 page_start = page_offset(page);
3554 page_end = page_start + PAGE_CACHE_SIZE - 1;
3555
Chris Masona52d9a82007-08-27 16:49:44 -04003556 if (!PageUptodate(page)) {
3557 ret = btrfs_readpage(NULL, page);
3558 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003559 if (page->mapping != mapping) {
3560 unlock_page(page);
3561 page_cache_release(page);
3562 goto again;
3563 }
Chris Masona52d9a82007-08-27 16:49:44 -04003564 if (!PageUptodate(page)) {
3565 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003566 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003567 }
3568 }
Chris Mason211c17f2008-05-15 09:13:45 -04003569 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003570
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003571 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003572 set_page_extent_mapped(page);
3573
3574 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3575 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003576 unlock_extent_cached(io_tree, page_start, page_end,
3577 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003578 unlock_page(page);
3579 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003580 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003581 btrfs_put_ordered_extent(ordered);
3582 goto again;
3583 }
3584
Josef Bacik2ac55d42010-02-03 19:33:23 +00003585 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003586 EXTENT_DIRTY | EXTENT_DELALLOC |
3587 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003588 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003589
Josef Bacik2ac55d42010-02-03 19:33:23 +00003590 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3591 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003592 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003593 unlock_extent_cached(io_tree, page_start, page_end,
3594 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003595 goto out_unlock;
3596 }
3597
Chris Masone6dcd2d2008-07-17 12:53:50 -04003598 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003599 if (!len)
3600 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003601 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003602 if (front)
3603 memset(kaddr, 0, offset);
3604 else
3605 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003606 flush_dcache_page(page);
3607 kunmap(page);
3608 }
Chris Mason247e7432008-07-17 12:53:51 -04003609 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003610 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003611 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3612 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003613
Chris Mason89642222008-07-24 09:41:53 -04003614out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003615 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003616 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003617 unlock_page(page);
3618 page_cache_release(page);
3619out:
3620 return ret;
3621}
3622
Josef Bacik695a0d02011-03-04 15:46:53 -05003623/*
3624 * This function puts in dummy file extents for the area we're creating a hole
3625 * for. So if we are truncating this file to a larger size we need to insert
3626 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3627 * the range between oldsize and size
3628 */
Josef Bacika41ad392011-01-31 15:30:16 -05003629int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003630{
3631 struct btrfs_trans_handle *trans;
3632 struct btrfs_root *root = BTRFS_I(inode)->root;
3633 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003634 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003635 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003636 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003637 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003638 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003639 u64 block_end = (size + mask) & ~mask;
3640 u64 last_byte;
3641 u64 cur_offset;
3642 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003643 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003644
3645 if (size <= hole_start)
3646 return 0;
3647
Yan Zheng9036c102008-10-30 14:19:41 -04003648 while (1) {
3649 struct btrfs_ordered_extent *ordered;
3650 btrfs_wait_ordered_range(inode, hole_start,
3651 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003652 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003653 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003654 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3655 if (!ordered)
3656 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003657 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3658 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003659 btrfs_put_ordered_extent(ordered);
3660 }
3661
Yan Zheng9036c102008-10-30 14:19:41 -04003662 cur_offset = hole_start;
3663 while (1) {
3664 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3665 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003666 if (IS_ERR(em)) {
3667 err = PTR_ERR(em);
3668 break;
3669 }
Yan Zheng9036c102008-10-30 14:19:41 -04003670 last_byte = min(extent_map_end(em), block_end);
3671 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003672 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003673 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003674 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003675
Miao Xie36423202011-12-14 20:12:02 -05003676 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003677 if (IS_ERR(trans)) {
3678 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003679 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003680 }
Yan, Zheng80825102009-11-12 09:35:36 +00003681
Josef Bacik5dc562c2012-08-17 13:14:17 -04003682 err = btrfs_drop_extents(trans, root, inode,
3683 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003684 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003685 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003686 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003687 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003688 break;
Miao Xie5b397372011-09-11 10:52:24 -04003689 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003690
Yan Zheng9036c102008-10-30 14:19:41 -04003691 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003692 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003693 0, hole_size, 0, hole_size,
3694 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003695 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003696 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003697 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003698 break;
Miao Xie5b397372011-09-11 10:52:24 -04003699 }
Yan, Zheng80825102009-11-12 09:35:36 +00003700
Josef Bacik5dc562c2012-08-17 13:14:17 -04003701 btrfs_drop_extent_cache(inode, cur_offset,
3702 cur_offset + hole_size - 1, 0);
3703 hole_em = alloc_extent_map();
3704 if (!hole_em) {
3705 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3706 &BTRFS_I(inode)->runtime_flags);
3707 goto next;
3708 }
3709 hole_em->start = cur_offset;
3710 hole_em->len = hole_size;
3711 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003712
Josef Bacik5dc562c2012-08-17 13:14:17 -04003713 hole_em->block_start = EXTENT_MAP_HOLE;
3714 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05003715 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003716 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3717 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3718 hole_em->generation = trans->transid;
3719
3720 while (1) {
3721 write_lock(&em_tree->lock);
3722 err = add_extent_mapping(em_tree, hole_em);
3723 if (!err)
3724 list_move(&hole_em->list,
3725 &em_tree->modified_extents);
3726 write_unlock(&em_tree->lock);
3727 if (err != -EEXIST)
3728 break;
3729 btrfs_drop_extent_cache(inode, cur_offset,
3730 cur_offset +
3731 hole_size - 1, 0);
3732 }
3733 free_extent_map(hole_em);
3734next:
Miao Xie36423202011-12-14 20:12:02 -05003735 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003736 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003737 }
3738 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003739 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003740 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003741 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003742 break;
3743 }
3744
Yan, Zhenga22285a2010-05-16 10:48:46 -04003745 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003746 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3747 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003748 return err;
3749}
3750
Josef Bacika41ad392011-01-31 15:30:16 -05003751static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003752{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003753 struct btrfs_root *root = BTRFS_I(inode)->root;
3754 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003755 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003756 int ret;
3757
Josef Bacika41ad392011-01-31 15:30:16 -05003758 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003759 return 0;
3760
Josef Bacika41ad392011-01-31 15:30:16 -05003761 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003762 truncate_pagecache(inode, oldsize, newsize);
3763 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003764 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003765 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003766
Miao Xief4a2f4c2011-12-14 20:12:01 -05003767 trans = btrfs_start_transaction(root, 1);
3768 if (IS_ERR(trans))
3769 return PTR_ERR(trans);
3770
3771 i_size_write(inode, newsize);
3772 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3773 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003774 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003775 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003776
Josef Bacika41ad392011-01-31 15:30:16 -05003777 /*
3778 * We're truncating a file that used to have good data down to
3779 * zero. Make sure it gets into the ordered flush list so that
3780 * any new writes get down to disk quickly.
3781 */
3782 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003783 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3784 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003785
Josef Bacika41ad392011-01-31 15:30:16 -05003786 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3787 truncate_setsize(inode, newsize);
3788 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003789 }
3790
Josef Bacika41ad392011-01-31 15:30:16 -05003791 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003792}
3793
Chris Mason39279cc2007-06-12 06:35:45 -04003794static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3795{
3796 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003797 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003798 int err;
3799
Li Zefanb83cc962010-12-20 16:04:08 +08003800 if (btrfs_root_readonly(root))
3801 return -EROFS;
3802
Chris Mason39279cc2007-06-12 06:35:45 -04003803 err = inode_change_ok(inode, attr);
3804 if (err)
3805 return err;
3806
Chris Mason5a3f23d2009-03-31 13:27:11 -04003807 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003808 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003809 if (err)
3810 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003811 }
Yan Zheng9036c102008-10-30 14:19:41 -04003812
Christoph Hellwig10257742010-06-04 11:30:02 +02003813 if (attr->ia_valid) {
3814 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003815 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003816 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003817
Josef Bacik22c44fe2011-11-30 10:45:38 -05003818 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003819 err = btrfs_acl_chmod(inode);
3820 }
3821
Chris Mason39279cc2007-06-12 06:35:45 -04003822 return err;
3823}
Chris Mason61295eb2008-01-14 16:24:38 -05003824
Al Virobd555972010-06-07 11:35:40 -04003825void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003826{
3827 struct btrfs_trans_handle *trans;
3828 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003829 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003830 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003831 int ret;
3832
liubo1abe9b82011-03-24 11:18:59 +00003833 trace_btrfs_inode_evict(inode);
3834
Chris Mason39279cc2007-06-12 06:35:45 -04003835 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003836 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003837 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003838 goto no_delete;
3839
Chris Mason39279cc2007-06-12 06:35:45 -04003840 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003841 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003842 goto no_delete;
3843 }
Al Virobd555972010-06-07 11:35:40 -04003844 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003845 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003846
Yan, Zhengc71bf092009-11-12 09:34:40 +00003847 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003848 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003849 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003850 goto no_delete;
3851 }
3852
Yan, Zheng76dda932009-09-21 16:00:26 -04003853 if (inode->i_nlink > 0) {
3854 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3855 goto no_delete;
3856 }
3857
Miao Xie66d8f3d2012-09-06 04:02:28 -06003858 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003859 if (!rsv) {
3860 btrfs_orphan_del(NULL, inode);
3861 goto no_delete;
3862 }
Josef Bacik4a338542011-08-29 11:01:31 -04003863 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003864 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003865 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003866
Chris Masondbe674a2008-07-17 12:54:05 -04003867 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003868
Josef Bacik4289a662011-08-05 13:22:24 -04003869 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003870 * This is a bit simpler than btrfs_truncate since we've already
3871 * reserved our space for our orphan item in the unlink, so we just
3872 * need to reserve some slack space in case we add bytes and update
3873 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003874 */
Yan, Zheng80825102009-11-12 09:35:36 +00003875 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00003876 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3877 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00003878
Josef Bacik726c35f2011-09-26 15:46:06 -04003879 /*
3880 * Try and steal from the global reserve since we will
3881 * likely not use this space anyway, we want to try as
3882 * hard as possible to get this to work.
3883 */
3884 if (ret)
3885 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3886
Yan, Zhengd68fc572010-05-16 10:49:58 -04003887 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003888 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003889 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003890 btrfs_orphan_del(NULL, inode);
3891 btrfs_free_block_rsv(root, rsv);
3892 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003893 }
3894
Miao Xie08e007d2012-10-16 11:33:38 +00003895 trans = btrfs_start_transaction_lflush(root, 1);
Josef Bacik4289a662011-08-05 13:22:24 -04003896 if (IS_ERR(trans)) {
3897 btrfs_orphan_del(NULL, inode);
3898 btrfs_free_block_rsv(root, rsv);
3899 goto no_delete;
3900 }
3901
3902 trans->block_rsv = rsv;
3903
Yan, Zhengd68fc572010-05-16 10:49:58 -04003904 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04003905 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00003906 break;
3907
Miao Xie8407aa42012-09-07 01:43:32 -06003908 trans->block_rsv = &root->fs_info->trans_block_rsv;
3909 ret = btrfs_update_inode(trans, root, inode);
3910 BUG_ON(ret);
3911
Yan, Zheng80825102009-11-12 09:35:36 +00003912 btrfs_end_transaction(trans, root);
3913 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00003914 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04003915 }
3916
Josef Bacik4289a662011-08-05 13:22:24 -04003917 btrfs_free_block_rsv(root, rsv);
3918
Yan, Zheng80825102009-11-12 09:35:36 +00003919 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003920 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003921 ret = btrfs_orphan_del(trans, inode);
3922 BUG_ON(ret);
3923 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003924
Josef Bacik4289a662011-08-05 13:22:24 -04003925 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003926 if (!(root == root->fs_info->tree_root ||
3927 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003928 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003929
Chris Mason54aa1f42007-06-22 14:16:25 -04003930 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003931 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003932no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02003933 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003934 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003935}
3936
3937/*
3938 * this returns the key found in the dir entry in the location pointer.
3939 * If no dir entries were found, location->objectid is 0.
3940 */
3941static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3942 struct btrfs_key *location)
3943{
3944 const char *name = dentry->d_name.name;
3945 int namelen = dentry->d_name.len;
3946 struct btrfs_dir_item *di;
3947 struct btrfs_path *path;
3948 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003949 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003950
3951 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003952 if (!path)
3953 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003954
Li Zefan33345d012011-04-20 10:31:50 +08003955 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003956 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003957 if (IS_ERR(di))
3958 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003959
David Sterbac7040052011-04-19 18:00:01 +02003960 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003961 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003962
Chris Mason5f39d392007-10-15 16:14:19 -04003963 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003964out:
Chris Mason39279cc2007-06-12 06:35:45 -04003965 btrfs_free_path(path);
3966 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003967out_err:
3968 location->objectid = 0;
3969 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003970}
3971
3972/*
3973 * when we hit a tree root in a directory, the btrfs part of the inode
3974 * needs to be changed to reflect the root directory of the tree root. This
3975 * is kind of like crossing a mount point.
3976 */
3977static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003978 struct inode *dir,
3979 struct dentry *dentry,
3980 struct btrfs_key *location,
3981 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003982{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003983 struct btrfs_path *path;
3984 struct btrfs_root *new_root;
3985 struct btrfs_root_ref *ref;
3986 struct extent_buffer *leaf;
3987 int ret;
3988 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003989
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003990 path = btrfs_alloc_path();
3991 if (!path) {
3992 err = -ENOMEM;
3993 goto out;
3994 }
Chris Mason39279cc2007-06-12 06:35:45 -04003995
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003996 err = -ENOENT;
3997 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3998 BTRFS_I(dir)->root->root_key.objectid,
3999 location->objectid);
4000 if (ret) {
4001 if (ret < 0)
4002 err = ret;
4003 goto out;
4004 }
Chris Mason39279cc2007-06-12 06:35:45 -04004005
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004006 leaf = path->nodes[0];
4007 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004008 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004009 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4010 goto out;
4011
4012 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4013 (unsigned long)(ref + 1),
4014 dentry->d_name.len);
4015 if (ret)
4016 goto out;
4017
David Sterbab3b4aa72011-04-21 01:20:15 +02004018 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004019
4020 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4021 if (IS_ERR(new_root)) {
4022 err = PTR_ERR(new_root);
4023 goto out;
4024 }
4025
4026 if (btrfs_root_refs(&new_root->root_item) == 0) {
4027 err = -ENOENT;
4028 goto out;
4029 }
4030
4031 *sub_root = new_root;
4032 location->objectid = btrfs_root_dirid(&new_root->root_item);
4033 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004034 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004035 err = 0;
4036out:
4037 btrfs_free_path(path);
4038 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004039}
4040
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004041static void inode_tree_add(struct inode *inode)
4042{
4043 struct btrfs_root *root = BTRFS_I(inode)->root;
4044 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004045 struct rb_node **p;
4046 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004047 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004048again:
4049 p = &root->inode_tree.rb_node;
4050 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004051
Al Viro1d3382cb2010-10-23 15:19:20 -04004052 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004053 return;
4054
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004055 spin_lock(&root->inode_lock);
4056 while (*p) {
4057 parent = *p;
4058 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4059
Li Zefan33345d012011-04-20 10:31:50 +08004060 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004061 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004062 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004063 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004064 else {
4065 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004066 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004067 rb_erase(parent, &root->inode_tree);
4068 RB_CLEAR_NODE(parent);
4069 spin_unlock(&root->inode_lock);
4070 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004071 }
4072 }
4073 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4074 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4075 spin_unlock(&root->inode_lock);
4076}
4077
4078static void inode_tree_del(struct inode *inode)
4079{
4080 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004081 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004082
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004083 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004084 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004085 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004086 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004087 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004088 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004089 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004090
Josef Bacik0af3d002010-06-21 14:48:16 -04004091 /*
4092 * Free space cache has inodes in the tree root, but the tree root has a
4093 * root_refs of 0, so this could end up dropping the tree root as a
4094 * snapshot, so we need the extra !root->fs_info->tree_root check to
4095 * make sure we don't drop it.
4096 */
4097 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4098 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004099 synchronize_srcu(&root->fs_info->subvol_srcu);
4100 spin_lock(&root->inode_lock);
4101 empty = RB_EMPTY_ROOT(&root->inode_tree);
4102 spin_unlock(&root->inode_lock);
4103 if (empty)
4104 btrfs_add_dead_root(root);
4105 }
4106}
4107
Jeff Mahoney143bede2012-03-01 14:56:26 +01004108void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004109{
4110 struct rb_node *node;
4111 struct rb_node *prev;
4112 struct btrfs_inode *entry;
4113 struct inode *inode;
4114 u64 objectid = 0;
4115
4116 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4117
4118 spin_lock(&root->inode_lock);
4119again:
4120 node = root->inode_tree.rb_node;
4121 prev = NULL;
4122 while (node) {
4123 prev = node;
4124 entry = rb_entry(node, struct btrfs_inode, rb_node);
4125
Li Zefan33345d012011-04-20 10:31:50 +08004126 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004127 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004128 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004129 node = node->rb_right;
4130 else
4131 break;
4132 }
4133 if (!node) {
4134 while (prev) {
4135 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004136 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004137 node = prev;
4138 break;
4139 }
4140 prev = rb_next(prev);
4141 }
4142 }
4143 while (node) {
4144 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004145 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004146 inode = igrab(&entry->vfs_inode);
4147 if (inode) {
4148 spin_unlock(&root->inode_lock);
4149 if (atomic_read(&inode->i_count) > 1)
4150 d_prune_aliases(inode);
4151 /*
Al Viro45321ac2010-06-07 13:43:19 -04004152 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004153 * the inode cache when its usage count
4154 * hits zero.
4155 */
4156 iput(inode);
4157 cond_resched();
4158 spin_lock(&root->inode_lock);
4159 goto again;
4160 }
4161
4162 if (cond_resched_lock(&root->inode_lock))
4163 goto again;
4164
4165 node = rb_next(node);
4166 }
4167 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004168}
4169
Chris Masone02119d2008-09-05 16:13:11 -04004170static int btrfs_init_locked_inode(struct inode *inode, void *p)
4171{
4172 struct btrfs_iget_args *args = p;
4173 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004174 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004175 return 0;
4176}
4177
4178static int btrfs_find_actor(struct inode *inode, void *opaque)
4179{
4180 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004181 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004182 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004183}
4184
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004185static struct inode *btrfs_iget_locked(struct super_block *s,
4186 u64 objectid,
4187 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004188{
4189 struct inode *inode;
4190 struct btrfs_iget_args args;
4191 args.ino = objectid;
4192 args.root = root;
4193
4194 inode = iget5_locked(s, objectid, btrfs_find_actor,
4195 btrfs_init_locked_inode,
4196 (void *)&args);
4197 return inode;
4198}
4199
Balaji Rao1a54ef82008-07-21 02:01:04 +05304200/* Get an inode object given its location and corresponding root.
4201 * Returns in *is_new if the inode was read from disk
4202 */
4203struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004204 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304205{
4206 struct inode *inode;
4207
4208 inode = btrfs_iget_locked(s, location->objectid, root);
4209 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004210 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304211
4212 if (inode->i_state & I_NEW) {
4213 BTRFS_I(inode)->root = root;
4214 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4215 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004216 if (!is_bad_inode(inode)) {
4217 inode_tree_add(inode);
4218 unlock_new_inode(inode);
4219 if (new)
4220 *new = 1;
4221 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004222 unlock_new_inode(inode);
4223 iput(inode);
4224 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004225 }
4226 }
4227
Balaji Rao1a54ef82008-07-21 02:01:04 +05304228 return inode;
4229}
4230
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004231static struct inode *new_simple_dir(struct super_block *s,
4232 struct btrfs_key *key,
4233 struct btrfs_root *root)
4234{
4235 struct inode *inode = new_inode(s);
4236
4237 if (!inode)
4238 return ERR_PTR(-ENOMEM);
4239
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004240 BTRFS_I(inode)->root = root;
4241 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004242 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004243
4244 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004245 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246 inode->i_fop = &simple_dir_operations;
4247 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4248 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4249
4250 return inode;
4251}
4252
Chris Mason3de45862008-11-17 21:02:50 -05004253struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004254{
Chris Masond3977122009-01-05 21:25:51 -05004255 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004256 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004257 struct btrfs_root *sub_root = root;
4258 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004259 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004260 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004261
4262 if (dentry->d_name.len > BTRFS_NAME_LEN)
4263 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004264
Jeff Layton39e3c952012-11-28 11:30:53 -05004265 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04004266 if (ret < 0)
4267 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004268
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004269 if (location.objectid == 0)
4270 return NULL;
4271
4272 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004273 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004274 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004275 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004276
4277 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4278
Yan, Zheng76dda932009-09-21 16:00:26 -04004279 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004280 ret = fixup_tree_root_location(root, dir, dentry,
4281 &location, &sub_root);
4282 if (ret < 0) {
4283 if (ret != -ENOENT)
4284 inode = ERR_PTR(ret);
4285 else
4286 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4287 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004288 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004289 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004290 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4291
Julia Lawall34d19ba2011-01-24 19:55:19 +00004292 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004293 down_read(&root->fs_info->cleanup_work_sem);
4294 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004295 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004296 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004297 if (ret)
4298 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004299 }
4300
Chris Mason3de45862008-11-17 21:02:50 -05004301 return inode;
4302}
4303
Nick Pigginfe15ce42011-01-07 17:49:23 +11004304static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004305{
4306 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004307 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004308
Li Zefan848cce02012-02-21 17:04:28 +08004309 if (!inode && !IS_ROOT(dentry))
4310 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004311
Li Zefan848cce02012-02-21 17:04:28 +08004312 if (inode) {
4313 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004314 if (btrfs_root_refs(&root->root_item) == 0)
4315 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004316
4317 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4318 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004319 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004320 return 0;
4321}
4322
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004323static void btrfs_dentry_release(struct dentry *dentry)
4324{
4325 if (dentry->d_fsdata)
4326 kfree(dentry->d_fsdata);
4327}
4328
Chris Mason3de45862008-11-17 21:02:50 -05004329static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004330 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004331{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004332 struct dentry *ret;
4333
4334 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04004335 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004336}
4337
Miao Xie16cdcec2011-04-22 18:12:22 +08004338unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004339 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4340};
4341
David Woodhousecbdf5a22008-08-06 19:42:33 +01004342static int btrfs_real_readdir(struct file *filp, void *dirent,
4343 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004344{
Al Viro496ad9a2013-01-23 17:07:38 -05004345 struct inode *inode = file_inode(filp);
Chris Mason39279cc2007-06-12 06:35:45 -04004346 struct btrfs_root *root = BTRFS_I(inode)->root;
4347 struct btrfs_item *item;
4348 struct btrfs_dir_item *di;
4349 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004350 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004351 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004352 struct list_head ins_list;
4353 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004354 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004355 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004356 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004357 unsigned char d_type;
4358 int over = 0;
4359 u32 di_cur;
4360 u32 di_total;
4361 u32 di_len;
4362 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004363 char tmp_name[32];
4364 char *name_ptr;
4365 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004366 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004367
4368 /* FIXME, use a real flag for deciding about the key type */
4369 if (root->fs_info->tree_root == root)
4370 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004371
Chris Mason39544012007-12-12 14:38:19 -05004372 /* special case for "." */
4373 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004374 over = filldir(dirent, ".", 1,
4375 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004376 if (over)
4377 return 0;
4378 filp->f_pos = 1;
4379 }
Chris Mason39544012007-12-12 14:38:19 -05004380 /* special case for .., just use the back ref */
4381 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004382 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004383 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004384 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004385 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004386 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004387 filp->f_pos = 2;
4388 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004389 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004390 if (!path)
4391 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004392
Josef Bacik026fd312011-05-13 10:32:11 -04004393 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004394
Miao Xie16cdcec2011-04-22 18:12:22 +08004395 if (key_type == BTRFS_DIR_INDEX_KEY) {
4396 INIT_LIST_HEAD(&ins_list);
4397 INIT_LIST_HEAD(&del_list);
4398 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4399 }
4400
Chris Mason39279cc2007-06-12 06:35:45 -04004401 btrfs_set_key_type(&key, key_type);
4402 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004403 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004404
Chris Mason39279cc2007-06-12 06:35:45 -04004405 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4406 if (ret < 0)
4407 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004408
4409 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004410 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004411 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004412 if (slot >= btrfs_header_nritems(leaf)) {
4413 ret = btrfs_next_leaf(root, path);
4414 if (ret < 0)
4415 goto err;
4416 else if (ret > 0)
4417 break;
4418 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004419 }
Chris Mason3de45862008-11-17 21:02:50 -05004420
Chris Mason5f39d392007-10-15 16:14:19 -04004421 item = btrfs_item_nr(leaf, slot);
4422 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4423
4424 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004425 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004426 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004427 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004428 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004429 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004430 if (key_type == BTRFS_DIR_INDEX_KEY &&
4431 btrfs_should_delete_dir_index(&del_list,
4432 found_key.offset))
4433 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004434
4435 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004436 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004437
Chris Mason39279cc2007-06-12 06:35:45 -04004438 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4439 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004440 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004441
4442 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004443 struct btrfs_key location;
4444
Josef Bacik22a94d42011-03-16 16:47:17 -04004445 if (verify_dir_item(root, leaf, di))
4446 break;
4447
Chris Mason5f39d392007-10-15 16:14:19 -04004448 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004449 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004450 name_ptr = tmp_name;
4451 } else {
4452 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004453 if (!name_ptr) {
4454 ret = -ENOMEM;
4455 goto err;
4456 }
Chris Mason5f39d392007-10-15 16:14:19 -04004457 }
4458 read_extent_buffer(leaf, name_ptr,
4459 (unsigned long)(di + 1), name_len);
4460
4461 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4462 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004463
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004464
Chris Mason3de45862008-11-17 21:02:50 -05004465 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004466 * skip it.
4467 *
4468 * In contrast to old kernels, we insert the snapshot's
4469 * dir item and dir index after it has been created, so
4470 * we won't find a reference to our own snapshot. We
4471 * still keep the following code for backward
4472 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004473 */
4474 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4475 location.objectid == root->root_key.objectid) {
4476 over = 0;
4477 goto skip;
4478 }
Chris Mason5f39d392007-10-15 16:14:19 -04004479 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004480 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004481 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004482
Chris Mason3de45862008-11-17 21:02:50 -05004483skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004484 if (name_ptr != tmp_name)
4485 kfree(name_ptr);
4486
Chris Mason39279cc2007-06-12 06:35:45 -04004487 if (over)
4488 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004489 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004490 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004491 di_cur += di_len;
4492 di = (struct btrfs_dir_item *)((char *)di + di_len);
4493 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004494next:
4495 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004496 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004497
Miao Xie16cdcec2011-04-22 18:12:22 +08004498 if (key_type == BTRFS_DIR_INDEX_KEY) {
4499 if (is_curr)
4500 filp->f_pos++;
4501 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4502 &ins_list);
4503 if (ret)
4504 goto nopos;
4505 }
4506
David Woodhouse49593bf2008-08-17 17:08:36 +01004507 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004508 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004509 /*
4510 * 32-bit glibc will use getdents64, but then strtol -
4511 * so the last number we can serve is this.
4512 */
4513 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004514 else
4515 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004516nopos:
4517 ret = 0;
4518err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004519 if (key_type == BTRFS_DIR_INDEX_KEY)
4520 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004521 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004522 return ret;
4523}
4524
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004525int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004526{
4527 struct btrfs_root *root = BTRFS_I(inode)->root;
4528 struct btrfs_trans_handle *trans;
4529 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004530 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004531
Josef Bacik72ac3c02012-05-23 14:13:11 -04004532 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004533 return 0;
4534
Liu Bo83eea1f2012-07-10 05:28:39 -06004535 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004536 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004537
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004538 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004539 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004540 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004541 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004542 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004543 if (IS_ERR(trans))
4544 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004545 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004546 }
4547 return ret;
4548}
4549
4550/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004551 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004552 * inode changes. But, it is most likely to find the inode in cache.
4553 * FIXME, needs more benchmarking...there are no reasons other than performance
4554 * to keep or drop this code.
4555 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004556int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004557{
4558 struct btrfs_root *root = BTRFS_I(inode)->root;
4559 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004560 int ret;
4561
Josef Bacik72ac3c02012-05-23 14:13:11 -04004562 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004563 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004564
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004565 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004566 if (IS_ERR(trans))
4567 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004568
4569 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004570 if (ret && ret == -ENOSPC) {
4571 /* whoops, lets try again with the full transaction */
4572 btrfs_end_transaction(trans, root);
4573 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004574 if (IS_ERR(trans))
4575 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004576
Chris Mason94b60442010-05-26 11:02:00 -04004577 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004578 }
Chris Mason39279cc2007-06-12 06:35:45 -04004579 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004580 if (BTRFS_I(inode)->delayed_node)
4581 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004582
4583 return ret;
4584}
4585
4586/*
4587 * This is a copy of file_update_time. We need this so we can return error on
4588 * ENOSPC for updating the inode in the case of file write and mmap writes.
4589 */
Josef Bacike41f9412012-03-26 09:46:47 -04004590static int btrfs_update_time(struct inode *inode, struct timespec *now,
4591 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004592{
Alexander Block2bc5565282012-06-15 09:49:33 +02004593 struct btrfs_root *root = BTRFS_I(inode)->root;
4594
4595 if (btrfs_root_readonly(root))
4596 return -EROFS;
4597
Josef Bacike41f9412012-03-26 09:46:47 -04004598 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004599 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004600 if (flags & S_CTIME)
4601 inode->i_ctime = *now;
4602 if (flags & S_MTIME)
4603 inode->i_mtime = *now;
4604 if (flags & S_ATIME)
4605 inode->i_atime = *now;
4606 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004607}
4608
Chris Masond352ac62008-09-29 15:18:18 -04004609/*
4610 * find the highest existing sequence number in a directory
4611 * and then set the in-memory index_cnt variable to reflect
4612 * free sequence numbers
4613 */
Josef Bacikaec74772008-07-24 12:12:38 -04004614static int btrfs_set_inode_index_count(struct inode *inode)
4615{
4616 struct btrfs_root *root = BTRFS_I(inode)->root;
4617 struct btrfs_key key, found_key;
4618 struct btrfs_path *path;
4619 struct extent_buffer *leaf;
4620 int ret;
4621
Li Zefan33345d012011-04-20 10:31:50 +08004622 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004623 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4624 key.offset = (u64)-1;
4625
4626 path = btrfs_alloc_path();
4627 if (!path)
4628 return -ENOMEM;
4629
4630 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4631 if (ret < 0)
4632 goto out;
4633 /* FIXME: we should be able to handle this */
4634 if (ret == 0)
4635 goto out;
4636 ret = 0;
4637
4638 /*
4639 * MAGIC NUMBER EXPLANATION:
4640 * since we search a directory based on f_pos we have to start at 2
4641 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4642 * else has to start at 2
4643 */
4644 if (path->slots[0] == 0) {
4645 BTRFS_I(inode)->index_cnt = 2;
4646 goto out;
4647 }
4648
4649 path->slots[0]--;
4650
4651 leaf = path->nodes[0];
4652 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4653
Li Zefan33345d012011-04-20 10:31:50 +08004654 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004655 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4656 BTRFS_I(inode)->index_cnt = 2;
4657 goto out;
4658 }
4659
4660 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4661out:
4662 btrfs_free_path(path);
4663 return ret;
4664}
4665
Chris Masond352ac62008-09-29 15:18:18 -04004666/*
4667 * helper to find a free sequence number in a given directory. This current
4668 * code is very simple, later versions will do smarter things in the btree
4669 */
Chris Mason3de45862008-11-17 21:02:50 -05004670int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004671{
4672 int ret = 0;
4673
4674 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004675 ret = btrfs_inode_delayed_dir_index_count(dir);
4676 if (ret) {
4677 ret = btrfs_set_inode_index_count(dir);
4678 if (ret)
4679 return ret;
4680 }
Josef Bacikaec74772008-07-24 12:12:38 -04004681 }
4682
Chris Mason00e4e6b2008-08-05 11:18:09 -04004683 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004684 BTRFS_I(dir)->index_cnt++;
4685
4686 return ret;
4687}
4688
Chris Mason39279cc2007-06-12 06:35:45 -04004689static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4690 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004691 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004692 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004693 u64 ref_objectid, u64 objectid,
4694 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004695{
4696 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004697 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004698 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004699 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004700 struct btrfs_inode_ref *ref;
4701 struct btrfs_key key[2];
4702 u32 sizes[2];
4703 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004704 int ret;
4705 int owner;
4706
Chris Mason5f39d392007-10-15 16:14:19 -04004707 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004708 if (!path)
4709 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004710
Chris Mason39279cc2007-06-12 06:35:45 -04004711 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004712 if (!inode) {
4713 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004714 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004715 }
Chris Mason39279cc2007-06-12 06:35:45 -04004716
Li Zefan581bb052011-04-20 10:06:11 +08004717 /*
4718 * we have to initialize this early, so we can reclaim the inode
4719 * number if we fail afterwards in this function.
4720 */
4721 inode->i_ino = objectid;
4722
Josef Bacikaec74772008-07-24 12:12:38 -04004723 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004724 trace_btrfs_inode_request(dir);
4725
Chris Mason3de45862008-11-17 21:02:50 -05004726 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004727 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004728 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004729 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004730 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004731 }
Josef Bacikaec74772008-07-24 12:12:38 -04004732 }
4733 /*
4734 * index_cnt is ignored for everything but a dir,
4735 * btrfs_get_inode_index_count has an explanation for the magic
4736 * number
4737 */
4738 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004739 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004740 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004741 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04004742
Josef Bacik5dc562c2012-08-17 13:14:17 -04004743 /*
4744 * We could have gotten an inode number from somebody who was fsynced
4745 * and then removed in this same transaction, so let's just set full
4746 * sync since it will be a full sync anyway and this will blow away the
4747 * old info in the log.
4748 */
4749 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4750
Al Viro569254b2011-07-24 17:08:40 -04004751 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004752 owner = 0;
4753 else
4754 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004755
4756 key[0].objectid = objectid;
4757 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4758 key[0].offset = 0;
4759
Mark Fashehf1863732012-08-08 11:32:27 -07004760 /*
4761 * Start new inodes with an inode_ref. This is slightly more
4762 * efficient for small numbers of hard links since they will
4763 * be packed into one item. Extended refs will kick in if we
4764 * add more hard links than can fit in the ref item.
4765 */
Chris Mason9c583092008-01-29 15:15:18 -05004766 key[1].objectid = objectid;
4767 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4768 key[1].offset = ref_objectid;
4769
4770 sizes[0] = sizeof(struct btrfs_inode_item);
4771 sizes[1] = name_len + sizeof(*ref);
4772
Chris Masonb9473432009-03-13 11:00:37 -04004773 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004774 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4775 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004776 goto fail;
4777
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004778 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004779 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004780 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004781 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4782 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004783 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4784 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004785 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004786
4787 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4788 struct btrfs_inode_ref);
4789 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004790 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004791 ptr = (unsigned long)(ref + 1);
4792 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4793
Chris Mason5f39d392007-10-15 16:14:19 -04004794 btrfs_mark_buffer_dirty(path->nodes[0]);
4795 btrfs_free_path(path);
4796
Chris Mason39279cc2007-06-12 06:35:45 -04004797 location = &BTRFS_I(inode)->location;
4798 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004799 location->offset = 0;
4800 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4801
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004802 btrfs_inherit_iflags(inode, dir);
4803
Al Viro569254b2011-07-24 17:08:40 -04004804 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004805 if (btrfs_test_opt(root, NODATASUM))
4806 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06004807 if (btrfs_test_opt(root, NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004808 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4809 }
4810
Chris Mason39279cc2007-06-12 06:35:45 -04004811 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004812 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004813
4814 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004815 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004816
Alexander Block8ea05e32012-07-25 17:35:53 +02004817 btrfs_update_root_times(trans, root);
4818
Chris Mason39279cc2007-06-12 06:35:45 -04004819 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004820fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004821 if (dir)
4822 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004823 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004824 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004825 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004826}
4827
4828static inline u8 btrfs_inode_type(struct inode *inode)
4829{
4830 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4831}
4832
Chris Masond352ac62008-09-29 15:18:18 -04004833/*
4834 * utility function to add 'inode' into 'parent_inode' with
4835 * a give name and a given sequence number.
4836 * if 'add_backref' is true, also insert a backref from the
4837 * inode to the parent directory.
4838 */
Chris Masone02119d2008-09-05 16:13:11 -04004839int btrfs_add_link(struct btrfs_trans_handle *trans,
4840 struct inode *parent_inode, struct inode *inode,
4841 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004842{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004843 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004844 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004845 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004846 u64 ino = btrfs_ino(inode);
4847 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004848
Li Zefan33345d012011-04-20 10:31:50 +08004849 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004850 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4851 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004852 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004853 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4854 key.offset = 0;
4855 }
Chris Mason39279cc2007-06-12 06:35:45 -04004856
Li Zefan33345d012011-04-20 10:31:50 +08004857 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004858 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4859 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004860 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004861 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004862 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4863 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004864 }
4865
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004866 /* Nothing to clean up yet */
4867 if (ret)
4868 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004869
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004870 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4871 parent_inode, &key,
4872 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05004873 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004874 goto fail_dir_item;
4875 else if (ret) {
4876 btrfs_abort_transaction(trans, root, ret);
4877 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004878 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004879
4880 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4881 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004882 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004883 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4884 ret = btrfs_update_inode(trans, root, parent_inode);
4885 if (ret)
4886 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004887 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004888
4889fail_dir_item:
4890 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4891 u64 local_index;
4892 int err;
4893 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4894 key.objectid, root->root_key.objectid,
4895 parent_ino, &local_index, name, name_len);
4896
4897 } else if (add_backref) {
4898 u64 local_index;
4899 int err;
4900
4901 err = btrfs_del_inode_ref(trans, root, name, name_len,
4902 ino, parent_ino, &local_index);
4903 }
4904 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004905}
4906
4907static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004908 struct inode *dir, struct dentry *dentry,
4909 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004910{
Josef Bacika1b075d2010-11-19 20:36:11 +00004911 int err = btrfs_add_link(trans, dir, inode,
4912 dentry->d_name.name, dentry->d_name.len,
4913 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004914 if (err > 0)
4915 err = -EEXIST;
4916 return err;
4917}
4918
Josef Bacik618e21d2007-07-11 10:18:17 -04004919static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004920 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004921{
4922 struct btrfs_trans_handle *trans;
4923 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004924 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004925 int err;
4926 int drop_inode = 0;
4927 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004928 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004929
4930 if (!new_valid_dev(rdev))
4931 return -EINVAL;
4932
Josef Bacik9ed74f22009-09-11 16:12:44 -04004933 /*
4934 * 2 for inode item and ref
4935 * 2 for dir items
4936 * 1 for xattr if selinux is on
4937 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004938 trans = btrfs_start_transaction(root, 5);
4939 if (IS_ERR(trans))
4940 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004941
Li Zefan581bb052011-04-20 10:06:11 +08004942 err = btrfs_find_free_ino(root, &objectid);
4943 if (err)
4944 goto out_unlock;
4945
Josef Bacikaec74772008-07-24 12:12:38 -04004946 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004947 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004948 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004949 if (IS_ERR(inode)) {
4950 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004951 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004952 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004953
Eric Paris2a7dba32011-02-01 11:05:39 -05004954 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004955 if (err) {
4956 drop_inode = 1;
4957 goto out_unlock;
4958 }
4959
Filipe Brandenburger2794ed02012-11-30 03:40:08 +00004960 err = btrfs_update_inode(trans, root, inode);
4961 if (err) {
4962 drop_inode = 1;
4963 goto out_unlock;
4964 }
4965
Casey Schauflerad19db72011-12-15 10:09:07 -05004966 /*
4967 * If the active LSM wants to access the inode during
4968 * d_instantiate it needs these. Smack checks to see
4969 * if the filesystem supports xattrs by looking at the
4970 * ops vector.
4971 */
4972
4973 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004974 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004975 if (err)
4976 drop_inode = 1;
4977 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004978 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004979 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05004980 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004981 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004982out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004983 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004984 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04004985 if (drop_inode) {
4986 inode_dec_link_count(inode);
4987 iput(inode);
4988 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004989 return err;
4990}
4991
Chris Mason39279cc2007-06-12 06:35:45 -04004992static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04004993 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04004994{
4995 struct btrfs_trans_handle *trans;
4996 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004997 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00004998 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004999 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005000 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005001 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005002
Josef Bacik9ed74f22009-09-11 16:12:44 -04005003 /*
5004 * 2 for inode item and ref
5005 * 2 for dir items
5006 * 1 for xattr if selinux is on
5007 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005008 trans = btrfs_start_transaction(root, 5);
5009 if (IS_ERR(trans))
5010 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005011
Li Zefan581bb052011-04-20 10:06:11 +08005012 err = btrfs_find_free_ino(root, &objectid);
5013 if (err)
5014 goto out_unlock;
5015
Josef Bacikaec74772008-07-24 12:12:38 -04005016 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005017 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005018 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005019 if (IS_ERR(inode)) {
5020 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005021 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005022 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005023 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005024
Eric Paris2a7dba32011-02-01 11:05:39 -05005025 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005026 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005027 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005028
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005029 err = btrfs_update_inode(trans, root, inode);
5030 if (err)
5031 goto out_unlock;
5032
Casey Schauflerad19db72011-12-15 10:09:07 -05005033 /*
5034 * If the active LSM wants to access the inode during
5035 * d_instantiate it needs these. Smack checks to see
5036 * if the filesystem supports xattrs by looking at the
5037 * ops vector.
5038 */
5039 inode->i_fop = &btrfs_file_operations;
5040 inode->i_op = &btrfs_file_inode_operations;
5041
Josef Bacika1b075d2010-11-19 20:36:11 +00005042 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005043 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005044 goto out_unlock;
5045
5046 inode->i_mapping->a_ops = &btrfs_aops;
5047 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5048 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5049 d_instantiate(dentry, inode);
5050
Chris Mason39279cc2007-06-12 06:35:45 -04005051out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005052 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005053 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005054 inode_dec_link_count(inode);
5055 iput(inode);
5056 }
Liu Bob53d3f52012-11-14 14:34:34 +00005057 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005058 return err;
5059}
5060
5061static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5062 struct dentry *dentry)
5063{
5064 struct btrfs_trans_handle *trans;
5065 struct btrfs_root *root = BTRFS_I(dir)->root;
5066 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005067 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005068 int err;
5069 int drop_inode = 0;
5070
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005071 /* do not allow sys_link's with other subvols of the same device */
5072 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005073 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005074
Mark Fashehf1863732012-08-08 11:32:27 -07005075 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005076 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005077
Chris Mason3de45862008-11-17 21:02:50 -05005078 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005079 if (err)
5080 goto fail;
5081
Yan, Zhenga22285a2010-05-16 10:48:46 -04005082 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005083 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005084 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005085 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005086 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005087 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005088 if (IS_ERR(trans)) {
5089 err = PTR_ERR(trans);
5090 goto fail;
5091 }
Chris Mason5f39d392007-10-15 16:14:19 -04005092
Miao Xie31534952011-04-13 13:19:21 +08005093 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005094 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005095 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005096 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005097 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005098
Josef Bacika1b075d2010-11-19 20:36:11 +00005099 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005100
Yan, Zhenga5719522009-09-24 09:17:31 -04005101 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005102 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005103 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005104 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005105 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005106 if (err)
5107 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005108 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005109 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005110 }
Chris Mason39279cc2007-06-12 06:35:45 -04005111
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005112 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005113fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005114 if (drop_inode) {
5115 inode_dec_link_count(inode);
5116 iput(inode);
5117 }
Liu Bob53d3f52012-11-14 14:34:34 +00005118 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005119 return err;
5120}
5121
Al Viro18bb1db2011-07-26 01:41:39 -04005122static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005123{
Chris Masonb9d86662008-05-02 16:13:49 -04005124 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005125 struct btrfs_trans_handle *trans;
5126 struct btrfs_root *root = BTRFS_I(dir)->root;
5127 int err = 0;
5128 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005129 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005130 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005131
Josef Bacik9ed74f22009-09-11 16:12:44 -04005132 /*
5133 * 2 items for inode and ref
5134 * 2 items for dir items
5135 * 1 for xattr if selinux is on
5136 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005137 trans = btrfs_start_transaction(root, 5);
5138 if (IS_ERR(trans))
5139 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005140
Li Zefan581bb052011-04-20 10:06:11 +08005141 err = btrfs_find_free_ino(root, &objectid);
5142 if (err)
5143 goto out_fail;
5144
Josef Bacikaec74772008-07-24 12:12:38 -04005145 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005146 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005147 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005148 if (IS_ERR(inode)) {
5149 err = PTR_ERR(inode);
5150 goto out_fail;
5151 }
Chris Mason5f39d392007-10-15 16:14:19 -04005152
Chris Mason39279cc2007-06-12 06:35:45 -04005153 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005154
Eric Paris2a7dba32011-02-01 11:05:39 -05005155 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005156 if (err)
5157 goto out_fail;
5158
Chris Mason39279cc2007-06-12 06:35:45 -04005159 inode->i_op = &btrfs_dir_inode_operations;
5160 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005161
Chris Masondbe674a2008-07-17 12:54:05 -04005162 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005163 err = btrfs_update_inode(trans, root, inode);
5164 if (err)
5165 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005166
Josef Bacika1b075d2010-11-19 20:36:11 +00005167 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5168 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005169 if (err)
5170 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005171
Chris Mason39279cc2007-06-12 06:35:45 -04005172 d_instantiate(dentry, inode);
5173 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005174
5175out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005176 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005177 if (drop_on_err)
5178 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005179 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005180 return err;
5181}
5182
Chris Masond352ac62008-09-29 15:18:18 -04005183/* helper for btfs_get_extent. Given an existing extent in the tree,
5184 * and an extent that you want to insert, deal with overlap and insert
5185 * the new extent into the tree.
5186 */
Chris Mason3b951512008-04-17 11:29:12 -04005187static int merge_extent_mapping(struct extent_map_tree *em_tree,
5188 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005189 struct extent_map *em,
5190 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005191{
5192 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005193
Chris Masone6dcd2d2008-07-17 12:53:50 -04005194 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5195 start_diff = map_start - em->start;
5196 em->start = map_start;
5197 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005198 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5199 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005200 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005201 em->block_len -= start_diff;
5202 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005203 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005204}
5205
Chris Masonc8b97812008-10-29 14:49:59 -04005206static noinline int uncompress_inline(struct btrfs_path *path,
5207 struct inode *inode, struct page *page,
5208 size_t pg_offset, u64 extent_offset,
5209 struct btrfs_file_extent_item *item)
5210{
5211 int ret;
5212 struct extent_buffer *leaf = path->nodes[0];
5213 char *tmp;
5214 size_t max_size;
5215 unsigned long inline_size;
5216 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005217 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005218
5219 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005220 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005221 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5222 inline_size = btrfs_file_extent_inline_item_len(leaf,
5223 btrfs_item_nr(leaf, path->slots[0]));
5224 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005225 if (!tmp)
5226 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005227 ptr = btrfs_file_extent_inline_start(item);
5228
5229 read_extent_buffer(leaf, tmp, ptr, inline_size);
5230
Chris Mason5b050f02008-11-11 09:34:41 -05005231 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005232 ret = btrfs_decompress(compress_type, tmp, page,
5233 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005234 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005235 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005236 unsigned long copy_size = min_t(u64,
5237 PAGE_CACHE_SIZE - pg_offset,
5238 max_size - extent_offset);
5239 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005240 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005241 }
5242 kfree(tmp);
5243 return 0;
5244}
5245
Chris Masond352ac62008-09-29 15:18:18 -04005246/*
5247 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005248 * the ugly parts come from merging extents from the disk with the in-ram
5249 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005250 * where the in-ram extents might be locked pending data=ordered completion.
5251 *
5252 * This also copies inline extents directly into the page.
5253 */
Chris Masond3977122009-01-05 21:25:51 -05005254
Chris Masona52d9a82007-08-27 16:49:44 -04005255struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005256 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005257 int create)
5258{
5259 int ret;
5260 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005261 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005262 u64 extent_start = 0;
5263 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005264 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005265 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005266 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005267 struct btrfs_root *root = BTRFS_I(inode)->root;
5268 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005269 struct extent_buffer *leaf;
5270 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005271 struct extent_map *em = NULL;
5272 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005273 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005274 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005275 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005276
Chris Masona52d9a82007-08-27 16:49:44 -04005277again:
Chris Mason890871b2009-09-02 16:24:52 -04005278 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005279 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005280 if (em)
5281 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005282 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005283
Chris Masona52d9a82007-08-27 16:49:44 -04005284 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005285 if (em->start > start || em->start + em->len <= start)
5286 free_extent_map(em);
5287 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005288 free_extent_map(em);
5289 else
5290 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005291 }
David Sterba172ddd62011-04-21 00:48:27 +02005292 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005293 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005294 err = -ENOMEM;
5295 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005296 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005297 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005298 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005299 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005300 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005301 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005302
5303 if (!path) {
5304 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005305 if (!path) {
5306 err = -ENOMEM;
5307 goto out;
5308 }
5309 /*
5310 * Chances are we'll be called again, so go ahead and do
5311 * readahead
5312 */
5313 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005314 }
5315
Chris Mason179e29e2007-11-01 11:28:41 -04005316 ret = btrfs_lookup_file_extent(trans, root, path,
5317 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005318 if (ret < 0) {
5319 err = ret;
5320 goto out;
5321 }
5322
5323 if (ret != 0) {
5324 if (path->slots[0] == 0)
5325 goto not_found;
5326 path->slots[0]--;
5327 }
5328
Chris Mason5f39d392007-10-15 16:14:19 -04005329 leaf = path->nodes[0];
5330 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005331 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005332 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005333 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5334 found_type = btrfs_key_type(&found_key);
5335 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005336 found_type != BTRFS_EXTENT_DATA_KEY) {
5337 goto not_found;
5338 }
5339
Chris Mason5f39d392007-10-15 16:14:19 -04005340 found_type = btrfs_file_extent_type(leaf, item);
5341 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005342 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005343 if (found_type == BTRFS_FILE_EXTENT_REG ||
5344 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005345 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005346 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005347 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5348 size_t size;
5349 size = btrfs_file_extent_inline_len(leaf, item);
5350 extent_end = (extent_start + size + root->sectorsize - 1) &
5351 ~((u64)root->sectorsize - 1);
5352 }
5353
5354 if (start >= extent_end) {
5355 path->slots[0]++;
5356 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5357 ret = btrfs_next_leaf(root, path);
5358 if (ret < 0) {
5359 err = ret;
5360 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005361 }
Yan Zheng9036c102008-10-30 14:19:41 -04005362 if (ret > 0)
5363 goto not_found;
5364 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005365 }
Yan Zheng9036c102008-10-30 14:19:41 -04005366 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5367 if (found_key.objectid != objectid ||
5368 found_key.type != BTRFS_EXTENT_DATA_KEY)
5369 goto not_found;
5370 if (start + len <= found_key.offset)
5371 goto not_found;
5372 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005373 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04005374 em->len = found_key.offset - start;
5375 goto not_found_em;
5376 }
5377
Yan Zhengd899e052008-10-30 14:25:28 -04005378 if (found_type == BTRFS_FILE_EXTENT_REG ||
5379 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005380 em->start = extent_start;
5381 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005382 em->orig_start = extent_start -
5383 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05005384 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5385 item);
Chris Masondb945352007-10-15 16:15:53 -04005386 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5387 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005388 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005389 goto insert;
5390 }
Li Zefan261507a02010-12-17 14:21:50 +08005391 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005392 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005393 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005394 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05005395 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005396 } else {
5397 bytenr += btrfs_file_extent_offset(leaf, item);
5398 em->block_start = bytenr;
5399 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005400 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5401 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005402 }
Chris Masona52d9a82007-08-27 16:49:44 -04005403 goto insert;
5404 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005405 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005406 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005407 size_t size;
5408 size_t extent_offset;
5409 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005410
Chris Masona52d9a82007-08-27 16:49:44 -04005411 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005412 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005413 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005414 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005415 goto out;
5416 }
5417
Yan Zheng9036c102008-10-30 14:19:41 -04005418 size = btrfs_file_extent_inline_len(leaf, item);
5419 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005420 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005421 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005422 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005423 em->len = (copy_size + root->sectorsize - 1) &
5424 ~((u64)root->sectorsize - 1);
Josef Bacikb4939682012-12-03 10:31:19 -05005425 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005426 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08005427 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005428 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005429 em->compress_type = compress_type;
5430 }
Yan689f9342007-10-29 11:41:07 -04005431 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005432 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005433 if (btrfs_file_extent_compression(leaf, item) !=
5434 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005435 ret = uncompress_inline(path, inode, page,
5436 pg_offset,
5437 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005438 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005439 } else {
5440 map = kmap(page);
5441 read_extent_buffer(leaf, map + pg_offset, ptr,
5442 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005443 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5444 memset(map + pg_offset + copy_size, 0,
5445 PAGE_CACHE_SIZE - pg_offset -
5446 copy_size);
5447 }
Chris Masonc8b97812008-10-29 14:49:59 -04005448 kunmap(page);
5449 }
Chris Mason179e29e2007-11-01 11:28:41 -04005450 flush_dcache_page(page);
5451 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005452 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005453 if (!trans) {
5454 kunmap(page);
5455 free_extent_map(em);
5456 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005457
David Sterbab3b4aa72011-04-21 01:20:15 +02005458 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005459 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005460
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005461 if (IS_ERR(trans))
5462 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005463 goto again;
5464 }
Chris Masonc8b97812008-10-29 14:49:59 -04005465 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005466 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005467 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005468 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005469 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005470 }
Chris Masond1310b22008-01-24 16:13:08 -05005471 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005472 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005473 goto insert;
5474 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005475 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005476 }
5477not_found:
5478 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005479 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005480 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005481not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005482 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005483 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005484insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005485 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005486 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005487 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5488 "[%llu %llu]\n", (unsigned long long)em->start,
5489 (unsigned long long)em->len,
5490 (unsigned long long)start,
5491 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005492 err = -EIO;
5493 goto out;
5494 }
Chris Masond1310b22008-01-24 16:13:08 -05005495
5496 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005497 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005498 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005499 /* it is possible that someone inserted the extent into the tree
5500 * while we had the lock dropped. It is also possible that
5501 * an overlapping map exists in the tree
5502 */
Chris Masona52d9a82007-08-27 16:49:44 -04005503 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005504 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005505
5506 ret = 0;
5507
Chris Mason3b951512008-04-17 11:29:12 -04005508 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005509 if (existing && (existing->start > start ||
5510 existing->start + existing->len <= start)) {
5511 free_extent_map(existing);
5512 existing = NULL;
5513 }
Chris Mason3b951512008-04-17 11:29:12 -04005514 if (!existing) {
5515 existing = lookup_extent_mapping(em_tree, em->start,
5516 em->len);
5517 if (existing) {
5518 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005519 em, start,
5520 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005521 free_extent_map(existing);
5522 if (err) {
5523 free_extent_map(em);
5524 em = NULL;
5525 }
5526 } else {
5527 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005528 free_extent_map(em);
5529 em = NULL;
5530 }
5531 } else {
5532 free_extent_map(em);
5533 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005534 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005535 }
Chris Masona52d9a82007-08-27 16:49:44 -04005536 }
Chris Mason890871b2009-09-02 16:24:52 -04005537 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005538out:
liubo1abe9b82011-03-24 11:18:59 +00005539
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005540 if (em)
5541 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005542
Chris Masonf4219502008-07-22 11:18:09 -04005543 if (path)
5544 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005545 if (trans) {
5546 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005547 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005548 err = ret;
5549 }
Chris Masona52d9a82007-08-27 16:49:44 -04005550 if (err) {
5551 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005552 return ERR_PTR(err);
5553 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005554 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005555 return em;
5556}
5557
Chris Masonec29ed52011-02-23 16:23:20 -05005558struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5559 size_t pg_offset, u64 start, u64 len,
5560 int create)
5561{
5562 struct extent_map *em;
5563 struct extent_map *hole_em = NULL;
5564 u64 range_start = start;
5565 u64 end;
5566 u64 found;
5567 u64 found_end;
5568 int err = 0;
5569
5570 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5571 if (IS_ERR(em))
5572 return em;
5573 if (em) {
5574 /*
5575 * if our em maps to a hole, there might
5576 * actually be delalloc bytes behind it
5577 */
5578 if (em->block_start != EXTENT_MAP_HOLE)
5579 return em;
5580 else
5581 hole_em = em;
5582 }
5583
5584 /* check to see if we've wrapped (len == -1 or similar) */
5585 end = start + len;
5586 if (end < start)
5587 end = (u64)-1;
5588 else
5589 end -= 1;
5590
5591 em = NULL;
5592
5593 /* ok, we didn't find anything, lets look for delalloc */
5594 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5595 end, len, EXTENT_DELALLOC, 1);
5596 found_end = range_start + found;
5597 if (found_end < range_start)
5598 found_end = (u64)-1;
5599
5600 /*
5601 * we didn't find anything useful, return
5602 * the original results from get_extent()
5603 */
5604 if (range_start > end || found_end <= start) {
5605 em = hole_em;
5606 hole_em = NULL;
5607 goto out;
5608 }
5609
5610 /* adjust the range_start to make sure it doesn't
5611 * go backwards from the start they passed in
5612 */
5613 range_start = max(start,range_start);
5614 found = found_end - range_start;
5615
5616 if (found > 0) {
5617 u64 hole_start = start;
5618 u64 hole_len = len;
5619
David Sterba172ddd62011-04-21 00:48:27 +02005620 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005621 if (!em) {
5622 err = -ENOMEM;
5623 goto out;
5624 }
5625 /*
5626 * when btrfs_get_extent can't find anything it
5627 * returns one huge hole
5628 *
5629 * make sure what it found really fits our range, and
5630 * adjust to make sure it is based on the start from
5631 * the caller
5632 */
5633 if (hole_em) {
5634 u64 calc_end = extent_map_end(hole_em);
5635
5636 if (calc_end <= start || (hole_em->start > end)) {
5637 free_extent_map(hole_em);
5638 hole_em = NULL;
5639 } else {
5640 hole_start = max(hole_em->start, start);
5641 hole_len = calc_end - hole_start;
5642 }
5643 }
5644 em->bdev = NULL;
5645 if (hole_em && range_start > hole_start) {
5646 /* our hole starts before our delalloc, so we
5647 * have to return just the parts of the hole
5648 * that go until the delalloc starts
5649 */
5650 em->len = min(hole_len,
5651 range_start - hole_start);
5652 em->start = hole_start;
5653 em->orig_start = hole_start;
5654 /*
5655 * don't adjust block start at all,
5656 * it is fixed at EXTENT_MAP_HOLE
5657 */
5658 em->block_start = hole_em->block_start;
5659 em->block_len = hole_len;
5660 } else {
5661 em->start = range_start;
5662 em->len = found;
5663 em->orig_start = range_start;
5664 em->block_start = EXTENT_MAP_DELALLOC;
5665 em->block_len = found;
5666 }
5667 } else if (hole_em) {
5668 return hole_em;
5669 }
5670out:
5671
5672 free_extent_map(hole_em);
5673 if (err) {
5674 free_extent_map(em);
5675 return ERR_PTR(err);
5676 }
5677 return em;
5678}
5679
Josef Bacik4b46fce2010-05-23 11:00:55 -04005680static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5681 u64 start, u64 len)
5682{
5683 struct btrfs_root *root = BTRFS_I(inode)->root;
5684 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04005685 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005686 struct btrfs_key ins;
5687 u64 alloc_hint;
5688 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005689
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005690 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005691 if (IS_ERR(trans))
5692 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005693
5694 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5695
5696 alloc_hint = get_extent_allocation_hint(inode, start, len);
5697 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005698 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005699 if (ret) {
5700 em = ERR_PTR(ret);
5701 goto out;
5702 }
5703
Josef Bacik70c8a912012-10-11 16:54:30 -04005704 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
5705 ins.offset, ins.offset, 0);
5706 if (IS_ERR(em))
5707 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005708
5709 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5710 ins.offset, ins.offset, 0);
5711 if (ret) {
5712 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5713 em = ERR_PTR(ret);
5714 }
5715out:
5716 btrfs_end_transaction(trans, root);
5717 return em;
5718}
5719
Chris Mason46bfbb52010-05-26 11:04:10 -04005720/*
5721 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5722 * block must be cow'd
5723 */
5724static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5725 struct inode *inode, u64 offset, u64 len)
5726{
5727 struct btrfs_path *path;
5728 int ret;
5729 struct extent_buffer *leaf;
5730 struct btrfs_root *root = BTRFS_I(inode)->root;
5731 struct btrfs_file_extent_item *fi;
5732 struct btrfs_key key;
5733 u64 disk_bytenr;
5734 u64 backref_offset;
5735 u64 extent_end;
5736 u64 num_bytes;
5737 int slot;
5738 int found_type;
5739
5740 path = btrfs_alloc_path();
5741 if (!path)
5742 return -ENOMEM;
5743
Li Zefan33345d012011-04-20 10:31:50 +08005744 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005745 offset, 0);
5746 if (ret < 0)
5747 goto out;
5748
5749 slot = path->slots[0];
5750 if (ret == 1) {
5751 if (slot == 0) {
5752 /* can't find the item, must cow */
5753 ret = 0;
5754 goto out;
5755 }
5756 slot--;
5757 }
5758 ret = 0;
5759 leaf = path->nodes[0];
5760 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005761 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005762 key.type != BTRFS_EXTENT_DATA_KEY) {
5763 /* not our file or wrong item type, must cow */
5764 goto out;
5765 }
5766
5767 if (key.offset > offset) {
5768 /* Wrong offset, must cow */
5769 goto out;
5770 }
5771
5772 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5773 found_type = btrfs_file_extent_type(leaf, fi);
5774 if (found_type != BTRFS_FILE_EXTENT_REG &&
5775 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5776 /* not a regular extent, must cow */
5777 goto out;
5778 }
5779 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5780 backref_offset = btrfs_file_extent_offset(leaf, fi);
5781
5782 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5783 if (extent_end < offset + len) {
5784 /* extent doesn't include our full range, must cow */
5785 goto out;
5786 }
5787
5788 if (btrfs_extent_readonly(root, disk_bytenr))
5789 goto out;
5790
5791 /*
5792 * look for other files referencing this extent, if we
5793 * find any we must cow
5794 */
Li Zefan33345d012011-04-20 10:31:50 +08005795 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005796 key.offset - backref_offset, disk_bytenr))
5797 goto out;
5798
5799 /*
5800 * adjust disk_bytenr and num_bytes to cover just the bytes
5801 * in this extent we are about to write. If there
5802 * are any csums in that range we have to cow in order
5803 * to keep the csums correct
5804 */
5805 disk_bytenr += backref_offset;
5806 disk_bytenr += offset - key.offset;
5807 num_bytes = min(offset + len, extent_end) - offset;
5808 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5809 goto out;
5810 /*
5811 * all of the above have passed, it is safe to overwrite this extent
5812 * without cow
5813 */
5814 ret = 1;
5815out:
5816 btrfs_free_path(path);
5817 return ret;
5818}
5819
Josef Bacikeb838e72012-07-31 16:28:48 -04005820static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5821 struct extent_state **cached_state, int writing)
5822{
5823 struct btrfs_ordered_extent *ordered;
5824 int ret = 0;
5825
5826 while (1) {
5827 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5828 0, cached_state);
5829 /*
5830 * We're concerned with the entire range that we're going to be
5831 * doing DIO to, so we need to make sure theres no ordered
5832 * extents in this range.
5833 */
5834 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5835 lockend - lockstart + 1);
5836
5837 /*
5838 * We need to make sure there are no buffered pages in this
5839 * range either, we could have raced between the invalidate in
5840 * generic_file_direct_write and locking the extent. The
5841 * invalidate needs to happen so that reads after a write do not
5842 * get stale data.
5843 */
5844 if (!ordered && (!writing ||
5845 !test_range_bit(&BTRFS_I(inode)->io_tree,
5846 lockstart, lockend, EXTENT_UPTODATE, 0,
5847 *cached_state)))
5848 break;
5849
5850 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5851 cached_state, GFP_NOFS);
5852
5853 if (ordered) {
5854 btrfs_start_ordered_extent(inode, ordered, 1);
5855 btrfs_put_ordered_extent(ordered);
5856 } else {
5857 /* Screw you mmap */
5858 ret = filemap_write_and_wait_range(inode->i_mapping,
5859 lockstart,
5860 lockend);
5861 if (ret)
5862 break;
5863
5864 /*
5865 * If we found a page that couldn't be invalidated just
5866 * fall back to buffered.
5867 */
5868 ret = invalidate_inode_pages2_range(inode->i_mapping,
5869 lockstart >> PAGE_CACHE_SHIFT,
5870 lockend >> PAGE_CACHE_SHIFT);
5871 if (ret)
5872 break;
5873 }
5874
5875 cond_resched();
5876 }
5877
5878 return ret;
5879}
5880
Josef Bacik69ffb542012-09-11 15:40:07 -04005881static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5882 u64 len, u64 orig_start,
5883 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05005884 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04005885{
5886 struct extent_map_tree *em_tree;
5887 struct extent_map *em;
5888 struct btrfs_root *root = BTRFS_I(inode)->root;
5889 int ret;
5890
5891 em_tree = &BTRFS_I(inode)->extent_tree;
5892 em = alloc_extent_map();
5893 if (!em)
5894 return ERR_PTR(-ENOMEM);
5895
5896 em->start = start;
5897 em->orig_start = orig_start;
5898 em->len = len;
5899 em->block_len = block_len;
5900 em->block_start = block_start;
5901 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05005902 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005903 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04005904 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5905 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05005906 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04005907
5908 do {
5909 btrfs_drop_extent_cache(inode, em->start,
5910 em->start + em->len - 1, 0);
5911 write_lock(&em_tree->lock);
5912 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04005913 if (!ret)
5914 list_move(&em->list,
5915 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04005916 write_unlock(&em_tree->lock);
5917 } while (ret == -EEXIST);
5918
5919 if (ret) {
5920 free_extent_map(em);
5921 return ERR_PTR(ret);
5922 }
5923
5924 return em;
5925}
5926
5927
Josef Bacik4b46fce2010-05-23 11:00:55 -04005928static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5929 struct buffer_head *bh_result, int create)
5930{
5931 struct extent_map *em;
5932 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04005933 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005934 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04005935 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005936 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005937 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04005938 int unlock_bits = EXTENT_LOCKED;
5939 int ret;
5940
Josef Bacikeb838e72012-07-31 16:28:48 -04005941 if (create) {
5942 ret = btrfs_delalloc_reserve_space(inode, len);
5943 if (ret)
5944 return ret;
5945 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04005946 } else {
5947 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04005948 }
5949
Josef Bacikc3298612012-08-03 16:49:19 -04005950 lockstart = start;
5951 lockend = start + len - 1;
5952
Josef Bacikeb838e72012-07-31 16:28:48 -04005953 /*
5954 * If this errors out it's because we couldn't invalidate pagecache for
5955 * this range and we need to fallback to buffered.
5956 */
5957 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
5958 return -ENOTBLK;
5959
5960 if (create) {
5961 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
5962 lockend, EXTENT_DELALLOC, NULL,
5963 &cached_state, GFP_NOFS);
5964 if (ret)
5965 goto unlock_err;
5966 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005967
5968 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04005969 if (IS_ERR(em)) {
5970 ret = PTR_ERR(em);
5971 goto unlock_err;
5972 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005973
5974 /*
5975 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5976 * io. INLINE is special, and we could probably kludge it in here, but
5977 * it's still buffered so for safety lets just fall back to the generic
5978 * buffered path.
5979 *
5980 * For COMPRESSED we _have_ to read the entire extent in so we can
5981 * decompress it, so there will be buffering required no matter what we
5982 * do, so go ahead and fallback to buffered.
5983 *
5984 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5985 * to buffered IO. Don't blame me, this is the price we pay for using
5986 * the generic code.
5987 */
5988 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5989 em->block_start == EXTENT_MAP_INLINE) {
5990 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04005991 ret = -ENOTBLK;
5992 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005993 }
5994
5995 /* Just a good old fashioned hole, return */
5996 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5997 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5998 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04005999 ret = 0;
6000 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006001 }
6002
6003 /*
6004 * We don't allocate a new extent in the following cases
6005 *
6006 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6007 * existing extent.
6008 * 2) The extent is marked as PREALLOC. We're good to go here and can
6009 * just use the extent.
6010 *
6011 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006012 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006013 len = min(len, em->len - (start - em->start));
6014 lockstart = start + len;
6015 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006016 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006017
6018 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6019 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6020 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006021 int type;
6022 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006023 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006024
6025 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6026 type = BTRFS_ORDERED_PREALLOC;
6027 else
6028 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006029 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006030 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006031
6032 /*
6033 * we're not going to log anything, but we do need
6034 * to make sure the current transaction stays open
6035 * while we look for nocow cross refs
6036 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006037 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006038 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006039 goto must_cow;
6040
6041 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006042 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006043 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006044
6045 if (type == BTRFS_ORDERED_PREALLOC) {
6046 free_extent_map(em);
6047 em = create_pinned_em(inode, start, len,
6048 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006049 block_start, len,
6050 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006051 if (IS_ERR(em)) {
6052 btrfs_end_transaction(trans, root);
6053 goto unlock_err;
6054 }
6055 }
6056
Chris Mason46bfbb52010-05-26 11:04:10 -04006057 ret = btrfs_add_ordered_extent_dio(inode, start,
6058 block_start, len, len, type);
6059 btrfs_end_transaction(trans, root);
6060 if (ret) {
6061 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006062 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006063 }
6064 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006065 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006066 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006067 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006068must_cow:
6069 /*
6070 * this will cow the extent, reset the len in case we changed
6071 * it above
6072 */
6073 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006074 free_extent_map(em);
6075 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006076 if (IS_ERR(em)) {
6077 ret = PTR_ERR(em);
6078 goto unlock_err;
6079 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006080 len = min(len, em->len - (start - em->start));
6081unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006082 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6083 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006084 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006085 bh_result->b_bdev = em->bdev;
6086 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006087 if (create) {
6088 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6089 set_buffer_new(bh_result);
6090
6091 /*
6092 * Need to update the i_size under the extent lock so buffered
6093 * readers will get the updated i_size when we unlock.
6094 */
6095 if (start + len > i_size_read(inode))
6096 i_size_write(inode, start + len);
6097 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006098
Josef Bacikeb838e72012-07-31 16:28:48 -04006099 /*
6100 * In the case of write we need to clear and unlock the entire range,
6101 * in the case of read we need to unlock only the end area that we
6102 * aren't using if there is any left over space.
6103 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006104 if (lockstart < lockend) {
6105 if (create && len < lockend - lockstart) {
6106 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006107 lockstart + len - 1,
6108 unlock_bits | EXTENT_DEFRAG, 1, 0,
Liu Bo24c03fa2012-08-22 20:10:38 -06006109 &cached_state, GFP_NOFS);
6110 /*
6111 * Beside unlock, we also need to cleanup reserved space
6112 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6113 */
6114 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6115 lockstart + len, lockend,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006116 unlock_bits | EXTENT_DO_ACCOUNTING |
6117 EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006118 } else {
6119 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6120 lockend, unlock_bits, 1, 0,
6121 &cached_state, GFP_NOFS);
6122 }
6123 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006124 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006125 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006126
Josef Bacik4b46fce2010-05-23 11:00:55 -04006127 free_extent_map(em);
6128
6129 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006130
6131unlock_err:
6132 if (create)
6133 unlock_bits |= EXTENT_DO_ACCOUNTING;
6134
6135 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6136 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6137 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006138}
6139
6140struct btrfs_dio_private {
6141 struct inode *inode;
6142 u64 logical_offset;
6143 u64 disk_bytenr;
6144 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006145 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006146
6147 /* number of bios pending for this dio */
6148 atomic_t pending_bios;
6149
6150 /* IO errors */
6151 int errors;
6152
6153 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006154};
6155
6156static void btrfs_endio_direct_read(struct bio *bio, int err)
6157{
Miao Xiee65e1532010-11-22 03:04:43 +00006158 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006159 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6160 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006161 struct inode *inode = dip->inode;
6162 struct btrfs_root *root = BTRFS_I(inode)->root;
6163 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006164
6165 start = dip->logical_offset;
6166 do {
6167 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6168 struct page *page = bvec->bv_page;
6169 char *kaddr;
6170 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006171 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006172 unsigned long flags;
6173
Josef Bacikc3298612012-08-03 16:49:19 -04006174 if (get_state_private(&BTRFS_I(inode)->io_tree,
6175 start, &private))
6176 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006177 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006178 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006179 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6180 csum, bvec->bv_len);
6181 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006182 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006183 local_irq_restore(flags);
6184
6185 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006186 if (csum != private) {
6187failed:
Li Zefan33345d012011-04-20 10:31:50 +08006188 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006189 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006190 (unsigned long long)btrfs_ino(inode),
6191 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006192 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006193 err = -EIO;
6194 }
6195 }
6196
6197 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006198 bvec++;
6199 } while (bvec <= bvec_end);
6200
6201 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006202 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006203 bio->bi_private = dip->private;
6204
Josef Bacik4b46fce2010-05-23 11:00:55 -04006205 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006206
6207 /* If we had a csum failure make sure to clear the uptodate flag */
6208 if (err)
6209 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006210 dio_end_io(bio, err);
6211}
6212
6213static void btrfs_endio_direct_write(struct bio *bio, int err)
6214{
6215 struct btrfs_dio_private *dip = bio->bi_private;
6216 struct inode *inode = dip->inode;
6217 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006218 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006219 u64 ordered_offset = dip->logical_offset;
6220 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006221 int ret;
6222
6223 if (err)
6224 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006225again:
6226 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6227 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006228 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006229 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006230 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006231
Josef Bacik5fd02042012-05-02 14:00:54 -04006232 ordered->work.func = finish_ordered_fn;
6233 ordered->work.flags = 0;
6234 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6235 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006236out_test:
6237 /*
6238 * our bio might span multiple ordered extents. If we haven't
6239 * completed the accounting for the whole dio, go back and try again
6240 */
6241 if (ordered_offset < dip->logical_offset + dip->bytes) {
6242 ordered_bytes = dip->logical_offset + dip->bytes -
6243 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006244 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006245 goto again;
6246 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006247out_done:
6248 bio->bi_private = dip->private;
6249
Josef Bacik4b46fce2010-05-23 11:00:55 -04006250 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006251
6252 /* If we had an error make sure to clear the uptodate flag */
6253 if (err)
6254 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006255 dio_end_io(bio, err);
6256}
6257
Chris Masoneaf25d92010-05-25 09:48:28 -04006258static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6259 struct bio *bio, int mirror_num,
6260 unsigned long bio_flags, u64 offset)
6261{
6262 int ret;
6263 struct btrfs_root *root = BTRFS_I(inode)->root;
6264 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006265 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006266 return 0;
6267}
6268
Miao Xiee65e1532010-11-22 03:04:43 +00006269static void btrfs_end_dio_bio(struct bio *bio, int err)
6270{
6271 struct btrfs_dio_private *dip = bio->bi_private;
6272
6273 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006274 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006275 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006276 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006277 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006278 dip->errors = 1;
6279
6280 /*
6281 * before atomic variable goto zero, we must make sure
6282 * dip->errors is perceived to be set.
6283 */
6284 smp_mb__before_atomic_dec();
6285 }
6286
6287 /* if there are more bios still pending for this dio, just exit */
6288 if (!atomic_dec_and_test(&dip->pending_bios))
6289 goto out;
6290
6291 if (dip->errors)
6292 bio_io_error(dip->orig_bio);
6293 else {
6294 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6295 bio_endio(dip->orig_bio, 0);
6296 }
6297out:
6298 bio_put(bio);
6299}
6300
6301static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6302 u64 first_sector, gfp_t gfp_flags)
6303{
6304 int nr_vecs = bio_get_nr_vecs(bdev);
6305 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6306}
6307
6308static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6309 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006310 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006311{
6312 int write = rw & REQ_WRITE;
6313 struct btrfs_root *root = BTRFS_I(inode)->root;
6314 int ret;
6315
Josef Bacikb812ce22012-11-16 13:56:32 -05006316 if (async_submit)
6317 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6318
Miao Xiee65e1532010-11-22 03:04:43 +00006319 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006320
6321 if (!write) {
6322 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6323 if (ret)
6324 goto err;
6325 }
Miao Xiee65e1532010-11-22 03:04:43 +00006326
Josef Bacik1ae39932011-04-06 14:41:34 -04006327 if (skip_sum)
6328 goto map;
6329
6330 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006331 ret = btrfs_wq_submit_bio(root->fs_info,
6332 inode, rw, bio, 0, 0,
6333 file_offset,
6334 __btrfs_submit_bio_start_direct_io,
6335 __btrfs_submit_bio_done);
6336 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006337 } else if (write) {
6338 /*
6339 * If we aren't doing async submit, calculate the csum of the
6340 * bio now.
6341 */
6342 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6343 if (ret)
6344 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006345 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006346 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006347 if (ret)
6348 goto err;
6349 }
Miao Xiee65e1532010-11-22 03:04:43 +00006350
Josef Bacik1ae39932011-04-06 14:41:34 -04006351map:
6352 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006353err:
6354 bio_put(bio);
6355 return ret;
6356}
6357
6358static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6359 int skip_sum)
6360{
6361 struct inode *inode = dip->inode;
6362 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006363 struct bio *bio;
6364 struct bio *orig_bio = dip->orig_bio;
6365 struct bio_vec *bvec = orig_bio->bi_io_vec;
6366 u64 start_sector = orig_bio->bi_sector;
6367 u64 file_offset = dip->logical_offset;
6368 u64 submit_len = 0;
6369 u64 map_length;
6370 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006371 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006372 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006373
Miao Xiee65e1532010-11-22 03:04:43 +00006374 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006375 ret = btrfs_map_block(root->fs_info, READ, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006376 &map_length, NULL, 0);
6377 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006378 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006379 return -EIO;
6380 }
6381
Josef Bacik02f57c72011-04-06 14:25:44 -04006382 if (map_length >= orig_bio->bi_size) {
6383 bio = orig_bio;
6384 goto submit;
6385 }
6386
Josef Bacik1ae39932011-04-06 14:41:34 -04006387 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006388 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6389 if (!bio)
6390 return -ENOMEM;
6391 bio->bi_private = dip;
6392 bio->bi_end_io = btrfs_end_dio_bio;
6393 atomic_inc(&dip->pending_bios);
6394
Miao Xiee65e1532010-11-22 03:04:43 +00006395 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6396 if (unlikely(map_length < submit_len + bvec->bv_len ||
6397 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6398 bvec->bv_offset) < bvec->bv_len)) {
6399 /*
6400 * inc the count before we submit the bio so
6401 * we know the end IO handler won't happen before
6402 * we inc the count. Otherwise, the dip might get freed
6403 * before we're done setting it up
6404 */
6405 atomic_inc(&dip->pending_bios);
6406 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6407 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006408 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006409 if (ret) {
6410 bio_put(bio);
6411 atomic_dec(&dip->pending_bios);
6412 goto out_err;
6413 }
6414
Miao Xiee65e1532010-11-22 03:04:43 +00006415 start_sector += submit_len >> 9;
6416 file_offset += submit_len;
6417
6418 submit_len = 0;
6419 nr_pages = 0;
6420
6421 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6422 start_sector, GFP_NOFS);
6423 if (!bio)
6424 goto out_err;
6425 bio->bi_private = dip;
6426 bio->bi_end_io = btrfs_end_dio_bio;
6427
6428 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006429 ret = btrfs_map_block(root->fs_info, READ,
6430 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006431 &map_length, NULL, 0);
6432 if (ret) {
6433 bio_put(bio);
6434 goto out_err;
6435 }
6436 } else {
6437 submit_len += bvec->bv_len;
6438 nr_pages ++;
6439 bvec++;
6440 }
6441 }
6442
Josef Bacik02f57c72011-04-06 14:25:44 -04006443submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006444 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006445 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006446 if (!ret)
6447 return 0;
6448
6449 bio_put(bio);
6450out_err:
6451 dip->errors = 1;
6452 /*
6453 * before atomic variable goto zero, we must
6454 * make sure dip->errors is perceived to be set.
6455 */
6456 smp_mb__before_atomic_dec();
6457 if (atomic_dec_and_test(&dip->pending_bios))
6458 bio_io_error(dip->orig_bio);
6459
6460 /* bio_end_io() will handle error, so we needn't return it */
6461 return 0;
6462}
6463
Josef Bacik4b46fce2010-05-23 11:00:55 -04006464static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6465 loff_t file_offset)
6466{
6467 struct btrfs_root *root = BTRFS_I(inode)->root;
6468 struct btrfs_dio_private *dip;
6469 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006470 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006471 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006472 int ret = 0;
6473
6474 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6475
6476 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6477 if (!dip) {
6478 ret = -ENOMEM;
6479 goto free_ordered;
6480 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006481
6482 dip->private = bio->bi_private;
6483 dip->inode = inode;
6484 dip->logical_offset = file_offset;
6485
Josef Bacik4b46fce2010-05-23 11:00:55 -04006486 dip->bytes = 0;
6487 do {
6488 dip->bytes += bvec->bv_len;
6489 bvec++;
6490 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6491
Chris Mason46bfbb52010-05-26 11:04:10 -04006492 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006493 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006494 dip->errors = 0;
6495 dip->orig_bio = bio;
6496 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006497
6498 if (write)
6499 bio->bi_end_io = btrfs_endio_direct_write;
6500 else
6501 bio->bi_end_io = btrfs_endio_direct_read;
6502
Miao Xiee65e1532010-11-22 03:04:43 +00006503 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6504 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006505 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006506free_ordered:
6507 /*
6508 * If this is a write, we need to clean up the reserved space and kill
6509 * the ordered extent.
6510 */
6511 if (write) {
6512 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006513 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006514 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6515 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6516 btrfs_free_reserved_extent(root, ordered->start,
6517 ordered->disk_len);
6518 btrfs_put_ordered_extent(ordered);
6519 btrfs_put_ordered_extent(ordered);
6520 }
6521 bio_endio(bio, ret);
6522}
6523
Chris Mason5a5f79b2010-05-26 21:33:37 -04006524static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6525 const struct iovec *iov, loff_t offset,
6526 unsigned long nr_segs)
6527{
6528 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006529 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006530 size_t size;
6531 unsigned long addr;
6532 unsigned blocksize_mask = root->sectorsize - 1;
6533 ssize_t retval = -EINVAL;
6534 loff_t end = offset;
6535
6536 if (offset & blocksize_mask)
6537 goto out;
6538
6539 /* Check the memory alignment. Blocks cannot straddle pages */
6540 for (seg = 0; seg < nr_segs; seg++) {
6541 addr = (unsigned long)iov[seg].iov_base;
6542 size = iov[seg].iov_len;
6543 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006544 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006545 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006546
6547 /* If this is a write we don't need to check anymore */
6548 if (rw & WRITE)
6549 continue;
6550
6551 /*
6552 * Check to make sure we don't have duplicate iov_base's in this
6553 * iovec, if so return EINVAL, otherwise we'll get csum errors
6554 * when reading back.
6555 */
6556 for (i = seg + 1; i < nr_segs; i++) {
6557 if (iov[seg].iov_base == iov[i].iov_base)
6558 goto out;
6559 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006560 }
6561 retval = 0;
6562out:
6563 return retval;
6564}
Josef Bacikeb838e72012-07-31 16:28:48 -04006565
Chris Mason16432982008-04-10 10:23:21 -04006566static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6567 const struct iovec *iov, loff_t offset,
6568 unsigned long nr_segs)
6569{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006570 struct file *file = iocb->ki_filp;
6571 struct inode *inode = file->f_mapping->host;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006572
Chris Mason5a5f79b2010-05-26 21:33:37 -04006573 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006574 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006575 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006576
Josef Bacikeb838e72012-07-31 16:28:48 -04006577 return __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006578 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6579 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6580 btrfs_submit_direct, 0);
Chris Mason16432982008-04-10 10:23:21 -04006581}
6582
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006583#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6584
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006585static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6586 __u64 start, __u64 len)
6587{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006588 int ret;
6589
6590 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6591 if (ret)
6592 return ret;
6593
Chris Masonec29ed52011-02-23 16:23:20 -05006594 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006595}
6596
Chris Mason9ebefb182007-06-15 13:50:00 -04006597int btrfs_readpage(struct file *file, struct page *page)
6598{
Chris Masond1310b22008-01-24 16:13:08 -05006599 struct extent_io_tree *tree;
6600 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006601 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006602}
Chris Mason1832a6d2007-12-21 16:27:21 -05006603
Chris Mason39279cc2007-06-12 06:35:45 -04006604static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6605{
Chris Masond1310b22008-01-24 16:13:08 -05006606 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006607
6608
6609 if (current->flags & PF_MEMALLOC) {
6610 redirty_page_for_writepage(wbc, page);
6611 unlock_page(page);
6612 return 0;
6613 }
Chris Masond1310b22008-01-24 16:13:08 -05006614 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006615 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6616}
Chris Mason39279cc2007-06-12 06:35:45 -04006617
Chris Masonf4219502008-07-22 11:18:09 -04006618int btrfs_writepages(struct address_space *mapping,
6619 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006620{
Chris Masond1310b22008-01-24 16:13:08 -05006621 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006622
Chris Masond1310b22008-01-24 16:13:08 -05006623 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006624 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6625}
6626
Chris Mason3ab2fb52007-11-08 10:59:22 -05006627static int
6628btrfs_readpages(struct file *file, struct address_space *mapping,
6629 struct list_head *pages, unsigned nr_pages)
6630{
Chris Masond1310b22008-01-24 16:13:08 -05006631 struct extent_io_tree *tree;
6632 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006633 return extent_readpages(tree, mapping, pages, nr_pages,
6634 btrfs_get_extent);
6635}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006636static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006637{
Chris Masond1310b22008-01-24 16:13:08 -05006638 struct extent_io_tree *tree;
6639 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006640 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006641
Chris Masond1310b22008-01-24 16:13:08 -05006642 tree = &BTRFS_I(page->mapping->host)->io_tree;
6643 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006644 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006645 if (ret == 1) {
6646 ClearPagePrivate(page);
6647 set_page_private(page, 0);
6648 page_cache_release(page);
6649 }
6650 return ret;
6651}
Chris Mason39279cc2007-06-12 06:35:45 -04006652
Chris Masone6dcd2d2008-07-17 12:53:50 -04006653static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6654{
Chris Mason98509cf2008-09-11 15:51:43 -04006655 if (PageWriteback(page) || PageDirty(page))
6656 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006657 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006658}
6659
Chris Masona52d9a82007-08-27 16:49:44 -04006660static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6661{
Josef Bacik5fd02042012-05-02 14:00:54 -04006662 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006663 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006664 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006665 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006666 u64 page_start = page_offset(page);
6667 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006668
Chris Mason8b62b722009-09-02 16:53:46 -04006669 /*
6670 * we have the page locked, so new writeback can't start,
6671 * and the dirty bit won't be cleared while we are here.
6672 *
6673 * Wait for IO on this page so that we can safely clear
6674 * the PagePrivate2 bit and do ordered accounting
6675 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006676 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006677
Josef Bacik5fd02042012-05-02 14:00:54 -04006678 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006679 if (offset) {
6680 btrfs_releasepage(page, GFP_NOFS);
6681 return;
6682 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006683 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Josef Bacik5fd02042012-05-02 14:00:54 -04006684 ordered = btrfs_lookup_ordered_extent(inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006685 page_offset(page));
6686 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006687 /*
6688 * IO on this page will never be started, so we need
6689 * to account for any ordered extents now
6690 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006691 clear_extent_bit(tree, page_start, page_end,
6692 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006693 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6694 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006695 /*
6696 * whoever cleared the private bit is responsible
6697 * for the finish_ordered_io
6698 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006699 if (TestClearPagePrivate2(page) &&
6700 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6701 PAGE_CACHE_SIZE, 1)) {
6702 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006703 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006704 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006705 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006706 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006707 }
6708 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006709 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006710 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6711 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006712 __btrfs_releasepage(page, GFP_NOFS);
6713
Chris Mason4a096752008-07-21 10:29:44 -04006714 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006715 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006716 ClearPagePrivate(page);
6717 set_page_private(page, 0);
6718 page_cache_release(page);
6719 }
Chris Mason39279cc2007-06-12 06:35:45 -04006720}
6721
Chris Mason9ebefb182007-06-15 13:50:00 -04006722/*
6723 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6724 * called from a page fault handler when a page is first dirtied. Hence we must
6725 * be careful to check for EOF conditions here. We set the page up correctly
6726 * for a written page which means we get ENOSPC checking when writing into
6727 * holes and correct delalloc and unwritten extent mapping on filesystems that
6728 * support these features.
6729 *
6730 * We are not allowed to take the i_mutex here so we have to play games to
6731 * protect against truncate races as the page could now be beyond EOF. Because
6732 * vmtruncate() writes the inode size before removing pages, once we have the
6733 * page lock we can determine safely if the page is beyond EOF. If it is not
6734 * beyond EOF, then the page is guaranteed safe against truncation until we
6735 * unlock the page.
6736 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006737int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006738{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006739 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05006740 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05006741 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006742 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6743 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006744 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006745 char *kaddr;
6746 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006747 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006748 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006749 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006750 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006751 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006752
Jan Karab2b5ef52012-06-12 16:20:45 +02006753 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006754 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006755 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006756 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006757 reserved = 1;
6758 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006759 if (ret) {
6760 if (ret == -ENOMEM)
6761 ret = VM_FAULT_OOM;
6762 else /* -ENOSPC, -EIO, etc */
6763 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006764 if (reserved)
6765 goto out;
6766 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006767 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006768
Nick Piggin56a76f82009-03-31 15:23:23 -07006769 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006770again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006771 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006772 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006773 page_start = page_offset(page);
6774 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006775
Chris Mason9ebefb182007-06-15 13:50:00 -04006776 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006777 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006778 /* page got truncated out from underneath us */
6779 goto out_unlock;
6780 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006781 wait_on_page_writeback(page);
6782
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006783 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006784 set_page_extent_mapped(page);
6785
Chris Masoneb84ae02008-07-17 13:53:27 -04006786 /*
6787 * we can't set the delalloc bits if there are pending ordered
6788 * extents. Drop our locks and wait for them to finish
6789 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006790 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6791 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006792 unlock_extent_cached(io_tree, page_start, page_end,
6793 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006794 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006795 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006796 btrfs_put_ordered_extent(ordered);
6797 goto again;
6798 }
6799
Josef Bacikfbf19082009-10-01 17:10:23 -04006800 /*
6801 * XXX - page_mkwrite gets called every time the page is dirtied, even
6802 * if it was already dirty, so for space accounting reasons we need to
6803 * clear any delalloc bits for the range we are fixing to save. There
6804 * is probably a better way to do this, but for now keep consistent with
6805 * prepare_pages in the normal write path.
6806 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006807 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006808 EXTENT_DIRTY | EXTENT_DELALLOC |
6809 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006810 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006811
Josef Bacik2ac55d42010-02-03 19:33:23 +00006812 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6813 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006814 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006815 unlock_extent_cached(io_tree, page_start, page_end,
6816 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006817 ret = VM_FAULT_SIGBUS;
6818 goto out_unlock;
6819 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006820 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006821
6822 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006823 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006824 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006825 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006826 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006827
Chris Masone6dcd2d2008-07-17 12:53:50 -04006828 if (zero_start != PAGE_CACHE_SIZE) {
6829 kaddr = kmap(page);
6830 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6831 flush_dcache_page(page);
6832 kunmap(page);
6833 }
Chris Mason247e7432008-07-17 12:53:51 -04006834 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006835 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006836 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006837
Chris Mason257c62e2009-10-13 13:21:08 -04006838 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6839 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006840 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006841
Josef Bacik2ac55d42010-02-03 19:33:23 +00006842 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006843
6844out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006845 if (!ret) {
6846 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006847 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006848 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006849 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006850out:
Josef Bacikec39e182012-01-12 19:10:12 -05006851 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006852out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006853 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006854 return ret;
6855}
6856
Josef Bacika41ad392011-01-31 15:30:16 -05006857static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006858{
6859 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006860 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006861 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006862 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006863 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04006864 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006865 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006866
Josef Bacik2aaa6652012-08-29 14:27:18 -04006867 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04006868 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006869 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006870
Chris Mason4a096752008-07-21 10:29:44 -04006871 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006872 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006873
Josef Bacikfcb80c22011-05-03 10:40:22 -04006874 /*
6875 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6876 * 3 things going on here
6877 *
6878 * 1) We need to reserve space for our orphan item and the space to
6879 * delete our orphan item. Lord knows we don't want to have a dangling
6880 * orphan item because we didn't reserve space to remove it.
6881 *
6882 * 2) We need to reserve space to update our inode.
6883 *
6884 * 3) We need to have something to cache all the space that is going to
6885 * be free'd up by the truncate operation, but also have some slack
6886 * space reserved in case it uses space during the truncate (thank you
6887 * very much snapshotting).
6888 *
6889 * And we need these to all be seperate. The fact is we can use alot of
6890 * space doing the truncate, and we have no earthly idea how much space
6891 * we will use, so we need the truncate reservation to be seperate so it
6892 * doesn't end up using space reserved for updating the inode or
6893 * removing the orphan item. We also need to be able to stop the
6894 * transaction and start a new one, which means we need to be able to
6895 * update the inode several times, and we have no idea of knowing how
6896 * many times that will be, so we can't just reserve 1 item for the
6897 * entirety of the opration, so that has to be done seperately as well.
6898 * Then there is the orphan item, which does indeed need to be held on
6899 * to for the whole operation, and we need nobody to touch this reserved
6900 * space except the orphan code.
6901 *
6902 * So that leaves us with
6903 *
6904 * 1) root->orphan_block_rsv - for the orphan deletion.
6905 * 2) rsv - for the truncate reservation, which we will steal from the
6906 * transaction reservation.
6907 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6908 * updating the inode.
6909 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06006910 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006911 if (!rsv)
6912 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006913 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006914 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006915
Josef Bacik907cbce2011-08-08 13:46:15 -04006916 /*
Josef Bacik07127182011-08-19 10:29:59 -04006917 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006918 * 1 for the orphan item we're going to add
6919 * 1 for the orphan item deletion
6920 * 1 for updating the inode.
6921 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04006922 trans = btrfs_start_transaction(root, 4);
6923 if (IS_ERR(trans)) {
6924 err = PTR_ERR(trans);
6925 goto out;
6926 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006927
Josef Bacik907cbce2011-08-08 13:46:15 -04006928 /* Migrate the slack space for the truncate to our reserve */
6929 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6930 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006931 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006932
6933 ret = btrfs_orphan_add(trans, inode);
6934 if (ret) {
6935 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006936 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006937 }
6938
Chris Mason5a3f23d2009-03-31 13:27:11 -04006939 /*
6940 * setattr is responsible for setting the ordered_data_close flag,
6941 * but that is only tested during the last file release. That
6942 * could happen well after the next commit, leaving a great big
6943 * window where new writes may get lost if someone chooses to write
6944 * to this file after truncating to zero
6945 *
6946 * The inode doesn't have any dirty data here, and so if we commit
6947 * this is a noop. If someone immediately starts writing to the inode
6948 * it is very likely we'll catch some of their writes in this
6949 * transaction, and the commit will find this file on the ordered
6950 * data list with good things to send down.
6951 *
6952 * This is a best effort solution, there is still a window where
6953 * using truncate to replace the contents of the file will
6954 * end up with a zero length file after a crash.
6955 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04006956 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
6957 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04006958 btrfs_add_ordered_operation(trans, root, inode);
6959
Josef Bacik5dc562c2012-08-17 13:14:17 -04006960 /*
6961 * So if we truncate and then write and fsync we normally would just
6962 * write the extents that changed, which is a problem if we need to
6963 * first truncate that entire inode. So set this flag so we write out
6964 * all of the extents in the inode to the sync log so we're completely
6965 * safe.
6966 */
6967 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006968 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04006969
Yan, Zheng80825102009-11-12 09:35:36 +00006970 while (1) {
6971 ret = btrfs_truncate_inode_items(trans, root, inode,
6972 inode->i_size,
6973 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006974 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05006975 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006976 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006977 }
Chris Mason39279cc2007-06-12 06:35:45 -04006978
Josef Bacikfcb80c22011-05-03 10:40:22 -04006979 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006980 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006981 if (ret) {
6982 err = ret;
6983 break;
6984 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04006985
Yan, Zheng80825102009-11-12 09:35:36 +00006986 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00006987 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006988
6989 trans = btrfs_start_transaction(root, 2);
6990 if (IS_ERR(trans)) {
6991 ret = err = PTR_ERR(trans);
6992 trans = NULL;
6993 break;
6994 }
6995
6996 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
6997 rsv, min_size);
6998 BUG_ON(ret); /* shouldn't happen */
6999 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007000 }
7001
7002 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007003 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007004 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007005 if (ret)
7006 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05007007 } else if (ret && inode->i_nlink > 0) {
7008 /*
7009 * Failed to do the truncate, remove us from the in memory
7010 * orphan list.
7011 */
7012 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00007013 }
7014
Chris Mason917c16b2011-11-08 14:49:59 -05007015 if (trans) {
7016 trans->block_rsv = &root->fs_info->trans_block_rsv;
7017 ret = btrfs_update_inode(trans, root, inode);
7018 if (ret && !err)
7019 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007020
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007021 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007022 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007023 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007024
7025out:
7026 btrfs_free_block_rsv(root, rsv);
7027
Josef Bacik3893e332011-01-31 16:03:11 -05007028 if (ret && !err)
7029 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007030
Josef Bacik3893e332011-01-31 16:03:11 -05007031 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007032}
7033
Sven Wegener3b963622008-06-09 21:57:42 -04007034/*
Chris Masond352ac62008-09-29 15:18:18 -04007035 * create a new subvolume directory/inode (helper for the ioctl).
7036 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007037int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007038 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007039{
Chris Mason39279cc2007-06-12 06:35:45 -04007040 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007041 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007042 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007043
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007044 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7045 new_dirid, new_dirid,
7046 S_IFDIR | (~current_umask() & S_IRWXUGO),
7047 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007048 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007049 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007050 inode->i_op = &btrfs_dir_inode_operations;
7051 inode->i_fop = &btrfs_dir_file_operations;
7052
Miklos Szeredibfe86842011-10-28 14:13:29 +02007053 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007054 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007055
Yan, Zheng76dda932009-09-21 16:00:26 -04007056 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007057
Yan, Zheng76dda932009-09-21 16:00:26 -04007058 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007059 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007060}
7061
Chris Mason39279cc2007-06-12 06:35:45 -04007062struct inode *btrfs_alloc_inode(struct super_block *sb)
7063{
7064 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007065 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007066
7067 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7068 if (!ei)
7069 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007070
7071 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007072 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007073 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007074 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007075 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007076 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007077 ei->disk_i_size = 0;
7078 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007079 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007080 ei->index_cnt = (u64)-1;
7081 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007082 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007083
Josef Bacik9e0baf62011-07-15 15:16:44 +00007084 spin_lock_init(&ei->lock);
7085 ei->outstanding_extents = 0;
7086 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007087
Josef Bacik72ac3c02012-05-23 14:13:11 -04007088 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007089 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007090
Miao Xie16cdcec2011-04-22 18:12:22 +08007091 ei->delayed_node = NULL;
7092
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007093 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007094 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007095 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7096 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007097 ei->io_tree.track_uptodate = 1;
7098 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007099 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007100 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007101 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007102 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007103 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007104 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007105 RB_CLEAR_NODE(&ei->rb_node);
7106
7107 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007108}
7109
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007110static void btrfs_i_callback(struct rcu_head *head)
7111{
7112 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007113 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7114}
7115
Chris Mason39279cc2007-06-12 06:35:45 -04007116void btrfs_destroy_inode(struct inode *inode)
7117{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007118 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007119 struct btrfs_root *root = BTRFS_I(inode)->root;
7120
Al Virob3d9b7a2012-06-09 13:51:19 -04007121 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007122 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007123 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7124 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007125 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7126 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007127
Chris Mason5a3f23d2009-03-31 13:27:11 -04007128 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007129 * This can happen where we create an inode, but somebody else also
7130 * created the same inode and we need to destroy the one we already
7131 * created.
7132 */
7133 if (!root)
7134 goto free;
7135
7136 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007137 * Make sure we're properly removed from the ordered operation
7138 * lists.
7139 */
7140 smp_mb();
7141 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7142 spin_lock(&root->fs_info->ordered_extent_lock);
7143 list_del_init(&BTRFS_I(inode)->ordered_operations);
7144 spin_unlock(&root->fs_info->ordered_extent_lock);
7145 }
7146
Josef Bacik8a35d952012-05-23 14:26:42 -04007147 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7148 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007149 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7150 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007151 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007152 }
Josef Bacik7b128762008-07-24 12:17:14 -04007153
Chris Masond3977122009-01-05 21:25:51 -05007154 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007155 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7156 if (!ordered)
7157 break;
7158 else {
Chris Masond3977122009-01-05 21:25:51 -05007159 printk(KERN_ERR "btrfs found ordered "
7160 "extent %llu %llu on inode cleanup\n",
7161 (unsigned long long)ordered->file_offset,
7162 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007163 btrfs_remove_ordered_extent(inode, ordered);
7164 btrfs_put_ordered_extent(ordered);
7165 btrfs_put_ordered_extent(ordered);
7166 }
7167 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007168 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007169 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007170free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007171 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007172 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007173}
7174
Al Viro45321ac2010-06-07 13:43:19 -04007175int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007176{
7177 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007178
Josef Bacik0af3d002010-06-21 14:48:16 -04007179 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007180 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007181 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007182 else
Al Viro45321ac2010-06-07 13:43:19 -04007183 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007184}
7185
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007186static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007187{
7188 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7189
7190 inode_init_once(&ei->vfs_inode);
7191}
7192
7193void btrfs_destroy_cachep(void)
7194{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007195 /*
7196 * Make sure all delayed rcu free inodes are flushed before we
7197 * destroy cache.
7198 */
7199 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007200 if (btrfs_inode_cachep)
7201 kmem_cache_destroy(btrfs_inode_cachep);
7202 if (btrfs_trans_handle_cachep)
7203 kmem_cache_destroy(btrfs_trans_handle_cachep);
7204 if (btrfs_transaction_cachep)
7205 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007206 if (btrfs_path_cachep)
7207 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007208 if (btrfs_free_space_cachep)
7209 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007210 if (btrfs_delalloc_work_cachep)
7211 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007212}
7213
7214int btrfs_init_cachep(void)
7215{
David Sterba837e1972012-09-07 03:00:48 -06007216 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007217 sizeof(struct btrfs_inode), 0,
7218 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007219 if (!btrfs_inode_cachep)
7220 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007221
David Sterba837e1972012-09-07 03:00:48 -06007222 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007223 sizeof(struct btrfs_trans_handle), 0,
7224 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007225 if (!btrfs_trans_handle_cachep)
7226 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007227
David Sterba837e1972012-09-07 03:00:48 -06007228 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007229 sizeof(struct btrfs_transaction), 0,
7230 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007231 if (!btrfs_transaction_cachep)
7232 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007233
David Sterba837e1972012-09-07 03:00:48 -06007234 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007235 sizeof(struct btrfs_path), 0,
7236 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007237 if (!btrfs_path_cachep)
7238 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007239
David Sterba837e1972012-09-07 03:00:48 -06007240 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007241 sizeof(struct btrfs_free_space), 0,
7242 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7243 if (!btrfs_free_space_cachep)
7244 goto fail;
7245
Miao Xie8ccf6f192012-10-25 09:28:04 +00007246 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7247 sizeof(struct btrfs_delalloc_work), 0,
7248 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7249 NULL);
7250 if (!btrfs_delalloc_work_cachep)
7251 goto fail;
7252
Chris Mason39279cc2007-06-12 06:35:45 -04007253 return 0;
7254fail:
7255 btrfs_destroy_cachep();
7256 return -ENOMEM;
7257}
7258
7259static int btrfs_getattr(struct vfsmount *mnt,
7260 struct dentry *dentry, struct kstat *stat)
7261{
7262 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007263 u32 blocksize = inode->i_sb->s_blocksize;
7264
Chris Mason39279cc2007-06-12 06:35:45 -04007265 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007266 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007267 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007268 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7269 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007270 return 0;
7271}
7272
Liu Bo75e7cb72011-03-22 10:12:20 +00007273/*
7274 * If a file is moved, it will inherit the cow and compression flags of the new
7275 * directory.
7276 */
7277static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7278{
7279 struct btrfs_inode *b_dir = BTRFS_I(dir);
7280 struct btrfs_inode *b_inode = BTRFS_I(inode);
7281
7282 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7283 b_inode->flags |= BTRFS_INODE_NODATACOW;
7284 else
7285 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7286
Liu Bobc178232012-06-14 02:23:18 -06007287 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007288 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007289 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7290 } else {
7291 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7292 BTRFS_INODE_NOCOMPRESS);
7293 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007294}
7295
Chris Masond3977122009-01-05 21:25:51 -05007296static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7297 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007298{
7299 struct btrfs_trans_handle *trans;
7300 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007301 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007302 struct inode *new_inode = new_dentry->d_inode;
7303 struct inode *old_inode = old_dentry->d_inode;
7304 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007305 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007306 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007307 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007308 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007309
Li Zefan33345d012011-04-20 10:31:50 +08007310 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007311 return -EPERM;
7312
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007313 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007314 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007315 return -EXDEV;
7316
Li Zefan33345d012011-04-20 10:31:50 +08007317 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7318 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007319 return -ENOTEMPTY;
7320
Chris Mason39279cc2007-06-12 06:35:45 -04007321 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007322 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007323 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007324
7325
7326 /* check for collisions, even if the name isn't there */
7327 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7328 new_dentry->d_name.name,
7329 new_dentry->d_name.len);
7330
7331 if (ret) {
7332 if (ret == -EEXIST) {
7333 /* we shouldn't get
7334 * eexist without a new_inode */
7335 if (!new_inode) {
7336 WARN_ON(1);
7337 return ret;
7338 }
7339 } else {
7340 /* maybe -EOVERFLOW */
7341 return ret;
7342 }
7343 }
7344 ret = 0;
7345
Chris Mason5a3f23d2009-03-31 13:27:11 -04007346 /*
7347 * we're using rename to replace one file with another.
7348 * and the replacement file is large. Start IO on it now so
7349 * we don't add too much work to the end of the transaction
7350 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007351 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007352 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7353 filemap_flush(old_inode->i_mapping);
7354
Yan, Zheng76dda932009-09-21 16:00:26 -04007355 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007356 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007357 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007358 /*
7359 * We want to reserve the absolute worst case amount of items. So if
7360 * both inodes are subvols and we need to unlink them then that would
7361 * require 4 item modifications, but if they are both normal inodes it
7362 * would require 5 item modifications, so we'll assume their normal
7363 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7364 * should cover the worst case number of items we'll modify.
7365 */
7366 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007367 if (IS_ERR(trans)) {
7368 ret = PTR_ERR(trans);
7369 goto out_notrans;
7370 }
Chris Mason5f39d392007-10-15 16:14:19 -04007371
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007372 if (dest != root)
7373 btrfs_record_root_in_trans(trans, dest);
7374
Yan, Zhenga5719522009-09-24 09:17:31 -04007375 ret = btrfs_set_inode_index(new_dir, &index);
7376 if (ret)
7377 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007378
Li Zefan33345d012011-04-20 10:31:50 +08007379 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007380 /* force full log commit if subvolume involved. */
7381 root->fs_info->last_trans_log_full_commit = trans->transid;
7382 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007383 ret = btrfs_insert_inode_ref(trans, dest,
7384 new_dentry->d_name.name,
7385 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007386 old_ino,
7387 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007388 if (ret)
7389 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007390 /*
7391 * this is an ugly little race, but the rename is required
7392 * to make sure that if we crash, the inode is either at the
7393 * old name or the new one. pinning the log transaction lets
7394 * us make sure we don't allow a log commit to come in after
7395 * we unlink the name but before we add the new name back in.
7396 */
7397 btrfs_pin_log_trans(root);
7398 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007399 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007400 * make sure the inode gets flushed if it is replacing
7401 * something.
7402 */
Li Zefan33345d012011-04-20 10:31:50 +08007403 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007404 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007405
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007406 inode_inc_iversion(old_dir);
7407 inode_inc_iversion(new_dir);
7408 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007409 old_dir->i_ctime = old_dir->i_mtime = ctime;
7410 new_dir->i_ctime = new_dir->i_mtime = ctime;
7411 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007412
Chris Mason12fcfd22009-03-24 10:24:20 -04007413 if (old_dentry->d_parent != new_dentry->d_parent)
7414 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7415
Li Zefan33345d012011-04-20 10:31:50 +08007416 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007417 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7418 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7419 old_dentry->d_name.name,
7420 old_dentry->d_name.len);
7421 } else {
Al Viro92986792011-03-04 17:14:37 +00007422 ret = __btrfs_unlink_inode(trans, root, old_dir,
7423 old_dentry->d_inode,
7424 old_dentry->d_name.name,
7425 old_dentry->d_name.len);
7426 if (!ret)
7427 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007428 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007429 if (ret) {
7430 btrfs_abort_transaction(trans, root, ret);
7431 goto out_fail;
7432 }
Chris Mason39279cc2007-06-12 06:35:45 -04007433
7434 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007435 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007436 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007437 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007438 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7439 root_objectid = BTRFS_I(new_inode)->location.objectid;
7440 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7441 root_objectid,
7442 new_dentry->d_name.name,
7443 new_dentry->d_name.len);
7444 BUG_ON(new_inode->i_nlink == 0);
7445 } else {
7446 ret = btrfs_unlink_inode(trans, dest, new_dir,
7447 new_dentry->d_inode,
7448 new_dentry->d_name.name,
7449 new_dentry->d_name.len);
7450 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007451 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007452 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007453 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007454 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007455 if (ret) {
7456 btrfs_abort_transaction(trans, root, ret);
7457 goto out_fail;
7458 }
Chris Mason39279cc2007-06-12 06:35:45 -04007459 }
Josef Bacikaec74772008-07-24 12:12:38 -04007460
Liu Bo75e7cb72011-03-22 10:12:20 +00007461 fixup_inode_flags(new_dir, old_inode);
7462
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007463 ret = btrfs_add_link(trans, new_dir, old_inode,
7464 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007465 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007466 if (ret) {
7467 btrfs_abort_transaction(trans, root, ret);
7468 goto out_fail;
7469 }
Chris Mason39279cc2007-06-12 06:35:45 -04007470
Li Zefan33345d012011-04-20 10:31:50 +08007471 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007472 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007473 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007474 btrfs_end_log_trans(root);
7475 }
Chris Mason39279cc2007-06-12 06:35:45 -04007476out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007477 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007478out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007479 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007480 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007481
Chris Mason39279cc2007-06-12 06:35:45 -04007482 return ret;
7483}
7484
Miao Xie8ccf6f192012-10-25 09:28:04 +00007485static void btrfs_run_delalloc_work(struct btrfs_work *work)
7486{
7487 struct btrfs_delalloc_work *delalloc_work;
7488
7489 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7490 work);
7491 if (delalloc_work->wait)
7492 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7493 else
7494 filemap_flush(delalloc_work->inode->i_mapping);
7495
7496 if (delalloc_work->delay_iput)
7497 btrfs_add_delayed_iput(delalloc_work->inode);
7498 else
7499 iput(delalloc_work->inode);
7500 complete(&delalloc_work->completion);
7501}
7502
7503struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7504 int wait, int delay_iput)
7505{
7506 struct btrfs_delalloc_work *work;
7507
7508 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7509 if (!work)
7510 return NULL;
7511
7512 init_completion(&work->completion);
7513 INIT_LIST_HEAD(&work->list);
7514 work->inode = inode;
7515 work->wait = wait;
7516 work->delay_iput = delay_iput;
7517 work->work.func = btrfs_run_delalloc_work;
7518
7519 return work;
7520}
7521
7522void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7523{
7524 wait_for_completion(&work->completion);
7525 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7526}
7527
Chris Masond352ac62008-09-29 15:18:18 -04007528/*
7529 * some fairly slow code that needs optimization. This walks the list
7530 * of all the inodes with pending delalloc and forces them to disk.
7531 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007532int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007533{
7534 struct list_head *head = &root->fs_info->delalloc_inodes;
7535 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007536 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007537 struct btrfs_delalloc_work *work, *next;
7538 struct list_head works;
7539 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04007540
Yan Zhengc146afa2008-11-12 14:34:12 -05007541 if (root->fs_info->sb->s_flags & MS_RDONLY)
7542 return -EROFS;
7543
Miao Xie8ccf6f192012-10-25 09:28:04 +00007544 INIT_LIST_HEAD(&works);
7545
Chris Mason75eff682008-12-15 15:54:40 -05007546 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007547 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007548 binode = list_entry(head->next, struct btrfs_inode,
7549 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007550 inode = igrab(&binode->vfs_inode);
7551 if (!inode)
7552 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007553 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007554 if (inode) {
Miao Xie8ccf6f192012-10-25 09:28:04 +00007555 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7556 if (!work) {
7557 ret = -ENOMEM;
7558 goto out;
7559 }
7560 list_add_tail(&work->list, &works);
7561 btrfs_queue_worker(&root->fs_info->flush_workers,
7562 &work->work);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007563 }
7564 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007565 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007566 }
Chris Mason75eff682008-12-15 15:54:40 -05007567 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007568
7569 /* the filemap_flush will queue IO into the worker threads, but
7570 * we have to make sure the IO is actually started and that
7571 * ordered extents get created before we return
7572 */
7573 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007574 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007575 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007576 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007577 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7578 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007579 }
7580 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007581out:
7582 list_for_each_entry_safe(work, next, &works, list) {
7583 list_del_init(&work->list);
7584 btrfs_wait_and_free_delalloc_work(work);
7585 }
7586 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04007587}
7588
Chris Mason39279cc2007-06-12 06:35:45 -04007589static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7590 const char *symname)
7591{
7592 struct btrfs_trans_handle *trans;
7593 struct btrfs_root *root = BTRFS_I(dir)->root;
7594 struct btrfs_path *path;
7595 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007596 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007597 int err;
7598 int drop_inode = 0;
7599 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007600 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007601 int name_len;
7602 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007603 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007604 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007605 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04007606
7607 name_len = strlen(symname) + 1;
7608 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7609 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007610
Josef Bacik9ed74f22009-09-11 16:12:44 -04007611 /*
7612 * 2 items for inode item and ref
7613 * 2 items for dir items
7614 * 1 item for xattr if selinux is on
7615 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007616 trans = btrfs_start_transaction(root, 5);
7617 if (IS_ERR(trans))
7618 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007619
Li Zefan581bb052011-04-20 10:06:11 +08007620 err = btrfs_find_free_ino(root, &objectid);
7621 if (err)
7622 goto out_unlock;
7623
Josef Bacikaec74772008-07-24 12:12:38 -04007624 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007625 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007626 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007627 if (IS_ERR(inode)) {
7628 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007629 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007630 }
Chris Mason39279cc2007-06-12 06:35:45 -04007631
Eric Paris2a7dba32011-02-01 11:05:39 -05007632 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007633 if (err) {
7634 drop_inode = 1;
7635 goto out_unlock;
7636 }
7637
Casey Schauflerad19db72011-12-15 10:09:07 -05007638 /*
7639 * If the active LSM wants to access the inode during
7640 * d_instantiate it needs these. Smack checks to see
7641 * if the filesystem supports xattrs by looking at the
7642 * ops vector.
7643 */
7644 inode->i_fop = &btrfs_file_operations;
7645 inode->i_op = &btrfs_file_inode_operations;
7646
Josef Bacika1b075d2010-11-19 20:36:11 +00007647 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007648 if (err)
7649 drop_inode = 1;
7650 else {
7651 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007652 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007653 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007654 }
Chris Mason39279cc2007-06-12 06:35:45 -04007655 if (drop_inode)
7656 goto out_unlock;
7657
7658 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007659 if (!path) {
7660 err = -ENOMEM;
7661 drop_inode = 1;
7662 goto out_unlock;
7663 }
Li Zefan33345d012011-04-20 10:31:50 +08007664 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007665 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007666 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7667 datasize = btrfs_file_extent_calc_inline_size(name_len);
7668 err = btrfs_insert_empty_item(trans, root, path, &key,
7669 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007670 if (err) {
7671 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007672 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007673 goto out_unlock;
7674 }
Chris Mason5f39d392007-10-15 16:14:19 -04007675 leaf = path->nodes[0];
7676 ei = btrfs_item_ptr(leaf, path->slots[0],
7677 struct btrfs_file_extent_item);
7678 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7679 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007680 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007681 btrfs_set_file_extent_encryption(leaf, ei, 0);
7682 btrfs_set_file_extent_compression(leaf, ei, 0);
7683 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7684 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7685
Chris Mason39279cc2007-06-12 06:35:45 -04007686 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007687 write_extent_buffer(leaf, symname, ptr, name_len);
7688 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007689 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007690
Chris Mason39279cc2007-06-12 06:35:45 -04007691 inode->i_op = &btrfs_symlink_inode_operations;
7692 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007693 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007694 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007695 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007696 err = btrfs_update_inode(trans, root, inode);
7697 if (err)
7698 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007699
7700out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007701 if (!err)
7702 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007703 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007704 if (drop_inode) {
7705 inode_dec_link_count(inode);
7706 iput(inode);
7707 }
Liu Bob53d3f52012-11-14 14:34:34 +00007708 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04007709 return err;
7710}
Chris Mason16432982008-04-10 10:23:21 -04007711
Josef Bacik0af3d002010-06-21 14:48:16 -04007712static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7713 u64 start, u64 num_bytes, u64 min_size,
7714 loff_t actual_len, u64 *alloc_hint,
7715 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007716{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007717 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7718 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007719 struct btrfs_root *root = BTRFS_I(inode)->root;
7720 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007721 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007722 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007723 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007724 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007725
Josef Bacik0af3d002010-06-21 14:48:16 -04007726 if (trans)
7727 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007728 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007729 if (own_trans) {
7730 trans = btrfs_start_transaction(root, 3);
7731 if (IS_ERR(trans)) {
7732 ret = PTR_ERR(trans);
7733 break;
7734 }
Yan Zhengd899e052008-10-30 14:25:28 -04007735 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007736
Yan, Zhengefa56462010-05-16 10:49:59 -04007737 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007738 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007739 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007740 if (own_trans)
7741 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007742 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007743 }
7744
Yan Zhengd899e052008-10-30 14:25:28 -04007745 ret = insert_reserved_file_extent(trans, inode,
7746 cur_offset, ins.objectid,
7747 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007748 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007749 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007750 if (ret) {
7751 btrfs_abort_transaction(trans, root, ret);
7752 if (own_trans)
7753 btrfs_end_transaction(trans, root);
7754 break;
7755 }
Chris Masona1ed8352009-09-11 12:27:37 -04007756 btrfs_drop_extent_cache(inode, cur_offset,
7757 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007758
Josef Bacik5dc562c2012-08-17 13:14:17 -04007759 em = alloc_extent_map();
7760 if (!em) {
7761 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7762 &BTRFS_I(inode)->runtime_flags);
7763 goto next;
7764 }
7765
7766 em->start = cur_offset;
7767 em->orig_start = cur_offset;
7768 em->len = ins.offset;
7769 em->block_start = ins.objectid;
7770 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05007771 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007772 em->bdev = root->fs_info->fs_devices->latest_bdev;
7773 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7774 em->generation = trans->transid;
7775
7776 while (1) {
7777 write_lock(&em_tree->lock);
7778 ret = add_extent_mapping(em_tree, em);
7779 if (!ret)
7780 list_move(&em->list,
7781 &em_tree->modified_extents);
7782 write_unlock(&em_tree->lock);
7783 if (ret != -EEXIST)
7784 break;
7785 btrfs_drop_extent_cache(inode, cur_offset,
7786 cur_offset + ins.offset - 1,
7787 0);
7788 }
7789 free_extent_map(em);
7790next:
Yan Zhengd899e052008-10-30 14:25:28 -04007791 num_bytes -= ins.offset;
7792 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007793 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007794
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007795 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007796 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007797 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007798 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007799 (actual_len > inode->i_size) &&
7800 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007801 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007802 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007803 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007804 i_size = cur_offset;
7805 i_size_write(inode, i_size);
7806 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007807 }
7808
Yan Zhengd899e052008-10-30 14:25:28 -04007809 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007810
7811 if (ret) {
7812 btrfs_abort_transaction(trans, root, ret);
7813 if (own_trans)
7814 btrfs_end_transaction(trans, root);
7815 break;
7816 }
Yan Zhengd899e052008-10-30 14:25:28 -04007817
Josef Bacik0af3d002010-06-21 14:48:16 -04007818 if (own_trans)
7819 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007820 }
Yan Zhengd899e052008-10-30 14:25:28 -04007821 return ret;
7822}
7823
Josef Bacik0af3d002010-06-21 14:48:16 -04007824int btrfs_prealloc_file_range(struct inode *inode, int mode,
7825 u64 start, u64 num_bytes, u64 min_size,
7826 loff_t actual_len, u64 *alloc_hint)
7827{
7828 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7829 min_size, actual_len, alloc_hint,
7830 NULL);
7831}
7832
7833int btrfs_prealloc_file_range_trans(struct inode *inode,
7834 struct btrfs_trans_handle *trans, int mode,
7835 u64 start, u64 num_bytes, u64 min_size,
7836 loff_t actual_len, u64 *alloc_hint)
7837{
7838 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7839 min_size, actual_len, alloc_hint, trans);
7840}
7841
Chris Masone6dcd2d2008-07-17 12:53:50 -04007842static int btrfs_set_page_dirty(struct page *page)
7843{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007844 return __set_page_dirty_nobuffers(page);
7845}
7846
Al Viro10556cb2011-06-20 19:28:19 -04007847static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007848{
Li Zefanb83cc962010-12-20 16:04:08 +08007849 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007850 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007851
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007852 if (mask & MAY_WRITE &&
7853 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7854 if (btrfs_root_readonly(root))
7855 return -EROFS;
7856 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7857 return -EACCES;
7858 }
Al Viro2830ba72011-06-20 19:16:29 -04007859 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007860}
Chris Mason39279cc2007-06-12 06:35:45 -04007861
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007862static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007863 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007864 .lookup = btrfs_lookup,
7865 .create = btrfs_create,
7866 .unlink = btrfs_unlink,
7867 .link = btrfs_link,
7868 .mkdir = btrfs_mkdir,
7869 .rmdir = btrfs_rmdir,
7870 .rename = btrfs_rename,
7871 .symlink = btrfs_symlink,
7872 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007873 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007874 .setxattr = btrfs_setxattr,
7875 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007876 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007877 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007878 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007879 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007880};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007881static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007882 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007883 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007884 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007885};
Yan, Zheng76dda932009-09-21 16:00:26 -04007886
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007887static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007888 .llseek = generic_file_llseek,
7889 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007890 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007891 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007892#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007893 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007894#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007895 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007896 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007897};
7898
Chris Masond1310b22008-01-24 16:13:08 -05007899static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007900 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007901 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007902 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007903 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007904 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007905 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007906 .set_bit_hook = btrfs_set_bit_hook,
7907 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007908 .merge_extent_hook = btrfs_merge_extent_hook,
7909 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007910};
7911
Chris Mason35054392009-01-21 13:11:13 -05007912/*
7913 * btrfs doesn't support the bmap operation because swapfiles
7914 * use bmap to make a mapping of extents in the file. They assume
7915 * these extents won't change over the life of the file and they
7916 * use the bmap result to do IO directly to the drive.
7917 *
7918 * the btrfs bmap call would return logical addresses that aren't
7919 * suitable for IO and they also will change frequently as COW
7920 * operations happen. So, swapfile + btrfs == corruption.
7921 *
7922 * For now we're avoiding this by dropping bmap.
7923 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007924static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007925 .readpage = btrfs_readpage,
7926 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007927 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007928 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007929 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007930 .invalidatepage = btrfs_invalidatepage,
7931 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007932 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007933 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007934};
7935
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007936static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007937 .readpage = btrfs_readpage,
7938 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007939 .invalidatepage = btrfs_invalidatepage,
7940 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007941};
7942
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007943static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007944 .getattr = btrfs_getattr,
7945 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007946 .setxattr = btrfs_setxattr,
7947 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007948 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007949 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007950 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007951 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007952 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007953 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007954};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007955static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007956 .getattr = btrfs_getattr,
7957 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007958 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007959 .setxattr = btrfs_setxattr,
7960 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007961 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007962 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007963 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007964 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04007965};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007966static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007967 .readlink = generic_readlink,
7968 .follow_link = page_follow_link_light,
7969 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007970 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05007971 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007972 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007973 .setxattr = btrfs_setxattr,
7974 .getxattr = btrfs_getxattr,
7975 .listxattr = btrfs_listxattr,
7976 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007977 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007978 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007979};
Yan, Zheng76dda932009-09-21 16:00:26 -04007980
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007981const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007982 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007983 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04007984};