blob: 82c7c66f8523b2d42273e11bfdd2ab602ea07fb8 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050043#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040044#include "ctree.h"
45#include "disk-io.h"
46#include "transaction.h"
47#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040048#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040049#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040050#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040051#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020052#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040053#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050054#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050055#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080056#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040057
58struct btrfs_iget_args {
59 u64 ino;
60 struct btrfs_root *root;
61};
62
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070063static const struct inode_operations btrfs_dir_inode_operations;
64static const struct inode_operations btrfs_symlink_inode_operations;
65static const struct inode_operations btrfs_dir_ro_inode_operations;
66static const struct inode_operations btrfs_special_inode_operations;
67static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070068static const struct address_space_operations btrfs_aops;
69static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070070static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050071static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
Miao Xie8ccf6f192012-10-25 09:28:04 +000074static struct kmem_cache *btrfs_delalloc_work_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075struct kmem_cache *btrfs_trans_handle_cachep;
76struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050078struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
80#define S_SHIFT 12
81static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
82 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
83 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
84 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
85 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
86 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
87 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
88 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
89};
90
Eric Sandeen3972f262013-01-12 02:57:22 +000091static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -050092static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040093static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050094static noinline int cow_file_range(struct inode *inode,
95 struct page *locked_page,
96 u64 start, u64 end, int *page_started,
97 unsigned long *nr_written, int unlock);
Josef Bacik70c8a912012-10-11 16:54:30 -040098static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
99 u64 len, u64 orig_start,
100 u64 block_start, u64 block_len,
101 u64 orig_block_len, int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400102
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000103static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500104 struct inode *inode, struct inode *dir,
105 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500106{
107 int err;
108
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000109 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500110 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500111 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500112 return err;
113}
114
Chris Masond352ac62008-09-29 15:18:18 -0400115/*
Chris Masonc8b97812008-10-29 14:49:59 -0400116 * this does all the hard work for inserting an inline extent into
117 * the btree. The caller should have done a btrfs_drop_extents so that
118 * no overlapping inline items exist in the btree
119 */
Chris Masond3977122009-01-05 21:25:51 -0500120static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400121 struct btrfs_root *root, struct inode *inode,
122 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000123 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400124 struct page **compressed_pages)
125{
126 struct btrfs_key key;
127 struct btrfs_path *path;
128 struct extent_buffer *leaf;
129 struct page *page = NULL;
130 char *kaddr;
131 unsigned long ptr;
132 struct btrfs_file_extent_item *ei;
133 int err = 0;
134 int ret;
135 size_t cur_size = size;
136 size_t datasize;
137 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400138
Li Zefanfe3f5662011-03-28 08:30:38 +0000139 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400140 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400141
Chris Masond3977122009-01-05 21:25:51 -0500142 path = btrfs_alloc_path();
143 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400144 return -ENOMEM;
145
Chris Masonb9473432009-03-13 11:00:37 -0400146 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400147
Li Zefan33345d012011-04-20 10:31:50 +0800148 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400149 key.offset = start;
150 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400151 datasize = btrfs_file_extent_calc_inline_size(cur_size);
152
153 inode_add_bytes(inode, size);
154 ret = btrfs_insert_empty_item(trans, root, path, &key,
155 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400156 if (ret) {
157 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400158 goto fail;
159 }
160 leaf = path->nodes[0];
161 ei = btrfs_item_ptr(leaf, path->slots[0],
162 struct btrfs_file_extent_item);
163 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
164 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
165 btrfs_set_file_extent_encryption(leaf, ei, 0);
166 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
167 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
168 ptr = btrfs_file_extent_inline_start(ei);
169
Li Zefan261507a02010-12-17 14:21:50 +0800170 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct page *cpage;
172 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500173 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400174 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500175 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400176 PAGE_CACHE_SIZE);
177
Cong Wang7ac687d2011-11-25 23:14:28 +0800178 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400179 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800180 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400181
182 i++;
183 ptr += cur_size;
184 compressed_size -= cur_size;
185 }
186 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800187 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400188 } else {
189 page = find_get_page(inode->i_mapping,
190 start >> PAGE_CACHE_SHIFT);
191 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800192 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 offset = start & (PAGE_CACHE_SIZE - 1);
194 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800195 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400196 page_cache_release(page);
197 }
198 btrfs_mark_buffer_dirty(leaf);
199 btrfs_free_path(path);
200
Yan, Zhengc21677542009-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, Zhengc21677542009-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 Bacik7a7eaa402011-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, Zhengc21677542009-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, Zhengc21677542009-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 Bacik7a7eaa402011-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, Zhengc21677542009-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, Zhengc21677542009-11-12 09:34:21 +0000690 /*
691 * here we're doing allocation and writeback of the
692 * compressed pages
693 */
694 btrfs_drop_extent_cache(inode, async_extent->start,
695 async_extent->start +
696 async_extent->ram_size - 1, 0);
697
David Sterba172ddd62011-04-21 00:48:27 +0200698 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100699 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500700 em->start = async_extent->start;
701 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500702 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400703 em->mod_start = em->start;
704 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500705
706 em->block_start = ins.objectid;
707 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500708 em->orig_block_len = ins.offset;
Chris Mason771ed682008-11-06 22:02:51 -0500709 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800710 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500711 set_bit(EXTENT_FLAG_PINNED, &em->flags);
712 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400713 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500714
Chris Masond3977122009-01-05 21:25:51 -0500715 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400716 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500717 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400718 if (!ret)
719 list_move(&em->list,
720 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400721 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500722 if (ret != -EEXIST) {
723 free_extent_map(em);
724 break;
725 }
726 btrfs_drop_extent_cache(inode, async_extent->start,
727 async_extent->start +
728 async_extent->ram_size - 1, 0);
729 }
730
Li Zefan261507a02010-12-17 14:21:50 +0800731 ret = btrfs_add_ordered_extent_compress(inode,
732 async_extent->start,
733 ins.objectid,
734 async_extent->ram_size,
735 ins.offset,
736 BTRFS_ORDERED_COMPRESSED,
737 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100738 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500739
Chris Mason771ed682008-11-06 22:02:51 -0500740 /*
741 * clear dirty, set writeback and unlock the pages.
742 */
743 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400744 &BTRFS_I(inode)->io_tree,
745 async_extent->start,
746 async_extent->start +
747 async_extent->ram_size - 1,
748 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
749 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400750 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400751 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500752
753 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500754 async_extent->start,
755 async_extent->ram_size,
756 ins.objectid,
757 ins.offset, async_extent->pages,
758 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500759
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100760 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500761 alloc_hint = ins.objectid + ins.offset;
762 kfree(async_extent);
763 cond_resched();
764 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100765 ret = 0;
766out:
767 return ret;
768out_free:
769 kfree(async_extent);
770 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500771}
772
Josef Bacik4b46fce2010-05-23 11:00:55 -0400773static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
774 u64 num_bytes)
775{
776 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
777 struct extent_map *em;
778 u64 alloc_hint = 0;
779
780 read_lock(&em_tree->lock);
781 em = search_extent_mapping(em_tree, start, num_bytes);
782 if (em) {
783 /*
784 * if block start isn't an actual block number then find the
785 * first block in this inode and use that as a hint. If that
786 * block is also bogus then just don't worry about it.
787 */
788 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
789 free_extent_map(em);
790 em = search_extent_mapping(em_tree, 0, 0);
791 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
792 alloc_hint = em->block_start;
793 if (em)
794 free_extent_map(em);
795 } else {
796 alloc_hint = em->block_start;
797 free_extent_map(em);
798 }
799 }
800 read_unlock(&em_tree->lock);
801
802 return alloc_hint;
803}
804
Chris Mason771ed682008-11-06 22:02:51 -0500805/*
806 * when extent_io.c finds a delayed allocation range in the file,
807 * the call backs end up in this code. The basic idea is to
808 * allocate extents on disk for the range, and create ordered data structs
809 * in ram to track those extents.
810 *
811 * locked_page is the page that writepage had locked already. We use
812 * it to make sure we don't do extra locks or unlocks.
813 *
814 * *page_started is set to one if we unlock locked_page and do everything
815 * required to start IO on it. It may be clean and already done with
816 * IO when we return.
817 */
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000818static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
819 struct inode *inode,
820 struct btrfs_root *root,
821 struct page *locked_page,
822 u64 start, u64 end, int *page_started,
823 unsigned long *nr_written,
824 int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500825{
Chris Mason771ed682008-11-06 22:02:51 -0500826 u64 alloc_hint = 0;
827 u64 num_bytes;
828 unsigned long ram_size;
829 u64 disk_num_bytes;
830 u64 cur_alloc_size;
831 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500832 struct btrfs_key ins;
833 struct extent_map *em;
834 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
835 int ret = 0;
836
Liu Bo83eea1f2012-07-10 05:28:39 -0600837 BUG_ON(btrfs_is_free_space_inode(inode));
Chris Mason771ed682008-11-06 22:02:51 -0500838
Chris Mason771ed682008-11-06 22:02:51 -0500839 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
840 num_bytes = max(blocksize, num_bytes);
841 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500842
Chris Mason4cb53002011-05-24 15:35:30 -0400843 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400844 if (num_bytes < 64 * 1024 &&
845 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400846 btrfs_add_inode_defrag(trans, inode);
847
Chris Mason771ed682008-11-06 22:02:51 -0500848 if (start == 0) {
849 /* lets try to make an inline extent */
850 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000851 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500852 if (ret == 0) {
853 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400854 &BTRFS_I(inode)->io_tree,
855 start, end, NULL,
856 EXTENT_CLEAR_UNLOCK_PAGE |
857 EXTENT_CLEAR_UNLOCK |
858 EXTENT_CLEAR_DELALLOC |
859 EXTENT_CLEAR_DIRTY |
860 EXTENT_SET_WRITEBACK |
861 EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000862
Chris Mason771ed682008-11-06 22:02:51 -0500863 *nr_written = *nr_written +
864 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
865 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500866 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100867 } else if (ret < 0) {
868 btrfs_abort_transaction(trans, root, ret);
869 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500870 }
871 }
Chris Masonc8b97812008-10-29 14:49:59 -0400872
873 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200874 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400875
Josef Bacik4b46fce2010-05-23 11:00:55 -0400876 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400877 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400878
Chris Masond3977122009-01-05 21:25:51 -0500879 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400880 unsigned long op;
881
Josef Bacik287a0ab2010-03-19 18:07:23 +0000882 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400883 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500884 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500885 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100886 if (ret < 0) {
887 btrfs_abort_transaction(trans, root, ret);
888 goto out_unlock;
889 }
Chris Masond3977122009-01-05 21:25:51 -0500890
David Sterba172ddd62011-04-21 00:48:27 +0200891 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100892 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400893 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500894 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500895 ram_size = ins.offset;
896 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400897 em->mod_start = em->start;
898 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400899
Chris Masone6dcd2d2008-07-17 12:53:50 -0400900 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400901 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500902 em->orig_block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400903 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400904 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400905 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -0400906
Chris Masond3977122009-01-05 21:25:51 -0500907 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400908 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400909 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -0400910 if (!ret)
911 list_move(&em->list,
912 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -0400913 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400914 if (ret != -EEXIST) {
915 free_extent_map(em);
916 break;
917 }
918 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400919 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400920 }
921
Chris Mason98d20f62008-04-14 09:46:10 -0400922 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400923 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500924 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100925 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400926
Yan Zheng17d217f2008-12-12 10:03:38 -0500927 if (root->root_key.objectid ==
928 BTRFS_DATA_RELOC_TREE_OBJECTID) {
929 ret = btrfs_reloc_clone_csums(inode, start,
930 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100931 if (ret) {
932 btrfs_abort_transaction(trans, root, ret);
933 goto out_unlock;
934 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500935 }
936
Chris Masond3977122009-01-05 21:25:51 -0500937 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400938 break;
Chris Masond3977122009-01-05 21:25:51 -0500939
Chris Masonc8b97812008-10-29 14:49:59 -0400940 /* we're not doing compressed IO, don't unlock the first
941 * page (which the caller expects to stay locked), don't
942 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400943 *
944 * Do set the Private2 bit so we know this page was properly
945 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400946 */
Chris Masona791e352009-10-08 11:27:10 -0400947 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
948 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
949 EXTENT_SET_PRIVATE2;
950
Chris Masonc8b97812008-10-29 14:49:59 -0400951 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
952 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400953 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400954 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500955 num_bytes -= cur_alloc_size;
956 alloc_hint = ins.objectid + ins.offset;
957 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400958 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100959out:
Chris Masonbe20aa92007-12-17 20:14:01 -0500960 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000961
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100962out_unlock:
963 extent_clear_unlock_delalloc(inode,
964 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400965 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966 EXTENT_CLEAR_UNLOCK_PAGE |
967 EXTENT_CLEAR_UNLOCK |
968 EXTENT_CLEAR_DELALLOC |
969 EXTENT_CLEAR_DIRTY |
970 EXTENT_SET_WRITEBACK |
971 EXTENT_END_WRITEBACK);
972
973 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500974}
Chris Masonc8b97812008-10-29 14:49:59 -0400975
Miao Xieb7d5b0a2012-11-01 07:32:18 +0000976static noinline int cow_file_range(struct inode *inode,
977 struct page *locked_page,
978 u64 start, u64 end, int *page_started,
979 unsigned long *nr_written,
980 int unlock)
981{
982 struct btrfs_trans_handle *trans;
983 struct btrfs_root *root = BTRFS_I(inode)->root;
984 int ret;
985
986 trans = btrfs_join_transaction(root);
987 if (IS_ERR(trans)) {
988 extent_clear_unlock_delalloc(inode,
989 &BTRFS_I(inode)->io_tree,
990 start, end, locked_page,
991 EXTENT_CLEAR_UNLOCK_PAGE |
992 EXTENT_CLEAR_UNLOCK |
993 EXTENT_CLEAR_DELALLOC |
994 EXTENT_CLEAR_DIRTY |
995 EXTENT_SET_WRITEBACK |
996 EXTENT_END_WRITEBACK);
997 return PTR_ERR(trans);
998 }
999 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1000
1001 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1002 page_started, nr_written, unlock);
1003
1004 btrfs_end_transaction(trans, root);
1005
1006 return ret;
1007}
1008
Chris Mason771ed682008-11-06 22:02:51 -05001009/*
1010 * work queue call back to started compression on a file and pages
1011 */
1012static noinline void async_cow_start(struct btrfs_work *work)
1013{
1014 struct async_cow *async_cow;
1015 int num_added = 0;
1016 async_cow = container_of(work, struct async_cow, work);
1017
1018 compress_file_range(async_cow->inode, async_cow->locked_page,
1019 async_cow->start, async_cow->end, async_cow,
1020 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001021 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001022 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001023 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001024 }
Chris Mason771ed682008-11-06 22:02:51 -05001025}
1026
1027/*
1028 * work queue call back to submit previously compressed pages
1029 */
1030static noinline void async_cow_submit(struct btrfs_work *work)
1031{
1032 struct async_cow *async_cow;
1033 struct btrfs_root *root;
1034 unsigned long nr_pages;
1035
1036 async_cow = container_of(work, struct async_cow, work);
1037
1038 root = async_cow->root;
1039 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1040 PAGE_CACHE_SHIFT;
1041
Josef Bacik66657b32012-08-01 15:36:24 -04001042 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001043 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001044 waitqueue_active(&root->fs_info->async_submit_wait))
1045 wake_up(&root->fs_info->async_submit_wait);
1046
Chris Masond3977122009-01-05 21:25:51 -05001047 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001048 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001049}
Chris Masonc8b97812008-10-29 14:49:59 -04001050
Chris Mason771ed682008-11-06 22:02:51 -05001051static noinline void async_cow_free(struct btrfs_work *work)
1052{
1053 struct async_cow *async_cow;
1054 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001055 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001056 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001057 kfree(async_cow);
1058}
1059
1060static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1061 u64 start, u64 end, int *page_started,
1062 unsigned long *nr_written)
1063{
1064 struct async_cow *async_cow;
1065 struct btrfs_root *root = BTRFS_I(inode)->root;
1066 unsigned long nr_pages;
1067 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001068 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001069
Chris Masona3429ab2009-10-08 12:30:20 -04001070 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1071 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001072 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001073 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001074 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001075 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001076 async_cow->root = root;
1077 async_cow->locked_page = locked_page;
1078 async_cow->start = start;
1079
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001080 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001081 cur_end = end;
1082 else
1083 cur_end = min(end, start + 512 * 1024 - 1);
1084
1085 async_cow->end = cur_end;
1086 INIT_LIST_HEAD(&async_cow->extents);
1087
1088 async_cow->work.func = async_cow_start;
1089 async_cow->work.ordered_func = async_cow_submit;
1090 async_cow->work.ordered_free = async_cow_free;
1091 async_cow->work.flags = 0;
1092
Chris Mason771ed682008-11-06 22:02:51 -05001093 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1094 PAGE_CACHE_SHIFT;
1095 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1096
1097 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1098 &async_cow->work);
1099
1100 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1101 wait_event(root->fs_info->async_submit_wait,
1102 (atomic_read(&root->fs_info->async_delalloc_pages) <
1103 limit));
1104 }
1105
Chris Masond3977122009-01-05 21:25:51 -05001106 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001107 atomic_read(&root->fs_info->async_delalloc_pages)) {
1108 wait_event(root->fs_info->async_submit_wait,
1109 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1110 0));
1111 }
1112
1113 *nr_written += nr_pages;
1114 start = cur_end + 1;
1115 }
1116 *page_started = 1;
1117 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001118}
1119
Chris Masond3977122009-01-05 21:25:51 -05001120static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001121 u64 bytenr, u64 num_bytes)
1122{
1123 int ret;
1124 struct btrfs_ordered_sum *sums;
1125 LIST_HEAD(list);
1126
Yan Zheng07d400a2009-01-06 11:42:00 -05001127 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001128 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001129 if (ret == 0 && list_empty(&list))
1130 return 0;
1131
1132 while (!list_empty(&list)) {
1133 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1134 list_del(&sums->list);
1135 kfree(sums);
1136 }
1137 return 1;
1138}
1139
Chris Masond352ac62008-09-29 15:18:18 -04001140/*
1141 * when nowcow writeback call back. This checks for snapshots or COW copies
1142 * of the extents that exist in the file, and COWs the file as required.
1143 *
1144 * If no cow copies or snapshots exist, we write directly to the existing
1145 * blocks on disk
1146 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001147static noinline int run_delalloc_nocow(struct inode *inode,
1148 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001149 u64 start, u64 end, int *page_started, int force,
1150 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001151{
Chris Masonbe20aa92007-12-17 20:14:01 -05001152 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001153 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001154 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001155 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001156 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001157 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001158 u64 cow_start;
1159 u64 cur_offset;
1160 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001161 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001162 u64 disk_bytenr;
1163 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001164 u64 disk_num_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001165 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001166 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001167 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001168 int nocow;
1169 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001170 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001171 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001172
1173 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001174 if (!path) {
1175 extent_clear_unlock_delalloc(inode,
1176 &BTRFS_I(inode)->io_tree,
1177 start, end, locked_page,
1178 EXTENT_CLEAR_UNLOCK_PAGE |
1179 EXTENT_CLEAR_UNLOCK |
1180 EXTENT_CLEAR_DELALLOC |
1181 EXTENT_CLEAR_DIRTY |
1182 EXTENT_SET_WRITEBACK |
1183 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001184 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001185 }
Li Zefan82d59022011-04-20 10:33:24 +08001186
Liu Bo83eea1f2012-07-10 05:28:39 -06001187 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001188
1189 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001190 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001191 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001192 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001193
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001194 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001195 extent_clear_unlock_delalloc(inode,
1196 &BTRFS_I(inode)->io_tree,
1197 start, end, locked_page,
1198 EXTENT_CLEAR_UNLOCK_PAGE |
1199 EXTENT_CLEAR_UNLOCK |
1200 EXTENT_CLEAR_DELALLOC |
1201 EXTENT_CLEAR_DIRTY |
1202 EXTENT_SET_WRITEBACK |
1203 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001204 btrfs_free_path(path);
1205 return PTR_ERR(trans);
1206 }
1207
Josef Bacik74b21072011-04-13 12:02:53 -04001208 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001209
Yan Zheng80ff3852008-10-30 14:20:02 -04001210 cow_start = (u64)-1;
1211 cur_offset = start;
1212 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001213 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001214 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001215 if (ret < 0) {
1216 btrfs_abort_transaction(trans, root, ret);
1217 goto error;
1218 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1220 leaf = path->nodes[0];
1221 btrfs_item_key_to_cpu(leaf, &found_key,
1222 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001223 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001224 found_key.type == BTRFS_EXTENT_DATA_KEY)
1225 path->slots[0]--;
1226 }
1227 check_prev = 0;
1228next_slot:
1229 leaf = path->nodes[0];
1230 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1231 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001232 if (ret < 0) {
1233 btrfs_abort_transaction(trans, root, ret);
1234 goto error;
1235 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001236 if (ret > 0)
1237 break;
1238 leaf = path->nodes[0];
1239 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001240
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 nocow = 0;
1242 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001243 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001245
Li Zefan33345d012011-04-20 10:31:50 +08001246 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1248 found_key.offset > end)
1249 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001250
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 if (found_key.offset > cur_offset) {
1252 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001253 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 goto out_check;
1255 }
Chris Masonc31f8832008-01-08 15:46:31 -05001256
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 fi = btrfs_item_ptr(leaf, path->slots[0],
1258 struct btrfs_file_extent_item);
1259 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001260
Yan Zhengd899e052008-10-30 14:25:28 -04001261 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1262 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001264 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 extent_end = found_key.offset +
1266 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001267 disk_num_bytes =
1268 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 if (extent_end <= start) {
1270 path->slots[0]++;
1271 goto next_slot;
1272 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001273 if (disk_bytenr == 0)
1274 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 if (btrfs_file_extent_compression(leaf, fi) ||
1276 btrfs_file_extent_encryption(leaf, fi) ||
1277 btrfs_file_extent_other_encoding(leaf, fi))
1278 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001279 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1280 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001281 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001283 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001284 found_key.offset -
1285 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001286 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001287 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001288 disk_bytenr += cur_offset - found_key.offset;
1289 num_bytes = min(end + 1, extent_end) - cur_offset;
1290 /*
1291 * force cow if csum exists in the range.
1292 * this ensure that csum for a given extent are
1293 * either valid or do not exist.
1294 */
1295 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1296 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 nocow = 1;
1298 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1299 extent_end = found_key.offset +
1300 btrfs_file_extent_inline_len(leaf, fi);
1301 extent_end = ALIGN(extent_end, root->sectorsize);
1302 } else {
1303 BUG_ON(1);
1304 }
1305out_check:
1306 if (extent_end <= start) {
1307 path->slots[0]++;
1308 goto next_slot;
1309 }
1310 if (!nocow) {
1311 if (cow_start == (u64)-1)
1312 cow_start = cur_offset;
1313 cur_offset = extent_end;
1314 if (cur_offset > end)
1315 break;
1316 path->slots[0]++;
1317 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001318 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001319
David Sterbab3b4aa72011-04-21 01:20:15 +02001320 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001322 ret = __cow_file_range(trans, inode, root, locked_page,
1323 cow_start, found_key.offset - 1,
1324 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001325 if (ret) {
1326 btrfs_abort_transaction(trans, root, ret);
1327 goto error;
1328 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001330 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001331
Yan Zhengd899e052008-10-30 14:25:28 -04001332 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1333 struct extent_map *em;
1334 struct extent_map_tree *em_tree;
1335 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001336 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001337 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001338 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001339 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001340 em->len = num_bytes;
1341 em->block_len = num_bytes;
1342 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001343 em->orig_block_len = disk_num_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001344 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001345 em->mod_start = em->start;
1346 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001347 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001348 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001349 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001350 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001351 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001352 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04001353 if (!ret)
1354 list_move(&em->list,
1355 &em_tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -04001356 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001357 if (ret != -EEXIST) {
1358 free_extent_map(em);
1359 break;
1360 }
1361 btrfs_drop_extent_cache(inode, em->start,
1362 em->start + em->len - 1, 0);
1363 }
1364 type = BTRFS_ORDERED_PREALLOC;
1365 } else {
1366 type = BTRFS_ORDERED_NOCOW;
1367 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001368
1369 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001370 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001371 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001372
Yan, Zhengefa56462010-05-16 10:49:59 -04001373 if (root->root_key.objectid ==
1374 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1375 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1376 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001377 if (ret) {
1378 btrfs_abort_transaction(trans, root, ret);
1379 goto error;
1380 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001381 }
1382
Yan Zhengd899e052008-10-30 14:25:28 -04001383 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001384 cur_offset, cur_offset + num_bytes - 1,
1385 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1386 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1387 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001388 cur_offset = extent_end;
1389 if (cur_offset > end)
1390 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001391 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001392 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001393
Josef Bacik17ca04a2012-05-31 15:58:55 -04001394 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001395 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001396 cur_offset = end;
1397 }
1398
Yan Zheng80ff3852008-10-30 14:20:02 -04001399 if (cow_start != (u64)-1) {
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001400 ret = __cow_file_range(trans, inode, root, locked_page,
1401 cow_start, end,
1402 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001403 if (ret) {
1404 btrfs_abort_transaction(trans, root, ret);
1405 goto error;
1406 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001407 }
1408
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001409error:
Miao Xiea698d0752012-09-20 01:51:59 -06001410 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001411 if (!ret)
1412 ret = err;
1413
Josef Bacik17ca04a2012-05-31 15:58:55 -04001414 if (ret && cur_offset < end)
1415 extent_clear_unlock_delalloc(inode,
1416 &BTRFS_I(inode)->io_tree,
1417 cur_offset, end, locked_page,
1418 EXTENT_CLEAR_UNLOCK_PAGE |
1419 EXTENT_CLEAR_UNLOCK |
1420 EXTENT_CLEAR_DELALLOC |
1421 EXTENT_CLEAR_DIRTY |
1422 EXTENT_SET_WRITEBACK |
1423 EXTENT_END_WRITEBACK);
1424
Yan Zheng7ea394f2008-08-05 13:05:02 -04001425 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001426 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001427}
1428
Chris Masond352ac62008-09-29 15:18:18 -04001429/*
1430 * extent_io.c call back to do delayed allocation processing
1431 */
Chris Masonc8b97812008-10-29 14:49:59 -04001432static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001433 u64 start, u64 end, int *page_started,
1434 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001435{
Chris Masonbe20aa92007-12-17 20:14:01 -05001436 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001437 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001438
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001439 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001440 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001441 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001442 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001443 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001444 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001445 } else if (!btrfs_test_opt(root, COMPRESS) &&
1446 !(BTRFS_I(inode)->force_compress) &&
1447 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001448 ret = cow_file_range(inode, locked_page, start, end,
1449 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001450 } else {
1451 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1452 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001453 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001454 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001455 }
Chris Masonb888db22007-08-27 16:49:44 -04001456 return ret;
1457}
1458
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001459static void btrfs_split_extent_hook(struct inode *inode,
1460 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001461{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001462 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001463 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001464 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001465
Josef Bacik9e0baf62011-07-15 15:16:44 +00001466 spin_lock(&BTRFS_I(inode)->lock);
1467 BTRFS_I(inode)->outstanding_extents++;
1468 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001469}
1470
1471/*
1472 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1473 * extents so we can keep track of new extents that are just merged onto old
1474 * extents, such as when we are doing sequential writes, so we can properly
1475 * account for the metadata space we'll need.
1476 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001477static void btrfs_merge_extent_hook(struct inode *inode,
1478 struct extent_state *new,
1479 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001480{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001481 /* not delalloc, ignore it */
1482 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001483 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001484
Josef Bacik9e0baf62011-07-15 15:16:44 +00001485 spin_lock(&BTRFS_I(inode)->lock);
1486 BTRFS_I(inode)->outstanding_extents--;
1487 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001488}
1489
Chris Masond352ac62008-09-29 15:18:18 -04001490/*
1491 * extent_io.c set_bit_hook, used to track delayed allocation
1492 * bytes in this file, and to maintain the list of inodes that
1493 * have pending delalloc work to be done.
1494 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001495static void btrfs_set_bit_hook(struct inode *inode,
1496 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001497{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001498
Chris Mason75eff682008-12-15 15:54:40 -05001499 /*
1500 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001501 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001502 * bit, which is only set or cleared with irqs on
1503 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001504 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001505 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001506 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001507 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001508
Josef Bacik9e0baf62011-07-15 15:16:44 +00001509 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001510 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001511 } else {
1512 spin_lock(&BTRFS_I(inode)->lock);
1513 BTRFS_I(inode)->outstanding_extents++;
1514 spin_unlock(&BTRFS_I(inode)->lock);
1515 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001516
Miao Xie963d6782013-01-29 10:10:51 +00001517 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1518 root->fs_info->delalloc_batch);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001519 spin_lock(&BTRFS_I(inode)->lock);
1520 BTRFS_I(inode)->delalloc_bytes += len;
1521 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1522 &BTRFS_I(inode)->runtime_flags)) {
1523 spin_lock(&root->fs_info->delalloc_lock);
1524 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1525 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1526 &root->fs_info->delalloc_inodes);
1527 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1528 &BTRFS_I(inode)->runtime_flags);
1529 }
1530 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001531 }
Miao Xiedf0af1a52013-01-29 10:11:59 +00001532 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001533 }
Chris Mason291d6732008-01-29 15:55:23 -05001534}
1535
Chris Masond352ac62008-09-29 15:18:18 -04001536/*
1537 * extent_io.c clear_bit_hook, see set_bit_hook for why
1538 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001539static void btrfs_clear_bit_hook(struct inode *inode,
1540 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001541{
Chris Mason75eff682008-12-15 15:54:40 -05001542 /*
1543 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001544 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001545 * bit, which is only set or cleared with irqs on
1546 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001547 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001548 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001549 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001550 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001551
Josef Bacik9e0baf62011-07-15 15:16:44 +00001552 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001553 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001554 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1555 spin_lock(&BTRFS_I(inode)->lock);
1556 BTRFS_I(inode)->outstanding_extents--;
1557 spin_unlock(&BTRFS_I(inode)->lock);
1558 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001559
1560 if (*bits & EXTENT_DO_ACCOUNTING)
1561 btrfs_delalloc_release_metadata(inode, len);
1562
Josef Bacik0cb59c92010-07-02 12:14:14 -04001563 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1564 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001565 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001566
Miao Xie963d6782013-01-29 10:10:51 +00001567 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1568 root->fs_info->delalloc_batch);
Miao Xiedf0af1a52013-01-29 10:11:59 +00001569 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001570 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001571 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a52013-01-29 10:11:59 +00001572 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1573 &BTRFS_I(inode)->runtime_flags)) {
1574 spin_lock(&root->fs_info->delalloc_lock);
1575 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1576 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1577 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1578 &BTRFS_I(inode)->runtime_flags);
1579 }
1580 spin_unlock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04001581 }
Miao Xiedf0af1a52013-01-29 10:11:59 +00001582 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001583 }
Chris Mason291d6732008-01-29 15:55:23 -05001584}
1585
Chris Masond352ac62008-09-29 15:18:18 -04001586/*
1587 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1588 * we don't create bios that span stripes or chunks
1589 */
Chris Mason239b14b2008-03-24 15:02:07 -04001590int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001591 size_t size, struct bio *bio,
1592 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001593{
1594 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Chris Masona62b9402008-10-03 16:31:08 -04001595 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001596 u64 length = 0;
1597 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001598 int ret;
1599
Chris Mason771ed682008-11-06 22:02:51 -05001600 if (bio_flags & EXTENT_BIO_COMPRESSED)
1601 return 0;
1602
Chris Masonf2d8d742008-04-21 10:03:05 -04001603 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001604 map_length = length;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001605 ret = btrfs_map_block(root->fs_info, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001606 &map_length, NULL, 0);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001607 /* Will always return 0 with map_multi == NULL */
Jeff Mahoney3444a972011-10-03 23:23:13 -04001608 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001609 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001610 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001611 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001612}
1613
Chris Masond352ac62008-09-29 15:18:18 -04001614/*
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 */
Chris Masond3977122009-01-05 21:25:51 -05001622static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1623 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001624 unsigned long bio_flags,
1625 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001626{
Chris Mason065631f2008-02-20 12:07:25 -05001627 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001628 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001629
Chris Masond20f7042008-12-08 16:58:54 -05001630 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001631 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001632 return 0;
1633}
Chris Masone0156402008-04-16 11:15:20 -04001634
Chris Mason4a69a412008-11-06 22:03:00 -05001635/*
1636 * in order to insert checksums into the metadata in large chunks,
1637 * we wait until bio submission time. All the pages in the bio are
1638 * checksummed and sums are attached onto the ordered extent record.
1639 *
1640 * At IO completion time the cums attached on the ordered extent record
1641 * are inserted into the btree
1642 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001643static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001644 int mirror_num, unsigned long bio_flags,
1645 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001646{
1647 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001648 int ret;
1649
1650 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1651 if (ret)
1652 bio_endio(bio, ret);
1653 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001654}
1655
Chris Masond352ac62008-09-29 15:18:18 -04001656/*
Chris Masoncad321a2008-12-17 14:51:42 -05001657 * extent_io.c submission hook. This does the right thing for csum calculation
1658 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001659 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001660static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001661 int mirror_num, unsigned long bio_flags,
1662 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001663{
1664 struct btrfs_root *root = BTRFS_I(inode)->root;
1665 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001666 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001667 int metadata = 0;
Josef Bacikb812ce22012-11-16 13:56:32 -05001668 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001669
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001670 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001671
Liu Bo83eea1f2012-07-10 05:28:39 -06001672 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001673 metadata = 2;
1674
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001675 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001676 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1677 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001678 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001679
Chris Masond20f7042008-12-08 16:58:54 -05001680 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001681 ret = btrfs_submit_compressed_read(inode, bio,
1682 mirror_num,
1683 bio_flags);
1684 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001685 } else if (!skip_sum) {
1686 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1687 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001688 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001689 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001690 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001691 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001692 /* csum items have already been cloned */
1693 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1694 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001695 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001696 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001697 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001698 bio_flags, bio_offset,
1699 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001700 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001701 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001702 } else if (!skip_sum) {
1703 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1704 if (ret)
1705 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001706 }
1707
Chris Mason0b86a832008-03-24 15:01:56 -04001708mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001709 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1710
1711out:
1712 if (ret < 0)
1713 bio_endio(bio, ret);
1714 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001715}
Chris Mason6885f302008-02-20 16:11:05 -05001716
Chris Masond352ac62008-09-29 15:18:18 -04001717/*
1718 * given a list of ordered sums record them in the inode. This happens
1719 * at IO completion time based on sums calculated at bio submission time.
1720 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001721static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001722 struct inode *inode, u64 file_offset,
1723 struct list_head *list)
1724{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001725 struct btrfs_ordered_sum *sum;
1726
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001727 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001728 btrfs_csum_file_blocks(trans,
1729 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001730 }
1731 return 0;
1732}
1733
Josef Bacik2ac55d42010-02-03 19:33:23 +00001734int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1735 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001736{
Julia Lawall6c1500f2012-11-03 20:30:18 +00001737 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001738 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001739 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001740}
1741
Chris Masond352ac62008-09-29 15:18:18 -04001742/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001743struct btrfs_writepage_fixup {
1744 struct page *page;
1745 struct btrfs_work work;
1746};
1747
Christoph Hellwigb2950862008-12-02 09:54:17 -05001748static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001749{
1750 struct btrfs_writepage_fixup *fixup;
1751 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001752 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001753 struct page *page;
1754 struct inode *inode;
1755 u64 page_start;
1756 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001757 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001758
1759 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1760 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001761again:
Chris Mason247e7432008-07-17 12:53:51 -04001762 lock_page(page);
1763 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1764 ClearPageChecked(page);
1765 goto out_page;
1766 }
1767
1768 inode = page->mapping->host;
1769 page_start = page_offset(page);
1770 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1771
Josef Bacik2ac55d42010-02-03 19:33:23 +00001772 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001773 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001774
1775 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001776 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001777 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001778
1779 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1780 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001781 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1782 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001783 unlock_page(page);
1784 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001785 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001786 goto again;
1787 }
Chris Mason247e7432008-07-17 12:53:51 -04001788
Jeff Mahoney87826df2012-02-15 16:23:57 +01001789 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1790 if (ret) {
1791 mapping_set_error(page->mapping, ret);
1792 end_extent_writepage(page, ret, page_start, page_end);
1793 ClearPageChecked(page);
1794 goto out;
1795 }
1796
Josef Bacik2ac55d42010-02-03 19:33:23 +00001797 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001798 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001799 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001800out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001801 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1802 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001803out_page:
1804 unlock_page(page);
1805 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001806 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001807}
1808
1809/*
1810 * There are a few paths in the higher layers of the kernel that directly
1811 * set the page dirty bit without asking the filesystem if it is a
1812 * good idea. This causes problems because we want to make sure COW
1813 * properly happens and the data=ordered rules are followed.
1814 *
Chris Masonc8b97812008-10-29 14:49:59 -04001815 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001816 * hasn't been properly setup for IO. We kick off an async process
1817 * to fix it up. The async helper will wait for ordered extents, set
1818 * the delalloc bit and make it safe to write the page.
1819 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001820static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001821{
1822 struct inode *inode = page->mapping->host;
1823 struct btrfs_writepage_fixup *fixup;
1824 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001825
Chris Mason8b62b722009-09-02 16:53:46 -04001826 /* this page is properly in the ordered list */
1827 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001828 return 0;
1829
1830 if (PageChecked(page))
1831 return -EAGAIN;
1832
1833 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1834 if (!fixup)
1835 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001836
Chris Mason247e7432008-07-17 12:53:51 -04001837 SetPageChecked(page);
1838 page_cache_get(page);
1839 fixup->work.func = btrfs_writepage_fixup_worker;
1840 fixup->page = page;
1841 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001842 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001843}
1844
Yan Zhengd899e052008-10-30 14:25:28 -04001845static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1846 struct inode *inode, u64 file_pos,
1847 u64 disk_bytenr, u64 disk_num_bytes,
1848 u64 num_bytes, u64 ram_bytes,
1849 u8 compression, u8 encryption,
1850 u16 other_encoding, int extent_type)
1851{
1852 struct btrfs_root *root = BTRFS_I(inode)->root;
1853 struct btrfs_file_extent_item *fi;
1854 struct btrfs_path *path;
1855 struct extent_buffer *leaf;
1856 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04001857 int ret;
1858
1859 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001860 if (!path)
1861 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001862
Chris Masonb9473432009-03-13 11:00:37 -04001863 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001864
1865 /*
1866 * we may be replacing one extent in the tree with another.
1867 * The new extent is pinned in the extent map, and we don't want
1868 * to drop it from the cache until it is completely in the btree.
1869 *
1870 * So, tell btrfs_drop_extents to leave this extent in the cache.
1871 * the caller is expected to unpin it and allow it to be merged
1872 * with the others.
1873 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001874 ret = btrfs_drop_extents(trans, root, inode, file_pos,
Josef Bacik26714852012-08-29 12:24:27 -04001875 file_pos + num_bytes, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001876 if (ret)
1877 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001878
Li Zefan33345d012011-04-20 10:31:50 +08001879 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001880 ins.offset = file_pos;
1881 ins.type = BTRFS_EXTENT_DATA_KEY;
1882 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001883 if (ret)
1884 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001885 leaf = path->nodes[0];
1886 fi = btrfs_item_ptr(leaf, path->slots[0],
1887 struct btrfs_file_extent_item);
1888 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1889 btrfs_set_file_extent_type(leaf, fi, extent_type);
1890 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1891 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1892 btrfs_set_file_extent_offset(leaf, fi, 0);
1893 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1894 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1895 btrfs_set_file_extent_compression(leaf, fi, compression);
1896 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1897 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001898
Yan Zhengd899e052008-10-30 14:25:28 -04001899 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04001900 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04001901
1902 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001903
1904 ins.objectid = disk_bytenr;
1905 ins.offset = disk_num_bytes;
1906 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001907 ret = btrfs_alloc_reserved_file_extent(trans, root,
1908 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001909 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001910out:
Yan Zhengd899e052008-10-30 14:25:28 -04001911 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001912
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001913 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001914}
1915
Chris Mason5d13a982009-03-13 11:41:46 -04001916/*
1917 * helper function for btrfs_finish_ordered_io, this
1918 * just reads in some of the csum leaves to prime them into ram
1919 * before we start the transaction. It limits the amount of btree
1920 * reads required while inside the transaction.
1921 */
Chris Masond352ac62008-09-29 15:18:18 -04001922/* as ordered data IO finishes, this gets called so we can finish
1923 * an ordered extent if the range of bytes in the file it covers are
1924 * fully written.
1925 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001926static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001927{
Josef Bacik5fd02042012-05-02 14:00:54 -04001928 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001929 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001930 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001931 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001932 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001933 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001934 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001935 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001936
Liu Bo83eea1f2012-07-10 05:28:39 -06001937 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001938
Josef Bacik5fd02042012-05-02 14:00:54 -04001939 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1940 ret = -EIO;
1941 goto out;
1942 }
1943
Yan, Zhengc21677542009-11-12 09:34:21 +00001944 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001945 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Josef Bacik6c760c02012-11-09 10:53:21 -05001946 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1947 if (nolock)
1948 trans = btrfs_join_transaction_nolock(root);
1949 else
1950 trans = btrfs_join_transaction(root);
1951 if (IS_ERR(trans)) {
1952 ret = PTR_ERR(trans);
1953 trans = NULL;
1954 goto out;
Yan, Zhengc21677542009-11-12 09:34:21 +00001955 }
Josef Bacik6c760c02012-11-09 10:53:21 -05001956 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1957 ret = btrfs_update_inode_fallback(trans, root, inode);
1958 if (ret) /* -ENOMEM or corruption */
1959 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00001960 goto out;
1961 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001962
Josef Bacik2ac55d42010-02-03 19:33:23 +00001963 lock_extent_bits(io_tree, ordered_extent->file_offset,
1964 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001965 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001966
Josef Bacik0cb59c92010-07-02 12:14:14 -04001967 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001968 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001969 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04001970 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001971 if (IS_ERR(trans)) {
1972 ret = PTR_ERR(trans);
1973 trans = NULL;
1974 goto out_unlock;
1975 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001976 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00001977
Chris Masonc8b97812008-10-29 14:49:59 -04001978 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001979 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001980 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001981 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001982 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001983 ordered_extent->file_offset,
1984 ordered_extent->file_offset +
1985 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001986 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001987 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001988 ret = insert_reserved_file_extent(trans, inode,
1989 ordered_extent->file_offset,
1990 ordered_extent->start,
1991 ordered_extent->disk_len,
1992 ordered_extent->len,
1993 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001994 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001995 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04001996 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04001997 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1998 ordered_extent->file_offset, ordered_extent->len,
1999 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002000 if (ret < 0) {
2001 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002002 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002003 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002004
Chris Masone6dcd2d2008-07-17 12:53:50 -04002005 add_pending_csums(trans, inode, ordered_extent->file_offset,
2006 &ordered_extent->list);
2007
Josef Bacik6c760c02012-11-09 10:53:21 -05002008 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2009 ret = btrfs_update_inode_fallback(trans, root, inode);
2010 if (ret) { /* -ENOMEM or corruption */
2011 btrfs_abort_transaction(trans, root, ret);
2012 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002013 }
2014 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002015out_unlock:
2016 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2017 ordered_extent->file_offset +
2018 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc21677542009-11-12 09:34:21 +00002019out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002020 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002021 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002022 if (trans)
2023 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002024
Josef Bacik5fd02042012-05-02 14:00:54 -04002025 if (ret)
2026 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2027 ordered_extent->file_offset +
2028 ordered_extent->len - 1, NULL, GFP_NOFS);
2029
2030 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002031 * This needs to be done to make sure anybody waiting knows we are done
2032 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002033 */
2034 btrfs_remove_ordered_extent(inode, ordered_extent);
2035
Chris Masone6dcd2d2008-07-17 12:53:50 -04002036 /* once for us */
2037 btrfs_put_ordered_extent(ordered_extent);
2038 /* once for the tree */
2039 btrfs_put_ordered_extent(ordered_extent);
2040
Josef Bacik5fd02042012-05-02 14:00:54 -04002041 return ret;
2042}
2043
2044static void finish_ordered_fn(struct btrfs_work *work)
2045{
2046 struct btrfs_ordered_extent *ordered_extent;
2047 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2048 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002049}
2050
Christoph Hellwigb2950862008-12-02 09:54:17 -05002051static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04002052 struct extent_state *state, int uptodate)
2053{
Josef Bacik5fd02042012-05-02 14:00:54 -04002054 struct inode *inode = page->mapping->host;
2055 struct btrfs_root *root = BTRFS_I(inode)->root;
2056 struct btrfs_ordered_extent *ordered_extent = NULL;
2057 struct btrfs_workers *workers;
2058
liubo1abe9b82011-03-24 11:18:59 +00002059 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2060
Chris Mason8b62b722009-09-02 16:53:46 -04002061 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002062 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2063 end - start + 1, uptodate))
2064 return 0;
2065
2066 ordered_extent->work.func = finish_ordered_fn;
2067 ordered_extent->work.flags = 0;
2068
Liu Bo83eea1f2012-07-10 05:28:39 -06002069 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002070 workers = &root->fs_info->endio_freespace_worker;
2071 else
2072 workers = &root->fs_info->endio_write_workers;
2073 btrfs_queue_worker(workers, &ordered_extent->work);
2074
2075 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002076}
2077
Chris Masond352ac62008-09-29 15:18:18 -04002078/*
Chris Masond352ac62008-09-29 15:18:18 -04002079 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002080 * if there's a match, we allow the bio to finish. If not, the code in
2081 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002082 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002083static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002084 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002085{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002086 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002087 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002088 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002089 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002090 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002091 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002092 struct btrfs_root *root = BTRFS_I(inode)->root;
2093 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002094
Chris Masond20f7042008-12-08 16:58:54 -05002095 if (PageChecked(page)) {
2096 ClearPageChecked(page);
2097 goto good;
2098 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002099
2100 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002101 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002102
Yan Zheng17d217f2008-12-12 10:03:38 -05002103 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002104 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002105 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2106 GFP_NOFS);
2107 return 0;
2108 }
2109
Yanc2e639f2008-02-04 08:57:25 -05002110 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002111 private = state->private;
2112 ret = 0;
2113 } else {
2114 ret = get_state_private(io_tree, start, &private);
2115 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002116 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002117 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002118 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002119
Chris Masonff79f812007-10-15 16:22:25 -04002120 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2121 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002122 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002123 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002124
Cong Wang7ac687d2011-11-25 23:14:28 +08002125 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002126good:
Chris Mason07157aa2007-08-30 08:50:51 -04002127 return 0;
2128
2129zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002130 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002131 "private %llu\n",
2132 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002133 (unsigned long long)start, csum,
2134 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002135 memset(kaddr + offset, 1, end - start + 1);
2136 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002137 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002138 if (private == 0)
2139 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002140 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002141}
Chris Masonb888db22007-08-27 16:49:44 -04002142
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002143struct delayed_iput {
2144 struct list_head list;
2145 struct inode *inode;
2146};
2147
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002148/* JDM: If this is fs-wide, why can't we add a pointer to
2149 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002150void btrfs_add_delayed_iput(struct inode *inode)
2151{
2152 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2153 struct delayed_iput *delayed;
2154
2155 if (atomic_add_unless(&inode->i_count, -1, 1))
2156 return;
2157
2158 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2159 delayed->inode = inode;
2160
2161 spin_lock(&fs_info->delayed_iput_lock);
2162 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2163 spin_unlock(&fs_info->delayed_iput_lock);
2164}
2165
2166void btrfs_run_delayed_iputs(struct btrfs_root *root)
2167{
2168 LIST_HEAD(list);
2169 struct btrfs_fs_info *fs_info = root->fs_info;
2170 struct delayed_iput *delayed;
2171 int empty;
2172
2173 spin_lock(&fs_info->delayed_iput_lock);
2174 empty = list_empty(&fs_info->delayed_iputs);
2175 spin_unlock(&fs_info->delayed_iput_lock);
2176 if (empty)
2177 return;
2178
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002179 spin_lock(&fs_info->delayed_iput_lock);
2180 list_splice_init(&fs_info->delayed_iputs, &list);
2181 spin_unlock(&fs_info->delayed_iput_lock);
2182
2183 while (!list_empty(&list)) {
2184 delayed = list_entry(list.next, struct delayed_iput, list);
2185 list_del(&delayed->list);
2186 iput(delayed->inode);
2187 kfree(delayed);
2188 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002189}
2190
Yan, Zhengd68fc572010-05-16 10:49:58 -04002191enum btrfs_orphan_cleanup_state {
2192 ORPHAN_CLEANUP_STARTED = 1,
2193 ORPHAN_CLEANUP_DONE = 2,
2194};
2195
2196/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002197 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002198 * files in the subvolume, it removes orphan item and frees block_rsv
2199 * structure.
2200 */
2201void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2202 struct btrfs_root *root)
2203{
Josef Bacik90290e12011-12-02 15:44:12 -05002204 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002205 int ret;
2206
Josef Bacik8a35d952012-05-23 14:26:42 -04002207 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002208 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2209 return;
2210
Josef Bacik90290e12011-12-02 15:44:12 -05002211 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002212 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002213 spin_unlock(&root->orphan_lock);
2214 return;
2215 }
2216
2217 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2218 spin_unlock(&root->orphan_lock);
2219 return;
2220 }
2221
2222 block_rsv = root->orphan_block_rsv;
2223 root->orphan_block_rsv = NULL;
2224 spin_unlock(&root->orphan_lock);
2225
Yan, Zhengd68fc572010-05-16 10:49:58 -04002226 if (root->orphan_item_inserted &&
2227 btrfs_root_refs(&root->root_item) > 0) {
2228 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2229 root->root_key.objectid);
2230 BUG_ON(ret);
2231 root->orphan_item_inserted = 0;
2232 }
2233
Josef Bacik90290e12011-12-02 15:44:12 -05002234 if (block_rsv) {
2235 WARN_ON(block_rsv->size > 0);
2236 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002237 }
2238}
2239
2240/*
Josef Bacik7b128762008-07-24 12:17:14 -04002241 * This creates an orphan entry for the given inode in case something goes
2242 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002243 *
2244 * NOTE: caller of this function should reserve 5 units of metadata for
2245 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002246 */
2247int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2248{
2249 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002250 struct btrfs_block_rsv *block_rsv = NULL;
2251 int reserve = 0;
2252 int insert = 0;
2253 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002254
Yan, Zhengd68fc572010-05-16 10:49:58 -04002255 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06002256 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002257 if (!block_rsv)
2258 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002259 }
2260
Yan, Zhengd68fc572010-05-16 10:49:58 -04002261 spin_lock(&root->orphan_lock);
2262 if (!root->orphan_block_rsv) {
2263 root->orphan_block_rsv = block_rsv;
2264 } else if (block_rsv) {
2265 btrfs_free_block_rsv(root, block_rsv);
2266 block_rsv = NULL;
2267 }
Josef Bacik7b128762008-07-24 12:17:14 -04002268
Josef Bacik8a35d952012-05-23 14:26:42 -04002269 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2270 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002271#if 0
2272 /*
2273 * For proper ENOSPC handling, we should do orphan
2274 * cleanup when mounting. But this introduces backward
2275 * compatibility issue.
2276 */
2277 if (!xchg(&root->orphan_item_inserted, 1))
2278 insert = 2;
2279 else
2280 insert = 1;
2281#endif
2282 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06002283 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002284 }
Josef Bacik7b128762008-07-24 12:17:14 -04002285
Josef Bacik72ac3c02012-05-23 14:13:11 -04002286 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2287 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002288 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002289 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002290
Yan, Zhengd68fc572010-05-16 10:49:58 -04002291 /* grab metadata reservation from transaction handle */
2292 if (reserve) {
2293 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002294 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002295 }
2296
2297 /* insert an orphan item to track this unlinked/truncated file */
2298 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002299 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002300 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002301 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2302 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002303 btrfs_abort_transaction(trans, root, ret);
2304 return ret;
2305 }
2306 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002307 }
2308
2309 /* insert an orphan item to track subvolume contains orphan files */
2310 if (insert >= 2) {
2311 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2312 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002313 if (ret && ret != -EEXIST) {
2314 btrfs_abort_transaction(trans, root, ret);
2315 return ret;
2316 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002317 }
2318 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002319}
2320
2321/*
2322 * We have done the truncate/delete so we can go ahead and remove the orphan
2323 * item for this particular inode.
2324 */
2325int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2326{
2327 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002328 int delete_item = 0;
2329 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002330 int ret = 0;
2331
Yan, Zhengd68fc572010-05-16 10:49:58 -04002332 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002333 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2334 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002335 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002336
Josef Bacik72ac3c02012-05-23 14:13:11 -04002337 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2338 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002339 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002340 spin_unlock(&root->orphan_lock);
2341
2342 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002343 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002344 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002345 }
2346
Josef Bacik8a35d952012-05-23 14:26:42 -04002347 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002348 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002349 atomic_dec(&root->orphan_inodes);
2350 }
Josef Bacik7b128762008-07-24 12:17:14 -04002351
Yan, Zhengd68fc572010-05-16 10:49:58 -04002352 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002353}
2354
2355/*
2356 * this cleans up any orphans that may be left on the list from the last use
2357 * of this root.
2358 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002359int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002360{
2361 struct btrfs_path *path;
2362 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002363 struct btrfs_key key, found_key;
2364 struct btrfs_trans_handle *trans;
2365 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002366 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002367 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2368
Yan, Zhengd68fc572010-05-16 10:49:58 -04002369 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002370 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002371
2372 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002373 if (!path) {
2374 ret = -ENOMEM;
2375 goto out;
2376 }
Josef Bacik7b128762008-07-24 12:17:14 -04002377 path->reada = -1;
2378
2379 key.objectid = BTRFS_ORPHAN_OBJECTID;
2380 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2381 key.offset = (u64)-1;
2382
Josef Bacik7b128762008-07-24 12:17:14 -04002383 while (1) {
2384 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002385 if (ret < 0)
2386 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002387
2388 /*
2389 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002390 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002391 * find the key and see if we have stuff that matches
2392 */
2393 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002394 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002395 if (path->slots[0] == 0)
2396 break;
2397 path->slots[0]--;
2398 }
2399
2400 /* pull out the item */
2401 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002402 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2403
2404 /* make sure the item matches what we want */
2405 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2406 break;
2407 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2408 break;
2409
2410 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002411 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002412
2413 /*
2414 * this is where we are basically btrfs_lookup, without the
2415 * crossing root thing. we store the inode number in the
2416 * offset of the orphan item.
2417 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002418
2419 if (found_key.offset == last_objectid) {
2420 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2421 "stopping orphan cleanup\n");
2422 ret = -EINVAL;
2423 goto out;
2424 }
2425
2426 last_objectid = found_key.offset;
2427
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002428 found_key.objectid = found_key.offset;
2429 found_key.type = BTRFS_INODE_ITEM_KEY;
2430 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002431 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002432 ret = PTR_RET(inode);
2433 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002434 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002435
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002436 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2437 struct btrfs_root *dead_root;
2438 struct btrfs_fs_info *fs_info = root->fs_info;
2439 int is_dead_root = 0;
2440
2441 /*
2442 * this is an orphan in the tree root. Currently these
2443 * could come from 2 sources:
2444 * a) a snapshot deletion in progress
2445 * b) a free space cache inode
2446 * We need to distinguish those two, as the snapshot
2447 * orphan must not get deleted.
2448 * find_dead_roots already ran before us, so if this
2449 * is a snapshot deletion, we should find the root
2450 * in the dead_roots list
2451 */
2452 spin_lock(&fs_info->trans_lock);
2453 list_for_each_entry(dead_root, &fs_info->dead_roots,
2454 root_list) {
2455 if (dead_root->root_key.objectid ==
2456 found_key.objectid) {
2457 is_dead_root = 1;
2458 break;
2459 }
2460 }
2461 spin_unlock(&fs_info->trans_lock);
2462 if (is_dead_root) {
2463 /* prevent this orphan from being found again */
2464 key.offset = found_key.objectid - 1;
2465 continue;
2466 }
2467 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002468 /*
2469 * Inode is already gone but the orphan item is still there,
2470 * kill the orphan item.
2471 */
2472 if (ret == -ESTALE) {
2473 trans = btrfs_start_transaction(root, 1);
2474 if (IS_ERR(trans)) {
2475 ret = PTR_ERR(trans);
2476 goto out;
2477 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002478 printk(KERN_ERR "auto deleting %Lu\n",
2479 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002480 ret = btrfs_del_orphan_item(trans, root,
2481 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002482 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002483 btrfs_end_transaction(trans, root);
2484 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002485 }
Josef Bacik7b128762008-07-24 12:17:14 -04002486
Josef Bacik7b128762008-07-24 12:17:14 -04002487 /*
2488 * add this inode to the orphan list so btrfs_orphan_del does
2489 * the proper thing when we hit it
2490 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002491 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2492 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002493
Josef Bacik7b128762008-07-24 12:17:14 -04002494 /* if we have links, this was a truncate, lets do that */
2495 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002496 if (!S_ISREG(inode->i_mode)) {
2497 WARN_ON(1);
2498 iput(inode);
2499 continue;
2500 }
Josef Bacik7b128762008-07-24 12:17:14 -04002501 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05002502
2503 /* 1 for the orphan item deletion. */
2504 trans = btrfs_start_transaction(root, 1);
2505 if (IS_ERR(trans)) {
2506 ret = PTR_ERR(trans);
2507 goto out;
2508 }
2509 ret = btrfs_orphan_add(trans, inode);
2510 btrfs_end_transaction(trans, root);
2511 if (ret)
2512 goto out;
2513
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002514 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002515 } else {
2516 nr_unlink++;
2517 }
2518
2519 /* this will do delete_inode and everything for us */
2520 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002521 if (ret)
2522 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002523 }
Miao Xie3254c872011-11-10 20:45:05 -05002524 /* release the path since we're done with it */
2525 btrfs_release_path(path);
2526
Yan, Zhengd68fc572010-05-16 10:49:58 -04002527 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2528
2529 if (root->orphan_block_rsv)
2530 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2531 (u64)-1);
2532
2533 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002534 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002535 if (!IS_ERR(trans))
2536 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002537 }
Josef Bacik7b128762008-07-24 12:17:14 -04002538
2539 if (nr_unlink)
2540 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2541 if (nr_truncate)
2542 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002543
2544out:
2545 if (ret)
2546 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2547 btrfs_free_path(path);
2548 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002549}
2550
Chris Masond352ac62008-09-29 15:18:18 -04002551/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002552 * very simple check to peek ahead in the leaf looking for xattrs. If we
2553 * don't find any xattrs, we know there can't be any acls.
2554 *
2555 * slot is the slot the inode is in, objectid is the objectid of the inode
2556 */
2557static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2558 int slot, u64 objectid)
2559{
2560 u32 nritems = btrfs_header_nritems(leaf);
2561 struct btrfs_key found_key;
2562 int scanned = 0;
2563
2564 slot++;
2565 while (slot < nritems) {
2566 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2567
2568 /* we found a different objectid, there must not be acls */
2569 if (found_key.objectid != objectid)
2570 return 0;
2571
2572 /* we found an xattr, assume we've got an acl */
2573 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2574 return 1;
2575
2576 /*
2577 * we found a key greater than an xattr key, there can't
2578 * be any acls later on
2579 */
2580 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2581 return 0;
2582
2583 slot++;
2584 scanned++;
2585
2586 /*
2587 * it goes inode, inode backrefs, xattrs, extents,
2588 * so if there are a ton of hard links to an inode there can
2589 * be a lot of backrefs. Don't waste time searching too hard,
2590 * this is just an optimization
2591 */
2592 if (scanned >= 8)
2593 break;
2594 }
2595 /* we hit the end of the leaf before we found an xattr or
2596 * something larger than an xattr. We have to assume the inode
2597 * has acls
2598 */
2599 return 1;
2600}
2601
2602/*
Chris Masond352ac62008-09-29 15:18:18 -04002603 * read an inode from the btree into the in-memory inode
2604 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002605static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002606{
2607 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002608 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002609 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002610 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002611 struct btrfs_root *root = BTRFS_I(inode)->root;
2612 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002613 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002614 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002615 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002616 bool filled = false;
2617
2618 ret = btrfs_fill_inode(inode, &rdev);
2619 if (!ret)
2620 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002621
2622 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002623 if (!path)
2624 goto make_bad;
2625
Josef Bacikd90c7322011-05-17 09:50:54 -04002626 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002627 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002628
Chris Mason39279cc2007-06-12 06:35:45 -04002629 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002630 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002631 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002632
Chris Mason5f39d392007-10-15 16:14:19 -04002633 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002634
2635 if (filled)
2636 goto cache_acl;
2637
Chris Mason5f39d392007-10-15 16:14:19 -04002638 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2639 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002640 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002641 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08002642 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2643 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04002644 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002645
2646 tspec = btrfs_inode_atime(inode_item);
2647 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2648 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2649
2650 tspec = btrfs_inode_mtime(inode_item);
2651 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2652 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2653
2654 tspec = btrfs_inode_ctime(inode_item);
2655 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2656 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2657
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002658 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002659 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002660 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2661
2662 /*
2663 * If we were modified in the current generation and evicted from memory
2664 * and then re-read we need to do a full sync since we don't have any
2665 * idea about which extents were modified before we were evicted from
2666 * cache.
2667 */
2668 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2669 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2670 &BTRFS_I(inode)->runtime_flags);
2671
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002672 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002673 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002674 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002675 rdev = btrfs_inode_rdev(leaf, inode_item);
2676
Josef Bacikaec74772008-07-24 12:12:38 -04002677 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002678 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002679cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002680 /*
2681 * try to precache a NULL acl entry for files that don't have
2682 * any xattrs or acls
2683 */
Li Zefan33345d012011-04-20 10:31:50 +08002684 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2685 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002686 if (!maybe_acls)
2687 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002688
Chris Mason39279cc2007-06-12 06:35:45 -04002689 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002690
Chris Mason39279cc2007-06-12 06:35:45 -04002691 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002692 case S_IFREG:
2693 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002694 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002695 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002696 inode->i_fop = &btrfs_file_operations;
2697 inode->i_op = &btrfs_file_inode_operations;
2698 break;
2699 case S_IFDIR:
2700 inode->i_fop = &btrfs_dir_file_operations;
2701 if (root == root->fs_info->tree_root)
2702 inode->i_op = &btrfs_dir_ro_inode_operations;
2703 else
2704 inode->i_op = &btrfs_dir_inode_operations;
2705 break;
2706 case S_IFLNK:
2707 inode->i_op = &btrfs_symlink_inode_operations;
2708 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002709 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002710 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002711 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002712 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002713 init_special_inode(inode, inode->i_mode, rdev);
2714 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002715 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002716
2717 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002718 return;
2719
2720make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002721 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002722 make_bad_inode(inode);
2723}
2724
Chris Masond352ac62008-09-29 15:18:18 -04002725/*
2726 * given a leaf and an inode, copy the inode fields into the leaf
2727 */
Chris Masone02119d2008-09-05 16:13:11 -04002728static void fill_inode_item(struct btrfs_trans_handle *trans,
2729 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002730 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002731 struct inode *inode)
2732{
Liu Bo51fab692012-12-27 09:01:21 +00002733 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002734
Liu Bo51fab692012-12-27 09:01:21 +00002735 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04002736
Liu Bo51fab692012-12-27 09:01:21 +00002737 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
2738 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
2739 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
2740 &token);
2741 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
2742 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002743
Liu Bo51fab692012-12-27 09:01:21 +00002744 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
2745 inode->i_atime.tv_sec, &token);
2746 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
2747 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04002748
Liu Bo51fab692012-12-27 09:01:21 +00002749 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
2750 inode->i_mtime.tv_sec, &token);
2751 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
2752 inode->i_mtime.tv_nsec, &token);
2753
2754 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
2755 inode->i_ctime.tv_sec, &token);
2756 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
2757 inode->i_ctime.tv_nsec, &token);
2758
2759 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
2760 &token);
2761 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
2762 &token);
2763 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
2764 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
2765 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
2766 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
2767 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04002768}
2769
Chris Masond352ac62008-09-29 15:18:18 -04002770/*
2771 * copy everything in the in-memory inode into the btree.
2772 */
Chris Mason21151332011-11-10 20:39:08 -05002773static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002774 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002775{
2776 struct btrfs_inode_item *inode_item;
2777 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002778 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002779 int ret;
2780
2781 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002782 if (!path)
2783 return -ENOMEM;
2784
Chris Masonb9473432009-03-13 11:00:37 -04002785 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002786 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2787 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002788 if (ret) {
2789 if (ret > 0)
2790 ret = -ENOENT;
2791 goto failed;
2792 }
2793
Chris Masonb4ce94d2009-02-04 09:25:08 -05002794 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002795 leaf = path->nodes[0];
2796 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002797 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002798
Chris Masone02119d2008-09-05 16:13:11 -04002799 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002800 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002801 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002802 ret = 0;
2803failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002804 btrfs_free_path(path);
2805 return ret;
2806}
2807
Chris Masond352ac62008-09-29 15:18:18 -04002808/*
Chris Mason21151332011-11-10 20:39:08 -05002809 * copy everything in the in-memory inode into the btree.
2810 */
2811noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2812 struct btrfs_root *root, struct inode *inode)
2813{
2814 int ret;
2815
2816 /*
2817 * If the inode is a free space inode, we can deadlock during commit
2818 * if we put it into the delayed code.
2819 *
2820 * The data relocation inode should also be directly updated
2821 * without delay
2822 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002823 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002824 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002825 btrfs_update_root_times(trans, root);
2826
Chris Mason21151332011-11-10 20:39:08 -05002827 ret = btrfs_delayed_update_inode(trans, root, inode);
2828 if (!ret)
2829 btrfs_set_inode_last_trans(trans, inode);
2830 return ret;
2831 }
2832
2833 return btrfs_update_inode_item(trans, root, inode);
2834}
2835
Josef Bacikbe6aef62012-10-22 15:43:12 -04002836noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2837 struct btrfs_root *root,
2838 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05002839{
2840 int ret;
2841
2842 ret = btrfs_update_inode(trans, root, inode);
2843 if (ret == -ENOSPC)
2844 return btrfs_update_inode_item(trans, root, inode);
2845 return ret;
2846}
2847
2848/*
Chris Masond352ac62008-09-29 15:18:18 -04002849 * unlink helper that gets used here in inode.c and in the tree logging
2850 * recovery code. It remove a link in a directory with a given name, and
2851 * also drops the back refs in the inode to the directory
2852 */
Al Viro92986792011-03-04 17:14:37 +00002853static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2854 struct btrfs_root *root,
2855 struct inode *dir, struct inode *inode,
2856 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002857{
2858 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002859 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002860 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002861 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002862 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002863 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002864 u64 ino = btrfs_ino(inode);
2865 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002866
2867 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002868 if (!path) {
2869 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002870 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002871 }
2872
Chris Masonb9473432009-03-13 11:00:37 -04002873 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002874 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002875 name, name_len, -1);
2876 if (IS_ERR(di)) {
2877 ret = PTR_ERR(di);
2878 goto err;
2879 }
2880 if (!di) {
2881 ret = -ENOENT;
2882 goto err;
2883 }
Chris Mason5f39d392007-10-15 16:14:19 -04002884 leaf = path->nodes[0];
2885 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002886 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002887 if (ret)
2888 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002889 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002890
Li Zefan33345d012011-04-20 10:31:50 +08002891 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2892 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002893 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002894 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002895 "inode %llu parent %llu\n", name_len, name,
2896 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002897 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002898 goto err;
2899 }
2900
Miao Xie16cdcec2011-04-22 18:12:22 +08002901 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002902 if (ret) {
2903 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002904 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002905 }
Chris Mason39279cc2007-06-12 06:35:45 -04002906
Chris Masone02119d2008-09-05 16:13:11 -04002907 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002908 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002909 if (ret != 0 && ret != -ENOENT) {
2910 btrfs_abort_transaction(trans, root, ret);
2911 goto err;
2912 }
Chris Masone02119d2008-09-05 16:13:11 -04002913
2914 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2915 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002916 if (ret == -ENOENT)
2917 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002918err:
2919 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002920 if (ret)
2921 goto out;
2922
2923 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002924 inode_inc_iversion(inode);
2925 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002926 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002927 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002928out:
Chris Mason39279cc2007-06-12 06:35:45 -04002929 return ret;
2930}
2931
Al Viro92986792011-03-04 17:14:37 +00002932int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2933 struct btrfs_root *root,
2934 struct inode *dir, struct inode *inode,
2935 const char *name, int name_len)
2936{
2937 int ret;
2938 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2939 if (!ret) {
2940 btrfs_drop_nlink(inode);
2941 ret = btrfs_update_inode(trans, root, inode);
2942 }
2943 return ret;
2944}
2945
2946
Yan, Zhenga22285a2010-05-16 10:48:46 -04002947/* helper to check if there is any shared block in the path */
2948static int check_path_shared(struct btrfs_root *root,
2949 struct btrfs_path *path)
2950{
2951 struct extent_buffer *eb;
2952 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00002953 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002954
2955 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002956 int ret;
2957
Yan, Zhenga22285a2010-05-16 10:48:46 -04002958 if (!path->nodes[level])
2959 break;
2960 eb = path->nodes[level];
2961 if (!btrfs_block_can_be_shared(root, eb))
2962 continue;
2963 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2964 &refs, NULL);
2965 if (refs > 1)
2966 return 1;
2967 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002968 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002969}
2970
2971/*
2972 * helper to start transaction for unlink and rmdir.
2973 *
2974 * unlink and rmdir are special in btrfs, they do not always free space.
2975 * so in enospc case, we should make sure they will free space before
2976 * allowing them to use the global metadata reservation.
2977 */
2978static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2979 struct dentry *dentry)
2980{
2981 struct btrfs_trans_handle *trans;
2982 struct btrfs_root *root = BTRFS_I(dir)->root;
2983 struct btrfs_path *path;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002984 struct btrfs_dir_item *di;
2985 struct inode *inode = dentry->d_inode;
2986 u64 index;
2987 int check_link = 1;
2988 int err = -ENOSPC;
2989 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002990 u64 ino = btrfs_ino(inode);
2991 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002992
Josef Bacike70bea52011-10-11 14:18:24 -04002993 /*
2994 * 1 for the possible orphan item
2995 * 1 for the dir item
2996 * 1 for the dir index
2997 * 1 for the inode ref
2998 * 1 for the inode ref in the tree log
2999 * 2 for the dir entries in the log
3000 * 1 for the inode
3001 */
3002 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003003 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3004 return trans;
3005
Li Zefan33345d012011-04-20 10:31:50 +08003006 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003007 return ERR_PTR(-ENOSPC);
3008
3009 /* check if there is someone else holds reference */
3010 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
3011 return ERR_PTR(-ENOSPC);
3012
3013 if (atomic_read(&inode->i_count) > 2)
3014 return ERR_PTR(-ENOSPC);
3015
3016 if (xchg(&root->fs_info->enospc_unlink, 1))
3017 return ERR_PTR(-ENOSPC);
3018
3019 path = btrfs_alloc_path();
3020 if (!path) {
3021 root->fs_info->enospc_unlink = 0;
3022 return ERR_PTR(-ENOMEM);
3023 }
3024
Josef Bacik3880a1b2011-10-14 14:46:51 -04003025 /* 1 for the orphan item */
3026 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003027 if (IS_ERR(trans)) {
3028 btrfs_free_path(path);
3029 root->fs_info->enospc_unlink = 0;
3030 return trans;
3031 }
3032
3033 path->skip_locking = 1;
3034 path->search_commit_root = 1;
3035
3036 ret = btrfs_lookup_inode(trans, root, path,
3037 &BTRFS_I(dir)->location, 0);
3038 if (ret < 0) {
3039 err = ret;
3040 goto out;
3041 }
3042 if (ret == 0) {
3043 if (check_path_shared(root, path))
3044 goto out;
3045 } else {
3046 check_link = 0;
3047 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003048 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003049
3050 ret = btrfs_lookup_inode(trans, root, path,
3051 &BTRFS_I(inode)->location, 0);
3052 if (ret < 0) {
3053 err = ret;
3054 goto out;
3055 }
3056 if (ret == 0) {
3057 if (check_path_shared(root, path))
3058 goto out;
3059 } else {
3060 check_link = 0;
3061 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003062 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003063
3064 if (ret == 0 && S_ISREG(inode->i_mode)) {
3065 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08003066 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003067 if (ret < 0) {
3068 err = ret;
3069 goto out;
3070 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003071 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003072 if (check_path_shared(root, path))
3073 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003074 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003075 }
3076
3077 if (!check_link) {
3078 err = 0;
3079 goto out;
3080 }
3081
Li Zefan33345d012011-04-20 10:31:50 +08003082 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003083 dentry->d_name.name, dentry->d_name.len, 0);
3084 if (IS_ERR(di)) {
3085 err = PTR_ERR(di);
3086 goto out;
3087 }
3088 if (di) {
3089 if (check_path_shared(root, path))
3090 goto out;
3091 } else {
3092 err = 0;
3093 goto out;
3094 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003095 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003096
Mark Fashehf1863732012-08-08 11:32:27 -07003097 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3098 dentry->d_name.len, ino, dir_ino, 0,
3099 &index);
3100 if (ret) {
3101 err = ret;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003102 goto out;
3103 }
Mark Fashehf1863732012-08-08 11:32:27 -07003104
Yan, Zhenga22285a2010-05-16 10:48:46 -04003105 if (check_path_shared(root, path))
3106 goto out;
Mark Fashehf1863732012-08-08 11:32:27 -07003107
David Sterbab3b4aa72011-04-21 01:20:15 +02003108 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003109
Miao Xie16cdcec2011-04-22 18:12:22 +08003110 /*
3111 * This is a commit root search, if we can lookup inode item and other
3112 * relative items in the commit root, it means the transaction of
3113 * dir/file creation has been committed, and the dir index item that we
3114 * delay to insert has also been inserted into the commit root. So
3115 * we needn't worry about the delayed insertion of the dir index item
3116 * here.
3117 */
Li Zefan33345d012011-04-20 10:31:50 +08003118 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003119 dentry->d_name.name, dentry->d_name.len, 0);
3120 if (IS_ERR(di)) {
3121 err = PTR_ERR(di);
3122 goto out;
3123 }
3124 BUG_ON(ret == -ENOENT);
3125 if (check_path_shared(root, path))
3126 goto out;
3127
3128 err = 0;
3129out:
3130 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003131 /* Migrate the orphan reservation over */
3132 if (!err)
3133 err = btrfs_block_rsv_migrate(trans->block_rsv,
3134 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003135 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003136
Yan, Zhenga22285a2010-05-16 10:48:46 -04003137 if (err) {
3138 btrfs_end_transaction(trans, root);
3139 root->fs_info->enospc_unlink = 0;
3140 return ERR_PTR(err);
3141 }
3142
3143 trans->block_rsv = &root->fs_info->global_block_rsv;
3144 return trans;
3145}
3146
3147static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3148 struct btrfs_root *root)
3149{
Miao Xie66d8f3d2012-09-06 04:02:28 -06003150 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003151 btrfs_block_rsv_release(root, trans->block_rsv,
3152 trans->bytes_reserved);
3153 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003154 BUG_ON(!root->fs_info->enospc_unlink);
3155 root->fs_info->enospc_unlink = 0;
3156 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003157 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003158}
3159
Chris Mason39279cc2007-06-12 06:35:45 -04003160static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3161{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003162 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003163 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003164 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003165 int ret;
3166
Yan, Zhenga22285a2010-05-16 10:48:46 -04003167 trans = __unlink_start_trans(dir, dentry);
3168 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003169 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003170
Chris Mason12fcfd22009-03-24 10:24:20 -04003171 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3172
Chris Masone02119d2008-09-05 16:13:11 -04003173 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3174 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003175 if (ret)
3176 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003177
Yan, Zhenga22285a2010-05-16 10:48:46 -04003178 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003179 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003180 if (ret)
3181 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003182 }
Josef Bacik7b128762008-07-24 12:17:14 -04003183
Tsutomu Itohb5324022011-07-19 07:27:20 +00003184out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003185 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003186 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003187 return ret;
3188}
3189
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003190int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3191 struct btrfs_root *root,
3192 struct inode *dir, u64 objectid,
3193 const char *name, int name_len)
3194{
3195 struct btrfs_path *path;
3196 struct extent_buffer *leaf;
3197 struct btrfs_dir_item *di;
3198 struct btrfs_key key;
3199 u64 index;
3200 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003201 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003202
3203 path = btrfs_alloc_path();
3204 if (!path)
3205 return -ENOMEM;
3206
Li Zefan33345d012011-04-20 10:31:50 +08003207 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003208 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003209 if (IS_ERR_OR_NULL(di)) {
3210 if (!di)
3211 ret = -ENOENT;
3212 else
3213 ret = PTR_ERR(di);
3214 goto out;
3215 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003216
3217 leaf = path->nodes[0];
3218 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3219 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3220 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003221 if (ret) {
3222 btrfs_abort_transaction(trans, root, ret);
3223 goto out;
3224 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003225 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003226
3227 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3228 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003229 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003230 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003231 if (ret != -ENOENT) {
3232 btrfs_abort_transaction(trans, root, ret);
3233 goto out;
3234 }
Li Zefan33345d012011-04-20 10:31:50 +08003235 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003236 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003237 if (IS_ERR_OR_NULL(di)) {
3238 if (!di)
3239 ret = -ENOENT;
3240 else
3241 ret = PTR_ERR(di);
3242 btrfs_abort_transaction(trans, root, ret);
3243 goto out;
3244 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003245
3246 leaf = path->nodes[0];
3247 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003248 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003249 index = key.offset;
3250 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003251 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003252
Miao Xie16cdcec2011-04-22 18:12:22 +08003253 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003254 if (ret) {
3255 btrfs_abort_transaction(trans, root, ret);
3256 goto out;
3257 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003258
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003259 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003260 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003261 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003262 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003263 if (ret)
3264 btrfs_abort_transaction(trans, root, ret);
3265out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003266 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003267 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003268}
3269
Chris Mason39279cc2007-06-12 06:35:45 -04003270static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3271{
3272 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003273 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003274 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003275 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04003276
David Sterbab3ae2442012-09-13 16:04:34 -06003277 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04003278 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06003279 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3280 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04003281
Yan, Zhenga22285a2010-05-16 10:48:46 -04003282 trans = __unlink_start_trans(dir, dentry);
3283 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003284 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003285
Li Zefan33345d012011-04-20 10:31:50 +08003286 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003287 err = btrfs_unlink_subvol(trans, root, dir,
3288 BTRFS_I(inode)->location.objectid,
3289 dentry->d_name.name,
3290 dentry->d_name.len);
3291 goto out;
3292 }
3293
Josef Bacik7b128762008-07-24 12:17:14 -04003294 err = btrfs_orphan_add(trans, inode);
3295 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003296 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003297
Chris Mason39279cc2007-06-12 06:35:45 -04003298 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003299 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3300 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003301 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003302 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003303out:
Yan, Zhenga22285a2010-05-16 10:48:46 -04003304 __unlink_end_trans(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00003305 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05003306
Chris Mason39279cc2007-06-12 06:35:45 -04003307 return err;
3308}
3309
Chris Mason323ac952008-10-01 19:05:46 -04003310/*
Chris Mason39279cc2007-06-12 06:35:45 -04003311 * this can truncate away extent items, csum items and directory items.
3312 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003313 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003314 *
3315 * csum items that cross the new i_size are truncated to the new size
3316 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003317 *
3318 * min_type is the minimum key type to truncate down to. If set to 0, this
3319 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003320 */
Yan, Zheng80825102009-11-12 09:35:36 +00003321int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3322 struct btrfs_root *root,
3323 struct inode *inode,
3324 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003325{
Chris Mason39279cc2007-06-12 06:35:45 -04003326 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003327 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003328 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003329 struct btrfs_key key;
3330 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003331 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003332 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003333 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003334 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003335 u64 mask = root->sectorsize - 1;
3336 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003337 int found_extent;
3338 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003339 int pending_del_nr = 0;
3340 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003341 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003342 int ret;
3343 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003344 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003345
3346 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003347
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003348 path = btrfs_alloc_path();
3349 if (!path)
3350 return -ENOMEM;
3351 path->reada = -1;
3352
Josef Bacik5dc562c2012-08-17 13:14:17 -04003353 /*
3354 * We want to drop from the next block forward in case this new size is
3355 * not block aligned since we will be keeping the last block of the
3356 * extent just the way it is.
3357 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003358 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003359 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003360
Miao Xie16cdcec2011-04-22 18:12:22 +08003361 /*
3362 * This function is also used to drop the items in the log tree before
3363 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3364 * it is used to drop the loged items. So we shouldn't kill the delayed
3365 * items.
3366 */
3367 if (min_type == 0 && root == BTRFS_I(inode)->root)
3368 btrfs_kill_delayed_inode_items(inode);
3369
Li Zefan33345d012011-04-20 10:31:50 +08003370 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003371 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003372 key.type = (u8)-1;
3373
Chris Mason85e21ba2008-01-29 15:11:36 -05003374search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003375 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003376 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003377 if (ret < 0) {
3378 err = ret;
3379 goto out;
3380 }
Chris Masond3977122009-01-05 21:25:51 -05003381
Chris Mason85e21ba2008-01-29 15:11:36 -05003382 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003383 /* there are no items in the tree for us to truncate, we're
3384 * done
3385 */
Yan, Zheng80825102009-11-12 09:35:36 +00003386 if (path->slots[0] == 0)
3387 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003388 path->slots[0]--;
3389 }
3390
Chris Masond3977122009-01-05 21:25:51 -05003391 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003392 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003393 leaf = path->nodes[0];
3394 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3395 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003396
Li Zefan33345d012011-04-20 10:31:50 +08003397 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003398 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003399
Chris Mason85e21ba2008-01-29 15:11:36 -05003400 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003401 break;
3402
Chris Mason5f39d392007-10-15 16:14:19 -04003403 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003404 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003405 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003406 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003407 extent_type = btrfs_file_extent_type(leaf, fi);
3408 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003409 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003410 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003411 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003412 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003413 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003414 }
Yan008630c2007-11-07 13:31:09 -05003415 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003416 }
Yan, Zheng80825102009-11-12 09:35:36 +00003417 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003418 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003419 } else {
3420 if (item_end < new_size)
3421 break;
3422 if (found_key.offset >= new_size)
3423 del_item = 1;
3424 else
3425 del_item = 0;
3426 }
Chris Mason39279cc2007-06-12 06:35:45 -04003427 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003428 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003429 if (found_type != BTRFS_EXTENT_DATA_KEY)
3430 goto delete;
3431
3432 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003433 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003434 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003435 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003436 u64 orig_num_bytes =
3437 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003438 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003439 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -05003440 extent_num_bytes = extent_num_bytes &
3441 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003442 btrfs_set_file_extent_num_bytes(leaf, fi,
3443 extent_num_bytes);
3444 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003445 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003446 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003447 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003448 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003449 } else {
Chris Masondb945352007-10-15 16:15:53 -04003450 extent_num_bytes =
3451 btrfs_file_extent_disk_num_bytes(leaf,
3452 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003453 extent_offset = found_key.offset -
3454 btrfs_file_extent_offset(leaf, fi);
3455
Chris Mason39279cc2007-06-12 06:35:45 -04003456 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003457 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003458 if (extent_start != 0) {
3459 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003460 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003461 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003462 }
3463 }
Chris Mason90692182008-02-08 13:49:28 -05003464 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003465 /*
3466 * we can't truncate inline items that have had
3467 * special encodings
3468 */
3469 if (!del_item &&
3470 btrfs_file_extent_compression(leaf, fi) == 0 &&
3471 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3472 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003473 u32 size = new_size - found_key.offset;
3474
3475 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003476 inode_sub_bytes(inode, item_end + 1 -
3477 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003478 }
3479 size =
3480 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003481 btrfs_truncate_item(trans, root, path,
3482 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003483 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003484 inode_sub_bytes(inode, item_end + 1 -
3485 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003486 }
Chris Mason39279cc2007-06-12 06:35:45 -04003487 }
Chris Mason179e29e2007-11-01 11:28:41 -04003488delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003489 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003490 if (!pending_del_nr) {
3491 /* no pending yet, add ourselves */
3492 pending_del_slot = path->slots[0];
3493 pending_del_nr = 1;
3494 } else if (pending_del_nr &&
3495 path->slots[0] + 1 == pending_del_slot) {
3496 /* hop on the pending chunk */
3497 pending_del_nr++;
3498 pending_del_slot = path->slots[0];
3499 } else {
Chris Masond3977122009-01-05 21:25:51 -05003500 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003501 }
Chris Mason39279cc2007-06-12 06:35:45 -04003502 } else {
3503 break;
3504 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003505 if (found_extent && (root->ref_cows ||
3506 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003507 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003508 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003509 extent_num_bytes, 0,
3510 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003511 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003512 BUG_ON(ret);
3513 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003514
Yan, Zheng80825102009-11-12 09:35:36 +00003515 if (found_type == BTRFS_INODE_ITEM_KEY)
3516 break;
3517
3518 if (path->slots[0] == 0 ||
3519 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003520 if (pending_del_nr) {
3521 ret = btrfs_del_items(trans, root, path,
3522 pending_del_slot,
3523 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003524 if (ret) {
3525 btrfs_abort_transaction(trans,
3526 root, ret);
3527 goto error;
3528 }
Yan, Zheng80825102009-11-12 09:35:36 +00003529 pending_del_nr = 0;
3530 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003531 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003532 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003533 } else {
3534 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003535 }
Chris Mason39279cc2007-06-12 06:35:45 -04003536 }
Yan, Zheng80825102009-11-12 09:35:36 +00003537out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003538 if (pending_del_nr) {
3539 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3540 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003541 if (ret)
3542 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003543 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003544error:
Chris Mason39279cc2007-06-12 06:35:45 -04003545 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003546 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003547}
3548
Chris Masona52d9a82007-08-27 16:49:44 -04003549/*
Josef Bacik2aaa6652012-08-29 14:27:18 -04003550 * btrfs_truncate_page - read, zero a chunk and write a page
3551 * @inode - inode that we're zeroing
3552 * @from - the offset to start zeroing
3553 * @len - the length to zero, 0 to zero the entire range respective to the
3554 * offset
3555 * @front - zero up to the offset instead of from the offset on
3556 *
3557 * This will find the page for the "from" offset and cow the page and zero the
3558 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04003559 */
Josef Bacik2aaa6652012-08-29 14:27:18 -04003560int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3561 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04003562{
Josef Bacik2aaa6652012-08-29 14:27:18 -04003563 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04003564 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003565 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3566 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003567 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003568 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003569 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003570 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3571 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3572 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003573 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003574 int ret = 0;
3575 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003576 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003577
Josef Bacik2aaa6652012-08-29 14:27:18 -04003578 if ((offset & (blocksize - 1)) == 0 &&
3579 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04003580 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003581 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003582 if (ret)
3583 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003584
Chris Mason211c17f2008-05-15 09:13:45 -04003585again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003586 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003587 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003588 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Miao Xieac6a2b32012-12-05 10:56:13 +00003589 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04003590 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003591 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003592
3593 page_start = page_offset(page);
3594 page_end = page_start + PAGE_CACHE_SIZE - 1;
3595
Chris Masona52d9a82007-08-27 16:49:44 -04003596 if (!PageUptodate(page)) {
3597 ret = btrfs_readpage(NULL, page);
3598 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003599 if (page->mapping != mapping) {
3600 unlock_page(page);
3601 page_cache_release(page);
3602 goto again;
3603 }
Chris Masona52d9a82007-08-27 16:49:44 -04003604 if (!PageUptodate(page)) {
3605 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003606 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003607 }
3608 }
Chris Mason211c17f2008-05-15 09:13:45 -04003609 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003610
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003611 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003612 set_page_extent_mapped(page);
3613
3614 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3615 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003616 unlock_extent_cached(io_tree, page_start, page_end,
3617 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003618 unlock_page(page);
3619 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003620 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003621 btrfs_put_ordered_extent(ordered);
3622 goto again;
3623 }
3624
Josef Bacik2ac55d42010-02-03 19:33:23 +00003625 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06003626 EXTENT_DIRTY | EXTENT_DELALLOC |
3627 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003628 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003629
Josef Bacik2ac55d42010-02-03 19:33:23 +00003630 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3631 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003632 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003633 unlock_extent_cached(io_tree, page_start, page_end,
3634 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003635 goto out_unlock;
3636 }
3637
Chris Masone6dcd2d2008-07-17 12:53:50 -04003638 if (offset != PAGE_CACHE_SIZE) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04003639 if (!len)
3640 len = PAGE_CACHE_SIZE - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003641 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04003642 if (front)
3643 memset(kaddr, 0, offset);
3644 else
3645 memset(kaddr + offset, 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003646 flush_dcache_page(page);
3647 kunmap(page);
3648 }
Chris Mason247e7432008-07-17 12:53:51 -04003649 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003650 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003651 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3652 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003653
Chris Mason89642222008-07-24 09:41:53 -04003654out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003655 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003656 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003657 unlock_page(page);
3658 page_cache_release(page);
3659out:
3660 return ret;
3661}
3662
Josef Bacik695a0d02011-03-04 15:46:53 -05003663/*
3664 * This function puts in dummy file extents for the area we're creating a hole
3665 * for. So if we are truncating this file to a larger size we need to insert
3666 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3667 * the range between oldsize and size
3668 */
Josef Bacika41ad392011-01-31 15:30:16 -05003669int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003670{
3671 struct btrfs_trans_handle *trans;
3672 struct btrfs_root *root = BTRFS_I(inode)->root;
3673 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003674 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003675 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003676 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003677 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003678 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003679 u64 block_end = (size + mask) & ~mask;
3680 u64 last_byte;
3681 u64 cur_offset;
3682 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003683 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003684
3685 if (size <= hole_start)
3686 return 0;
3687
Yan Zheng9036c102008-10-30 14:19:41 -04003688 while (1) {
3689 struct btrfs_ordered_extent *ordered;
3690 btrfs_wait_ordered_range(inode, hole_start,
3691 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003692 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003693 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003694 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3695 if (!ordered)
3696 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003697 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3698 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003699 btrfs_put_ordered_extent(ordered);
3700 }
3701
Yan Zheng9036c102008-10-30 14:19:41 -04003702 cur_offset = hole_start;
3703 while (1) {
3704 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3705 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003706 if (IS_ERR(em)) {
3707 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00003708 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003709 break;
3710 }
Yan Zheng9036c102008-10-30 14:19:41 -04003711 last_byte = min(extent_map_end(em), block_end);
3712 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003713 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003714 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04003715 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003716
Miao Xie36423202011-12-14 20:12:02 -05003717 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003718 if (IS_ERR(trans)) {
3719 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003720 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003721 }
Yan, Zheng80825102009-11-12 09:35:36 +00003722
Josef Bacik5dc562c2012-08-17 13:14:17 -04003723 err = btrfs_drop_extents(trans, root, inode,
3724 cur_offset,
Josef Bacik26714852012-08-29 12:24:27 -04003725 cur_offset + hole_size, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003726 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003727 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003728 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003729 break;
Miao Xie5b397372011-09-11 10:52:24 -04003730 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003731
Yan Zheng9036c102008-10-30 14:19:41 -04003732 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003733 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003734 0, hole_size, 0, hole_size,
3735 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003736 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003737 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003738 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003739 break;
Miao Xie5b397372011-09-11 10:52:24 -04003740 }
Yan, Zheng80825102009-11-12 09:35:36 +00003741
Josef Bacik5dc562c2012-08-17 13:14:17 -04003742 btrfs_drop_extent_cache(inode, cur_offset,
3743 cur_offset + hole_size - 1, 0);
3744 hole_em = alloc_extent_map();
3745 if (!hole_em) {
3746 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3747 &BTRFS_I(inode)->runtime_flags);
3748 goto next;
3749 }
3750 hole_em->start = cur_offset;
3751 hole_em->len = hole_size;
3752 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003753
Josef Bacik5dc562c2012-08-17 13:14:17 -04003754 hole_em->block_start = EXTENT_MAP_HOLE;
3755 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05003756 hole_em->orig_block_len = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003757 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3758 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3759 hole_em->generation = trans->transid;
3760
3761 while (1) {
3762 write_lock(&em_tree->lock);
3763 err = add_extent_mapping(em_tree, hole_em);
3764 if (!err)
3765 list_move(&hole_em->list,
3766 &em_tree->modified_extents);
3767 write_unlock(&em_tree->lock);
3768 if (err != -EEXIST)
3769 break;
3770 btrfs_drop_extent_cache(inode, cur_offset,
3771 cur_offset +
3772 hole_size - 1, 0);
3773 }
3774 free_extent_map(hole_em);
3775next:
Miao Xie36423202011-12-14 20:12:02 -05003776 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003777 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003778 }
3779 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003780 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003781 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003782 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003783 break;
3784 }
3785
Yan, Zhenga22285a2010-05-16 10:48:46 -04003786 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003787 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3788 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003789 return err;
3790}
3791
Eric Sandeen3972f262013-01-12 02:57:22 +00003792static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00003793{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003794 struct btrfs_root *root = BTRFS_I(inode)->root;
3795 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003796 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00003797 loff_t newsize = attr->ia_size;
3798 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00003799 int ret;
3800
Josef Bacika41ad392011-01-31 15:30:16 -05003801 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003802 return 0;
3803
Eric Sandeen3972f262013-01-12 02:57:22 +00003804 /*
3805 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3806 * special case where we need to update the times despite not having
3807 * these flags set. For all other operations the VFS set these flags
3808 * explicitly if it wants a timestamp update.
3809 */
3810 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
3811 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
3812
Josef Bacika41ad392011-01-31 15:30:16 -05003813 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003814 truncate_pagecache(inode, oldsize, newsize);
3815 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003816 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003817 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003818
Miao Xief4a2f4c2011-12-14 20:12:01 -05003819 trans = btrfs_start_transaction(root, 1);
3820 if (IS_ERR(trans))
3821 return PTR_ERR(trans);
3822
3823 i_size_write(inode, newsize);
3824 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3825 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003826 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003827 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003828
Josef Bacika41ad392011-01-31 15:30:16 -05003829 /*
3830 * We're truncating a file that used to have good data down to
3831 * zero. Make sure it gets into the ordered flush list so that
3832 * any new writes get down to disk quickly.
3833 */
3834 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003835 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3836 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003837
Josef Bacikf3fe8202013-01-07 17:03:21 -05003838 /*
3839 * 1 for the orphan item we're going to add
3840 * 1 for the orphan item deletion.
3841 */
3842 trans = btrfs_start_transaction(root, 2);
3843 if (IS_ERR(trans))
3844 return PTR_ERR(trans);
3845
3846 /*
3847 * We need to do this in case we fail at _any_ point during the
3848 * actual truncate. Once we do the truncate_setsize we could
3849 * invalidate pages which forces any outstanding ordered io to
3850 * be instantly completed which will give us extents that need
3851 * to be truncated. If we fail to get an orphan inode down we
3852 * could have left over extents that were never meant to live,
3853 * so we need to garuntee from this point on that everything
3854 * will be consistent.
3855 */
3856 ret = btrfs_orphan_add(trans, inode);
3857 btrfs_end_transaction(trans, root);
3858 if (ret)
3859 return ret;
3860
Josef Bacika41ad392011-01-31 15:30:16 -05003861 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3862 truncate_setsize(inode, newsize);
3863 ret = btrfs_truncate(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003864 if (ret && inode->i_nlink)
3865 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003866 }
3867
Josef Bacika41ad392011-01-31 15:30:16 -05003868 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003869}
3870
Chris Mason39279cc2007-06-12 06:35:45 -04003871static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3872{
3873 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003874 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003875 int err;
3876
Li Zefanb83cc962010-12-20 16:04:08 +08003877 if (btrfs_root_readonly(root))
3878 return -EROFS;
3879
Chris Mason39279cc2007-06-12 06:35:45 -04003880 err = inode_change_ok(inode, attr);
3881 if (err)
3882 return err;
3883
Chris Mason5a3f23d2009-03-31 13:27:11 -04003884 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00003885 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00003886 if (err)
3887 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003888 }
Yan Zheng9036c102008-10-30 14:19:41 -04003889
Christoph Hellwig10257742010-06-04 11:30:02 +02003890 if (attr->ia_valid) {
3891 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003892 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003893 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003894
Josef Bacik22c44fe2011-11-30 10:45:38 -05003895 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003896 err = btrfs_acl_chmod(inode);
3897 }
3898
Chris Mason39279cc2007-06-12 06:35:45 -04003899 return err;
3900}
Chris Mason61295eb2008-01-14 16:24:38 -05003901
Al Virobd555972010-06-07 11:35:40 -04003902void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003903{
3904 struct btrfs_trans_handle *trans;
3905 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003906 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003907 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003908 int ret;
3909
liubo1abe9b82011-03-24 11:18:59 +00003910 trace_btrfs_inode_evict(inode);
3911
Chris Mason39279cc2007-06-12 06:35:45 -04003912 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003913 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003914 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003915 goto no_delete;
3916
Chris Mason39279cc2007-06-12 06:35:45 -04003917 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003918 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003919 goto no_delete;
3920 }
Al Virobd555972010-06-07 11:35:40 -04003921 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003922 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003923
Yan, Zhengc71bf092009-11-12 09:34:40 +00003924 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003925 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003926 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003927 goto no_delete;
3928 }
3929
Yan, Zheng76dda932009-09-21 16:00:26 -04003930 if (inode->i_nlink > 0) {
3931 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3932 goto no_delete;
3933 }
3934
Miao Xie0e8c36a2012-12-19 06:59:51 +00003935 ret = btrfs_commit_inode_delayed_inode(inode);
3936 if (ret) {
3937 btrfs_orphan_del(NULL, inode);
3938 goto no_delete;
3939 }
3940
Miao Xie66d8f3d2012-09-06 04:02:28 -06003941 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04003942 if (!rsv) {
3943 btrfs_orphan_del(NULL, inode);
3944 goto no_delete;
3945 }
Josef Bacik4a338542011-08-29 11:01:31 -04003946 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003947 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003948 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003949
Chris Masondbe674a2008-07-17 12:54:05 -04003950 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003951
Josef Bacik4289a662011-08-05 13:22:24 -04003952 /*
Miao Xie8407aa42012-09-07 01:43:32 -06003953 * This is a bit simpler than btrfs_truncate since we've already
3954 * reserved our space for our orphan item in the unlink, so we just
3955 * need to reserve some slack space in case we add bytes and update
3956 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04003957 */
Yan, Zheng80825102009-11-12 09:35:36 +00003958 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00003959 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3960 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00003961
Josef Bacik726c35f2011-09-26 15:46:06 -04003962 /*
3963 * Try and steal from the global reserve since we will
3964 * likely not use this space anyway, we want to try as
3965 * hard as possible to get this to work.
3966 */
3967 if (ret)
3968 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3969
Yan, Zhengd68fc572010-05-16 10:49:58 -04003970 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003971 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003972 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003973 btrfs_orphan_del(NULL, inode);
3974 btrfs_free_block_rsv(root, rsv);
3975 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003976 }
3977
Miao Xie0e8c36a2012-12-19 06:59:51 +00003978 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04003979 if (IS_ERR(trans)) {
3980 btrfs_orphan_del(NULL, inode);
3981 btrfs_free_block_rsv(root, rsv);
3982 goto no_delete;
3983 }
3984
3985 trans->block_rsv = rsv;
3986
Yan, Zhengd68fc572010-05-16 10:49:58 -04003987 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04003988 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00003989 break;
3990
Miao Xie8407aa42012-09-07 01:43:32 -06003991 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003992 btrfs_end_transaction(trans, root);
3993 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00003994 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04003995 }
3996
Josef Bacik4289a662011-08-05 13:22:24 -04003997 btrfs_free_block_rsv(root, rsv);
3998
Yan, Zheng80825102009-11-12 09:35:36 +00003999 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04004000 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00004001 ret = btrfs_orphan_del(trans, inode);
4002 BUG_ON(ret);
4003 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004004
Josef Bacik4289a662011-08-05 13:22:24 -04004005 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08004006 if (!(root == root->fs_info->tree_root ||
4007 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08004008 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08004009
Chris Mason54aa1f42007-06-22 14:16:25 -04004010 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004011 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004012no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02004013 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00004014 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004015}
4016
4017/*
4018 * this returns the key found in the dir entry in the location pointer.
4019 * If no dir entries were found, location->objectid is 0.
4020 */
4021static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4022 struct btrfs_key *location)
4023{
4024 const char *name = dentry->d_name.name;
4025 int namelen = dentry->d_name.len;
4026 struct btrfs_dir_item *di;
4027 struct btrfs_path *path;
4028 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04004029 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004030
4031 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004032 if (!path)
4033 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05004034
Li Zefan33345d012011-04-20 10:31:50 +08004035 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04004036 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04004037 if (IS_ERR(di))
4038 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05004039
David Sterbac7040052011-04-19 18:00:01 +02004040 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05004041 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05004042
Chris Mason5f39d392007-10-15 16:14:19 -04004043 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04004044out:
Chris Mason39279cc2007-06-12 06:35:45 -04004045 btrfs_free_path(path);
4046 return ret;
Chris Mason39544012007-12-12 14:38:19 -05004047out_err:
4048 location->objectid = 0;
4049 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004050}
4051
4052/*
4053 * when we hit a tree root in a directory, the btrfs part of the inode
4054 * needs to be changed to reflect the root directory of the tree root. This
4055 * is kind of like crossing a mount point.
4056 */
4057static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004058 struct inode *dir,
4059 struct dentry *dentry,
4060 struct btrfs_key *location,
4061 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04004062{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004063 struct btrfs_path *path;
4064 struct btrfs_root *new_root;
4065 struct btrfs_root_ref *ref;
4066 struct extent_buffer *leaf;
4067 int ret;
4068 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004069
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004070 path = btrfs_alloc_path();
4071 if (!path) {
4072 err = -ENOMEM;
4073 goto out;
4074 }
Chris Mason39279cc2007-06-12 06:35:45 -04004075
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004076 err = -ENOENT;
4077 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4078 BTRFS_I(dir)->root->root_key.objectid,
4079 location->objectid);
4080 if (ret) {
4081 if (ret < 0)
4082 err = ret;
4083 goto out;
4084 }
Chris Mason39279cc2007-06-12 06:35:45 -04004085
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004086 leaf = path->nodes[0];
4087 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08004088 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004089 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4090 goto out;
4091
4092 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4093 (unsigned long)(ref + 1),
4094 dentry->d_name.len);
4095 if (ret)
4096 goto out;
4097
David Sterbab3b4aa72011-04-21 01:20:15 +02004098 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004099
4100 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4101 if (IS_ERR(new_root)) {
4102 err = PTR_ERR(new_root);
4103 goto out;
4104 }
4105
4106 if (btrfs_root_refs(&new_root->root_item) == 0) {
4107 err = -ENOENT;
4108 goto out;
4109 }
4110
4111 *sub_root = new_root;
4112 location->objectid = btrfs_root_dirid(&new_root->root_item);
4113 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004114 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115 err = 0;
4116out:
4117 btrfs_free_path(path);
4118 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004119}
4120
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004121static void inode_tree_add(struct inode *inode)
4122{
4123 struct btrfs_root *root = BTRFS_I(inode)->root;
4124 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004125 struct rb_node **p;
4126 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004127 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004128again:
4129 p = &root->inode_tree.rb_node;
4130 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004131
Al Viro1d3382c2010-10-23 15:19:20 -04004132 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004133 return;
4134
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004135 spin_lock(&root->inode_lock);
4136 while (*p) {
4137 parent = *p;
4138 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4139
Li Zefan33345d012011-04-20 10:31:50 +08004140 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004141 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004142 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004143 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004144 else {
4145 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004146 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004147 rb_erase(parent, &root->inode_tree);
4148 RB_CLEAR_NODE(parent);
4149 spin_unlock(&root->inode_lock);
4150 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004151 }
4152 }
4153 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4154 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4155 spin_unlock(&root->inode_lock);
4156}
4157
4158static void inode_tree_del(struct inode *inode)
4159{
4160 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004161 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004162
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004163 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004164 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004165 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004166 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004167 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004168 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004169 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004170
Josef Bacik0af3d002010-06-21 14:48:16 -04004171 /*
4172 * Free space cache has inodes in the tree root, but the tree root has a
4173 * root_refs of 0, so this could end up dropping the tree root as a
4174 * snapshot, so we need the extra !root->fs_info->tree_root check to
4175 * make sure we don't drop it.
4176 */
4177 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4178 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004179 synchronize_srcu(&root->fs_info->subvol_srcu);
4180 spin_lock(&root->inode_lock);
4181 empty = RB_EMPTY_ROOT(&root->inode_tree);
4182 spin_unlock(&root->inode_lock);
4183 if (empty)
4184 btrfs_add_dead_root(root);
4185 }
4186}
4187
Jeff Mahoney143bede2012-03-01 14:56:26 +01004188void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004189{
4190 struct rb_node *node;
4191 struct rb_node *prev;
4192 struct btrfs_inode *entry;
4193 struct inode *inode;
4194 u64 objectid = 0;
4195
4196 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4197
4198 spin_lock(&root->inode_lock);
4199again:
4200 node = root->inode_tree.rb_node;
4201 prev = NULL;
4202 while (node) {
4203 prev = node;
4204 entry = rb_entry(node, struct btrfs_inode, rb_node);
4205
Li Zefan33345d012011-04-20 10:31:50 +08004206 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004207 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004208 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004209 node = node->rb_right;
4210 else
4211 break;
4212 }
4213 if (!node) {
4214 while (prev) {
4215 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004216 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004217 node = prev;
4218 break;
4219 }
4220 prev = rb_next(prev);
4221 }
4222 }
4223 while (node) {
4224 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004225 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004226 inode = igrab(&entry->vfs_inode);
4227 if (inode) {
4228 spin_unlock(&root->inode_lock);
4229 if (atomic_read(&inode->i_count) > 1)
4230 d_prune_aliases(inode);
4231 /*
Al Viro45321ac2010-06-07 13:43:19 -04004232 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004233 * the inode cache when its usage count
4234 * hits zero.
4235 */
4236 iput(inode);
4237 cond_resched();
4238 spin_lock(&root->inode_lock);
4239 goto again;
4240 }
4241
4242 if (cond_resched_lock(&root->inode_lock))
4243 goto again;
4244
4245 node = rb_next(node);
4246 }
4247 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004248}
4249
Chris Masone02119d2008-09-05 16:13:11 -04004250static int btrfs_init_locked_inode(struct inode *inode, void *p)
4251{
4252 struct btrfs_iget_args *args = p;
4253 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004254 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004255 return 0;
4256}
4257
4258static int btrfs_find_actor(struct inode *inode, void *opaque)
4259{
4260 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004261 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004262 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004263}
4264
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004265static struct inode *btrfs_iget_locked(struct super_block *s,
4266 u64 objectid,
4267 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004268{
4269 struct inode *inode;
4270 struct btrfs_iget_args args;
4271 args.ino = objectid;
4272 args.root = root;
4273
4274 inode = iget5_locked(s, objectid, btrfs_find_actor,
4275 btrfs_init_locked_inode,
4276 (void *)&args);
4277 return inode;
4278}
4279
Balaji Rao1a54ef82008-07-21 02:01:04 +05304280/* Get an inode object given its location and corresponding root.
4281 * Returns in *is_new if the inode was read from disk
4282 */
4283struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004284 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304285{
4286 struct inode *inode;
4287
4288 inode = btrfs_iget_locked(s, location->objectid, root);
4289 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004290 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304291
4292 if (inode->i_state & I_NEW) {
4293 BTRFS_I(inode)->root = root;
4294 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4295 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004296 if (!is_bad_inode(inode)) {
4297 inode_tree_add(inode);
4298 unlock_new_inode(inode);
4299 if (new)
4300 *new = 1;
4301 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004302 unlock_new_inode(inode);
4303 iput(inode);
4304 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004305 }
4306 }
4307
Balaji Rao1a54ef82008-07-21 02:01:04 +05304308 return inode;
4309}
4310
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004311static struct inode *new_simple_dir(struct super_block *s,
4312 struct btrfs_key *key,
4313 struct btrfs_root *root)
4314{
4315 struct inode *inode = new_inode(s);
4316
4317 if (!inode)
4318 return ERR_PTR(-ENOMEM);
4319
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004320 BTRFS_I(inode)->root = root;
4321 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004322 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004323
4324 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004325 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004326 inode->i_fop = &simple_dir_operations;
4327 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4328 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4329
4330 return inode;
4331}
4332
Chris Mason3de45862008-11-17 21:02:50 -05004333struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004334{
Chris Masond3977122009-01-05 21:25:51 -05004335 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004336 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004337 struct btrfs_root *sub_root = root;
4338 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004339 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004340 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004341
4342 if (dentry->d_name.len > BTRFS_NAME_LEN)
4343 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004344
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004345 if (unlikely(d_need_lookup(dentry))) {
4346 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4347 kfree(dentry->d_fsdata);
4348 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004349 /* This thing is hashed, drop it for now */
4350 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004351 } else {
4352 ret = btrfs_inode_by_name(dir, dentry, &location);
4353 }
Chris Mason5f39d392007-10-15 16:14:19 -04004354
Chris Mason39279cc2007-06-12 06:35:45 -04004355 if (ret < 0)
4356 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004357
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004358 if (location.objectid == 0)
4359 return NULL;
4360
4361 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004362 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004363 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004364 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004365
4366 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4367
Yan, Zheng76dda932009-09-21 16:00:26 -04004368 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004369 ret = fixup_tree_root_location(root, dir, dentry,
4370 &location, &sub_root);
4371 if (ret < 0) {
4372 if (ret != -ENOENT)
4373 inode = ERR_PTR(ret);
4374 else
4375 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4376 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004377 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004378 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004379 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4380
Julia Lawall34d19ba2011-01-24 19:55:19 +00004381 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004382 down_read(&root->fs_info->cleanup_work_sem);
4383 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004384 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004385 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004386 if (ret)
4387 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004388 }
4389
Chris Mason3de45862008-11-17 21:02:50 -05004390 return inode;
4391}
4392
Nick Pigginfe15ce42011-01-07 17:49:23 +11004393static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004394{
4395 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004396 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004397
Li Zefan848cce02012-02-21 17:04:28 +08004398 if (!inode && !IS_ROOT(dentry))
4399 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004400
Li Zefan848cce02012-02-21 17:04:28 +08004401 if (inode) {
4402 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004403 if (btrfs_root_refs(&root->root_item) == 0)
4404 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004405
4406 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4407 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004408 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004409 return 0;
4410}
4411
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004412static void btrfs_dentry_release(struct dentry *dentry)
4413{
4414 if (dentry->d_fsdata)
4415 kfree(dentry->d_fsdata);
4416}
4417
Chris Mason3de45862008-11-17 21:02:50 -05004418static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004419 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004420{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004421 struct dentry *ret;
4422
4423 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4424 if (unlikely(d_need_lookup(dentry))) {
4425 spin_lock(&dentry->d_lock);
4426 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4427 spin_unlock(&dentry->d_lock);
4428 }
4429 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004430}
4431
Miao Xie16cdcec2011-04-22 18:12:22 +08004432unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004433 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4434};
4435
David Woodhousecbdf5a22008-08-06 19:42:33 +01004436static int btrfs_real_readdir(struct file *filp, void *dirent,
4437 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004438{
Chris Mason6da6aba2007-12-18 16:15:09 -05004439 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004440 struct btrfs_root *root = BTRFS_I(inode)->root;
4441 struct btrfs_item *item;
4442 struct btrfs_dir_item *di;
4443 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004444 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004445 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004446 struct list_head ins_list;
4447 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004448 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004449 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004450 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004451 unsigned char d_type;
4452 int over = 0;
4453 u32 di_cur;
4454 u32 di_total;
4455 u32 di_len;
4456 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004457 char tmp_name[32];
4458 char *name_ptr;
4459 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004460 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004461
4462 /* FIXME, use a real flag for deciding about the key type */
4463 if (root->fs_info->tree_root == root)
4464 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004465
Chris Mason39544012007-12-12 14:38:19 -05004466 /* special case for "." */
4467 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004468 over = filldir(dirent, ".", 1,
4469 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004470 if (over)
4471 return 0;
4472 filp->f_pos = 1;
4473 }
Chris Mason39544012007-12-12 14:38:19 -05004474 /* special case for .., just use the back ref */
4475 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004476 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004477 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004478 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004479 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004480 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004481 filp->f_pos = 2;
4482 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004483 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004484 if (!path)
4485 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004486
Josef Bacik026fd312011-05-13 10:32:11 -04004487 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004488
Miao Xie16cdcec2011-04-22 18:12:22 +08004489 if (key_type == BTRFS_DIR_INDEX_KEY) {
4490 INIT_LIST_HEAD(&ins_list);
4491 INIT_LIST_HEAD(&del_list);
4492 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4493 }
4494
Chris Mason39279cc2007-06-12 06:35:45 -04004495 btrfs_set_key_type(&key, key_type);
4496 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004497 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004498
Chris Mason39279cc2007-06-12 06:35:45 -04004499 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4500 if (ret < 0)
4501 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004502
4503 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004504 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004505 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004506 if (slot >= btrfs_header_nritems(leaf)) {
4507 ret = btrfs_next_leaf(root, path);
4508 if (ret < 0)
4509 goto err;
4510 else if (ret > 0)
4511 break;
4512 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004513 }
Chris Mason3de45862008-11-17 21:02:50 -05004514
Chris Mason5f39d392007-10-15 16:14:19 -04004515 item = btrfs_item_nr(leaf, slot);
4516 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4517
4518 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004519 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004520 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004521 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004522 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004523 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004524 if (key_type == BTRFS_DIR_INDEX_KEY &&
4525 btrfs_should_delete_dir_index(&del_list,
4526 found_key.offset))
4527 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004528
4529 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004530 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004531
Chris Mason39279cc2007-06-12 06:35:45 -04004532 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4533 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004534 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004535
4536 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004537 struct btrfs_key location;
4538
Josef Bacik22a94d42011-03-16 16:47:17 -04004539 if (verify_dir_item(root, leaf, di))
4540 break;
4541
Chris Mason5f39d392007-10-15 16:14:19 -04004542 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004543 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004544 name_ptr = tmp_name;
4545 } else {
4546 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004547 if (!name_ptr) {
4548 ret = -ENOMEM;
4549 goto err;
4550 }
Chris Mason5f39d392007-10-15 16:14:19 -04004551 }
4552 read_extent_buffer(leaf, name_ptr,
4553 (unsigned long)(di + 1), name_len);
4554
4555 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4556 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004557
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004558
Chris Mason3de45862008-11-17 21:02:50 -05004559 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004560 * skip it.
4561 *
4562 * In contrast to old kernels, we insert the snapshot's
4563 * dir item and dir index after it has been created, so
4564 * we won't find a reference to our own snapshot. We
4565 * still keep the following code for backward
4566 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004567 */
4568 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4569 location.objectid == root->root_key.objectid) {
4570 over = 0;
4571 goto skip;
4572 }
Chris Mason5f39d392007-10-15 16:14:19 -04004573 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004574 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004575 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004576
Chris Mason3de45862008-11-17 21:02:50 -05004577skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004578 if (name_ptr != tmp_name)
4579 kfree(name_ptr);
4580
Chris Mason39279cc2007-06-12 06:35:45 -04004581 if (over)
4582 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004583 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004584 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004585 di_cur += di_len;
4586 di = (struct btrfs_dir_item *)((char *)di + di_len);
4587 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004588next:
4589 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004590 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004591
Miao Xie16cdcec2011-04-22 18:12:22 +08004592 if (key_type == BTRFS_DIR_INDEX_KEY) {
4593 if (is_curr)
4594 filp->f_pos++;
4595 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4596 &ins_list);
4597 if (ret)
4598 goto nopos;
4599 }
4600
David Woodhouse49593bf2008-08-17 17:08:36 +01004601 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004602 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004603 /*
4604 * 32-bit glibc will use getdents64, but then strtol -
4605 * so the last number we can serve is this.
4606 */
4607 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004608 else
4609 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004610nopos:
4611 ret = 0;
4612err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004613 if (key_type == BTRFS_DIR_INDEX_KEY)
4614 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004615 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004616 return ret;
4617}
4618
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004619int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004620{
4621 struct btrfs_root *root = BTRFS_I(inode)->root;
4622 struct btrfs_trans_handle *trans;
4623 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004624 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004625
Josef Bacik72ac3c02012-05-23 14:13:11 -04004626 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004627 return 0;
4628
Liu Bo83eea1f2012-07-10 05:28:39 -06004629 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004630 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004631
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004632 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004633 if (nolock)
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004634 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004635 else
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004636 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004637 if (IS_ERR(trans))
4638 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06004639 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004640 }
4641 return ret;
4642}
4643
4644/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004645 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004646 * inode changes. But, it is most likely to find the inode in cache.
4647 * FIXME, needs more benchmarking...there are no reasons other than performance
4648 * to keep or drop this code.
4649 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004650int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004651{
4652 struct btrfs_root *root = BTRFS_I(inode)->root;
4653 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004654 int ret;
4655
Josef Bacik72ac3c02012-05-23 14:13:11 -04004656 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004657 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004658
Josef Bacik7a7eaa402011-04-13 12:54:33 -04004659 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004660 if (IS_ERR(trans))
4661 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004662
4663 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004664 if (ret && ret == -ENOSPC) {
4665 /* whoops, lets try again with the full transaction */
4666 btrfs_end_transaction(trans, root);
4667 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004668 if (IS_ERR(trans))
4669 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004670
Chris Mason94b60442010-05-26 11:02:00 -04004671 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004672 }
Chris Mason39279cc2007-06-12 06:35:45 -04004673 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004674 if (BTRFS_I(inode)->delayed_node)
4675 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004676
4677 return ret;
4678}
4679
4680/*
4681 * This is a copy of file_update_time. We need this so we can return error on
4682 * ENOSPC for updating the inode in the case of file write and mmap writes.
4683 */
Josef Bacike41f9412012-03-26 09:46:47 -04004684static int btrfs_update_time(struct inode *inode, struct timespec *now,
4685 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004686{
Alexander Block2bc5565282012-06-15 09:49:33 +02004687 struct btrfs_root *root = BTRFS_I(inode)->root;
4688
4689 if (btrfs_root_readonly(root))
4690 return -EROFS;
4691
Josef Bacike41f9412012-03-26 09:46:47 -04004692 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004693 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004694 if (flags & S_CTIME)
4695 inode->i_ctime = *now;
4696 if (flags & S_MTIME)
4697 inode->i_mtime = *now;
4698 if (flags & S_ATIME)
4699 inode->i_atime = *now;
4700 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004701}
4702
Chris Masond352ac62008-09-29 15:18:18 -04004703/*
4704 * find the highest existing sequence number in a directory
4705 * and then set the in-memory index_cnt variable to reflect
4706 * free sequence numbers
4707 */
Josef Bacikaec74772008-07-24 12:12:38 -04004708static int btrfs_set_inode_index_count(struct inode *inode)
4709{
4710 struct btrfs_root *root = BTRFS_I(inode)->root;
4711 struct btrfs_key key, found_key;
4712 struct btrfs_path *path;
4713 struct extent_buffer *leaf;
4714 int ret;
4715
Li Zefan33345d012011-04-20 10:31:50 +08004716 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004717 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4718 key.offset = (u64)-1;
4719
4720 path = btrfs_alloc_path();
4721 if (!path)
4722 return -ENOMEM;
4723
4724 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4725 if (ret < 0)
4726 goto out;
4727 /* FIXME: we should be able to handle this */
4728 if (ret == 0)
4729 goto out;
4730 ret = 0;
4731
4732 /*
4733 * MAGIC NUMBER EXPLANATION:
4734 * since we search a directory based on f_pos we have to start at 2
4735 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4736 * else has to start at 2
4737 */
4738 if (path->slots[0] == 0) {
4739 BTRFS_I(inode)->index_cnt = 2;
4740 goto out;
4741 }
4742
4743 path->slots[0]--;
4744
4745 leaf = path->nodes[0];
4746 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4747
Li Zefan33345d012011-04-20 10:31:50 +08004748 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004749 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4750 BTRFS_I(inode)->index_cnt = 2;
4751 goto out;
4752 }
4753
4754 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4755out:
4756 btrfs_free_path(path);
4757 return ret;
4758}
4759
Chris Masond352ac62008-09-29 15:18:18 -04004760/*
4761 * helper to find a free sequence number in a given directory. This current
4762 * code is very simple, later versions will do smarter things in the btree
4763 */
Chris Mason3de45862008-11-17 21:02:50 -05004764int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004765{
4766 int ret = 0;
4767
4768 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004769 ret = btrfs_inode_delayed_dir_index_count(dir);
4770 if (ret) {
4771 ret = btrfs_set_inode_index_count(dir);
4772 if (ret)
4773 return ret;
4774 }
Josef Bacikaec74772008-07-24 12:12:38 -04004775 }
4776
Chris Mason00e4e6b2008-08-05 11:18:09 -04004777 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004778 BTRFS_I(dir)->index_cnt++;
4779
4780 return ret;
4781}
4782
Chris Mason39279cc2007-06-12 06:35:45 -04004783static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4784 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004785 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004786 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004787 u64 ref_objectid, u64 objectid,
4788 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004789{
4790 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004791 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004792 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004793 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004794 struct btrfs_inode_ref *ref;
4795 struct btrfs_key key[2];
4796 u32 sizes[2];
4797 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004798 int ret;
4799 int owner;
4800
Chris Mason5f39d392007-10-15 16:14:19 -04004801 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004802 if (!path)
4803 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004804
Chris Mason39279cc2007-06-12 06:35:45 -04004805 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004806 if (!inode) {
4807 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004808 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004809 }
Chris Mason39279cc2007-06-12 06:35:45 -04004810
Li Zefan581bb052011-04-20 10:06:11 +08004811 /*
4812 * we have to initialize this early, so we can reclaim the inode
4813 * number if we fail afterwards in this function.
4814 */
4815 inode->i_ino = objectid;
4816
Josef Bacikaec74772008-07-24 12:12:38 -04004817 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004818 trace_btrfs_inode_request(dir);
4819
Chris Mason3de45862008-11-17 21:02:50 -05004820 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004821 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004822 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004823 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004824 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004825 }
Josef Bacikaec74772008-07-24 12:12:38 -04004826 }
4827 /*
4828 * index_cnt is ignored for everything but a dir,
4829 * btrfs_get_inode_index_count has an explanation for the magic
4830 * number
4831 */
4832 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004833 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004834 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004835 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04004836
Josef Bacik5dc562c2012-08-17 13:14:17 -04004837 /*
4838 * We could have gotten an inode number from somebody who was fsynced
4839 * and then removed in this same transaction, so let's just set full
4840 * sync since it will be a full sync anyway and this will blow away the
4841 * old info in the log.
4842 */
4843 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4844
Al Viro569254b2011-07-24 17:08:40 -04004845 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004846 owner = 0;
4847 else
4848 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004849
4850 key[0].objectid = objectid;
4851 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4852 key[0].offset = 0;
4853
Mark Fashehf1863732012-08-08 11:32:27 -07004854 /*
4855 * Start new inodes with an inode_ref. This is slightly more
4856 * efficient for small numbers of hard links since they will
4857 * be packed into one item. Extended refs will kick in if we
4858 * add more hard links than can fit in the ref item.
4859 */
Chris Mason9c583092008-01-29 15:15:18 -05004860 key[1].objectid = objectid;
4861 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4862 key[1].offset = ref_objectid;
4863
4864 sizes[0] = sizeof(struct btrfs_inode_item);
4865 sizes[1] = name_len + sizeof(*ref);
4866
Chris Masonb9473432009-03-13 11:00:37 -04004867 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004868 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4869 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004870 goto fail;
4871
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004872 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004873 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004874 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004875 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4876 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004877 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4878 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004879 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004880
4881 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4882 struct btrfs_inode_ref);
4883 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004884 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004885 ptr = (unsigned long)(ref + 1);
4886 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4887
Chris Mason5f39d392007-10-15 16:14:19 -04004888 btrfs_mark_buffer_dirty(path->nodes[0]);
4889 btrfs_free_path(path);
4890
Chris Mason39279cc2007-06-12 06:35:45 -04004891 location = &BTRFS_I(inode)->location;
4892 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004893 location->offset = 0;
4894 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4895
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004896 btrfs_inherit_iflags(inode, dir);
4897
Al Viro569254b2011-07-24 17:08:40 -04004898 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004899 if (btrfs_test_opt(root, NODATASUM))
4900 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo213490b2012-09-11 08:33:50 -06004901 if (btrfs_test_opt(root, NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004902 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4903 }
4904
Chris Mason39279cc2007-06-12 06:35:45 -04004905 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004906 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004907
4908 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004909 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004910
Alexander Block8ea05e32012-07-25 17:35:53 +02004911 btrfs_update_root_times(trans, root);
4912
Chris Mason39279cc2007-06-12 06:35:45 -04004913 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004914fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004915 if (dir)
4916 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004917 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004918 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004919 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004920}
4921
4922static inline u8 btrfs_inode_type(struct inode *inode)
4923{
4924 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4925}
4926
Chris Masond352ac62008-09-29 15:18:18 -04004927/*
4928 * utility function to add 'inode' into 'parent_inode' with
4929 * a give name and a given sequence number.
4930 * if 'add_backref' is true, also insert a backref from the
4931 * inode to the parent directory.
4932 */
Chris Masone02119d2008-09-05 16:13:11 -04004933int btrfs_add_link(struct btrfs_trans_handle *trans,
4934 struct inode *parent_inode, struct inode *inode,
4935 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004936{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004937 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004938 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004939 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004940 u64 ino = btrfs_ino(inode);
4941 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004942
Li Zefan33345d012011-04-20 10:31:50 +08004943 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004944 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4945 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004946 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004947 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4948 key.offset = 0;
4949 }
Chris Mason39279cc2007-06-12 06:35:45 -04004950
Li Zefan33345d012011-04-20 10:31:50 +08004951 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004952 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4953 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004954 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004955 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004956 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4957 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004958 }
4959
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004960 /* Nothing to clean up yet */
4961 if (ret)
4962 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004963
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004964 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4965 parent_inode, &key,
4966 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05004967 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004968 goto fail_dir_item;
4969 else if (ret) {
4970 btrfs_abort_transaction(trans, root, ret);
4971 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004972 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004973
4974 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4975 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004976 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004977 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4978 ret = btrfs_update_inode(trans, root, parent_inode);
4979 if (ret)
4980 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004981 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004982
4983fail_dir_item:
4984 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4985 u64 local_index;
4986 int err;
4987 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4988 key.objectid, root->root_key.objectid,
4989 parent_ino, &local_index, name, name_len);
4990
4991 } else if (add_backref) {
4992 u64 local_index;
4993 int err;
4994
4995 err = btrfs_del_inode_ref(trans, root, name, name_len,
4996 ino, parent_ino, &local_index);
4997 }
4998 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004999}
5000
5001static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00005002 struct inode *dir, struct dentry *dentry,
5003 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005004{
Josef Bacika1b075d2010-11-19 20:36:11 +00005005 int err = btrfs_add_link(trans, dir, inode,
5006 dentry->d_name.name, dentry->d_name.len,
5007 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005008 if (err > 0)
5009 err = -EEXIST;
5010 return err;
5011}
5012
Josef Bacik618e21d2007-07-11 10:18:17 -04005013static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04005014 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04005015{
5016 struct btrfs_trans_handle *trans;
5017 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005018 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04005019 int err;
5020 int drop_inode = 0;
5021 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005022 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04005023
5024 if (!new_valid_dev(rdev))
5025 return -EINVAL;
5026
Josef Bacik9ed74f22009-09-11 16:12:44 -04005027 /*
5028 * 2 for inode item and ref
5029 * 2 for dir items
5030 * 1 for xattr if selinux is on
5031 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005032 trans = btrfs_start_transaction(root, 5);
5033 if (IS_ERR(trans))
5034 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005035
Li Zefan581bb052011-04-20 10:06:11 +08005036 err = btrfs_find_free_ino(root, &objectid);
5037 if (err)
5038 goto out_unlock;
5039
Josef Bacikaec74772008-07-24 12:12:38 -04005040 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005041 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005042 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005043 if (IS_ERR(inode)) {
5044 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005045 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005046 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005047
Eric Paris2a7dba32011-02-01 11:05:39 -05005048 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005049 if (err) {
5050 drop_inode = 1;
5051 goto out_unlock;
5052 }
5053
Casey Schauflerad19db72011-12-15 10:09:07 -05005054 /*
5055 * If the active LSM wants to access the inode during
5056 * d_instantiate it needs these. Smack checks to see
5057 * if the filesystem supports xattrs by looking at the
5058 * ops vector.
5059 */
5060
5061 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00005062 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04005063 if (err)
5064 drop_inode = 1;
5065 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04005066 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04005067 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05005068 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04005069 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005070out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005071 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005072 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04005073 if (drop_inode) {
5074 inode_dec_link_count(inode);
5075 iput(inode);
5076 }
Josef Bacik618e21d2007-07-11 10:18:17 -04005077 return err;
5078}
5079
Chris Mason39279cc2007-06-12 06:35:45 -04005080static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04005081 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04005082{
5083 struct btrfs_trans_handle *trans;
5084 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05005085 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005086 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04005087 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04005088 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005089 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005090
Josef Bacik9ed74f22009-09-11 16:12:44 -04005091 /*
5092 * 2 for inode item and ref
5093 * 2 for dir items
5094 * 1 for xattr if selinux is on
5095 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005096 trans = btrfs_start_transaction(root, 5);
5097 if (IS_ERR(trans))
5098 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005099
Li Zefan581bb052011-04-20 10:06:11 +08005100 err = btrfs_find_free_ino(root, &objectid);
5101 if (err)
5102 goto out_unlock;
5103
Josef Bacikaec74772008-07-24 12:12:38 -04005104 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005105 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005106 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005107 if (IS_ERR(inode)) {
5108 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005109 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04005110 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005111 drop_inode_on_err = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005112
Eric Paris2a7dba32011-02-01 11:05:39 -05005113 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005114 if (err)
Josef Bacik33268ea2008-07-24 12:16:36 -04005115 goto out_unlock;
Josef Bacik33268ea2008-07-24 12:16:36 -04005116
Filipe Brandenburger9185aa52012-11-30 03:40:08 +00005117 err = btrfs_update_inode(trans, root, inode);
5118 if (err)
5119 goto out_unlock;
5120
Casey Schauflerad19db72011-12-15 10:09:07 -05005121 /*
5122 * If the active LSM wants to access the inode during
5123 * d_instantiate it needs these. Smack checks to see
5124 * if the filesystem supports xattrs by looking at the
5125 * ops vector.
5126 */
5127 inode->i_fop = &btrfs_file_operations;
5128 inode->i_op = &btrfs_file_inode_operations;
5129
Josef Bacika1b075d2010-11-19 20:36:11 +00005130 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005131 if (err)
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005132 goto out_unlock;
5133
5134 inode->i_mapping->a_ops = &btrfs_aops;
5135 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5136 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5137 d_instantiate(dentry, inode);
5138
Chris Mason39279cc2007-06-12 06:35:45 -04005139out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005140 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00005141 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005142 inode_dec_link_count(inode);
5143 iput(inode);
5144 }
Liu Bob53d3f52012-11-14 14:34:34 +00005145 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005146 return err;
5147}
5148
5149static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5150 struct dentry *dentry)
5151{
5152 struct btrfs_trans_handle *trans;
5153 struct btrfs_root *root = BTRFS_I(dir)->root;
5154 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005155 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04005156 int err;
5157 int drop_inode = 0;
5158
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005159 /* do not allow sys_link's with other subvols of the same device */
5160 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005161 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005162
Mark Fashehf1863732012-08-08 11:32:27 -07005163 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00005164 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005165
Chris Mason3de45862008-11-17 21:02:50 -05005166 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005167 if (err)
5168 goto fail;
5169
Yan, Zhenga22285a2010-05-16 10:48:46 -04005170 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005171 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005172 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005173 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005174 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005175 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005176 if (IS_ERR(trans)) {
5177 err = PTR_ERR(trans);
5178 goto fail;
5179 }
Chris Mason5f39d392007-10-15 16:14:19 -04005180
Miao Xie31534952011-04-13 13:19:21 +08005181 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005182 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005183 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6e2010-10-23 11:11:40 -04005184 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04005185 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04005186
Josef Bacika1b075d2010-11-19 20:36:11 +00005187 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005188
Yan, Zhenga5719522009-09-24 09:17:31 -04005189 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005190 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005191 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005192 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005193 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005194 if (err)
5195 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005196 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005197 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005198 }
Chris Mason39279cc2007-06-12 06:35:45 -04005199
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005200 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005201fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005202 if (drop_inode) {
5203 inode_dec_link_count(inode);
5204 iput(inode);
5205 }
Liu Bob53d3f52012-11-14 14:34:34 +00005206 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005207 return err;
5208}
5209
Al Viro18bb1db2011-07-26 01:41:39 -04005210static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005211{
Chris Masonb9d86662008-05-02 16:13:49 -04005212 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005213 struct btrfs_trans_handle *trans;
5214 struct btrfs_root *root = BTRFS_I(dir)->root;
5215 int err = 0;
5216 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005217 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005218 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005219
Josef Bacik9ed74f22009-09-11 16:12:44 -04005220 /*
5221 * 2 items for inode and ref
5222 * 2 items for dir items
5223 * 1 for xattr if selinux is on
5224 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005225 trans = btrfs_start_transaction(root, 5);
5226 if (IS_ERR(trans))
5227 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005228
Li Zefan581bb052011-04-20 10:06:11 +08005229 err = btrfs_find_free_ino(root, &objectid);
5230 if (err)
5231 goto out_fail;
5232
Josef Bacikaec74772008-07-24 12:12:38 -04005233 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005234 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005235 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005236 if (IS_ERR(inode)) {
5237 err = PTR_ERR(inode);
5238 goto out_fail;
5239 }
Chris Mason5f39d392007-10-15 16:14:19 -04005240
Chris Mason39279cc2007-06-12 06:35:45 -04005241 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005242
Eric Paris2a7dba32011-02-01 11:05:39 -05005243 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005244 if (err)
5245 goto out_fail;
5246
Chris Mason39279cc2007-06-12 06:35:45 -04005247 inode->i_op = &btrfs_dir_inode_operations;
5248 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005249
Chris Masondbe674a2008-07-17 12:54:05 -04005250 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005251 err = btrfs_update_inode(trans, root, inode);
5252 if (err)
5253 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005254
Josef Bacika1b075d2010-11-19 20:36:11 +00005255 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5256 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005257 if (err)
5258 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005259
Chris Mason39279cc2007-06-12 06:35:45 -04005260 d_instantiate(dentry, inode);
5261 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005262
5263out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005264 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005265 if (drop_on_err)
5266 iput(inode);
Liu Bob53d3f52012-11-14 14:34:34 +00005267 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005268 return err;
5269}
5270
Chris Masond352ac62008-09-29 15:18:18 -04005271/* helper for btfs_get_extent. Given an existing extent in the tree,
5272 * and an extent that you want to insert, deal with overlap and insert
5273 * the new extent into the tree.
5274 */
Chris Mason3b951512008-04-17 11:29:12 -04005275static int merge_extent_mapping(struct extent_map_tree *em_tree,
5276 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005277 struct extent_map *em,
5278 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005279{
5280 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005281
Chris Masone6dcd2d2008-07-17 12:53:50 -04005282 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5283 start_diff = map_start - em->start;
5284 em->start = map_start;
5285 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005286 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5287 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005288 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005289 em->block_len -= start_diff;
5290 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005291 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005292}
5293
Chris Masonc8b97812008-10-29 14:49:59 -04005294static noinline int uncompress_inline(struct btrfs_path *path,
5295 struct inode *inode, struct page *page,
5296 size_t pg_offset, u64 extent_offset,
5297 struct btrfs_file_extent_item *item)
5298{
5299 int ret;
5300 struct extent_buffer *leaf = path->nodes[0];
5301 char *tmp;
5302 size_t max_size;
5303 unsigned long inline_size;
5304 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005305 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005306
5307 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005308 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005309 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5310 inline_size = btrfs_file_extent_inline_item_len(leaf,
5311 btrfs_item_nr(leaf, path->slots[0]));
5312 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005313 if (!tmp)
5314 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005315 ptr = btrfs_file_extent_inline_start(item);
5316
5317 read_extent_buffer(leaf, tmp, ptr, inline_size);
5318
Chris Mason5b050f02008-11-11 09:34:41 -05005319 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005320 ret = btrfs_decompress(compress_type, tmp, page,
5321 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005322 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005323 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005324 unsigned long copy_size = min_t(u64,
5325 PAGE_CACHE_SIZE - pg_offset,
5326 max_size - extent_offset);
5327 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005328 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005329 }
5330 kfree(tmp);
5331 return 0;
5332}
5333
Chris Masond352ac62008-09-29 15:18:18 -04005334/*
5335 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005336 * the ugly parts come from merging extents from the disk with the in-ram
5337 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005338 * where the in-ram extents might be locked pending data=ordered completion.
5339 *
5340 * This also copies inline extents directly into the page.
5341 */
Chris Masond3977122009-01-05 21:25:51 -05005342
Chris Masona52d9a82007-08-27 16:49:44 -04005343struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005344 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005345 int create)
5346{
5347 int ret;
5348 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005349 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005350 u64 extent_start = 0;
5351 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005352 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005353 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005354 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005355 struct btrfs_root *root = BTRFS_I(inode)->root;
5356 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005357 struct extent_buffer *leaf;
5358 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005359 struct extent_map *em = NULL;
5360 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005361 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005362 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005363 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005364
Chris Masona52d9a82007-08-27 16:49:44 -04005365again:
Chris Mason890871b2009-09-02 16:24:52 -04005366 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005367 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005368 if (em)
5369 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005370 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005371
Chris Masona52d9a82007-08-27 16:49:44 -04005372 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005373 if (em->start > start || em->start + em->len <= start)
5374 free_extent_map(em);
5375 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005376 free_extent_map(em);
5377 else
5378 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005379 }
David Sterba172ddd62011-04-21 00:48:27 +02005380 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005381 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005382 err = -ENOMEM;
5383 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005384 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005385 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005386 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005387 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005388 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005389 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005390
5391 if (!path) {
5392 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005393 if (!path) {
5394 err = -ENOMEM;
5395 goto out;
5396 }
5397 /*
5398 * Chances are we'll be called again, so go ahead and do
5399 * readahead
5400 */
5401 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005402 }
5403
Chris Mason179e29e2007-11-01 11:28:41 -04005404 ret = btrfs_lookup_file_extent(trans, root, path,
5405 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005406 if (ret < 0) {
5407 err = ret;
5408 goto out;
5409 }
5410
5411 if (ret != 0) {
5412 if (path->slots[0] == 0)
5413 goto not_found;
5414 path->slots[0]--;
5415 }
5416
Chris Mason5f39d392007-10-15 16:14:19 -04005417 leaf = path->nodes[0];
5418 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005419 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005420 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005421 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5422 found_type = btrfs_key_type(&found_key);
5423 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005424 found_type != BTRFS_EXTENT_DATA_KEY) {
5425 goto not_found;
5426 }
5427
Chris Mason5f39d392007-10-15 16:14:19 -04005428 found_type = btrfs_file_extent_type(leaf, item);
5429 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005430 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005431 if (found_type == BTRFS_FILE_EXTENT_REG ||
5432 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005433 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005434 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005435 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5436 size_t size;
5437 size = btrfs_file_extent_inline_len(leaf, item);
5438 extent_end = (extent_start + size + root->sectorsize - 1) &
5439 ~((u64)root->sectorsize - 1);
5440 }
5441
5442 if (start >= extent_end) {
5443 path->slots[0]++;
5444 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5445 ret = btrfs_next_leaf(root, path);
5446 if (ret < 0) {
5447 err = ret;
5448 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005449 }
Yan Zheng9036c102008-10-30 14:19:41 -04005450 if (ret > 0)
5451 goto not_found;
5452 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005453 }
Yan Zheng9036c102008-10-30 14:19:41 -04005454 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5455 if (found_key.objectid != objectid ||
5456 found_key.type != BTRFS_EXTENT_DATA_KEY)
5457 goto not_found;
5458 if (start + len <= found_key.offset)
5459 goto not_found;
5460 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005461 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04005462 em->len = found_key.offset - start;
5463 goto not_found_em;
5464 }
5465
Yan Zhengd899e052008-10-30 14:25:28 -04005466 if (found_type == BTRFS_FILE_EXTENT_REG ||
5467 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005468 em->start = extent_start;
5469 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005470 em->orig_start = extent_start -
5471 btrfs_file_extent_offset(leaf, item);
Josef Bacikb4939682012-12-03 10:31:19 -05005472 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5473 item);
Chris Masondb945352007-10-15 16:15:53 -04005474 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5475 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005476 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005477 goto insert;
5478 }
Li Zefan261507a02010-12-17 14:21:50 +08005479 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005480 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005481 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005482 em->block_start = bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05005483 em->block_len = em->orig_block_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005484 } else {
5485 bytenr += btrfs_file_extent_offset(leaf, item);
5486 em->block_start = bytenr;
5487 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005488 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5489 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005490 }
Chris Masona52d9a82007-08-27 16:49:44 -04005491 goto insert;
5492 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005493 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005494 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005495 size_t size;
5496 size_t extent_offset;
5497 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005498
Chris Masona52d9a82007-08-27 16:49:44 -04005499 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005500 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005501 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005502 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005503 goto out;
5504 }
5505
Yan Zheng9036c102008-10-30 14:19:41 -04005506 size = btrfs_file_extent_inline_len(leaf, item);
5507 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005508 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005509 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005510 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005511 em->len = (copy_size + root->sectorsize - 1) &
5512 ~((u64)root->sectorsize - 1);
Josef Bacikb4939682012-12-03 10:31:19 -05005513 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005514 em->orig_start = em->start;
Li Zefan261507a02010-12-17 14:21:50 +08005515 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005516 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005517 em->compress_type = compress_type;
5518 }
Yan689f9342007-10-29 11:41:07 -04005519 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005520 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005521 if (btrfs_file_extent_compression(leaf, item) !=
5522 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005523 ret = uncompress_inline(path, inode, page,
5524 pg_offset,
5525 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005526 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005527 } else {
5528 map = kmap(page);
5529 read_extent_buffer(leaf, map + pg_offset, ptr,
5530 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005531 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5532 memset(map + pg_offset + copy_size, 0,
5533 PAGE_CACHE_SIZE - pg_offset -
5534 copy_size);
5535 }
Chris Masonc8b97812008-10-29 14:49:59 -04005536 kunmap(page);
5537 }
Chris Mason179e29e2007-11-01 11:28:41 -04005538 flush_dcache_page(page);
5539 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005540 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005541 if (!trans) {
5542 kunmap(page);
5543 free_extent_map(em);
5544 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005545
David Sterbab3b4aa72011-04-21 01:20:15 +02005546 btrfs_release_path(path);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005547 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005548
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005549 if (IS_ERR(trans))
5550 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005551 goto again;
5552 }
Chris Masonc8b97812008-10-29 14:49:59 -04005553 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005554 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005555 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005556 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005557 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005558 }
Chris Masond1310b22008-01-24 16:13:08 -05005559 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005560 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005561 goto insert;
5562 } else {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005563 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005564 }
5565not_found:
5566 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04005567 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005568 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005569not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005570 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005571 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005572insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005573 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005574 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005575 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5576 "[%llu %llu]\n", (unsigned long long)em->start,
5577 (unsigned long long)em->len,
5578 (unsigned long long)start,
5579 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005580 err = -EIO;
5581 goto out;
5582 }
Chris Masond1310b22008-01-24 16:13:08 -05005583
5584 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005585 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005586 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005587 /* it is possible that someone inserted the extent into the tree
5588 * while we had the lock dropped. It is also possible that
5589 * an overlapping map exists in the tree
5590 */
Chris Masona52d9a82007-08-27 16:49:44 -04005591 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005592 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005593
5594 ret = 0;
5595
Chris Mason3b951512008-04-17 11:29:12 -04005596 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005597 if (existing && (existing->start > start ||
5598 existing->start + existing->len <= start)) {
5599 free_extent_map(existing);
5600 existing = NULL;
5601 }
Chris Mason3b951512008-04-17 11:29:12 -04005602 if (!existing) {
5603 existing = lookup_extent_mapping(em_tree, em->start,
5604 em->len);
5605 if (existing) {
5606 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005607 em, start,
5608 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005609 free_extent_map(existing);
5610 if (err) {
5611 free_extent_map(em);
5612 em = NULL;
5613 }
5614 } else {
5615 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005616 free_extent_map(em);
5617 em = NULL;
5618 }
5619 } else {
5620 free_extent_map(em);
5621 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005622 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005623 }
Chris Masona52d9a82007-08-27 16:49:44 -04005624 }
Chris Mason890871b2009-09-02 16:24:52 -04005625 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005626out:
liubo1abe9b82011-03-24 11:18:59 +00005627
Tsutomu Itohf0bd95e2012-10-01 03:08:37 -06005628 if (em)
5629 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00005630
Chris Masonf4219502008-07-22 11:18:09 -04005631 if (path)
5632 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005633 if (trans) {
5634 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005635 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005636 err = ret;
5637 }
Chris Masona52d9a82007-08-27 16:49:44 -04005638 if (err) {
5639 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005640 return ERR_PTR(err);
5641 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005642 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005643 return em;
5644}
5645
Chris Masonec29ed52011-02-23 16:23:20 -05005646struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5647 size_t pg_offset, u64 start, u64 len,
5648 int create)
5649{
5650 struct extent_map *em;
5651 struct extent_map *hole_em = NULL;
5652 u64 range_start = start;
5653 u64 end;
5654 u64 found;
5655 u64 found_end;
5656 int err = 0;
5657
5658 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5659 if (IS_ERR(em))
5660 return em;
5661 if (em) {
5662 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00005663 * if our em maps to
5664 * - a hole or
5665 * - a pre-alloc extent,
5666 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05005667 */
Liu Bof9e4fb52013-01-07 10:10:12 +00005668 if (em->block_start != EXTENT_MAP_HOLE &&
5669 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05005670 return em;
5671 else
5672 hole_em = em;
5673 }
5674
5675 /* check to see if we've wrapped (len == -1 or similar) */
5676 end = start + len;
5677 if (end < start)
5678 end = (u64)-1;
5679 else
5680 end -= 1;
5681
5682 em = NULL;
5683
5684 /* ok, we didn't find anything, lets look for delalloc */
5685 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5686 end, len, EXTENT_DELALLOC, 1);
5687 found_end = range_start + found;
5688 if (found_end < range_start)
5689 found_end = (u64)-1;
5690
5691 /*
5692 * we didn't find anything useful, return
5693 * the original results from get_extent()
5694 */
5695 if (range_start > end || found_end <= start) {
5696 em = hole_em;
5697 hole_em = NULL;
5698 goto out;
5699 }
5700
5701 /* adjust the range_start to make sure it doesn't
5702 * go backwards from the start they passed in
5703 */
5704 range_start = max(start,range_start);
5705 found = found_end - range_start;
5706
5707 if (found > 0) {
5708 u64 hole_start = start;
5709 u64 hole_len = len;
5710
David Sterba172ddd62011-04-21 00:48:27 +02005711 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005712 if (!em) {
5713 err = -ENOMEM;
5714 goto out;
5715 }
5716 /*
5717 * when btrfs_get_extent can't find anything it
5718 * returns one huge hole
5719 *
5720 * make sure what it found really fits our range, and
5721 * adjust to make sure it is based on the start from
5722 * the caller
5723 */
5724 if (hole_em) {
5725 u64 calc_end = extent_map_end(hole_em);
5726
5727 if (calc_end <= start || (hole_em->start > end)) {
5728 free_extent_map(hole_em);
5729 hole_em = NULL;
5730 } else {
5731 hole_start = max(hole_em->start, start);
5732 hole_len = calc_end - hole_start;
5733 }
5734 }
5735 em->bdev = NULL;
5736 if (hole_em && range_start > hole_start) {
5737 /* our hole starts before our delalloc, so we
5738 * have to return just the parts of the hole
5739 * that go until the delalloc starts
5740 */
5741 em->len = min(hole_len,
5742 range_start - hole_start);
5743 em->start = hole_start;
5744 em->orig_start = hole_start;
5745 /*
5746 * don't adjust block start at all,
5747 * it is fixed at EXTENT_MAP_HOLE
5748 */
5749 em->block_start = hole_em->block_start;
5750 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00005751 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
5752 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05005753 } else {
5754 em->start = range_start;
5755 em->len = found;
5756 em->orig_start = range_start;
5757 em->block_start = EXTENT_MAP_DELALLOC;
5758 em->block_len = found;
5759 }
5760 } else if (hole_em) {
5761 return hole_em;
5762 }
5763out:
5764
5765 free_extent_map(hole_em);
5766 if (err) {
5767 free_extent_map(em);
5768 return ERR_PTR(err);
5769 }
5770 return em;
5771}
5772
Josef Bacik4b46fce2010-05-23 11:00:55 -04005773static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5774 u64 start, u64 len)
5775{
5776 struct btrfs_root *root = BTRFS_I(inode)->root;
5777 struct btrfs_trans_handle *trans;
Josef Bacik70c8a912012-10-11 16:54:30 -04005778 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005779 struct btrfs_key ins;
5780 u64 alloc_hint;
5781 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005782
Josef Bacik7a7eaa402011-04-13 12:54:33 -04005783 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005784 if (IS_ERR(trans))
5785 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005786
5787 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5788
5789 alloc_hint = get_extent_allocation_hint(inode, start, len);
5790 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005791 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005792 if (ret) {
5793 em = ERR_PTR(ret);
5794 goto out;
5795 }
5796
Josef Bacik70c8a912012-10-11 16:54:30 -04005797 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
5798 ins.offset, ins.offset, 0);
5799 if (IS_ERR(em))
5800 goto out;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005801
5802 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5803 ins.offset, ins.offset, 0);
5804 if (ret) {
5805 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5806 em = ERR_PTR(ret);
5807 }
5808out:
5809 btrfs_end_transaction(trans, root);
5810 return em;
5811}
5812
Chris Mason46bfbb52010-05-26 11:04:10 -04005813/*
5814 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5815 * block must be cow'd
5816 */
5817static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5818 struct inode *inode, u64 offset, u64 len)
5819{
5820 struct btrfs_path *path;
5821 int ret;
5822 struct extent_buffer *leaf;
5823 struct btrfs_root *root = BTRFS_I(inode)->root;
5824 struct btrfs_file_extent_item *fi;
5825 struct btrfs_key key;
5826 u64 disk_bytenr;
5827 u64 backref_offset;
5828 u64 extent_end;
5829 u64 num_bytes;
5830 int slot;
5831 int found_type;
5832
5833 path = btrfs_alloc_path();
5834 if (!path)
5835 return -ENOMEM;
5836
Li Zefan33345d012011-04-20 10:31:50 +08005837 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005838 offset, 0);
5839 if (ret < 0)
5840 goto out;
5841
5842 slot = path->slots[0];
5843 if (ret == 1) {
5844 if (slot == 0) {
5845 /* can't find the item, must cow */
5846 ret = 0;
5847 goto out;
5848 }
5849 slot--;
5850 }
5851 ret = 0;
5852 leaf = path->nodes[0];
5853 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005854 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005855 key.type != BTRFS_EXTENT_DATA_KEY) {
5856 /* not our file or wrong item type, must cow */
5857 goto out;
5858 }
5859
5860 if (key.offset > offset) {
5861 /* Wrong offset, must cow */
5862 goto out;
5863 }
5864
5865 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5866 found_type = btrfs_file_extent_type(leaf, fi);
5867 if (found_type != BTRFS_FILE_EXTENT_REG &&
5868 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5869 /* not a regular extent, must cow */
5870 goto out;
5871 }
5872 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5873 backref_offset = btrfs_file_extent_offset(leaf, fi);
5874
5875 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5876 if (extent_end < offset + len) {
5877 /* extent doesn't include our full range, must cow */
5878 goto out;
5879 }
5880
5881 if (btrfs_extent_readonly(root, disk_bytenr))
5882 goto out;
5883
5884 /*
5885 * look for other files referencing this extent, if we
5886 * find any we must cow
5887 */
Li Zefan33345d012011-04-20 10:31:50 +08005888 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005889 key.offset - backref_offset, disk_bytenr))
5890 goto out;
5891
5892 /*
5893 * adjust disk_bytenr and num_bytes to cover just the bytes
5894 * in this extent we are about to write. If there
5895 * are any csums in that range we have to cow in order
5896 * to keep the csums correct
5897 */
5898 disk_bytenr += backref_offset;
5899 disk_bytenr += offset - key.offset;
5900 num_bytes = min(offset + len, extent_end) - offset;
5901 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5902 goto out;
5903 /*
5904 * all of the above have passed, it is safe to overwrite this extent
5905 * without cow
5906 */
5907 ret = 1;
5908out:
5909 btrfs_free_path(path);
5910 return ret;
5911}
5912
Josef Bacikeb838e72012-07-31 16:28:48 -04005913static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5914 struct extent_state **cached_state, int writing)
5915{
5916 struct btrfs_ordered_extent *ordered;
5917 int ret = 0;
5918
5919 while (1) {
5920 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5921 0, cached_state);
5922 /*
5923 * We're concerned with the entire range that we're going to be
5924 * doing DIO to, so we need to make sure theres no ordered
5925 * extents in this range.
5926 */
5927 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5928 lockend - lockstart + 1);
5929
5930 /*
5931 * We need to make sure there are no buffered pages in this
5932 * range either, we could have raced between the invalidate in
5933 * generic_file_direct_write and locking the extent. The
5934 * invalidate needs to happen so that reads after a write do not
5935 * get stale data.
5936 */
5937 if (!ordered && (!writing ||
5938 !test_range_bit(&BTRFS_I(inode)->io_tree,
5939 lockstart, lockend, EXTENT_UPTODATE, 0,
5940 *cached_state)))
5941 break;
5942
5943 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5944 cached_state, GFP_NOFS);
5945
5946 if (ordered) {
5947 btrfs_start_ordered_extent(inode, ordered, 1);
5948 btrfs_put_ordered_extent(ordered);
5949 } else {
5950 /* Screw you mmap */
5951 ret = filemap_write_and_wait_range(inode->i_mapping,
5952 lockstart,
5953 lockend);
5954 if (ret)
5955 break;
5956
5957 /*
5958 * If we found a page that couldn't be invalidated just
5959 * fall back to buffered.
5960 */
5961 ret = invalidate_inode_pages2_range(inode->i_mapping,
5962 lockstart >> PAGE_CACHE_SHIFT,
5963 lockend >> PAGE_CACHE_SHIFT);
5964 if (ret)
5965 break;
5966 }
5967
5968 cond_resched();
5969 }
5970
5971 return ret;
5972}
5973
Josef Bacik69ffb542012-09-11 15:40:07 -04005974static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5975 u64 len, u64 orig_start,
5976 u64 block_start, u64 block_len,
Josef Bacikb4939682012-12-03 10:31:19 -05005977 u64 orig_block_len, int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04005978{
5979 struct extent_map_tree *em_tree;
5980 struct extent_map *em;
5981 struct btrfs_root *root = BTRFS_I(inode)->root;
5982 int ret;
5983
5984 em_tree = &BTRFS_I(inode)->extent_tree;
5985 em = alloc_extent_map();
5986 if (!em)
5987 return ERR_PTR(-ENOMEM);
5988
5989 em->start = start;
5990 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04005991 em->mod_start = start;
5992 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04005993 em->len = len;
5994 em->block_len = block_len;
5995 em->block_start = block_start;
5996 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05005997 em->orig_block_len = orig_block_len;
Josef Bacik70c8a912012-10-11 16:54:30 -04005998 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04005999 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6000 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05006001 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04006002
6003 do {
6004 btrfs_drop_extent_cache(inode, em->start,
6005 em->start + em->len - 1, 0);
6006 write_lock(&em_tree->lock);
6007 ret = add_extent_mapping(em_tree, em);
Josef Bacik70c8a912012-10-11 16:54:30 -04006008 if (!ret)
6009 list_move(&em->list,
6010 &em_tree->modified_extents);
Josef Bacik69ffb542012-09-11 15:40:07 -04006011 write_unlock(&em_tree->lock);
6012 } while (ret == -EEXIST);
6013
6014 if (ret) {
6015 free_extent_map(em);
6016 return ERR_PTR(ret);
6017 }
6018
6019 return em;
6020}
6021
6022
Josef Bacik4b46fce2010-05-23 11:00:55 -04006023static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6024 struct buffer_head *bh_result, int create)
6025{
6026 struct extent_map *em;
6027 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04006028 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006029 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04006030 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006031 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04006032 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04006033 int unlock_bits = EXTENT_LOCKED;
6034 int ret;
6035
Josef Bacikeb838e72012-07-31 16:28:48 -04006036 if (create) {
6037 ret = btrfs_delalloc_reserve_space(inode, len);
6038 if (ret)
6039 return ret;
6040 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04006041 } else {
6042 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04006043 }
6044
Josef Bacikc3298612012-08-03 16:49:19 -04006045 lockstart = start;
6046 lockend = start + len - 1;
6047
Josef Bacikeb838e72012-07-31 16:28:48 -04006048 /*
6049 * If this errors out it's because we couldn't invalidate pagecache for
6050 * this range and we need to fallback to buffered.
6051 */
6052 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6053 return -ENOTBLK;
6054
6055 if (create) {
6056 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6057 lockend, EXTENT_DELALLOC, NULL,
6058 &cached_state, GFP_NOFS);
6059 if (ret)
6060 goto unlock_err;
6061 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006062
6063 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04006064 if (IS_ERR(em)) {
6065 ret = PTR_ERR(em);
6066 goto unlock_err;
6067 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006068
6069 /*
6070 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6071 * io. INLINE is special, and we could probably kludge it in here, but
6072 * it's still buffered so for safety lets just fall back to the generic
6073 * buffered path.
6074 *
6075 * For COMPRESSED we _have_ to read the entire extent in so we can
6076 * decompress it, so there will be buffering required no matter what we
6077 * do, so go ahead and fallback to buffered.
6078 *
6079 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6080 * to buffered IO. Don't blame me, this is the price we pay for using
6081 * the generic code.
6082 */
6083 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6084 em->block_start == EXTENT_MAP_INLINE) {
6085 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006086 ret = -ENOTBLK;
6087 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006088 }
6089
6090 /* Just a good old fashioned hole, return */
6091 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6092 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6093 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006094 ret = 0;
6095 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006096 }
6097
6098 /*
6099 * We don't allocate a new extent in the following cases
6100 *
6101 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6102 * existing extent.
6103 * 2) The extent is marked as PREALLOC. We're good to go here and can
6104 * just use the extent.
6105 *
6106 */
Chris Mason46bfbb52010-05-26 11:04:10 -04006107 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04006108 len = min(len, em->len - (start - em->start));
6109 lockstart = start + len;
6110 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04006111 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006112
6113 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6114 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6115 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006116 int type;
6117 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04006118 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006119
6120 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6121 type = BTRFS_ORDERED_PREALLOC;
6122 else
6123 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006124 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006125 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006126
6127 /*
6128 * we're not going to log anything, but we do need
6129 * to make sure the current transaction stays open
6130 * while we look for nocow cross refs
6131 */
Josef Bacik7a7eaa402011-04-13 12:54:33 -04006132 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006133 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006134 goto must_cow;
6135
6136 if (can_nocow_odirect(trans, inode, start, len) == 1) {
Josef Bacik70c8a912012-10-11 16:54:30 -04006137 u64 orig_start = em->orig_start;
Josef Bacikb4939682012-12-03 10:31:19 -05006138 u64 orig_block_len = em->orig_block_len;
Josef Bacik69ffb542012-09-11 15:40:07 -04006139
6140 if (type == BTRFS_ORDERED_PREALLOC) {
6141 free_extent_map(em);
6142 em = create_pinned_em(inode, start, len,
6143 orig_start,
Josef Bacikb4939682012-12-03 10:31:19 -05006144 block_start, len,
6145 orig_block_len, type);
Josef Bacik69ffb542012-09-11 15:40:07 -04006146 if (IS_ERR(em)) {
6147 btrfs_end_transaction(trans, root);
6148 goto unlock_err;
6149 }
6150 }
6151
Chris Mason46bfbb52010-05-26 11:04:10 -04006152 ret = btrfs_add_ordered_extent_dio(inode, start,
6153 block_start, len, len, type);
6154 btrfs_end_transaction(trans, root);
6155 if (ret) {
6156 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006157 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006158 }
6159 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006160 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006161 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006162 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006163must_cow:
6164 /*
6165 * this will cow the extent, reset the len in case we changed
6166 * it above
6167 */
6168 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04006169 free_extent_map(em);
6170 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006171 if (IS_ERR(em)) {
6172 ret = PTR_ERR(em);
6173 goto unlock_err;
6174 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006175 len = min(len, em->len - (start - em->start));
6176unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006177 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6178 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006179 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006180 bh_result->b_bdev = em->bdev;
6181 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006182 if (create) {
6183 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6184 set_buffer_new(bh_result);
6185
6186 /*
6187 * Need to update the i_size under the extent lock so buffered
6188 * readers will get the updated i_size when we unlock.
6189 */
6190 if (start + len > i_size_read(inode))
6191 i_size_write(inode, start + len);
6192 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006193
Josef Bacikeb838e72012-07-31 16:28:48 -04006194 /*
6195 * In the case of write we need to clear and unlock the entire range,
6196 * in the case of read we need to unlock only the end area that we
6197 * aren't using if there is any left over space.
6198 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006199 if (lockstart < lockend) {
6200 if (create && len < lockend - lockstart) {
6201 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006202 lockstart + len - 1,
6203 unlock_bits | EXTENT_DEFRAG, 1, 0,
Liu Bo24c03fa2012-08-22 20:10:38 -06006204 &cached_state, GFP_NOFS);
6205 /*
6206 * Beside unlock, we also need to cleanup reserved space
6207 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6208 */
6209 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6210 lockstart + len, lockend,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006211 unlock_bits | EXTENT_DO_ACCOUNTING |
6212 EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06006213 } else {
6214 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6215 lockend, unlock_bits, 1, 0,
6216 &cached_state, GFP_NOFS);
6217 }
6218 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006219 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006220 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006221
Josef Bacik4b46fce2010-05-23 11:00:55 -04006222 free_extent_map(em);
6223
6224 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006225
6226unlock_err:
6227 if (create)
6228 unlock_bits |= EXTENT_DO_ACCOUNTING;
6229
6230 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6231 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6232 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006233}
6234
6235struct btrfs_dio_private {
6236 struct inode *inode;
6237 u64 logical_offset;
6238 u64 disk_bytenr;
6239 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006240 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006241
6242 /* number of bios pending for this dio */
6243 atomic_t pending_bios;
6244
6245 /* IO errors */
6246 int errors;
6247
6248 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006249};
6250
6251static void btrfs_endio_direct_read(struct bio *bio, int err)
6252{
Miao Xiee65e1532010-11-22 03:04:43 +00006253 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006254 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6255 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006256 struct inode *inode = dip->inode;
6257 struct btrfs_root *root = BTRFS_I(inode)->root;
6258 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006259
6260 start = dip->logical_offset;
6261 do {
6262 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6263 struct page *page = bvec->bv_page;
6264 char *kaddr;
6265 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006266 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006267 unsigned long flags;
6268
Josef Bacikc3298612012-08-03 16:49:19 -04006269 if (get_state_private(&BTRFS_I(inode)->io_tree,
6270 start, &private))
6271 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006272 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006273 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006274 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6275 csum, bvec->bv_len);
6276 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006277 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006278 local_irq_restore(flags);
6279
6280 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006281 if (csum != private) {
6282failed:
Li Zefan33345d012011-04-20 10:31:50 +08006283 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006284 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006285 (unsigned long long)btrfs_ino(inode),
6286 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006287 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006288 err = -EIO;
6289 }
6290 }
6291
6292 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006293 bvec++;
6294 } while (bvec <= bvec_end);
6295
6296 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006297 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006298 bio->bi_private = dip->private;
6299
Josef Bacik4b46fce2010-05-23 11:00:55 -04006300 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006301
6302 /* If we had a csum failure make sure to clear the uptodate flag */
6303 if (err)
6304 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006305 dio_end_io(bio, err);
6306}
6307
6308static void btrfs_endio_direct_write(struct bio *bio, int err)
6309{
6310 struct btrfs_dio_private *dip = bio->bi_private;
6311 struct inode *inode = dip->inode;
6312 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006313 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006314 u64 ordered_offset = dip->logical_offset;
6315 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006316 int ret;
6317
6318 if (err)
6319 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006320again:
6321 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6322 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006323 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006324 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006325 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006326
Josef Bacik5fd02042012-05-02 14:00:54 -04006327 ordered->work.func = finish_ordered_fn;
6328 ordered->work.flags = 0;
6329 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6330 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006331out_test:
6332 /*
6333 * our bio might span multiple ordered extents. If we haven't
6334 * completed the accounting for the whole dio, go back and try again
6335 */
6336 if (ordered_offset < dip->logical_offset + dip->bytes) {
6337 ordered_bytes = dip->logical_offset + dip->bytes -
6338 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006339 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006340 goto again;
6341 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006342out_done:
6343 bio->bi_private = dip->private;
6344
Josef Bacik4b46fce2010-05-23 11:00:55 -04006345 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006346
6347 /* If we had an error make sure to clear the uptodate flag */
6348 if (err)
6349 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006350 dio_end_io(bio, err);
6351}
6352
Chris Masoneaf25d92010-05-25 09:48:28 -04006353static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6354 struct bio *bio, int mirror_num,
6355 unsigned long bio_flags, u64 offset)
6356{
6357 int ret;
6358 struct btrfs_root *root = BTRFS_I(inode)->root;
6359 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006360 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006361 return 0;
6362}
6363
Miao Xiee65e1532010-11-22 03:04:43 +00006364static void btrfs_end_dio_bio(struct bio *bio, int err)
6365{
6366 struct btrfs_dio_private *dip = bio->bi_private;
6367
6368 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006369 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006370 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006371 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006372 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006373 dip->errors = 1;
6374
6375 /*
6376 * before atomic variable goto zero, we must make sure
6377 * dip->errors is perceived to be set.
6378 */
6379 smp_mb__before_atomic_dec();
6380 }
6381
6382 /* if there are more bios still pending for this dio, just exit */
6383 if (!atomic_dec_and_test(&dip->pending_bios))
6384 goto out;
6385
6386 if (dip->errors)
6387 bio_io_error(dip->orig_bio);
6388 else {
6389 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6390 bio_endio(dip->orig_bio, 0);
6391 }
6392out:
6393 bio_put(bio);
6394}
6395
6396static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6397 u64 first_sector, gfp_t gfp_flags)
6398{
6399 int nr_vecs = bio_get_nr_vecs(bdev);
6400 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6401}
6402
6403static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6404 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006405 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006406{
6407 int write = rw & REQ_WRITE;
6408 struct btrfs_root *root = BTRFS_I(inode)->root;
6409 int ret;
6410
Josef Bacikb812ce22012-11-16 13:56:32 -05006411 if (async_submit)
6412 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6413
Miao Xiee65e1532010-11-22 03:04:43 +00006414 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006415
6416 if (!write) {
6417 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6418 if (ret)
6419 goto err;
6420 }
Miao Xiee65e1532010-11-22 03:04:43 +00006421
Josef Bacik1ae39932011-04-06 14:41:34 -04006422 if (skip_sum)
6423 goto map;
6424
6425 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006426 ret = btrfs_wq_submit_bio(root->fs_info,
6427 inode, rw, bio, 0, 0,
6428 file_offset,
6429 __btrfs_submit_bio_start_direct_io,
6430 __btrfs_submit_bio_done);
6431 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006432 } else if (write) {
6433 /*
6434 * If we aren't doing async submit, calculate the csum of the
6435 * bio now.
6436 */
6437 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6438 if (ret)
6439 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006440 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006441 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006442 if (ret)
6443 goto err;
6444 }
Miao Xiee65e1532010-11-22 03:04:43 +00006445
Josef Bacik1ae39932011-04-06 14:41:34 -04006446map:
6447 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006448err:
6449 bio_put(bio);
6450 return ret;
6451}
6452
6453static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6454 int skip_sum)
6455{
6456 struct inode *inode = dip->inode;
6457 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00006458 struct bio *bio;
6459 struct bio *orig_bio = dip->orig_bio;
6460 struct bio_vec *bvec = orig_bio->bi_io_vec;
6461 u64 start_sector = orig_bio->bi_sector;
6462 u64 file_offset = dip->logical_offset;
6463 u64 submit_len = 0;
6464 u64 map_length;
6465 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006466 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006467 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006468
Miao Xiee65e1532010-11-22 03:04:43 +00006469 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006470 ret = btrfs_map_block(root->fs_info, READ, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006471 &map_length, NULL, 0);
6472 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006473 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006474 return -EIO;
6475 }
6476
Josef Bacik02f57c72011-04-06 14:25:44 -04006477 if (map_length >= orig_bio->bi_size) {
6478 bio = orig_bio;
6479 goto submit;
6480 }
6481
Josef Bacik1ae39932011-04-06 14:41:34 -04006482 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006483 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6484 if (!bio)
6485 return -ENOMEM;
6486 bio->bi_private = dip;
6487 bio->bi_end_io = btrfs_end_dio_bio;
6488 atomic_inc(&dip->pending_bios);
6489
Miao Xiee65e1532010-11-22 03:04:43 +00006490 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6491 if (unlikely(map_length < submit_len + bvec->bv_len ||
6492 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6493 bvec->bv_offset) < bvec->bv_len)) {
6494 /*
6495 * inc the count before we submit the bio so
6496 * we know the end IO handler won't happen before
6497 * we inc the count. Otherwise, the dip might get freed
6498 * before we're done setting it up
6499 */
6500 atomic_inc(&dip->pending_bios);
6501 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6502 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006503 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006504 if (ret) {
6505 bio_put(bio);
6506 atomic_dec(&dip->pending_bios);
6507 goto out_err;
6508 }
6509
Miao Xiee65e1532010-11-22 03:04:43 +00006510 start_sector += submit_len >> 9;
6511 file_offset += submit_len;
6512
6513 submit_len = 0;
6514 nr_pages = 0;
6515
6516 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6517 start_sector, GFP_NOFS);
6518 if (!bio)
6519 goto out_err;
6520 bio->bi_private = dip;
6521 bio->bi_end_io = btrfs_end_dio_bio;
6522
6523 map_length = orig_bio->bi_size;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01006524 ret = btrfs_map_block(root->fs_info, READ,
6525 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00006526 &map_length, NULL, 0);
6527 if (ret) {
6528 bio_put(bio);
6529 goto out_err;
6530 }
6531 } else {
6532 submit_len += bvec->bv_len;
6533 nr_pages ++;
6534 bvec++;
6535 }
6536 }
6537
Josef Bacik02f57c72011-04-06 14:25:44 -04006538submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006539 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006540 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006541 if (!ret)
6542 return 0;
6543
6544 bio_put(bio);
6545out_err:
6546 dip->errors = 1;
6547 /*
6548 * before atomic variable goto zero, we must
6549 * make sure dip->errors is perceived to be set.
6550 */
6551 smp_mb__before_atomic_dec();
6552 if (atomic_dec_and_test(&dip->pending_bios))
6553 bio_io_error(dip->orig_bio);
6554
6555 /* bio_end_io() will handle error, so we needn't return it */
6556 return 0;
6557}
6558
Josef Bacik4b46fce2010-05-23 11:00:55 -04006559static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6560 loff_t file_offset)
6561{
6562 struct btrfs_root *root = BTRFS_I(inode)->root;
6563 struct btrfs_dio_private *dip;
6564 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006565 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006566 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006567 int ret = 0;
6568
6569 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6570
6571 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6572 if (!dip) {
6573 ret = -ENOMEM;
6574 goto free_ordered;
6575 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006576
6577 dip->private = bio->bi_private;
6578 dip->inode = inode;
6579 dip->logical_offset = file_offset;
6580
Josef Bacik4b46fce2010-05-23 11:00:55 -04006581 dip->bytes = 0;
6582 do {
6583 dip->bytes += bvec->bv_len;
6584 bvec++;
6585 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6586
Chris Mason46bfbb52010-05-26 11:04:10 -04006587 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006588 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006589 dip->errors = 0;
6590 dip->orig_bio = bio;
6591 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006592
6593 if (write)
6594 bio->bi_end_io = btrfs_endio_direct_write;
6595 else
6596 bio->bi_end_io = btrfs_endio_direct_read;
6597
Miao Xiee65e1532010-11-22 03:04:43 +00006598 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6599 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006600 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006601free_ordered:
6602 /*
6603 * If this is a write, we need to clean up the reserved space and kill
6604 * the ordered extent.
6605 */
6606 if (write) {
6607 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006608 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006609 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6610 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6611 btrfs_free_reserved_extent(root, ordered->start,
6612 ordered->disk_len);
6613 btrfs_put_ordered_extent(ordered);
6614 btrfs_put_ordered_extent(ordered);
6615 }
6616 bio_endio(bio, ret);
6617}
6618
Chris Mason5a5f79b2010-05-26 21:33:37 -04006619static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6620 const struct iovec *iov, loff_t offset,
6621 unsigned long nr_segs)
6622{
6623 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006624 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006625 size_t size;
6626 unsigned long addr;
6627 unsigned blocksize_mask = root->sectorsize - 1;
6628 ssize_t retval = -EINVAL;
6629 loff_t end = offset;
6630
6631 if (offset & blocksize_mask)
6632 goto out;
6633
6634 /* Check the memory alignment. Blocks cannot straddle pages */
6635 for (seg = 0; seg < nr_segs; seg++) {
6636 addr = (unsigned long)iov[seg].iov_base;
6637 size = iov[seg].iov_len;
6638 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006639 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006640 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006641
6642 /* If this is a write we don't need to check anymore */
6643 if (rw & WRITE)
6644 continue;
6645
6646 /*
6647 * Check to make sure we don't have duplicate iov_base's in this
6648 * iovec, if so return EINVAL, otherwise we'll get csum errors
6649 * when reading back.
6650 */
6651 for (i = seg + 1; i < nr_segs; i++) {
6652 if (iov[seg].iov_base == iov[i].iov_base)
6653 goto out;
6654 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006655 }
6656 retval = 0;
6657out:
6658 return retval;
6659}
Josef Bacikeb838e72012-07-31 16:28:48 -04006660
Chris Mason16432982008-04-10 10:23:21 -04006661static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6662 const struct iovec *iov, loff_t offset,
6663 unsigned long nr_segs)
6664{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006665 struct file *file = iocb->ki_filp;
6666 struct inode *inode = file->f_mapping->host;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006667
Chris Mason5a5f79b2010-05-26 21:33:37 -04006668 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006669 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006670 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006671
Josef Bacikeb838e72012-07-31 16:28:48 -04006672 return __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006673 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6674 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6675 btrfs_submit_direct, 0);
Chris Mason16432982008-04-10 10:23:21 -04006676}
6677
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006678#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6679
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006680static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6681 __u64 start, __u64 len)
6682{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00006683 int ret;
6684
6685 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6686 if (ret)
6687 return ret;
6688
Chris Masonec29ed52011-02-23 16:23:20 -05006689 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006690}
6691
Chris Mason9ebefb182007-06-15 13:50:00 -04006692int btrfs_readpage(struct file *file, struct page *page)
6693{
Chris Masond1310b22008-01-24 16:13:08 -05006694 struct extent_io_tree *tree;
6695 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006696 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006697}
Chris Mason1832a6d2007-12-21 16:27:21 -05006698
Chris Mason39279cc2007-06-12 06:35:45 -04006699static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6700{
Chris Masond1310b22008-01-24 16:13:08 -05006701 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006702
6703
6704 if (current->flags & PF_MEMALLOC) {
6705 redirty_page_for_writepage(wbc, page);
6706 unlock_page(page);
6707 return 0;
6708 }
Chris Masond1310b22008-01-24 16:13:08 -05006709 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006710 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6711}
Chris Mason39279cc2007-06-12 06:35:45 -04006712
Chris Masonf4219502008-07-22 11:18:09 -04006713int btrfs_writepages(struct address_space *mapping,
6714 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006715{
Chris Masond1310b22008-01-24 16:13:08 -05006716 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006717
Chris Masond1310b22008-01-24 16:13:08 -05006718 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006719 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6720}
6721
Chris Mason3ab2fb52007-11-08 10:59:22 -05006722static int
6723btrfs_readpages(struct file *file, struct address_space *mapping,
6724 struct list_head *pages, unsigned nr_pages)
6725{
Chris Masond1310b22008-01-24 16:13:08 -05006726 struct extent_io_tree *tree;
6727 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006728 return extent_readpages(tree, mapping, pages, nr_pages,
6729 btrfs_get_extent);
6730}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006731static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006732{
Chris Masond1310b22008-01-24 16:13:08 -05006733 struct extent_io_tree *tree;
6734 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006735 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006736
Chris Masond1310b22008-01-24 16:13:08 -05006737 tree = &BTRFS_I(page->mapping->host)->io_tree;
6738 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006739 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006740 if (ret == 1) {
6741 ClearPagePrivate(page);
6742 set_page_private(page, 0);
6743 page_cache_release(page);
6744 }
6745 return ret;
6746}
Chris Mason39279cc2007-06-12 06:35:45 -04006747
Chris Masone6dcd2d2008-07-17 12:53:50 -04006748static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6749{
Chris Mason98509cf2008-09-11 15:51:43 -04006750 if (PageWriteback(page) || PageDirty(page))
6751 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006752 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006753}
6754
Chris Masona52d9a82007-08-27 16:49:44 -04006755static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6756{
Josef Bacik5fd02042012-05-02 14:00:54 -04006757 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006758 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006759 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006760 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006761 u64 page_start = page_offset(page);
6762 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006763
Chris Mason8b62b722009-09-02 16:53:46 -04006764 /*
6765 * we have the page locked, so new writeback can't start,
6766 * and the dirty bit won't be cleared while we are here.
6767 *
6768 * Wait for IO on this page so that we can safely clear
6769 * the PagePrivate2 bit and do ordered accounting
6770 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006771 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006772
Josef Bacik5fd02042012-05-02 14:00:54 -04006773 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006774 if (offset) {
6775 btrfs_releasepage(page, GFP_NOFS);
6776 return;
6777 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006778 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Miao Xie4eee4fa2012-12-21 09:17:45 +00006779 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
Chris Masone6dcd2d2008-07-17 12:53:50 -04006780 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006781 /*
6782 * IO on this page will never be started, so we need
6783 * to account for any ordered extents now
6784 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006785 clear_extent_bit(tree, page_start, page_end,
6786 EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006787 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6788 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006789 /*
6790 * whoever cleared the private bit is responsible
6791 * for the finish_ordered_io
6792 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006793 if (TestClearPagePrivate2(page) &&
6794 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6795 PAGE_CACHE_SIZE, 1)) {
6796 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006797 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006798 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006799 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006800 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006801 }
6802 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006803 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06006804 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6805 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006806 __btrfs_releasepage(page, GFP_NOFS);
6807
Chris Mason4a096752008-07-21 10:29:44 -04006808 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006809 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006810 ClearPagePrivate(page);
6811 set_page_private(page, 0);
6812 page_cache_release(page);
6813 }
Chris Mason39279cc2007-06-12 06:35:45 -04006814}
6815
Chris Mason9ebefb182007-06-15 13:50:00 -04006816/*
6817 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6818 * called from a page fault handler when a page is first dirtied. Hence we must
6819 * be careful to check for EOF conditions here. We set the page up correctly
6820 * for a written page which means we get ENOSPC checking when writing into
6821 * holes and correct delalloc and unwritten extent mapping on filesystems that
6822 * support these features.
6823 *
6824 * We are not allowed to take the i_mutex here so we have to play games to
6825 * protect against truncate races as the page could now be beyond EOF. Because
6826 * vmtruncate() writes the inode size before removing pages, once we have the
6827 * page lock we can determine safely if the page is beyond EOF. If it is not
6828 * beyond EOF, then the page is guaranteed safe against truncation until we
6829 * unlock the page.
6830 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006831int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006832{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006833 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006834 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006835 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006836 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6837 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006838 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006839 char *kaddr;
6840 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006841 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006842 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006843 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006844 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006845 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006846
Jan Karab2b5ef52012-06-12 16:20:45 +02006847 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006848 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006849 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006850 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006851 reserved = 1;
6852 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006853 if (ret) {
6854 if (ret == -ENOMEM)
6855 ret = VM_FAULT_OOM;
6856 else /* -ENOSPC, -EIO, etc */
6857 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006858 if (reserved)
6859 goto out;
6860 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006861 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006862
Nick Piggin56a76f82009-03-31 15:23:23 -07006863 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006864again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006865 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006866 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006867 page_start = page_offset(page);
6868 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006869
Chris Mason9ebefb182007-06-15 13:50:00 -04006870 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006871 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006872 /* page got truncated out from underneath us */
6873 goto out_unlock;
6874 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006875 wait_on_page_writeback(page);
6876
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006877 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006878 set_page_extent_mapped(page);
6879
Chris Masoneb84ae02008-07-17 13:53:27 -04006880 /*
6881 * we can't set the delalloc bits if there are pending ordered
6882 * extents. Drop our locks and wait for them to finish
6883 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006884 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6885 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006886 unlock_extent_cached(io_tree, page_start, page_end,
6887 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006888 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006889 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006890 btrfs_put_ordered_extent(ordered);
6891 goto again;
6892 }
6893
Josef Bacikfbf19082009-10-01 17:10:23 -04006894 /*
6895 * XXX - page_mkwrite gets called every time the page is dirtied, even
6896 * if it was already dirty, so for space accounting reasons we need to
6897 * clear any delalloc bits for the range we are fixing to save. There
6898 * is probably a better way to do this, but for now keep consistent with
6899 * prepare_pages in the normal write path.
6900 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006901 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06006902 EXTENT_DIRTY | EXTENT_DELALLOC |
6903 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006904 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006905
Josef Bacik2ac55d42010-02-03 19:33:23 +00006906 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6907 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006908 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006909 unlock_extent_cached(io_tree, page_start, page_end,
6910 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006911 ret = VM_FAULT_SIGBUS;
6912 goto out_unlock;
6913 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006914 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006915
6916 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006917 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006918 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006919 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006920 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006921
Chris Masone6dcd2d2008-07-17 12:53:50 -04006922 if (zero_start != PAGE_CACHE_SIZE) {
6923 kaddr = kmap(page);
6924 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6925 flush_dcache_page(page);
6926 kunmap(page);
6927 }
Chris Mason247e7432008-07-17 12:53:51 -04006928 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006929 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006930 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006931
Chris Mason257c62e2009-10-13 13:21:08 -04006932 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6933 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06006934 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04006935
Josef Bacik2ac55d42010-02-03 19:33:23 +00006936 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006937
6938out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006939 if (!ret) {
6940 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006941 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006942 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006943 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006944out:
Josef Bacikec39e182012-01-12 19:10:12 -05006945 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006946out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006947 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006948 return ret;
6949}
6950
Josef Bacika41ad392011-01-31 15:30:16 -05006951static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006952{
6953 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006954 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006955 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006956 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006957 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04006958 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006959 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006960
Josef Bacik2aaa6652012-08-29 14:27:18 -04006961 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
Josef Bacik5d5e1032009-10-13 16:46:49 -04006962 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006963 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006964
Chris Mason4a096752008-07-21 10:29:44 -04006965 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006966 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006967
Josef Bacikfcb80c22011-05-03 10:40:22 -04006968 /*
6969 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6970 * 3 things going on here
6971 *
6972 * 1) We need to reserve space for our orphan item and the space to
6973 * delete our orphan item. Lord knows we don't want to have a dangling
6974 * orphan item because we didn't reserve space to remove it.
6975 *
6976 * 2) We need to reserve space to update our inode.
6977 *
6978 * 3) We need to have something to cache all the space that is going to
6979 * be free'd up by the truncate operation, but also have some slack
6980 * space reserved in case it uses space during the truncate (thank you
6981 * very much snapshotting).
6982 *
6983 * And we need these to all be seperate. The fact is we can use alot of
6984 * space doing the truncate, and we have no earthly idea how much space
6985 * we will use, so we need the truncate reservation to be seperate so it
6986 * doesn't end up using space reserved for updating the inode or
6987 * removing the orphan item. We also need to be able to stop the
6988 * transaction and start a new one, which means we need to be able to
6989 * update the inode several times, and we have no idea of knowing how
6990 * many times that will be, so we can't just reserve 1 item for the
6991 * entirety of the opration, so that has to be done seperately as well.
6992 * Then there is the orphan item, which does indeed need to be held on
6993 * to for the whole operation, and we need nobody to touch this reserved
6994 * space except the orphan code.
6995 *
6996 * So that leaves us with
6997 *
6998 * 1) root->orphan_block_rsv - for the orphan deletion.
6999 * 2) rsv - for the truncate reservation, which we will steal from the
7000 * transaction reservation.
7001 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7002 * updating the inode.
7003 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06007004 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007005 if (!rsv)
7006 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04007007 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04007008 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05007009
Josef Bacik907cbce2011-08-08 13:46:15 -04007010 /*
Josef Bacik07127182011-08-19 10:29:59 -04007011 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04007012 * 1 for updating the inode.
7013 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05007014 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007015 if (IS_ERR(trans)) {
7016 err = PTR_ERR(trans);
7017 goto out;
7018 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05007019
Josef Bacik907cbce2011-08-08 13:46:15 -04007020 /* Migrate the slack space for the truncate to our reserve */
7021 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7022 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04007023 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05007024
Chris Mason5a3f23d2009-03-31 13:27:11 -04007025 /*
7026 * setattr is responsible for setting the ordered_data_close flag,
7027 * but that is only tested during the last file release. That
7028 * could happen well after the next commit, leaving a great big
7029 * window where new writes may get lost if someone chooses to write
7030 * to this file after truncating to zero
7031 *
7032 * The inode doesn't have any dirty data here, and so if we commit
7033 * this is a noop. If someone immediately starts writing to the inode
7034 * it is very likely we'll catch some of their writes in this
7035 * transaction, and the commit will find this file on the ordered
7036 * data list with good things to send down.
7037 *
7038 * This is a best effort solution, there is still a window where
7039 * using truncate to replace the contents of the file will
7040 * end up with a zero length file after a crash.
7041 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04007042 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7043 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007044 btrfs_add_ordered_operation(trans, root, inode);
7045
Josef Bacik5dc562c2012-08-17 13:14:17 -04007046 /*
7047 * So if we truncate and then write and fsync we normally would just
7048 * write the extents that changed, which is a problem if we need to
7049 * first truncate that entire inode. So set this flag so we write out
7050 * all of the extents in the inode to the sync log so we're completely
7051 * safe.
7052 */
7053 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007054 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007055
Yan, Zheng80825102009-11-12 09:35:36 +00007056 while (1) {
7057 ret = btrfs_truncate_inode_items(trans, root, inode,
7058 inode->i_size,
7059 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007060 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05007061 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007062 break;
Josef Bacik3893e332011-01-31 16:03:11 -05007063 }
Chris Mason39279cc2007-06-12 06:35:45 -04007064
Josef Bacikfcb80c22011-05-03 10:40:22 -04007065 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007066 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007067 if (ret) {
7068 err = ret;
7069 break;
7070 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04007071
Yan, Zheng80825102009-11-12 09:35:36 +00007072 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007073 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04007074
7075 trans = btrfs_start_transaction(root, 2);
7076 if (IS_ERR(trans)) {
7077 ret = err = PTR_ERR(trans);
7078 trans = NULL;
7079 break;
7080 }
7081
7082 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7083 rsv, min_size);
7084 BUG_ON(ret); /* shouldn't happen */
7085 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007086 }
7087
7088 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04007089 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00007090 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05007091 if (ret)
7092 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00007093 }
7094
Chris Mason917c16b2011-11-08 14:49:59 -05007095 if (trans) {
7096 trans->block_rsv = &root->fs_info->trans_block_rsv;
7097 ret = btrfs_update_inode(trans, root, inode);
7098 if (ret && !err)
7099 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04007100
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007101 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00007102 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05007103 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04007104
7105out:
7106 btrfs_free_block_rsv(root, rsv);
7107
Josef Bacik3893e332011-01-31 16:03:11 -05007108 if (ret && !err)
7109 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05007110
Josef Bacik3893e332011-01-31 16:03:11 -05007111 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007112}
7113
Sven Wegener3b963622008-06-09 21:57:42 -04007114/*
Chris Masond352ac62008-09-29 15:18:18 -04007115 * create a new subvolume directory/inode (helper for the ioctl).
7116 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05007117int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007118 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04007119{
Chris Mason39279cc2007-06-12 06:35:45 -04007120 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04007121 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007122 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007123
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01007124 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7125 new_dirid, new_dirid,
7126 S_IFDIR | (~current_umask() & S_IRWXUGO),
7127 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007128 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007129 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007130 inode->i_op = &btrfs_dir_inode_operations;
7131 inode->i_fop = &btrfs_dir_file_operations;
7132
Miklos Szeredibfe86842011-10-28 14:13:29 +02007133 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007134 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007135
Yan, Zheng76dda932009-09-21 16:00:26 -04007136 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007137
Yan, Zheng76dda932009-09-21 16:00:26 -04007138 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007139 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007140}
7141
Chris Mason39279cc2007-06-12 06:35:45 -04007142struct inode *btrfs_alloc_inode(struct super_block *sb)
7143{
7144 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007145 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007146
7147 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7148 if (!ei)
7149 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007150
7151 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007152 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007153 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007154 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007155 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007156 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007157 ei->disk_i_size = 0;
7158 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007159 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007160 ei->index_cnt = (u64)-1;
7161 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06007162 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007163
Josef Bacik9e0baf62011-07-15 15:16:44 +00007164 spin_lock_init(&ei->lock);
7165 ei->outstanding_extents = 0;
7166 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007167
Josef Bacik72ac3c02012-05-23 14:13:11 -04007168 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007169 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007170
Miao Xie16cdcec2011-04-22 18:12:22 +08007171 ei->delayed_node = NULL;
7172
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007173 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007174 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007175 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7176 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007177 ei->io_tree.track_uptodate = 1;
7178 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05007179 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007180 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007181 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007182 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007183 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007184 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007185 RB_CLEAR_NODE(&ei->rb_node);
7186
7187 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007188}
7189
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007190static void btrfs_i_callback(struct rcu_head *head)
7191{
7192 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007193 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7194}
7195
Chris Mason39279cc2007-06-12 06:35:45 -04007196void btrfs_destroy_inode(struct inode *inode)
7197{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007198 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007199 struct btrfs_root *root = BTRFS_I(inode)->root;
7200
Al Virob3d9b7a2012-06-09 13:51:19 -04007201 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007202 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007203 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7204 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007205 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7206 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007207
Chris Mason5a3f23d2009-03-31 13:27:11 -04007208 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007209 * This can happen where we create an inode, but somebody else also
7210 * created the same inode and we need to destroy the one we already
7211 * created.
7212 */
7213 if (!root)
7214 goto free;
7215
7216 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007217 * Make sure we're properly removed from the ordered operation
7218 * lists.
7219 */
7220 smp_mb();
7221 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7222 spin_lock(&root->fs_info->ordered_extent_lock);
7223 list_del_init(&BTRFS_I(inode)->ordered_operations);
7224 spin_unlock(&root->fs_info->ordered_extent_lock);
7225 }
7226
Josef Bacik8a35d952012-05-23 14:26:42 -04007227 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7228 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007229 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7230 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007231 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007232 }
Josef Bacik7b128762008-07-24 12:17:14 -04007233
Chris Masond3977122009-01-05 21:25:51 -05007234 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007235 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7236 if (!ordered)
7237 break;
7238 else {
Chris Masond3977122009-01-05 21:25:51 -05007239 printk(KERN_ERR "btrfs found ordered "
7240 "extent %llu %llu on inode cleanup\n",
7241 (unsigned long long)ordered->file_offset,
7242 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007243 btrfs_remove_ordered_extent(inode, ordered);
7244 btrfs_put_ordered_extent(ordered);
7245 btrfs_put_ordered_extent(ordered);
7246 }
7247 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007248 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007249 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007250free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007251 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007252 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007253}
7254
Al Viro45321ac2010-06-07 13:43:19 -04007255int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007256{
7257 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007258
Josef Bacik0af3d002010-06-21 14:48:16 -04007259 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007260 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007261 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007262 else
Al Viro45321ac2010-06-07 13:43:19 -04007263 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007264}
7265
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007266static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007267{
7268 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7269
7270 inode_init_once(&ei->vfs_inode);
7271}
7272
7273void btrfs_destroy_cachep(void)
7274{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10007275 /*
7276 * Make sure all delayed rcu free inodes are flushed before we
7277 * destroy cache.
7278 */
7279 rcu_barrier();
Chris Mason39279cc2007-06-12 06:35:45 -04007280 if (btrfs_inode_cachep)
7281 kmem_cache_destroy(btrfs_inode_cachep);
7282 if (btrfs_trans_handle_cachep)
7283 kmem_cache_destroy(btrfs_trans_handle_cachep);
7284 if (btrfs_transaction_cachep)
7285 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007286 if (btrfs_path_cachep)
7287 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007288 if (btrfs_free_space_cachep)
7289 kmem_cache_destroy(btrfs_free_space_cachep);
Miao Xie8ccf6f192012-10-25 09:28:04 +00007290 if (btrfs_delalloc_work_cachep)
7291 kmem_cache_destroy(btrfs_delalloc_work_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007292}
7293
7294int btrfs_init_cachep(void)
7295{
David Sterba837e1972012-09-07 03:00:48 -06007296 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007297 sizeof(struct btrfs_inode), 0,
7298 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007299 if (!btrfs_inode_cachep)
7300 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007301
David Sterba837e1972012-09-07 03:00:48 -06007302 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007303 sizeof(struct btrfs_trans_handle), 0,
7304 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007305 if (!btrfs_trans_handle_cachep)
7306 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007307
David Sterba837e1972012-09-07 03:00:48 -06007308 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007309 sizeof(struct btrfs_transaction), 0,
7310 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007311 if (!btrfs_transaction_cachep)
7312 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007313
David Sterba837e1972012-09-07 03:00:48 -06007314 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007315 sizeof(struct btrfs_path), 0,
7316 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007317 if (!btrfs_path_cachep)
7318 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007319
David Sterba837e1972012-09-07 03:00:48 -06007320 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05007321 sizeof(struct btrfs_free_space), 0,
7322 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7323 if (!btrfs_free_space_cachep)
7324 goto fail;
7325
Miao Xie8ccf6f192012-10-25 09:28:04 +00007326 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7327 sizeof(struct btrfs_delalloc_work), 0,
7328 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7329 NULL);
7330 if (!btrfs_delalloc_work_cachep)
7331 goto fail;
7332
Chris Mason39279cc2007-06-12 06:35:45 -04007333 return 0;
7334fail:
7335 btrfs_destroy_cachep();
7336 return -ENOMEM;
7337}
7338
7339static int btrfs_getattr(struct vfsmount *mnt,
7340 struct dentry *dentry, struct kstat *stat)
7341{
Miao Xiedf0af1a52013-01-29 10:11:59 +00007342 u64 delalloc_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04007343 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007344 u32 blocksize = inode->i_sb->s_blocksize;
7345
Chris Mason39279cc2007-06-12 06:35:45 -04007346 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007347 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007348 stat->blksize = PAGE_CACHE_SIZE;
Miao Xiedf0af1a52013-01-29 10:11:59 +00007349
7350 spin_lock(&BTRFS_I(inode)->lock);
7351 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7352 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05007353 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a52013-01-29 10:11:59 +00007354 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007355 return 0;
7356}
7357
Liu Bo75e7cb72011-03-22 10:12:20 +00007358/*
7359 * If a file is moved, it will inherit the cow and compression flags of the new
7360 * directory.
7361 */
7362static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7363{
7364 struct btrfs_inode *b_dir = BTRFS_I(dir);
7365 struct btrfs_inode *b_inode = BTRFS_I(inode);
7366
7367 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7368 b_inode->flags |= BTRFS_INODE_NODATACOW;
7369 else
7370 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7371
Liu Bobc178232012-06-14 02:23:18 -06007372 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007373 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007374 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7375 } else {
7376 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7377 BTRFS_INODE_NOCOMPRESS);
7378 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007379}
7380
Chris Masond3977122009-01-05 21:25:51 -05007381static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7382 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007383{
7384 struct btrfs_trans_handle *trans;
7385 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007386 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007387 struct inode *new_inode = new_dentry->d_inode;
7388 struct inode *old_inode = old_dentry->d_inode;
7389 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007390 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007391 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007392 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007393 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007394
Li Zefan33345d012011-04-20 10:31:50 +08007395 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007396 return -EPERM;
7397
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007398 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007399 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007400 return -EXDEV;
7401
Li Zefan33345d012011-04-20 10:31:50 +08007402 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7403 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007404 return -ENOTEMPTY;
7405
Chris Mason39279cc2007-06-12 06:35:45 -04007406 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007407 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007408 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05007409
7410
7411 /* check for collisions, even if the name isn't there */
7412 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7413 new_dentry->d_name.name,
7414 new_dentry->d_name.len);
7415
7416 if (ret) {
7417 if (ret == -EEXIST) {
7418 /* we shouldn't get
7419 * eexist without a new_inode */
7420 if (!new_inode) {
7421 WARN_ON(1);
7422 return ret;
7423 }
7424 } else {
7425 /* maybe -EOVERFLOW */
7426 return ret;
7427 }
7428 }
7429 ret = 0;
7430
Chris Mason5a3f23d2009-03-31 13:27:11 -04007431 /*
7432 * we're using rename to replace one file with another.
7433 * and the replacement file is large. Start IO on it now so
7434 * we don't add too much work to the end of the transaction
7435 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007436 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007437 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7438 filemap_flush(old_inode->i_mapping);
7439
Yan, Zheng76dda932009-09-21 16:00:26 -04007440 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007441 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007442 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007443 /*
7444 * We want to reserve the absolute worst case amount of items. So if
7445 * both inodes are subvols and we need to unlink them then that would
7446 * require 4 item modifications, but if they are both normal inodes it
7447 * would require 5 item modifications, so we'll assume their normal
7448 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7449 * should cover the worst case number of items we'll modify.
7450 */
7451 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007452 if (IS_ERR(trans)) {
7453 ret = PTR_ERR(trans);
7454 goto out_notrans;
7455 }
Chris Mason5f39d392007-10-15 16:14:19 -04007456
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007457 if (dest != root)
7458 btrfs_record_root_in_trans(trans, dest);
7459
Yan, Zhenga5719522009-09-24 09:17:31 -04007460 ret = btrfs_set_inode_index(new_dir, &index);
7461 if (ret)
7462 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007463
Li Zefan33345d012011-04-20 10:31:50 +08007464 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007465 /* force full log commit if subvolume involved. */
7466 root->fs_info->last_trans_log_full_commit = trans->transid;
7467 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007468 ret = btrfs_insert_inode_ref(trans, dest,
7469 new_dentry->d_name.name,
7470 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007471 old_ino,
7472 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007473 if (ret)
7474 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007475 /*
7476 * this is an ugly little race, but the rename is required
7477 * to make sure that if we crash, the inode is either at the
7478 * old name or the new one. pinning the log transaction lets
7479 * us make sure we don't allow a log commit to come in after
7480 * we unlink the name but before we add the new name back in.
7481 */
7482 btrfs_pin_log_trans(root);
7483 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007484 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007485 * make sure the inode gets flushed if it is replacing
7486 * something.
7487 */
Li Zefan33345d012011-04-20 10:31:50 +08007488 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007489 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007490
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007491 inode_inc_iversion(old_dir);
7492 inode_inc_iversion(new_dir);
7493 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007494 old_dir->i_ctime = old_dir->i_mtime = ctime;
7495 new_dir->i_ctime = new_dir->i_mtime = ctime;
7496 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007497
Chris Mason12fcfd22009-03-24 10:24:20 -04007498 if (old_dentry->d_parent != new_dentry->d_parent)
7499 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7500
Li Zefan33345d012011-04-20 10:31:50 +08007501 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007502 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7503 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7504 old_dentry->d_name.name,
7505 old_dentry->d_name.len);
7506 } else {
Al Viro92986792011-03-04 17:14:37 +00007507 ret = __btrfs_unlink_inode(trans, root, old_dir,
7508 old_dentry->d_inode,
7509 old_dentry->d_name.name,
7510 old_dentry->d_name.len);
7511 if (!ret)
7512 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007513 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007514 if (ret) {
7515 btrfs_abort_transaction(trans, root, ret);
7516 goto out_fail;
7517 }
Chris Mason39279cc2007-06-12 06:35:45 -04007518
7519 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007520 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007521 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007522 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007523 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7524 root_objectid = BTRFS_I(new_inode)->location.objectid;
7525 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7526 root_objectid,
7527 new_dentry->d_name.name,
7528 new_dentry->d_name.len);
7529 BUG_ON(new_inode->i_nlink == 0);
7530 } else {
7531 ret = btrfs_unlink_inode(trans, dest, new_dir,
7532 new_dentry->d_inode,
7533 new_dentry->d_name.name,
7534 new_dentry->d_name.len);
7535 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007536 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007537 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007538 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007539 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007540 if (ret) {
7541 btrfs_abort_transaction(trans, root, ret);
7542 goto out_fail;
7543 }
Chris Mason39279cc2007-06-12 06:35:45 -04007544 }
Josef Bacikaec74772008-07-24 12:12:38 -04007545
Liu Bo75e7cb72011-03-22 10:12:20 +00007546 fixup_inode_flags(new_dir, old_inode);
7547
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007548 ret = btrfs_add_link(trans, new_dir, old_inode,
7549 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007550 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007551 if (ret) {
7552 btrfs_abort_transaction(trans, root, ret);
7553 goto out_fail;
7554 }
Chris Mason39279cc2007-06-12 06:35:45 -04007555
Li Zefan33345d012011-04-20 10:31:50 +08007556 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007557 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007558 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007559 btrfs_end_log_trans(root);
7560 }
Chris Mason39279cc2007-06-12 06:35:45 -04007561out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007562 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007563out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007564 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007565 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007566
Chris Mason39279cc2007-06-12 06:35:45 -04007567 return ret;
7568}
7569
Miao Xie8ccf6f192012-10-25 09:28:04 +00007570static void btrfs_run_delalloc_work(struct btrfs_work *work)
7571{
7572 struct btrfs_delalloc_work *delalloc_work;
7573
7574 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7575 work);
7576 if (delalloc_work->wait)
7577 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7578 else
7579 filemap_flush(delalloc_work->inode->i_mapping);
7580
7581 if (delalloc_work->delay_iput)
7582 btrfs_add_delayed_iput(delalloc_work->inode);
7583 else
7584 iput(delalloc_work->inode);
7585 complete(&delalloc_work->completion);
7586}
7587
7588struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7589 int wait, int delay_iput)
7590{
7591 struct btrfs_delalloc_work *work;
7592
7593 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7594 if (!work)
7595 return NULL;
7596
7597 init_completion(&work->completion);
7598 INIT_LIST_HEAD(&work->list);
7599 work->inode = inode;
7600 work->wait = wait;
7601 work->delay_iput = delay_iput;
7602 work->work.func = btrfs_run_delalloc_work;
7603
7604 return work;
7605}
7606
7607void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7608{
7609 wait_for_completion(&work->completion);
7610 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7611}
7612
Chris Masond352ac62008-09-29 15:18:18 -04007613/*
7614 * some fairly slow code that needs optimization. This walks the list
7615 * of all the inodes with pending delalloc and forces them to disk.
7616 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007617int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007618{
Chris Masonea8c2812008-08-04 23:17:27 -04007619 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007620 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007621 struct btrfs_delalloc_work *work, *next;
7622 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00007623 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007624 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04007625
Yan Zhengc146afa2008-11-12 14:34:12 -05007626 if (root->fs_info->sb->s_flags & MS_RDONLY)
7627 return -EROFS;
7628
Miao Xie8ccf6f192012-10-25 09:28:04 +00007629 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007630 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00007631
Chris Mason75eff682008-12-15 15:54:40 -05007632 spin_lock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007633 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
7634 while (!list_empty(&splice)) {
7635 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04007636 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007637
7638 list_del_init(&binode->delalloc_inodes);
7639
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007640 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a52013-01-29 10:11:59 +00007641 if (!inode) {
7642 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
7643 &binode->runtime_flags);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007644 continue;
Miao Xiedf0af1a52013-01-29 10:11:59 +00007645 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007646
7647 list_add_tail(&binode->delalloc_inodes,
7648 &root->fs_info->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007649 spin_unlock(&root->fs_info->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007650
7651 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7652 if (unlikely(!work)) {
7653 ret = -ENOMEM;
7654 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007655 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007656 list_add_tail(&work->list, &works);
7657 btrfs_queue_worker(&root->fs_info->flush_workers,
7658 &work->work);
7659
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007660 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007661 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007662 }
Chris Mason75eff682008-12-15 15:54:40 -05007663 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007664
Miao Xie1eafa6c2013-01-22 10:49:00 +00007665 list_for_each_entry_safe(work, next, &works, list) {
7666 list_del_init(&work->list);
7667 btrfs_wait_and_free_delalloc_work(work);
7668 }
7669
Chris Mason8c8bee12008-09-29 11:19:10 -04007670 /* the filemap_flush will queue IO into the worker threads, but
7671 * we have to make sure the IO is actually started and that
7672 * ordered extents get created before we return
7673 */
7674 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007675 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007676 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007677 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007678 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7679 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007680 }
7681 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xie1eafa6c2013-01-22 10:49:00 +00007682 return 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00007683out:
7684 list_for_each_entry_safe(work, next, &works, list) {
7685 list_del_init(&work->list);
7686 btrfs_wait_and_free_delalloc_work(work);
7687 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00007688
7689 if (!list_empty_careful(&splice)) {
7690 spin_lock(&root->fs_info->delalloc_lock);
7691 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
7692 spin_unlock(&root->fs_info->delalloc_lock);
7693 }
Miao Xie8ccf6f192012-10-25 09:28:04 +00007694 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04007695}
7696
Chris Mason39279cc2007-06-12 06:35:45 -04007697static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7698 const char *symname)
7699{
7700 struct btrfs_trans_handle *trans;
7701 struct btrfs_root *root = BTRFS_I(dir)->root;
7702 struct btrfs_path *path;
7703 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007704 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007705 int err;
7706 int drop_inode = 0;
7707 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007708 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007709 int name_len;
7710 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007711 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007712 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007713 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04007714
7715 name_len = strlen(symname) + 1;
7716 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7717 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007718
Josef Bacik9ed74f22009-09-11 16:12:44 -04007719 /*
7720 * 2 items for inode item and ref
7721 * 2 items for dir items
7722 * 1 item for xattr if selinux is on
7723 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007724 trans = btrfs_start_transaction(root, 5);
7725 if (IS_ERR(trans))
7726 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007727
Li Zefan581bb052011-04-20 10:06:11 +08007728 err = btrfs_find_free_ino(root, &objectid);
7729 if (err)
7730 goto out_unlock;
7731
Josef Bacikaec74772008-07-24 12:12:38 -04007732 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007733 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007734 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007735 if (IS_ERR(inode)) {
7736 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007737 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007738 }
Chris Mason39279cc2007-06-12 06:35:45 -04007739
Eric Paris2a7dba32011-02-01 11:05:39 -05007740 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007741 if (err) {
7742 drop_inode = 1;
7743 goto out_unlock;
7744 }
7745
Casey Schauflerad19db72011-12-15 10:09:07 -05007746 /*
7747 * If the active LSM wants to access the inode during
7748 * d_instantiate it needs these. Smack checks to see
7749 * if the filesystem supports xattrs by looking at the
7750 * ops vector.
7751 */
7752 inode->i_fop = &btrfs_file_operations;
7753 inode->i_op = &btrfs_file_inode_operations;
7754
Josef Bacika1b075d2010-11-19 20:36:11 +00007755 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007756 if (err)
7757 drop_inode = 1;
7758 else {
7759 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007760 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007761 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007762 }
Chris Mason39279cc2007-06-12 06:35:45 -04007763 if (drop_inode)
7764 goto out_unlock;
7765
7766 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007767 if (!path) {
7768 err = -ENOMEM;
7769 drop_inode = 1;
7770 goto out_unlock;
7771 }
Li Zefan33345d012011-04-20 10:31:50 +08007772 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007773 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007774 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7775 datasize = btrfs_file_extent_calc_inline_size(name_len);
7776 err = btrfs_insert_empty_item(trans, root, path, &key,
7777 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007778 if (err) {
7779 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007780 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007781 goto out_unlock;
7782 }
Chris Mason5f39d392007-10-15 16:14:19 -04007783 leaf = path->nodes[0];
7784 ei = btrfs_item_ptr(leaf, path->slots[0],
7785 struct btrfs_file_extent_item);
7786 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7787 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007788 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007789 btrfs_set_file_extent_encryption(leaf, ei, 0);
7790 btrfs_set_file_extent_compression(leaf, ei, 0);
7791 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7792 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7793
Chris Mason39279cc2007-06-12 06:35:45 -04007794 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007795 write_extent_buffer(leaf, symname, ptr, name_len);
7796 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007797 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007798
Chris Mason39279cc2007-06-12 06:35:45 -04007799 inode->i_op = &btrfs_symlink_inode_operations;
7800 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007801 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007802 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007803 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007804 err = btrfs_update_inode(trans, root, inode);
7805 if (err)
7806 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007807
7808out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007809 if (!err)
7810 d_instantiate(dentry, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007811 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007812 if (drop_inode) {
7813 inode_dec_link_count(inode);
7814 iput(inode);
7815 }
Liu Bob53d3f52012-11-14 14:34:34 +00007816 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04007817 return err;
7818}
Chris Mason16432982008-04-10 10:23:21 -04007819
Josef Bacik0af3d002010-06-21 14:48:16 -04007820static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7821 u64 start, u64 num_bytes, u64 min_size,
7822 loff_t actual_len, u64 *alloc_hint,
7823 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007824{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007825 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7826 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007827 struct btrfs_root *root = BTRFS_I(inode)->root;
7828 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007829 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007830 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007831 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007832 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007833
Josef Bacik0af3d002010-06-21 14:48:16 -04007834 if (trans)
7835 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007836 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007837 if (own_trans) {
7838 trans = btrfs_start_transaction(root, 3);
7839 if (IS_ERR(trans)) {
7840 ret = PTR_ERR(trans);
7841 break;
7842 }
Yan Zhengd899e052008-10-30 14:25:28 -04007843 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007844
Yan, Zhengefa56462010-05-16 10:49:59 -04007845 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007846 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007847 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007848 if (own_trans)
7849 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007850 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007851 }
7852
Yan Zhengd899e052008-10-30 14:25:28 -04007853 ret = insert_reserved_file_extent(trans, inode,
7854 cur_offset, ins.objectid,
7855 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007856 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007857 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007858 if (ret) {
7859 btrfs_abort_transaction(trans, root, ret);
7860 if (own_trans)
7861 btrfs_end_transaction(trans, root);
7862 break;
7863 }
Chris Masona1ed8352009-09-11 12:27:37 -04007864 btrfs_drop_extent_cache(inode, cur_offset,
7865 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007866
Josef Bacik5dc562c2012-08-17 13:14:17 -04007867 em = alloc_extent_map();
7868 if (!em) {
7869 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7870 &BTRFS_I(inode)->runtime_flags);
7871 goto next;
7872 }
7873
7874 em->start = cur_offset;
7875 em->orig_start = cur_offset;
7876 em->len = ins.offset;
7877 em->block_start = ins.objectid;
7878 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05007879 em->orig_block_len = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04007880 em->bdev = root->fs_info->fs_devices->latest_bdev;
7881 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7882 em->generation = trans->transid;
7883
7884 while (1) {
7885 write_lock(&em_tree->lock);
7886 ret = add_extent_mapping(em_tree, em);
7887 if (!ret)
7888 list_move(&em->list,
7889 &em_tree->modified_extents);
7890 write_unlock(&em_tree->lock);
7891 if (ret != -EEXIST)
7892 break;
7893 btrfs_drop_extent_cache(inode, cur_offset,
7894 cur_offset + ins.offset - 1,
7895 0);
7896 }
7897 free_extent_map(em);
7898next:
Yan Zhengd899e052008-10-30 14:25:28 -04007899 num_bytes -= ins.offset;
7900 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007901 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007902
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007903 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007904 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007905 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007906 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007907 (actual_len > inode->i_size) &&
7908 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007909 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007910 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007911 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007912 i_size = cur_offset;
7913 i_size_write(inode, i_size);
7914 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007915 }
7916
Yan Zhengd899e052008-10-30 14:25:28 -04007917 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007918
7919 if (ret) {
7920 btrfs_abort_transaction(trans, root, ret);
7921 if (own_trans)
7922 btrfs_end_transaction(trans, root);
7923 break;
7924 }
Yan Zhengd899e052008-10-30 14:25:28 -04007925
Josef Bacik0af3d002010-06-21 14:48:16 -04007926 if (own_trans)
7927 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007928 }
Yan Zhengd899e052008-10-30 14:25:28 -04007929 return ret;
7930}
7931
Josef Bacik0af3d002010-06-21 14:48:16 -04007932int btrfs_prealloc_file_range(struct inode *inode, int mode,
7933 u64 start, u64 num_bytes, u64 min_size,
7934 loff_t actual_len, u64 *alloc_hint)
7935{
7936 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7937 min_size, actual_len, alloc_hint,
7938 NULL);
7939}
7940
7941int btrfs_prealloc_file_range_trans(struct inode *inode,
7942 struct btrfs_trans_handle *trans, int mode,
7943 u64 start, u64 num_bytes, u64 min_size,
7944 loff_t actual_len, u64 *alloc_hint)
7945{
7946 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7947 min_size, actual_len, alloc_hint, trans);
7948}
7949
Chris Masone6dcd2d2008-07-17 12:53:50 -04007950static int btrfs_set_page_dirty(struct page *page)
7951{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007952 return __set_page_dirty_nobuffers(page);
7953}
7954
Al Viro10556cb2011-06-20 19:28:19 -04007955static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007956{
Li Zefanb83cc962010-12-20 16:04:08 +08007957 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007958 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007959
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007960 if (mask & MAY_WRITE &&
7961 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7962 if (btrfs_root_readonly(root))
7963 return -EROFS;
7964 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7965 return -EACCES;
7966 }
Al Viro2830ba72011-06-20 19:16:29 -04007967 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007968}
Chris Mason39279cc2007-06-12 06:35:45 -04007969
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007970static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007971 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007972 .lookup = btrfs_lookup,
7973 .create = btrfs_create,
7974 .unlink = btrfs_unlink,
7975 .link = btrfs_link,
7976 .mkdir = btrfs_mkdir,
7977 .rmdir = btrfs_rmdir,
7978 .rename = btrfs_rename,
7979 .symlink = btrfs_symlink,
7980 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007981 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007982 .setxattr = btrfs_setxattr,
7983 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007984 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007985 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007986 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007987 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007988};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007989static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007990 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007991 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007992 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007993};
Yan, Zheng76dda932009-09-21 16:00:26 -04007994
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007995static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007996 .llseek = generic_file_llseek,
7997 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007998 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007999 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008000#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04008001 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04008002#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04008003 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04008004 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04008005};
8006
Chris Masond1310b22008-01-24 16:13:08 -05008007static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04008008 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05008009 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04008010 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008011 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008012 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04008013 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05008014 .set_bit_hook = btrfs_set_bit_hook,
8015 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04008016 .merge_extent_hook = btrfs_merge_extent_hook,
8017 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04008018};
8019
Chris Mason35054392009-01-21 13:11:13 -05008020/*
8021 * btrfs doesn't support the bmap operation because swapfiles
8022 * use bmap to make a mapping of extents in the file. They assume
8023 * these extents won't change over the life of the file and they
8024 * use the bmap result to do IO directly to the drive.
8025 *
8026 * the btrfs bmap call would return logical addresses that aren't
8027 * suitable for IO and they also will change frequently as COW
8028 * operations happen. So, swapfile + btrfs == corruption.
8029 *
8030 * For now we're avoiding this by dropping bmap.
8031 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008032static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008033 .readpage = btrfs_readpage,
8034 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04008035 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05008036 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04008037 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04008038 .invalidatepage = btrfs_invalidatepage,
8039 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04008040 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02008041 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04008042};
8043
Alexey Dobriyan7f094102009-09-21 17:01:10 -07008044static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04008045 .readpage = btrfs_readpage,
8046 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04008047 .invalidatepage = btrfs_invalidatepage,
8048 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04008049};
8050
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008051static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008052 .getattr = btrfs_getattr,
8053 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008054 .setxattr = btrfs_setxattr,
8055 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05008056 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008057 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05008058 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008059 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008060 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008061 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008062};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008063static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04008064 .getattr = btrfs_getattr,
8065 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008066 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008067 .setxattr = btrfs_setxattr,
8068 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04008069 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04008070 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008071 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008072 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04008073};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07008074static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04008075 .readlink = generic_readlink,
8076 .follow_link = page_follow_link_light,
8077 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00008078 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05008079 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05008080 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05008081 .setxattr = btrfs_setxattr,
8082 .getxattr = btrfs_getxattr,
8083 .listxattr = btrfs_listxattr,
8084 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02008085 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04008086 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04008087};
Yan, Zheng76dda932009-09-21 16:00:26 -04008088
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04008089const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04008090 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04008091 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04008092};