blob: 878116d9625d71f36d59c0cbf09fd41a7e8ccc88 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050042#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040043#include "ctree.h"
44#include "disk-io.h"
45#include "transaction.h"
46#include "btrfs_inode.h"
47#include "ioctl.h"
48#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040049#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040050#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040051#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020052#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040053#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050054#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050055#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080056#include "inode-map.h"
Chris Mason39279cc2007-06-12 06:35:45 -040057
58struct btrfs_iget_args {
59 u64 ino;
60 struct btrfs_root *root;
61};
62
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070063static const struct inode_operations btrfs_dir_inode_operations;
64static const struct inode_operations btrfs_symlink_inode_operations;
65static const struct inode_operations btrfs_dir_ro_inode_operations;
66static const struct inode_operations btrfs_special_inode_operations;
67static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070068static const struct address_space_operations btrfs_aops;
69static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070070static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050071static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
74struct kmem_cache *btrfs_trans_handle_cachep;
75struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078
79#define S_SHIFT 12
80static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
81 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
82 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
83 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
84 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
85 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
86 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
87 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
88};
89
Josef Bacika41ad392011-01-31 15:30:16 -050090static int btrfs_setsize(struct inode *inode, loff_t newsize);
91static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -040092static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050093static noinline int cow_file_range(struct inode *inode,
94 struct page *locked_page,
95 u64 start, u64 end, int *page_started,
96 unsigned long *nr_written, int unlock);
Chris Mason21151332011-11-10 20:39:08 -050097static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
98 struct btrfs_root *root, struct inode *inode);
Josef Bacik7b128762008-07-24 12:17:14 -040099
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000100static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500101 struct inode *inode, struct inode *dir,
102 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500103{
104 int err;
105
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000106 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500107 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500108 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500109 return err;
110}
111
Chris Masond352ac62008-09-29 15:18:18 -0400112/*
Chris Masonc8b97812008-10-29 14:49:59 -0400113 * this does all the hard work for inserting an inline extent into
114 * the btree. The caller should have done a btrfs_drop_extents so that
115 * no overlapping inline items exist in the btree
116 */
Chris Masond3977122009-01-05 21:25:51 -0500117static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400118 struct btrfs_root *root, struct inode *inode,
119 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000120 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400121 struct page **compressed_pages)
122{
123 struct btrfs_key key;
124 struct btrfs_path *path;
125 struct extent_buffer *leaf;
126 struct page *page = NULL;
127 char *kaddr;
128 unsigned long ptr;
129 struct btrfs_file_extent_item *ei;
130 int err = 0;
131 int ret;
132 size_t cur_size = size;
133 size_t datasize;
134 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400135
Li Zefanfe3f5662011-03-28 08:30:38 +0000136 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400137 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400138
Chris Masond3977122009-01-05 21:25:51 -0500139 path = btrfs_alloc_path();
140 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400141 return -ENOMEM;
142
Chris Masonb9473432009-03-13 11:00:37 -0400143 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400144
Li Zefan33345d012011-04-20 10:31:50 +0800145 key.objectid = btrfs_ino(inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400146 key.offset = start;
147 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400148 datasize = btrfs_file_extent_calc_inline_size(cur_size);
149
150 inode_add_bytes(inode, size);
151 ret = btrfs_insert_empty_item(trans, root, path, &key,
152 datasize);
Chris Masonc8b97812008-10-29 14:49:59 -0400153 if (ret) {
154 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400155 goto fail;
156 }
157 leaf = path->nodes[0];
158 ei = btrfs_item_ptr(leaf, path->slots[0],
159 struct btrfs_file_extent_item);
160 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
161 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
162 btrfs_set_file_extent_encryption(leaf, ei, 0);
163 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
164 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
165 ptr = btrfs_file_extent_inline_start(ei);
166
Li Zefan261507a02010-12-17 14:21:50 +0800167 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400168 struct page *cpage;
169 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500170 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400171 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500172 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400173 PAGE_CACHE_SIZE);
174
Cong Wang7ac687d2011-11-25 23:14:28 +0800175 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400176 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800177 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400178
179 i++;
180 ptr += cur_size;
181 compressed_size -= cur_size;
182 }
183 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800184 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400185 } else {
186 page = find_get_page(inode->i_mapping,
187 start >> PAGE_CACHE_SHIFT);
188 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800189 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400190 offset = start & (PAGE_CACHE_SIZE - 1);
191 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800192 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400193 page_cache_release(page);
194 }
195 btrfs_mark_buffer_dirty(leaf);
196 btrfs_free_path(path);
197
Yan, Zhengc2167752009-11-12 09:34:21 +0000198 /*
199 * we're an inline extent, so nobody can
200 * extend the file past i_size without locking
201 * a page we already have locked.
202 *
203 * We must do any isize and inode updates
204 * before we unlock the pages. Otherwise we
205 * could end up racing with unlink.
206 */
Chris Masonc8b97812008-10-29 14:49:59 -0400207 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100208 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000209
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100210 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400211fail:
212 btrfs_free_path(path);
213 return err;
214}
215
216
217/*
218 * conditionally insert an inline extent into the file. This
219 * does the checks required to make sure the data is small enough
220 * to fit as an inline extent.
221 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400222static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400223 struct btrfs_root *root,
224 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000225 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400226 struct page **compressed_pages)
227{
228 u64 isize = i_size_read(inode);
229 u64 actual_end = min(end + 1, isize);
230 u64 inline_len = actual_end - start;
231 u64 aligned_end = (end + root->sectorsize - 1) &
232 ~((u64)root->sectorsize - 1);
233 u64 hint_byte;
234 u64 data_len = inline_len;
235 int ret;
236
237 if (compressed_size)
238 data_len = compressed_size;
239
240 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400241 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400242 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
243 (!compressed_size &&
244 (actual_end & (root->sectorsize - 1)) == 0) ||
245 end + 1 < isize ||
246 data_len > root->fs_info->max_inline) {
247 return 1;
248 }
249
Josef Bacik5dc562c2012-08-17 13:14:17 -0400250 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400251 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100252 if (ret)
253 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400254
255 if (isize > actual_end)
256 inline_len = min_t(u64, isize, actual_end);
257 ret = insert_inline_extent(trans, root, inode, start,
258 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000259 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400260 if (ret && ret != -ENOSPC) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100261 btrfs_abort_transaction(trans, root, ret);
262 return ret;
Josef Bacik2adcac12012-05-23 16:10:14 -0400263 } else if (ret == -ENOSPC) {
264 return 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100265 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400266
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400267 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400268 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400269 return 0;
270}
271
Chris Mason771ed682008-11-06 22:02:51 -0500272struct async_extent {
273 u64 start;
274 u64 ram_size;
275 u64 compressed_size;
276 struct page **pages;
277 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800278 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500279 struct list_head list;
280};
281
282struct async_cow {
283 struct inode *inode;
284 struct btrfs_root *root;
285 struct page *locked_page;
286 u64 start;
287 u64 end;
288 struct list_head extents;
289 struct btrfs_work work;
290};
291
292static noinline int add_async_extent(struct async_cow *cow,
293 u64 start, u64 ram_size,
294 u64 compressed_size,
295 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800296 unsigned long nr_pages,
297 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500298{
299 struct async_extent *async_extent;
300
301 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100302 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500303 async_extent->start = start;
304 async_extent->ram_size = ram_size;
305 async_extent->compressed_size = compressed_size;
306 async_extent->pages = pages;
307 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800308 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500309 list_add_tail(&async_extent->list, &cow->extents);
310 return 0;
311}
312
Chris Masonc8b97812008-10-29 14:49:59 -0400313/*
Chris Mason771ed682008-11-06 22:02:51 -0500314 * we create compressed extents in two phases. The first
315 * phase compresses a range of pages that have already been
316 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400317 *
Chris Mason771ed682008-11-06 22:02:51 -0500318 * This is done inside an ordered work queue, and the compression
319 * is spread across many cpus. The actual IO submission is step
320 * two, and the ordered work queue takes care of making sure that
321 * happens in the same order things were put onto the queue by
322 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400323 *
Chris Mason771ed682008-11-06 22:02:51 -0500324 * If this code finds it can't get good compression, it puts an
325 * entry onto the work queue to write the uncompressed bytes. This
326 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300327 * are written in the same order that the flusher thread sent them
328 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400329 */
Chris Mason771ed682008-11-06 22:02:51 -0500330static noinline int compress_file_range(struct inode *inode,
331 struct page *locked_page,
332 u64 start, u64 end,
333 struct async_cow *async_cow,
334 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400335{
336 struct btrfs_root *root = BTRFS_I(inode)->root;
337 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400338 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400339 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400340 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500341 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400342 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400343 struct page **pages = NULL;
344 unsigned long nr_pages;
345 unsigned long nr_pages_ret = 0;
346 unsigned long total_compressed = 0;
347 unsigned long total_in = 0;
348 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500349 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400350 int i;
351 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800352 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400353
Liu Bo4cb13e52012-03-29 09:57:45 -0400354 /* if this is a small write inside eof, kick off a defrag */
355 if ((end - start + 1) < 16 * 1024 &&
356 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400357 btrfs_add_inode_defrag(NULL, inode);
358
Chris Mason42dc7ba2008-12-15 11:44:56 -0500359 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400360again:
361 will_compress = 0;
362 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
363 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
364
Chris Masonf03d9301f2009-02-04 09:31:06 -0500365 /*
366 * we don't want to send crud past the end of i_size through
367 * compression, that's just a waste of CPU time. So, if the
368 * end of the file is before the start of our current
369 * requested range of bytes, we bail out to the uncompressed
370 * cleanup code that can deal with all of this.
371 *
372 * It isn't really the fastest way to fix things, but this is a
373 * very uncommon corner.
374 */
375 if (actual_end <= start)
376 goto cleanup_and_bail_uncompressed;
377
Chris Masonc8b97812008-10-29 14:49:59 -0400378 total_compressed = actual_end - start;
379
380 /* we want to make sure that amount of ram required to uncompress
381 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500382 * of a compressed extent to 128k. This is a crucial number
383 * because it also controls how easily we can spread reads across
384 * cpus for decompression.
385 *
386 * We also want to make sure the amount of IO required to do
387 * a random read is reasonably small, so we limit the size of
388 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400389 */
390 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400391 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500392 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400393 total_in = 0;
394 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400395
Chris Mason771ed682008-11-06 22:02:51 -0500396 /*
397 * we do compression for mount -o compress and when the
398 * inode has not been flagged as nocompress. This flag can
399 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400400 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200401 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500402 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000403 (BTRFS_I(inode)->force_compress) ||
404 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400405 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400406 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800407 if (!pages) {
408 /* just bail out to the uncompressed code */
409 goto cont;
410 }
Chris Mason179e29e2007-11-01 11:28:41 -0400411
Li Zefan261507a02010-12-17 14:21:50 +0800412 if (BTRFS_I(inode)->force_compress)
413 compress_type = BTRFS_I(inode)->force_compress;
414
415 ret = btrfs_compress_pages(compress_type,
416 inode->i_mapping, start,
417 total_compressed, pages,
418 nr_pages, &nr_pages_ret,
419 &total_in,
420 &total_compressed,
421 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400422
423 if (!ret) {
424 unsigned long offset = total_compressed &
425 (PAGE_CACHE_SIZE - 1);
426 struct page *page = pages[nr_pages_ret - 1];
427 char *kaddr;
428
429 /* zero the tail end of the last page, we might be
430 * sending it down to disk
431 */
432 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800433 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400434 memset(kaddr + offset, 0,
435 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800436 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400437 }
438 will_compress = 1;
439 }
440 }
Li Zefan560f7d72011-09-08 10:22:01 +0800441cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400442 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400443 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100444 if (IS_ERR(trans)) {
445 ret = PTR_ERR(trans);
446 trans = NULL;
447 goto cleanup_and_out;
448 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400449 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500450
Chris Masonc8b97812008-10-29 14:49:59 -0400451 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500452 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400453 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500454 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400455 */
456 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000457 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400458 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500459 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400460 ret = cow_file_range_inline(trans, root, inode,
461 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000462 total_compressed,
463 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400464 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100465 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500466 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100467 * inline extent creation worked or returned error,
468 * we don't need to create any more async work items.
469 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500470 */
Chris Masonc8b97812008-10-29 14:49:59 -0400471 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400472 &BTRFS_I(inode)->io_tree,
473 start, end, NULL,
474 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400475 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400476 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000477
478 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 goto free_pages_out;
480 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000481 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400482 }
483
484 if (will_compress) {
485 /*
486 * we aren't doing an inline extent round the compressed size
487 * up to a block size boundary so the allocator does sane
488 * things
489 */
490 total_compressed = (total_compressed + blocksize - 1) &
491 ~(blocksize - 1);
492
493 /*
494 * one last check to make sure the compression is really a
495 * win, compare the page count read with the blocks on disk
496 */
497 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
498 ~(PAGE_CACHE_SIZE - 1);
499 if (total_compressed >= total_in) {
500 will_compress = 0;
501 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400502 num_bytes = total_in;
503 }
504 }
505 if (!will_compress && pages) {
506 /*
507 * the compression code ran but failed to make things smaller,
508 * free any pages it allocated and our page pointer array
509 */
510 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400511 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400512 page_cache_release(pages[i]);
513 }
514 kfree(pages);
515 pages = NULL;
516 total_compressed = 0;
517 nr_pages_ret = 0;
518
519 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500520 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
521 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500522 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500523 }
Chris Masonc8b97812008-10-29 14:49:59 -0400524 }
Chris Mason771ed682008-11-06 22:02:51 -0500525 if (will_compress) {
526 *num_added += 1;
527
528 /* the async work queues will take care of doing actual
529 * allocation on disk for these compressed pages,
530 * and will submit them to the elevator.
531 */
532 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800533 total_compressed, pages, nr_pages_ret,
534 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500535
Yan, Zheng24ae6362010-12-06 07:02:36 +0000536 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500537 start += num_bytes;
538 pages = NULL;
539 cond_resched();
540 goto again;
541 }
542 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500543cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500544 /*
545 * No compression, but we still need to write the pages in
546 * the file we've been given so far. redirty the locked
547 * page if it corresponds to our extent and set things up
548 * for the async work queue to run cow_file_range to do
549 * the normal delalloc dance
550 */
551 if (page_offset(locked_page) >= start &&
552 page_offset(locked_page) <= end) {
553 __set_page_dirty_nobuffers(locked_page);
554 /* unlocked later on in the async handlers */
555 }
Li Zefan261507a02010-12-17 14:21:50 +0800556 add_async_extent(async_cow, start, end - start + 1,
557 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500558 *num_added += 1;
559 }
560
561out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100562 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500563
564free_pages_out:
565 for (i = 0; i < nr_pages_ret; i++) {
566 WARN_ON(pages[i]->mapping);
567 page_cache_release(pages[i]);
568 }
Chris Masond3977122009-01-05 21:25:51 -0500569 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500570
571 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100572
573cleanup_and_out:
574 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
575 start, end, NULL,
576 EXTENT_CLEAR_UNLOCK_PAGE |
577 EXTENT_CLEAR_DIRTY |
578 EXTENT_CLEAR_DELALLOC |
579 EXTENT_SET_WRITEBACK |
580 EXTENT_END_WRITEBACK);
581 if (!trans || IS_ERR(trans))
582 btrfs_error(root->fs_info, ret, "Failed to join transaction");
583 else
584 btrfs_abort_transaction(trans, root, ret);
585 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500586}
587
588/*
589 * phase two of compressed writeback. This is the ordered portion
590 * of the code, which only gets called in the order the work was
591 * queued. We walk all the async extents created by compress_file_range
592 * and send them down to the disk.
593 */
594static noinline int submit_compressed_extents(struct inode *inode,
595 struct async_cow *async_cow)
596{
597 struct async_extent *async_extent;
598 u64 alloc_hint = 0;
599 struct btrfs_trans_handle *trans;
600 struct btrfs_key ins;
601 struct extent_map *em;
602 struct btrfs_root *root = BTRFS_I(inode)->root;
603 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
604 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500605 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500606
607 if (list_empty(&async_cow->extents))
608 return 0;
609
Chris Mason771ed682008-11-06 22:02:51 -0500610
Chris Masond3977122009-01-05 21:25:51 -0500611 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500612 async_extent = list_entry(async_cow->extents.next,
613 struct async_extent, list);
614 list_del(&async_extent->list);
615
616 io_tree = &BTRFS_I(inode)->io_tree;
617
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500618retry:
Chris Mason771ed682008-11-06 22:02:51 -0500619 /* did the compression code fall back to uncompressed IO? */
620 if (!async_extent->pages) {
621 int page_started = 0;
622 unsigned long nr_written = 0;
623
624 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000625 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100626 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500627
628 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500629 ret = cow_file_range(inode, async_cow->locked_page,
630 async_extent->start,
631 async_extent->start +
632 async_extent->ram_size - 1,
633 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500634
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100635 /* JDM XXX */
636
Chris Mason771ed682008-11-06 22:02:51 -0500637 /*
638 * if page_started, cow_file_range inserted an
639 * inline extent and took care of all the unlocking
640 * and IO for us. Otherwise, we need to submit
641 * all those pages down to the drive.
642 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500643 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500644 extent_write_locked_range(io_tree,
645 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500646 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500647 async_extent->ram_size - 1,
648 btrfs_get_extent,
649 WB_SYNC_ALL);
650 kfree(async_extent);
651 cond_resched();
652 continue;
653 }
654
655 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100656 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500657
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400658 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100659 if (IS_ERR(trans)) {
660 ret = PTR_ERR(trans);
661 } else {
662 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
663 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500664 async_extent->compressed_size,
665 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500666 0, alloc_hint, &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100667 if (ret)
668 btrfs_abort_transaction(trans, root, ret);
669 btrfs_end_transaction(trans, root);
670 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000671
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500672 if (ret) {
673 int i;
674 for (i = 0; i < async_extent->nr_pages; i++) {
675 WARN_ON(async_extent->pages[i]->mapping);
676 page_cache_release(async_extent->pages[i]);
677 }
678 kfree(async_extent->pages);
679 async_extent->nr_pages = 0;
680 async_extent->pages = NULL;
681 unlock_extent(io_tree, async_extent->start,
682 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100683 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100684 if (ret == -ENOSPC)
685 goto retry;
686 goto out_free; /* JDM: Requeue? */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500687 }
688
Yan, Zhengc2167752009-11-12 09:34:21 +0000689 /*
690 * here we're doing allocation and writeback of the
691 * compressed pages
692 */
693 btrfs_drop_extent_cache(inode, async_extent->start,
694 async_extent->start +
695 async_extent->ram_size - 1, 0);
696
David Sterba172ddd62011-04-21 00:48:27 +0200697 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100698 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500699 em->start = async_extent->start;
700 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500701 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500702
703 em->block_start = ins.objectid;
704 em->block_len = ins.offset;
705 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800706 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500707 set_bit(EXTENT_FLAG_PINNED, &em->flags);
708 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
709
Chris Masond3977122009-01-05 21:25:51 -0500710 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400711 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500712 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400713 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500714 if (ret != -EEXIST) {
715 free_extent_map(em);
716 break;
717 }
718 btrfs_drop_extent_cache(inode, async_extent->start,
719 async_extent->start +
720 async_extent->ram_size - 1, 0);
721 }
722
Li Zefan261507a02010-12-17 14:21:50 +0800723 ret = btrfs_add_ordered_extent_compress(inode,
724 async_extent->start,
725 ins.objectid,
726 async_extent->ram_size,
727 ins.offset,
728 BTRFS_ORDERED_COMPRESSED,
729 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100730 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500731
Chris Mason771ed682008-11-06 22:02:51 -0500732 /*
733 * clear dirty, set writeback and unlock the pages.
734 */
735 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400736 &BTRFS_I(inode)->io_tree,
737 async_extent->start,
738 async_extent->start +
739 async_extent->ram_size - 1,
740 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
741 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400742 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400743 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500744
745 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500746 async_extent->start,
747 async_extent->ram_size,
748 ins.objectid,
749 ins.offset, async_extent->pages,
750 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500751
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100752 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500753 alloc_hint = ins.objectid + ins.offset;
754 kfree(async_extent);
755 cond_resched();
756 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100757 ret = 0;
758out:
759 return ret;
760out_free:
761 kfree(async_extent);
762 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500763}
764
Josef Bacik4b46fce2010-05-23 11:00:55 -0400765static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
766 u64 num_bytes)
767{
768 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
769 struct extent_map *em;
770 u64 alloc_hint = 0;
771
772 read_lock(&em_tree->lock);
773 em = search_extent_mapping(em_tree, start, num_bytes);
774 if (em) {
775 /*
776 * if block start isn't an actual block number then find the
777 * first block in this inode and use that as a hint. If that
778 * block is also bogus then just don't worry about it.
779 */
780 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
781 free_extent_map(em);
782 em = search_extent_mapping(em_tree, 0, 0);
783 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
784 alloc_hint = em->block_start;
785 if (em)
786 free_extent_map(em);
787 } else {
788 alloc_hint = em->block_start;
789 free_extent_map(em);
790 }
791 }
792 read_unlock(&em_tree->lock);
793
794 return alloc_hint;
795}
796
Chris Mason771ed682008-11-06 22:02:51 -0500797/*
798 * when extent_io.c finds a delayed allocation range in the file,
799 * the call backs end up in this code. The basic idea is to
800 * allocate extents on disk for the range, and create ordered data structs
801 * in ram to track those extents.
802 *
803 * locked_page is the page that writepage had locked already. We use
804 * it to make sure we don't do extra locks or unlocks.
805 *
806 * *page_started is set to one if we unlock locked_page and do everything
807 * required to start IO on it. It may be clean and already done with
808 * IO when we return.
809 */
810static noinline int cow_file_range(struct inode *inode,
811 struct page *locked_page,
812 u64 start, u64 end, int *page_started,
813 unsigned long *nr_written,
814 int unlock)
815{
816 struct btrfs_root *root = BTRFS_I(inode)->root;
817 struct btrfs_trans_handle *trans;
818 u64 alloc_hint = 0;
819 u64 num_bytes;
820 unsigned long ram_size;
821 u64 disk_num_bytes;
822 u64 cur_alloc_size;
823 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500824 struct btrfs_key ins;
825 struct extent_map *em;
826 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
827 int ret = 0;
828
Liu Bo83eea1f2012-07-10 05:28:39 -0600829 BUG_ON(btrfs_is_free_space_inode(inode));
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400830 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100831 if (IS_ERR(trans)) {
832 extent_clear_unlock_delalloc(inode,
833 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400834 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100835 EXTENT_CLEAR_UNLOCK_PAGE |
836 EXTENT_CLEAR_UNLOCK |
837 EXTENT_CLEAR_DELALLOC |
838 EXTENT_CLEAR_DIRTY |
839 EXTENT_SET_WRITEBACK |
840 EXTENT_END_WRITEBACK);
841 return PTR_ERR(trans);
842 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400843 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500844
Chris Mason771ed682008-11-06 22:02:51 -0500845 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
846 num_bytes = max(blocksize, num_bytes);
847 disk_num_bytes = num_bytes;
848 ret = 0;
849
Chris Mason4cb53002011-05-24 15:35:30 -0400850 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400851 if (num_bytes < 64 * 1024 &&
852 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400853 btrfs_add_inode_defrag(trans, inode);
854
Chris Mason771ed682008-11-06 22:02:51 -0500855 if (start == 0) {
856 /* lets try to make an inline extent */
857 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000858 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500859 if (ret == 0) {
860 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400861 &BTRFS_I(inode)->io_tree,
862 start, end, NULL,
863 EXTENT_CLEAR_UNLOCK_PAGE |
864 EXTENT_CLEAR_UNLOCK |
865 EXTENT_CLEAR_DELALLOC |
866 EXTENT_CLEAR_DIRTY |
867 EXTENT_SET_WRITEBACK |
868 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000869
Chris Mason771ed682008-11-06 22:02:51 -0500870 *nr_written = *nr_written +
871 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
872 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500873 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100874 } else if (ret < 0) {
875 btrfs_abort_transaction(trans, root, ret);
876 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500877 }
878 }
Chris Masonc8b97812008-10-29 14:49:59 -0400879
880 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200881 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400882
Josef Bacik4b46fce2010-05-23 11:00:55 -0400883 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400884 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400885
Chris Masond3977122009-01-05 21:25:51 -0500886 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400887 unsigned long op;
888
Josef Bacik287a0ab2010-03-19 18:07:23 +0000889 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400890 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500891 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500892 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100893 if (ret < 0) {
894 btrfs_abort_transaction(trans, root, ret);
895 goto out_unlock;
896 }
Chris Masond3977122009-01-05 21:25:51 -0500897
David Sterba172ddd62011-04-21 00:48:27 +0200898 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100899 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400900 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500901 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500902 ram_size = ins.offset;
903 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400904
Chris Masone6dcd2d2008-07-17 12:53:50 -0400905 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400906 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400907 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400908 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400909
Chris Masond3977122009-01-05 21:25:51 -0500910 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400911 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400912 ret = add_extent_mapping(em_tree, em);
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 }
Chris Mason771ed682008-11-06 22:02:51 -0500959 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100960out:
Chris Masonb888db22007-08-27 16:49:44 -0400961 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400962
Chris Masonbe20aa92007-12-17 20:14:01 -0500963 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100964out_unlock:
965 extent_clear_unlock_delalloc(inode,
966 &BTRFS_I(inode)->io_tree,
Josef Bacikbeb42dd2012-05-30 15:35:17 -0400967 start, end, locked_page,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100968 EXTENT_CLEAR_UNLOCK_PAGE |
969 EXTENT_CLEAR_UNLOCK |
970 EXTENT_CLEAR_DELALLOC |
971 EXTENT_CLEAR_DIRTY |
972 EXTENT_SET_WRITEBACK |
973 EXTENT_END_WRITEBACK);
974
975 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500976}
Chris Masonc8b97812008-10-29 14:49:59 -0400977
Chris Mason771ed682008-11-06 22:02:51 -0500978/*
979 * work queue call back to started compression on a file and pages
980 */
981static noinline void async_cow_start(struct btrfs_work *work)
982{
983 struct async_cow *async_cow;
984 int num_added = 0;
985 async_cow = container_of(work, struct async_cow, work);
986
987 compress_file_range(async_cow->inode, async_cow->locked_page,
988 async_cow->start, async_cow->end, async_cow,
989 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -0400990 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -0600991 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -0500992 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -0400993 }
Chris Mason771ed682008-11-06 22:02:51 -0500994}
995
996/*
997 * work queue call back to submit previously compressed pages
998 */
999static noinline void async_cow_submit(struct btrfs_work *work)
1000{
1001 struct async_cow *async_cow;
1002 struct btrfs_root *root;
1003 unsigned long nr_pages;
1004
1005 async_cow = container_of(work, struct async_cow, work);
1006
1007 root = async_cow->root;
1008 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1009 PAGE_CACHE_SHIFT;
1010
Josef Bacik66657b32012-08-01 15:36:24 -04001011 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Liu Bo287082b2012-06-28 04:02:24 -06001012 5 * 1024 * 1024 &&
Chris Mason771ed682008-11-06 22:02:51 -05001013 waitqueue_active(&root->fs_info->async_submit_wait))
1014 wake_up(&root->fs_info->async_submit_wait);
1015
Chris Masond3977122009-01-05 21:25:51 -05001016 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001017 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001018}
Chris Masonc8b97812008-10-29 14:49:59 -04001019
Chris Mason771ed682008-11-06 22:02:51 -05001020static noinline void async_cow_free(struct btrfs_work *work)
1021{
1022 struct async_cow *async_cow;
1023 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001024 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001025 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001026 kfree(async_cow);
1027}
1028
1029static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1030 u64 start, u64 end, int *page_started,
1031 unsigned long *nr_written)
1032{
1033 struct async_cow *async_cow;
1034 struct btrfs_root *root = BTRFS_I(inode)->root;
1035 unsigned long nr_pages;
1036 u64 cur_end;
Liu Bo287082b2012-06-28 04:02:24 -06001037 int limit = 10 * 1024 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -05001038
Chris Masona3429ab2009-10-08 12:30:20 -04001039 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1040 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001041 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001042 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001043 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001044 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001045 async_cow->root = root;
1046 async_cow->locked_page = locked_page;
1047 async_cow->start = start;
1048
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001049 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001050 cur_end = end;
1051 else
1052 cur_end = min(end, start + 512 * 1024 - 1);
1053
1054 async_cow->end = cur_end;
1055 INIT_LIST_HEAD(&async_cow->extents);
1056
1057 async_cow->work.func = async_cow_start;
1058 async_cow->work.ordered_func = async_cow_submit;
1059 async_cow->work.ordered_free = async_cow_free;
1060 async_cow->work.flags = 0;
1061
Chris Mason771ed682008-11-06 22:02:51 -05001062 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1063 PAGE_CACHE_SHIFT;
1064 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1065
1066 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1067 &async_cow->work);
1068
1069 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1070 wait_event(root->fs_info->async_submit_wait,
1071 (atomic_read(&root->fs_info->async_delalloc_pages) <
1072 limit));
1073 }
1074
Chris Masond3977122009-01-05 21:25:51 -05001075 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001076 atomic_read(&root->fs_info->async_delalloc_pages)) {
1077 wait_event(root->fs_info->async_submit_wait,
1078 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1079 0));
1080 }
1081
1082 *nr_written += nr_pages;
1083 start = cur_end + 1;
1084 }
1085 *page_started = 1;
1086 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001087}
1088
Chris Masond3977122009-01-05 21:25:51 -05001089static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001090 u64 bytenr, u64 num_bytes)
1091{
1092 int ret;
1093 struct btrfs_ordered_sum *sums;
1094 LIST_HEAD(list);
1095
Yan Zheng07d400a2009-01-06 11:42:00 -05001096 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001097 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001098 if (ret == 0 && list_empty(&list))
1099 return 0;
1100
1101 while (!list_empty(&list)) {
1102 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1103 list_del(&sums->list);
1104 kfree(sums);
1105 }
1106 return 1;
1107}
1108
Chris Masond352ac62008-09-29 15:18:18 -04001109/*
1110 * when nowcow writeback call back. This checks for snapshots or COW copies
1111 * of the extents that exist in the file, and COWs the file as required.
1112 *
1113 * If no cow copies or snapshots exist, we write directly to the existing
1114 * blocks on disk
1115 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001116static noinline int run_delalloc_nocow(struct inode *inode,
1117 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001118 u64 start, u64 end, int *page_started, int force,
1119 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001120{
Chris Masonbe20aa92007-12-17 20:14:01 -05001121 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001122 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001123 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001124 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001125 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001126 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001127 u64 cow_start;
1128 u64 cur_offset;
1129 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001130 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001131 u64 disk_bytenr;
1132 u64 num_bytes;
1133 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001134 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001135 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001136 int nocow;
1137 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001138 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001139 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001140
1141 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001142 if (!path) {
1143 extent_clear_unlock_delalloc(inode,
1144 &BTRFS_I(inode)->io_tree,
1145 start, end, locked_page,
1146 EXTENT_CLEAR_UNLOCK_PAGE |
1147 EXTENT_CLEAR_UNLOCK |
1148 EXTENT_CLEAR_DELALLOC |
1149 EXTENT_CLEAR_DIRTY |
1150 EXTENT_SET_WRITEBACK |
1151 EXTENT_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001152 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001153 }
Li Zefan82d59022011-04-20 10:33:24 +08001154
Liu Bo83eea1f2012-07-10 05:28:39 -06001155 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001156
1157 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001158 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001159 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001160 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001161
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001162 if (IS_ERR(trans)) {
Josef Bacik17ca04a2012-05-31 15:58:55 -04001163 extent_clear_unlock_delalloc(inode,
1164 &BTRFS_I(inode)->io_tree,
1165 start, end, locked_page,
1166 EXTENT_CLEAR_UNLOCK_PAGE |
1167 EXTENT_CLEAR_UNLOCK |
1168 EXTENT_CLEAR_DELALLOC |
1169 EXTENT_CLEAR_DIRTY |
1170 EXTENT_SET_WRITEBACK |
1171 EXTENT_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001172 btrfs_free_path(path);
1173 return PTR_ERR(trans);
1174 }
1175
Josef Bacik74b21072011-04-13 12:02:53 -04001176 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001177
Yan Zheng80ff3852008-10-30 14:20:02 -04001178 cow_start = (u64)-1;
1179 cur_offset = start;
1180 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001181 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001182 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001183 if (ret < 0) {
1184 btrfs_abort_transaction(trans, root, ret);
1185 goto error;
1186 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001187 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1188 leaf = path->nodes[0];
1189 btrfs_item_key_to_cpu(leaf, &found_key,
1190 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001191 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001192 found_key.type == BTRFS_EXTENT_DATA_KEY)
1193 path->slots[0]--;
1194 }
1195 check_prev = 0;
1196next_slot:
1197 leaf = path->nodes[0];
1198 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1199 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001200 if (ret < 0) {
1201 btrfs_abort_transaction(trans, root, ret);
1202 goto error;
1203 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001204 if (ret > 0)
1205 break;
1206 leaf = path->nodes[0];
1207 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001208
Yan Zheng80ff3852008-10-30 14:20:02 -04001209 nocow = 0;
1210 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001211 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001212 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001213
Li Zefan33345d012011-04-20 10:31:50 +08001214 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001215 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1216 found_key.offset > end)
1217 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001218
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 if (found_key.offset > cur_offset) {
1220 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001221 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001222 goto out_check;
1223 }
Chris Masonc31f8832008-01-08 15:46:31 -05001224
Yan Zheng80ff3852008-10-30 14:20:02 -04001225 fi = btrfs_item_ptr(leaf, path->slots[0],
1226 struct btrfs_file_extent_item);
1227 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001228
Yan Zhengd899e052008-10-30 14:25:28 -04001229 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1230 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001231 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001232 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001233 extent_end = found_key.offset +
1234 btrfs_file_extent_num_bytes(leaf, fi);
1235 if (extent_end <= start) {
1236 path->slots[0]++;
1237 goto next_slot;
1238 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001239 if (disk_bytenr == 0)
1240 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 if (btrfs_file_extent_compression(leaf, fi) ||
1242 btrfs_file_extent_encryption(leaf, fi) ||
1243 btrfs_file_extent_other_encoding(leaf, fi))
1244 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001245 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1246 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001247 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001248 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001249 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001250 found_key.offset -
1251 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001252 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001253 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001254 disk_bytenr += cur_offset - found_key.offset;
1255 num_bytes = min(end + 1, extent_end) - cur_offset;
1256 /*
1257 * force cow if csum exists in the range.
1258 * this ensure that csum for a given extent are
1259 * either valid or do not exist.
1260 */
1261 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1262 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001263 nocow = 1;
1264 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1265 extent_end = found_key.offset +
1266 btrfs_file_extent_inline_len(leaf, fi);
1267 extent_end = ALIGN(extent_end, root->sectorsize);
1268 } else {
1269 BUG_ON(1);
1270 }
1271out_check:
1272 if (extent_end <= start) {
1273 path->slots[0]++;
1274 goto next_slot;
1275 }
1276 if (!nocow) {
1277 if (cow_start == (u64)-1)
1278 cow_start = cur_offset;
1279 cur_offset = extent_end;
1280 if (cur_offset > end)
1281 break;
1282 path->slots[0]++;
1283 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001284 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001285
David Sterbab3b4aa72011-04-21 01:20:15 +02001286 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001287 if (cow_start != (u64)-1) {
1288 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001289 found_key.offset - 1, page_started,
1290 nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001291 if (ret) {
1292 btrfs_abort_transaction(trans, root, ret);
1293 goto error;
1294 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001295 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001296 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001297
Yan Zhengd899e052008-10-30 14:25:28 -04001298 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1299 struct extent_map *em;
1300 struct extent_map_tree *em_tree;
1301 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001302 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001303 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001304 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001305 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001306 em->len = num_bytes;
1307 em->block_len = num_bytes;
1308 em->block_start = disk_bytenr;
1309 em->bdev = root->fs_info->fs_devices->latest_bdev;
1310 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -06001311 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Yan Zhengd899e052008-10-30 14:25:28 -04001312 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001313 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001314 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001315 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001316 if (ret != -EEXIST) {
1317 free_extent_map(em);
1318 break;
1319 }
1320 btrfs_drop_extent_cache(inode, em->start,
1321 em->start + em->len - 1, 0);
1322 }
1323 type = BTRFS_ORDERED_PREALLOC;
1324 } else {
1325 type = BTRFS_ORDERED_NOCOW;
1326 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001327
1328 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001329 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001330 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001331
Yan, Zhengefa56462010-05-16 10:49:59 -04001332 if (root->root_key.objectid ==
1333 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1334 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1335 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001336 if (ret) {
1337 btrfs_abort_transaction(trans, root, ret);
1338 goto error;
1339 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001340 }
1341
Yan Zhengd899e052008-10-30 14:25:28 -04001342 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001343 cur_offset, cur_offset + num_bytes - 1,
1344 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1345 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1346 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 cur_offset = extent_end;
1348 if (cur_offset > end)
1349 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001350 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001351 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001352
Josef Bacik17ca04a2012-05-31 15:58:55 -04001353 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001355 cur_offset = end;
1356 }
1357
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 if (cow_start != (u64)-1) {
1359 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001360 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001361 if (ret) {
1362 btrfs_abort_transaction(trans, root, ret);
1363 goto error;
1364 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 }
1366
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001367error:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001368 if (nolock) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001369 err = btrfs_end_transaction_nolock(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001370 } else {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001371 err = btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001372 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001373 if (!ret)
1374 ret = err;
1375
Josef Bacik17ca04a2012-05-31 15:58:55 -04001376 if (ret && cur_offset < end)
1377 extent_clear_unlock_delalloc(inode,
1378 &BTRFS_I(inode)->io_tree,
1379 cur_offset, end, locked_page,
1380 EXTENT_CLEAR_UNLOCK_PAGE |
1381 EXTENT_CLEAR_UNLOCK |
1382 EXTENT_CLEAR_DELALLOC |
1383 EXTENT_CLEAR_DIRTY |
1384 EXTENT_SET_WRITEBACK |
1385 EXTENT_END_WRITEBACK);
1386
Yan Zheng7ea394f2008-08-05 13:05:02 -04001387 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001388 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001389}
1390
Chris Masond352ac62008-09-29 15:18:18 -04001391/*
1392 * extent_io.c call back to do delayed allocation processing
1393 */
Chris Masonc8b97812008-10-29 14:49:59 -04001394static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001395 u64 start, u64 end, int *page_started,
1396 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001397{
Chris Masonbe20aa92007-12-17 20:14:01 -05001398 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001399 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001400
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001401 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001402 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001403 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001404 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001405 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001406 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001407 } else if (!btrfs_test_opt(root, COMPRESS) &&
1408 !(BTRFS_I(inode)->force_compress) &&
1409 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001410 ret = cow_file_range(inode, locked_page, start, end,
1411 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001412 } else {
1413 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1414 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001415 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001416 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001417 }
Chris Masonb888db22007-08-27 16:49:44 -04001418 return ret;
1419}
1420
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001421static void btrfs_split_extent_hook(struct inode *inode,
1422 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001423{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001424 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001425 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001426 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001427
Josef Bacik9e0baf62011-07-15 15:16:44 +00001428 spin_lock(&BTRFS_I(inode)->lock);
1429 BTRFS_I(inode)->outstanding_extents++;
1430 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001431}
1432
1433/*
1434 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1435 * extents so we can keep track of new extents that are just merged onto old
1436 * extents, such as when we are doing sequential writes, so we can properly
1437 * account for the metadata space we'll need.
1438 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001439static void btrfs_merge_extent_hook(struct inode *inode,
1440 struct extent_state *new,
1441 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001442{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001443 /* not delalloc, ignore it */
1444 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001445 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001446
Josef Bacik9e0baf62011-07-15 15:16:44 +00001447 spin_lock(&BTRFS_I(inode)->lock);
1448 BTRFS_I(inode)->outstanding_extents--;
1449 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001450}
1451
Chris Masond352ac62008-09-29 15:18:18 -04001452/*
1453 * extent_io.c set_bit_hook, used to track delayed allocation
1454 * bytes in this file, and to maintain the list of inodes that
1455 * have pending delalloc work to be done.
1456 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001457static void btrfs_set_bit_hook(struct inode *inode,
1458 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001459{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001460
Chris Mason75eff682008-12-15 15:54:40 -05001461 /*
1462 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001463 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001464 * bit, which is only set or cleared with irqs on
1465 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001466 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001467 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001468 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001469 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001470
Josef Bacik9e0baf62011-07-15 15:16:44 +00001471 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001472 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001473 } else {
1474 spin_lock(&BTRFS_I(inode)->lock);
1475 BTRFS_I(inode)->outstanding_extents++;
1476 spin_unlock(&BTRFS_I(inode)->lock);
1477 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001478
Chris Mason75eff682008-12-15 15:54:40 -05001479 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001480 BTRFS_I(inode)->delalloc_bytes += len;
1481 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001482 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001483 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1484 &root->fs_info->delalloc_inodes);
1485 }
Chris Mason75eff682008-12-15 15:54:40 -05001486 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001487 }
Chris Mason291d6732008-01-29 15:55:23 -05001488}
1489
Chris Masond352ac62008-09-29 15:18:18 -04001490/*
1491 * extent_io.c clear_bit_hook, see set_bit_hook for why
1492 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001493static void btrfs_clear_bit_hook(struct inode *inode,
1494 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001495{
Chris Mason75eff682008-12-15 15:54:40 -05001496 /*
1497 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001498 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001499 * bit, which is only set or cleared with irqs on
1500 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001501 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001502 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001503 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001504 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001505
Josef Bacik9e0baf62011-07-15 15:16:44 +00001506 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001507 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001508 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1509 spin_lock(&BTRFS_I(inode)->lock);
1510 BTRFS_I(inode)->outstanding_extents--;
1511 spin_unlock(&BTRFS_I(inode)->lock);
1512 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001513
1514 if (*bits & EXTENT_DO_ACCOUNTING)
1515 btrfs_delalloc_release_metadata(inode, len);
1516
Josef Bacik0cb59c92010-07-02 12:14:14 -04001517 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1518 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001519 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001520
Chris Mason75eff682008-12-15 15:54:40 -05001521 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001522 root->fs_info->delalloc_bytes -= len;
1523 BTRFS_I(inode)->delalloc_bytes -= len;
1524
Josef Bacik0cb59c92010-07-02 12:14:14 -04001525 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001526 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1527 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1528 }
Chris Mason75eff682008-12-15 15:54:40 -05001529 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001530 }
Chris Mason291d6732008-01-29 15:55:23 -05001531}
1532
Chris Masond352ac62008-09-29 15:18:18 -04001533/*
1534 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1535 * we don't create bios that span stripes or chunks
1536 */
Chris Mason239b14b2008-03-24 15:02:07 -04001537int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001538 size_t size, struct bio *bio,
1539 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001540{
1541 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1542 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001543 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001544 u64 length = 0;
1545 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001546 int ret;
1547
Chris Mason771ed682008-11-06 22:02:51 -05001548 if (bio_flags & EXTENT_BIO_COMPRESSED)
1549 return 0;
1550
Chris Masonf2d8d742008-04-21 10:03:05 -04001551 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001552 map_tree = &root->fs_info->mapping_tree;
1553 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001554 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001555 &map_length, NULL, 0);
Jeff Mahoney3444a972011-10-03 23:23:13 -04001556 /* Will always return 0 or 1 with map_multi == NULL */
1557 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001558 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001559 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001560 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001561}
1562
Chris Masond352ac62008-09-29 15:18:18 -04001563/*
1564 * in order to insert checksums into the metadata in large chunks,
1565 * we wait until bio submission time. All the pages in the bio are
1566 * checksummed and sums are attached onto the ordered extent record.
1567 *
1568 * At IO completion time the cums attached on the ordered extent record
1569 * are inserted into the btree
1570 */
Chris Masond3977122009-01-05 21:25:51 -05001571static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1572 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001573 unsigned long bio_flags,
1574 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001575{
Chris Mason065631f2008-02-20 12:07:25 -05001576 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001577 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001578
Chris Masond20f7042008-12-08 16:58:54 -05001579 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001580 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001581 return 0;
1582}
Chris Masone0156402008-04-16 11:15:20 -04001583
Chris Mason4a69a412008-11-06 22:03:00 -05001584/*
1585 * in order to insert checksums into the metadata in large chunks,
1586 * we wait until bio submission time. All the pages in the bio are
1587 * checksummed and sums are attached onto the ordered extent record.
1588 *
1589 * At IO completion time the cums attached on the ordered extent record
1590 * are inserted into the btree
1591 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001592static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001593 int mirror_num, unsigned long bio_flags,
1594 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001595{
1596 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001597 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001598}
1599
Chris Masond352ac62008-09-29 15:18:18 -04001600/*
Chris Masoncad321a2008-12-17 14:51:42 -05001601 * extent_io.c submission hook. This does the right thing for csum calculation
1602 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001603 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001604static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001605 int mirror_num, unsigned long bio_flags,
1606 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001607{
1608 struct btrfs_root *root = BTRFS_I(inode)->root;
1609 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001610 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001611 int metadata = 0;
Chris Mason44b8bd72008-04-16 11:14:51 -04001612
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001613 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001614
Liu Bo83eea1f2012-07-10 05:28:39 -06001615 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001616 metadata = 2;
1617
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001618 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001619 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1620 if (ret)
1621 return ret;
1622
Chris Masond20f7042008-12-08 16:58:54 -05001623 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001624 return btrfs_submit_compressed_read(inode, bio,
1625 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001626 } else if (!skip_sum) {
1627 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1628 if (ret)
1629 return ret;
1630 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001631 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001632 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001633 /* csum items have already been cloned */
1634 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1635 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001636 /* we're doing a write, do the async checksumming */
1637 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001638 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001639 bio_flags, bio_offset,
1640 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001641 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001642 }
1643
Chris Mason0b86a832008-03-24 15:01:56 -04001644mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001645 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001646}
Chris Mason6885f302008-02-20 16:11:05 -05001647
Chris Masond352ac62008-09-29 15:18:18 -04001648/*
1649 * given a list of ordered sums record them in the inode. This happens
1650 * at IO completion time based on sums calculated at bio submission time.
1651 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001652static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001653 struct inode *inode, u64 file_offset,
1654 struct list_head *list)
1655{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001656 struct btrfs_ordered_sum *sum;
1657
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001658 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001659 btrfs_csum_file_blocks(trans,
1660 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001661 }
1662 return 0;
1663}
1664
Josef Bacik2ac55d42010-02-03 19:33:23 +00001665int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1666 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001667{
Chris Masond3977122009-01-05 21:25:51 -05001668 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001669 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001670 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001671 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001672}
1673
Chris Masond352ac62008-09-29 15:18:18 -04001674/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001675struct btrfs_writepage_fixup {
1676 struct page *page;
1677 struct btrfs_work work;
1678};
1679
Christoph Hellwigb2950862008-12-02 09:54:17 -05001680static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001681{
1682 struct btrfs_writepage_fixup *fixup;
1683 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001684 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001685 struct page *page;
1686 struct inode *inode;
1687 u64 page_start;
1688 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001689 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001690
1691 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1692 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001693again:
Chris Mason247e7432008-07-17 12:53:51 -04001694 lock_page(page);
1695 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1696 ClearPageChecked(page);
1697 goto out_page;
1698 }
1699
1700 inode = page->mapping->host;
1701 page_start = page_offset(page);
1702 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1703
Josef Bacik2ac55d42010-02-03 19:33:23 +00001704 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001705 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001706
1707 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001708 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001709 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001710
1711 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1712 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001713 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1714 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001715 unlock_page(page);
1716 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001717 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001718 goto again;
1719 }
Chris Mason247e7432008-07-17 12:53:51 -04001720
Jeff Mahoney87826df2012-02-15 16:23:57 +01001721 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1722 if (ret) {
1723 mapping_set_error(page->mapping, ret);
1724 end_extent_writepage(page, ret, page_start, page_end);
1725 ClearPageChecked(page);
1726 goto out;
1727 }
1728
Josef Bacik2ac55d42010-02-03 19:33:23 +00001729 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001730 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001731 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001732out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001733 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1734 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001735out_page:
1736 unlock_page(page);
1737 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001738 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001739}
1740
1741/*
1742 * There are a few paths in the higher layers of the kernel that directly
1743 * set the page dirty bit without asking the filesystem if it is a
1744 * good idea. This causes problems because we want to make sure COW
1745 * properly happens and the data=ordered rules are followed.
1746 *
Chris Masonc8b97812008-10-29 14:49:59 -04001747 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001748 * hasn't been properly setup for IO. We kick off an async process
1749 * to fix it up. The async helper will wait for ordered extents, set
1750 * the delalloc bit and make it safe to write the page.
1751 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001752static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001753{
1754 struct inode *inode = page->mapping->host;
1755 struct btrfs_writepage_fixup *fixup;
1756 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001757
Chris Mason8b62b722009-09-02 16:53:46 -04001758 /* this page is properly in the ordered list */
1759 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001760 return 0;
1761
1762 if (PageChecked(page))
1763 return -EAGAIN;
1764
1765 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1766 if (!fixup)
1767 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001768
Chris Mason247e7432008-07-17 12:53:51 -04001769 SetPageChecked(page);
1770 page_cache_get(page);
1771 fixup->work.func = btrfs_writepage_fixup_worker;
1772 fixup->page = page;
1773 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001774 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001775}
1776
Yan Zhengd899e052008-10-30 14:25:28 -04001777static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1778 struct inode *inode, u64 file_pos,
1779 u64 disk_bytenr, u64 disk_num_bytes,
1780 u64 num_bytes, u64 ram_bytes,
1781 u8 compression, u8 encryption,
1782 u16 other_encoding, int extent_type)
1783{
1784 struct btrfs_root *root = BTRFS_I(inode)->root;
1785 struct btrfs_file_extent_item *fi;
1786 struct btrfs_path *path;
1787 struct extent_buffer *leaf;
1788 struct btrfs_key ins;
1789 u64 hint;
1790 int ret;
1791
1792 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001793 if (!path)
1794 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001795
Chris Masonb9473432009-03-13 11:00:37 -04001796 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001797
1798 /*
1799 * we may be replacing one extent in the tree with another.
1800 * The new extent is pinned in the extent map, and we don't want
1801 * to drop it from the cache until it is completely in the btree.
1802 *
1803 * So, tell btrfs_drop_extents to leave this extent in the cache.
1804 * the caller is expected to unpin it and allow it to be merged
1805 * with the others.
1806 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001807 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1808 file_pos + num_bytes,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001809 &hint, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001810 if (ret)
1811 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001812
Li Zefan33345d012011-04-20 10:31:50 +08001813 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001814 ins.offset = file_pos;
1815 ins.type = BTRFS_EXTENT_DATA_KEY;
1816 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001817 if (ret)
1818 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001819 leaf = path->nodes[0];
1820 fi = btrfs_item_ptr(leaf, path->slots[0],
1821 struct btrfs_file_extent_item);
1822 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1823 btrfs_set_file_extent_type(leaf, fi, extent_type);
1824 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1825 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1826 btrfs_set_file_extent_offset(leaf, fi, 0);
1827 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1828 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1829 btrfs_set_file_extent_compression(leaf, fi, compression);
1830 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1831 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001832
1833 btrfs_unlock_up_safe(path, 1);
1834 btrfs_set_lock_blocking(leaf);
1835
Yan Zhengd899e052008-10-30 14:25:28 -04001836 btrfs_mark_buffer_dirty(leaf);
1837
1838 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001839
1840 ins.objectid = disk_bytenr;
1841 ins.offset = disk_num_bytes;
1842 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001843 ret = btrfs_alloc_reserved_file_extent(trans, root,
1844 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001845 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001846out:
Yan Zhengd899e052008-10-30 14:25:28 -04001847 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001848
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001849 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001850}
1851
Chris Mason5d13a982009-03-13 11:41:46 -04001852/*
1853 * helper function for btrfs_finish_ordered_io, this
1854 * just reads in some of the csum leaves to prime them into ram
1855 * before we start the transaction. It limits the amount of btree
1856 * reads required while inside the transaction.
1857 */
Chris Masond352ac62008-09-29 15:18:18 -04001858/* as ordered data IO finishes, this gets called so we can finish
1859 * an ordered extent if the range of bytes in the file it covers are
1860 * fully written.
1861 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001862static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001863{
Josef Bacik5fd02042012-05-02 14:00:54 -04001864 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001865 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001866 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001867 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001868 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001869 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001870 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001871 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001872
Liu Bo83eea1f2012-07-10 05:28:39 -06001873 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001874
Josef Bacik5fd02042012-05-02 14:00:54 -04001875 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1876 ret = -EIO;
1877 goto out;
1878 }
1879
Yan, Zhengc2167752009-11-12 09:34:21 +00001880 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001881 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Yan, Zhengc2167752009-11-12 09:34:21 +00001882 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1883 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001884 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001885 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001886 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001887 trans = btrfs_join_transaction(root);
Liu Bod280e5b2012-08-21 21:13:25 -06001888 if (IS_ERR(trans)) {
1889 ret = PTR_ERR(trans);
1890 trans = NULL;
1891 goto out;
1892 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001893 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason21151332011-11-10 20:39:08 -05001894 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001895 if (ret) /* -ENOMEM or corruption */
1896 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001897 }
1898 goto out;
1899 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001900
Josef Bacik2ac55d42010-02-03 19:33:23 +00001901 lock_extent_bits(io_tree, ordered_extent->file_offset,
1902 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001903 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001904
Josef Bacik0cb59c92010-07-02 12:14:14 -04001905 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001906 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001907 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001908 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001909 if (IS_ERR(trans)) {
1910 ret = PTR_ERR(trans);
1911 trans = NULL;
1912 goto out_unlock;
1913 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001914 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001915
Chris Masonc8b97812008-10-29 14:49:59 -04001916 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001917 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001918 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001919 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001920 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001921 ordered_extent->file_offset,
1922 ordered_extent->file_offset +
1923 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001924 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001925 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001926 ret = insert_reserved_file_extent(trans, inode,
1927 ordered_extent->file_offset,
1928 ordered_extent->start,
1929 ordered_extent->disk_len,
1930 ordered_extent->len,
1931 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001932 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001933 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04001934 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04001935 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1936 ordered_extent->file_offset, ordered_extent->len,
1937 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001938 if (ret < 0) {
1939 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001940 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001941 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001942
Chris Masone6dcd2d2008-07-17 12:53:50 -04001943 add_pending_csums(trans, inode, ordered_extent->file_offset,
1944 &ordered_extent->list);
1945
Josef Bacik1ef30be2011-04-05 19:25:36 -04001946 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Miao Xiea39f7522011-09-11 10:52:25 -04001947 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Chris Mason21151332011-11-10 20:39:08 -05001948 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001949 if (ret) { /* -ENOMEM or corruption */
1950 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001951 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001952 }
Josef Bacik7c735312012-08-13 15:43:26 -04001953 } else {
1954 btrfs_set_inode_last_trans(trans, inode);
Josef Bacik1ef30be2011-04-05 19:25:36 -04001955 }
1956 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04001957out_unlock:
1958 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1959 ordered_extent->file_offset +
1960 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00001961out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001962 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001963 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001964 if (trans) {
1965 if (nolock)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001966 btrfs_end_transaction_nolock(trans, root);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001967 else
Josef Bacik0cb59c92010-07-02 12:14:14 -04001968 btrfs_end_transaction(trans, root);
1969 }
1970
Josef Bacik5fd02042012-05-02 14:00:54 -04001971 if (ret)
1972 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
1973 ordered_extent->file_offset +
1974 ordered_extent->len - 1, NULL, GFP_NOFS);
1975
1976 /*
1977 * This needs to be dont to make sure anybody waiting knows we are done
1978 * upating everything for this ordered extent.
1979 */
1980 btrfs_remove_ordered_extent(inode, ordered_extent);
1981
Chris Masone6dcd2d2008-07-17 12:53:50 -04001982 /* once for us */
1983 btrfs_put_ordered_extent(ordered_extent);
1984 /* once for the tree */
1985 btrfs_put_ordered_extent(ordered_extent);
1986
Josef Bacik5fd02042012-05-02 14:00:54 -04001987 return ret;
1988}
1989
1990static void finish_ordered_fn(struct btrfs_work *work)
1991{
1992 struct btrfs_ordered_extent *ordered_extent;
1993 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
1994 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001995}
1996
Christoph Hellwigb2950862008-12-02 09:54:17 -05001997static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001998 struct extent_state *state, int uptodate)
1999{
Josef Bacik5fd02042012-05-02 14:00:54 -04002000 struct inode *inode = page->mapping->host;
2001 struct btrfs_root *root = BTRFS_I(inode)->root;
2002 struct btrfs_ordered_extent *ordered_extent = NULL;
2003 struct btrfs_workers *workers;
2004
liubo1abe9b82011-03-24 11:18:59 +00002005 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2006
Chris Mason8b62b722009-09-02 16:53:46 -04002007 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002008 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2009 end - start + 1, uptodate))
2010 return 0;
2011
2012 ordered_extent->work.func = finish_ordered_fn;
2013 ordered_extent->work.flags = 0;
2014
Liu Bo83eea1f2012-07-10 05:28:39 -06002015 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002016 workers = &root->fs_info->endio_freespace_worker;
2017 else
2018 workers = &root->fs_info->endio_write_workers;
2019 btrfs_queue_worker(workers, &ordered_extent->work);
2020
2021 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002022}
2023
Chris Masond352ac62008-09-29 15:18:18 -04002024/*
Chris Masond352ac62008-09-29 15:18:18 -04002025 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002026 * if there's a match, we allow the bio to finish. If not, the code in
2027 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002028 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002029static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002030 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002031{
Chris Mason35ebb932007-10-30 16:56:53 -04002032 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04002033 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002034 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002035 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002036 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002037 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002038 struct btrfs_root *root = BTRFS_I(inode)->root;
2039 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002040
Chris Masond20f7042008-12-08 16:58:54 -05002041 if (PageChecked(page)) {
2042 ClearPageChecked(page);
2043 goto good;
2044 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002045
2046 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002047 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002048
Yan Zheng17d217f2008-12-12 10:03:38 -05002049 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002050 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002051 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2052 GFP_NOFS);
2053 return 0;
2054 }
2055
Yanc2e639f2008-02-04 08:57:25 -05002056 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002057 private = state->private;
2058 ret = 0;
2059 } else {
2060 ret = get_state_private(io_tree, start, &private);
2061 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002062 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002063 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002064 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002065
Chris Masonff79f812007-10-15 16:22:25 -04002066 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2067 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002068 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002069 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002070
Cong Wang7ac687d2011-11-25 23:14:28 +08002071 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002072good:
Chris Mason07157aa2007-08-30 08:50:51 -04002073 return 0;
2074
2075zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002076 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002077 "private %llu\n",
2078 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002079 (unsigned long long)start, csum,
2080 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002081 memset(kaddr + offset, 1, end - start + 1);
2082 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002083 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002084 if (private == 0)
2085 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002086 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002087}
Chris Masonb888db22007-08-27 16:49:44 -04002088
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002089struct delayed_iput {
2090 struct list_head list;
2091 struct inode *inode;
2092};
2093
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002094/* JDM: If this is fs-wide, why can't we add a pointer to
2095 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002096void btrfs_add_delayed_iput(struct inode *inode)
2097{
2098 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2099 struct delayed_iput *delayed;
2100
2101 if (atomic_add_unless(&inode->i_count, -1, 1))
2102 return;
2103
2104 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2105 delayed->inode = inode;
2106
2107 spin_lock(&fs_info->delayed_iput_lock);
2108 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2109 spin_unlock(&fs_info->delayed_iput_lock);
2110}
2111
2112void btrfs_run_delayed_iputs(struct btrfs_root *root)
2113{
2114 LIST_HEAD(list);
2115 struct btrfs_fs_info *fs_info = root->fs_info;
2116 struct delayed_iput *delayed;
2117 int empty;
2118
2119 spin_lock(&fs_info->delayed_iput_lock);
2120 empty = list_empty(&fs_info->delayed_iputs);
2121 spin_unlock(&fs_info->delayed_iput_lock);
2122 if (empty)
2123 return;
2124
2125 down_read(&root->fs_info->cleanup_work_sem);
2126 spin_lock(&fs_info->delayed_iput_lock);
2127 list_splice_init(&fs_info->delayed_iputs, &list);
2128 spin_unlock(&fs_info->delayed_iput_lock);
2129
2130 while (!list_empty(&list)) {
2131 delayed = list_entry(list.next, struct delayed_iput, list);
2132 list_del(&delayed->list);
2133 iput(delayed->inode);
2134 kfree(delayed);
2135 }
2136 up_read(&root->fs_info->cleanup_work_sem);
2137}
2138
Yan, Zhengd68fc572010-05-16 10:49:58 -04002139enum btrfs_orphan_cleanup_state {
2140 ORPHAN_CLEANUP_STARTED = 1,
2141 ORPHAN_CLEANUP_DONE = 2,
2142};
2143
2144/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002145 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002146 * files in the subvolume, it removes orphan item and frees block_rsv
2147 * structure.
2148 */
2149void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2150 struct btrfs_root *root)
2151{
Josef Bacik90290e12011-12-02 15:44:12 -05002152 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002153 int ret;
2154
Josef Bacik8a35d952012-05-23 14:26:42 -04002155 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002156 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2157 return;
2158
Josef Bacik90290e12011-12-02 15:44:12 -05002159 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002160 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002161 spin_unlock(&root->orphan_lock);
2162 return;
2163 }
2164
2165 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2166 spin_unlock(&root->orphan_lock);
2167 return;
2168 }
2169
2170 block_rsv = root->orphan_block_rsv;
2171 root->orphan_block_rsv = NULL;
2172 spin_unlock(&root->orphan_lock);
2173
Yan, Zhengd68fc572010-05-16 10:49:58 -04002174 if (root->orphan_item_inserted &&
2175 btrfs_root_refs(&root->root_item) > 0) {
2176 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2177 root->root_key.objectid);
2178 BUG_ON(ret);
2179 root->orphan_item_inserted = 0;
2180 }
2181
Josef Bacik90290e12011-12-02 15:44:12 -05002182 if (block_rsv) {
2183 WARN_ON(block_rsv->size > 0);
2184 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002185 }
2186}
2187
2188/*
Josef Bacik7b128762008-07-24 12:17:14 -04002189 * This creates an orphan entry for the given inode in case something goes
2190 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002191 *
2192 * NOTE: caller of this function should reserve 5 units of metadata for
2193 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002194 */
2195int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2196{
2197 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002198 struct btrfs_block_rsv *block_rsv = NULL;
2199 int reserve = 0;
2200 int insert = 0;
2201 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002202
Yan, Zhengd68fc572010-05-16 10:49:58 -04002203 if (!root->orphan_block_rsv) {
2204 block_rsv = btrfs_alloc_block_rsv(root);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002205 if (!block_rsv)
2206 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002207 }
2208
Yan, Zhengd68fc572010-05-16 10:49:58 -04002209 spin_lock(&root->orphan_lock);
2210 if (!root->orphan_block_rsv) {
2211 root->orphan_block_rsv = block_rsv;
2212 } else if (block_rsv) {
2213 btrfs_free_block_rsv(root, block_rsv);
2214 block_rsv = NULL;
2215 }
Josef Bacik7b128762008-07-24 12:17:14 -04002216
Josef Bacik8a35d952012-05-23 14:26:42 -04002217 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2218 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002219#if 0
2220 /*
2221 * For proper ENOSPC handling, we should do orphan
2222 * cleanup when mounting. But this introduces backward
2223 * compatibility issue.
2224 */
2225 if (!xchg(&root->orphan_item_inserted, 1))
2226 insert = 2;
2227 else
2228 insert = 1;
2229#endif
2230 insert = 1;
Josef Bacik8a35d952012-05-23 14:26:42 -04002231 atomic_dec(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002232 }
Josef Bacik7b128762008-07-24 12:17:14 -04002233
Josef Bacik72ac3c02012-05-23 14:13:11 -04002234 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2235 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002236 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002237 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002238
Yan, Zhengd68fc572010-05-16 10:49:58 -04002239 /* grab metadata reservation from transaction handle */
2240 if (reserve) {
2241 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002242 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002243 }
2244
2245 /* insert an orphan item to track this unlinked/truncated file */
2246 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002247 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002248 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002249 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2250 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002251 btrfs_abort_transaction(trans, root, ret);
2252 return ret;
2253 }
2254 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002255 }
2256
2257 /* insert an orphan item to track subvolume contains orphan files */
2258 if (insert >= 2) {
2259 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2260 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002261 if (ret && ret != -EEXIST) {
2262 btrfs_abort_transaction(trans, root, ret);
2263 return ret;
2264 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002265 }
2266 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002267}
2268
2269/*
2270 * We have done the truncate/delete so we can go ahead and remove the orphan
2271 * item for this particular inode.
2272 */
2273int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2274{
2275 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002276 int delete_item = 0;
2277 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002278 int ret = 0;
2279
Yan, Zhengd68fc572010-05-16 10:49:58 -04002280 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002281 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2282 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002283 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002284
Josef Bacik72ac3c02012-05-23 14:13:11 -04002285 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2286 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002287 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002288 spin_unlock(&root->orphan_lock);
2289
2290 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002291 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002292 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002293 }
2294
Josef Bacik8a35d952012-05-23 14:26:42 -04002295 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002296 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002297 atomic_dec(&root->orphan_inodes);
2298 }
Josef Bacik7b128762008-07-24 12:17:14 -04002299
Yan, Zhengd68fc572010-05-16 10:49:58 -04002300 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002301}
2302
2303/*
2304 * this cleans up any orphans that may be left on the list from the last use
2305 * of this root.
2306 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002307int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002308{
2309 struct btrfs_path *path;
2310 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002311 struct btrfs_key key, found_key;
2312 struct btrfs_trans_handle *trans;
2313 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002314 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002315 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2316
Yan, Zhengd68fc572010-05-16 10:49:58 -04002317 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002318 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002319
2320 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002321 if (!path) {
2322 ret = -ENOMEM;
2323 goto out;
2324 }
Josef Bacik7b128762008-07-24 12:17:14 -04002325 path->reada = -1;
2326
2327 key.objectid = BTRFS_ORPHAN_OBJECTID;
2328 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2329 key.offset = (u64)-1;
2330
Josef Bacik7b128762008-07-24 12:17:14 -04002331 while (1) {
2332 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002333 if (ret < 0)
2334 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002335
2336 /*
2337 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002338 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002339 * find the key and see if we have stuff that matches
2340 */
2341 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002342 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002343 if (path->slots[0] == 0)
2344 break;
2345 path->slots[0]--;
2346 }
2347
2348 /* pull out the item */
2349 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002350 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2351
2352 /* make sure the item matches what we want */
2353 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2354 break;
2355 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2356 break;
2357
2358 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002359 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002360
2361 /*
2362 * this is where we are basically btrfs_lookup, without the
2363 * crossing root thing. we store the inode number in the
2364 * offset of the orphan item.
2365 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002366
2367 if (found_key.offset == last_objectid) {
2368 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2369 "stopping orphan cleanup\n");
2370 ret = -EINVAL;
2371 goto out;
2372 }
2373
2374 last_objectid = found_key.offset;
2375
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002376 found_key.objectid = found_key.offset;
2377 found_key.type = BTRFS_INODE_ITEM_KEY;
2378 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002379 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002380 ret = PTR_RET(inode);
2381 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002382 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002383
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002384 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2385 struct btrfs_root *dead_root;
2386 struct btrfs_fs_info *fs_info = root->fs_info;
2387 int is_dead_root = 0;
2388
2389 /*
2390 * this is an orphan in the tree root. Currently these
2391 * could come from 2 sources:
2392 * a) a snapshot deletion in progress
2393 * b) a free space cache inode
2394 * We need to distinguish those two, as the snapshot
2395 * orphan must not get deleted.
2396 * find_dead_roots already ran before us, so if this
2397 * is a snapshot deletion, we should find the root
2398 * in the dead_roots list
2399 */
2400 spin_lock(&fs_info->trans_lock);
2401 list_for_each_entry(dead_root, &fs_info->dead_roots,
2402 root_list) {
2403 if (dead_root->root_key.objectid ==
2404 found_key.objectid) {
2405 is_dead_root = 1;
2406 break;
2407 }
2408 }
2409 spin_unlock(&fs_info->trans_lock);
2410 if (is_dead_root) {
2411 /* prevent this orphan from being found again */
2412 key.offset = found_key.objectid - 1;
2413 continue;
2414 }
2415 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002416 /*
2417 * Inode is already gone but the orphan item is still there,
2418 * kill the orphan item.
2419 */
2420 if (ret == -ESTALE) {
2421 trans = btrfs_start_transaction(root, 1);
2422 if (IS_ERR(trans)) {
2423 ret = PTR_ERR(trans);
2424 goto out;
2425 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002426 printk(KERN_ERR "auto deleting %Lu\n",
2427 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002428 ret = btrfs_del_orphan_item(trans, root,
2429 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002430 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002431 btrfs_end_transaction(trans, root);
2432 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002433 }
Josef Bacik7b128762008-07-24 12:17:14 -04002434
Josef Bacik7b128762008-07-24 12:17:14 -04002435 /*
2436 * add this inode to the orphan list so btrfs_orphan_del does
2437 * the proper thing when we hit it
2438 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002439 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2440 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002441
Josef Bacik7b128762008-07-24 12:17:14 -04002442 /* if we have links, this was a truncate, lets do that */
2443 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002444 if (!S_ISREG(inode->i_mode)) {
2445 WARN_ON(1);
2446 iput(inode);
2447 continue;
2448 }
Josef Bacik7b128762008-07-24 12:17:14 -04002449 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002450 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002451 } else {
2452 nr_unlink++;
2453 }
2454
2455 /* this will do delete_inode and everything for us */
2456 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002457 if (ret)
2458 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002459 }
Miao Xie3254c872011-11-10 20:45:05 -05002460 /* release the path since we're done with it */
2461 btrfs_release_path(path);
2462
Yan, Zhengd68fc572010-05-16 10:49:58 -04002463 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2464
2465 if (root->orphan_block_rsv)
2466 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2467 (u64)-1);
2468
2469 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002470 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002471 if (!IS_ERR(trans))
2472 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002473 }
Josef Bacik7b128762008-07-24 12:17:14 -04002474
2475 if (nr_unlink)
2476 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2477 if (nr_truncate)
2478 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002479
2480out:
2481 if (ret)
2482 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2483 btrfs_free_path(path);
2484 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002485}
2486
Chris Masond352ac62008-09-29 15:18:18 -04002487/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002488 * very simple check to peek ahead in the leaf looking for xattrs. If we
2489 * don't find any xattrs, we know there can't be any acls.
2490 *
2491 * slot is the slot the inode is in, objectid is the objectid of the inode
2492 */
2493static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2494 int slot, u64 objectid)
2495{
2496 u32 nritems = btrfs_header_nritems(leaf);
2497 struct btrfs_key found_key;
2498 int scanned = 0;
2499
2500 slot++;
2501 while (slot < nritems) {
2502 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2503
2504 /* we found a different objectid, there must not be acls */
2505 if (found_key.objectid != objectid)
2506 return 0;
2507
2508 /* we found an xattr, assume we've got an acl */
2509 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2510 return 1;
2511
2512 /*
2513 * we found a key greater than an xattr key, there can't
2514 * be any acls later on
2515 */
2516 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2517 return 0;
2518
2519 slot++;
2520 scanned++;
2521
2522 /*
2523 * it goes inode, inode backrefs, xattrs, extents,
2524 * so if there are a ton of hard links to an inode there can
2525 * be a lot of backrefs. Don't waste time searching too hard,
2526 * this is just an optimization
2527 */
2528 if (scanned >= 8)
2529 break;
2530 }
2531 /* we hit the end of the leaf before we found an xattr or
2532 * something larger than an xattr. We have to assume the inode
2533 * has acls
2534 */
2535 return 1;
2536}
2537
2538/*
Chris Masond352ac62008-09-29 15:18:18 -04002539 * read an inode from the btree into the in-memory inode
2540 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002541static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002542{
2543 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002544 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002545 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002546 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002547 struct btrfs_root *root = BTRFS_I(inode)->root;
2548 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002549 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002550 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002551 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002552 bool filled = false;
2553
2554 ret = btrfs_fill_inode(inode, &rdev);
2555 if (!ret)
2556 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002557
2558 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002559 if (!path)
2560 goto make_bad;
2561
Josef Bacikd90c7322011-05-17 09:50:54 -04002562 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002563 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002564
Chris Mason39279cc2007-06-12 06:35:45 -04002565 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002566 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002567 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002568
Chris Mason5f39d392007-10-15 16:14:19 -04002569 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002570
2571 if (filled)
2572 goto cache_acl;
2573
Chris Mason5f39d392007-10-15 16:14:19 -04002574 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2575 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002576 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002577 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002578 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2579 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002580 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002581
2582 tspec = btrfs_inode_atime(inode_item);
2583 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2584 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2585
2586 tspec = btrfs_inode_mtime(inode_item);
2587 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2588 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2589
2590 tspec = btrfs_inode_ctime(inode_item);
2591 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2592 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2593
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002594 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002595 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002596 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2597
2598 /*
2599 * If we were modified in the current generation and evicted from memory
2600 * and then re-read we need to do a full sync since we don't have any
2601 * idea about which extents were modified before we were evicted from
2602 * cache.
2603 */
2604 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2605 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2606 &BTRFS_I(inode)->runtime_flags);
2607
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002608 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002609 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002610 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002611 rdev = btrfs_inode_rdev(leaf, inode_item);
2612
Josef Bacikaec74772008-07-24 12:12:38 -04002613 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002614 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002615cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002616 /*
2617 * try to precache a NULL acl entry for files that don't have
2618 * any xattrs or acls
2619 */
Li Zefan33345d012011-04-20 10:31:50 +08002620 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2621 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002622 if (!maybe_acls)
2623 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002624
Chris Mason39279cc2007-06-12 06:35:45 -04002625 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002626
Chris Mason39279cc2007-06-12 06:35:45 -04002627 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002628 case S_IFREG:
2629 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002630 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002631 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002632 inode->i_fop = &btrfs_file_operations;
2633 inode->i_op = &btrfs_file_inode_operations;
2634 break;
2635 case S_IFDIR:
2636 inode->i_fop = &btrfs_dir_file_operations;
2637 if (root == root->fs_info->tree_root)
2638 inode->i_op = &btrfs_dir_ro_inode_operations;
2639 else
2640 inode->i_op = &btrfs_dir_inode_operations;
2641 break;
2642 case S_IFLNK:
2643 inode->i_op = &btrfs_symlink_inode_operations;
2644 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002645 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002646 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002647 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002648 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002649 init_special_inode(inode, inode->i_mode, rdev);
2650 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002651 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002652
2653 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002654 return;
2655
2656make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002657 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002658 make_bad_inode(inode);
2659}
2660
Chris Masond352ac62008-09-29 15:18:18 -04002661/*
2662 * given a leaf and an inode, copy the inode fields into the leaf
2663 */
Chris Masone02119d2008-09-05 16:13:11 -04002664static void fill_inode_item(struct btrfs_trans_handle *trans,
2665 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002666 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002667 struct inode *inode)
2668{
Chris Mason5f39d392007-10-15 16:14:19 -04002669 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2670 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002671 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002672 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2673 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2674
2675 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2676 inode->i_atime.tv_sec);
2677 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2678 inode->i_atime.tv_nsec);
2679
2680 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2681 inode->i_mtime.tv_sec);
2682 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2683 inode->i_mtime.tv_nsec);
2684
2685 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2686 inode->i_ctime.tv_sec);
2687 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2688 inode->i_ctime.tv_nsec);
2689
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002690 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002691 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002692 btrfs_set_inode_sequence(leaf, item, inode->i_version);
Chris Masone02119d2008-09-05 16:13:11 -04002693 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002694 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002695 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f12011-05-11 15:26:06 -04002696 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002697}
2698
Chris Masond352ac62008-09-29 15:18:18 -04002699/*
2700 * copy everything in the in-memory inode into the btree.
2701 */
Chris Mason21151332011-11-10 20:39:08 -05002702static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002703 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002704{
2705 struct btrfs_inode_item *inode_item;
2706 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002707 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002708 int ret;
2709
2710 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002711 if (!path)
2712 return -ENOMEM;
2713
Chris Masonb9473432009-03-13 11:00:37 -04002714 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002715 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2716 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002717 if (ret) {
2718 if (ret > 0)
2719 ret = -ENOENT;
2720 goto failed;
2721 }
2722
Chris Masonb4ce94d2009-02-04 09:25:08 -05002723 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002724 leaf = path->nodes[0];
2725 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002726 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002727
Chris Masone02119d2008-09-05 16:13:11 -04002728 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002729 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002730 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002731 ret = 0;
2732failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002733 btrfs_free_path(path);
2734 return ret;
2735}
2736
Chris Masond352ac62008-09-29 15:18:18 -04002737/*
Chris Mason21151332011-11-10 20:39:08 -05002738 * copy everything in the in-memory inode into the btree.
2739 */
2740noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2741 struct btrfs_root *root, struct inode *inode)
2742{
2743 int ret;
2744
2745 /*
2746 * If the inode is a free space inode, we can deadlock during commit
2747 * if we put it into the delayed code.
2748 *
2749 * The data relocation inode should also be directly updated
2750 * without delay
2751 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002752 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002753 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002754 btrfs_update_root_times(trans, root);
2755
Chris Mason21151332011-11-10 20:39:08 -05002756 ret = btrfs_delayed_update_inode(trans, root, inode);
2757 if (!ret)
2758 btrfs_set_inode_last_trans(trans, inode);
2759 return ret;
2760 }
2761
2762 return btrfs_update_inode_item(trans, root, inode);
2763}
2764
2765static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2766 struct btrfs_root *root, struct inode *inode)
2767{
2768 int ret;
2769
2770 ret = btrfs_update_inode(trans, root, inode);
2771 if (ret == -ENOSPC)
2772 return btrfs_update_inode_item(trans, root, inode);
2773 return ret;
2774}
2775
2776/*
Chris Masond352ac62008-09-29 15:18:18 -04002777 * unlink helper that gets used here in inode.c and in the tree logging
2778 * recovery code. It remove a link in a directory with a given name, and
2779 * also drops the back refs in the inode to the directory
2780 */
Al Viro92986792011-03-04 17:14:37 +00002781static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2782 struct btrfs_root *root,
2783 struct inode *dir, struct inode *inode,
2784 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002785{
2786 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002787 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002788 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002789 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002790 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002791 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002792 u64 ino = btrfs_ino(inode);
2793 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002794
2795 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002796 if (!path) {
2797 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002798 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002799 }
2800
Chris Masonb9473432009-03-13 11:00:37 -04002801 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002802 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002803 name, name_len, -1);
2804 if (IS_ERR(di)) {
2805 ret = PTR_ERR(di);
2806 goto err;
2807 }
2808 if (!di) {
2809 ret = -ENOENT;
2810 goto err;
2811 }
Chris Mason5f39d392007-10-15 16:14:19 -04002812 leaf = path->nodes[0];
2813 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002814 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002815 if (ret)
2816 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002817 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002818
Li Zefan33345d012011-04-20 10:31:50 +08002819 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2820 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002821 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002822 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002823 "inode %llu parent %llu\n", name_len, name,
2824 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002825 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002826 goto err;
2827 }
2828
Miao Xie16cdcec2011-04-22 18:12:22 +08002829 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002830 if (ret) {
2831 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002832 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002833 }
Chris Mason39279cc2007-06-12 06:35:45 -04002834
Chris Masone02119d2008-09-05 16:13:11 -04002835 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002836 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002837 if (ret != 0 && ret != -ENOENT) {
2838 btrfs_abort_transaction(trans, root, ret);
2839 goto err;
2840 }
Chris Masone02119d2008-09-05 16:13:11 -04002841
2842 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2843 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002844 if (ret == -ENOENT)
2845 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002846err:
2847 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002848 if (ret)
2849 goto out;
2850
2851 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002852 inode_inc_iversion(inode);
2853 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002854 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002855 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002856out:
Chris Mason39279cc2007-06-12 06:35:45 -04002857 return ret;
2858}
2859
Al Viro92986792011-03-04 17:14:37 +00002860int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2861 struct btrfs_root *root,
2862 struct inode *dir, struct inode *inode,
2863 const char *name, int name_len)
2864{
2865 int ret;
2866 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2867 if (!ret) {
2868 btrfs_drop_nlink(inode);
2869 ret = btrfs_update_inode(trans, root, inode);
2870 }
2871 return ret;
2872}
2873
2874
Yan, Zhenga22285a2010-05-16 10:48:46 -04002875/* helper to check if there is any shared block in the path */
2876static int check_path_shared(struct btrfs_root *root,
2877 struct btrfs_path *path)
2878{
2879 struct extent_buffer *eb;
2880 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002881 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002882
2883 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002884 int ret;
2885
Yan, Zhenga22285a2010-05-16 10:48:46 -04002886 if (!path->nodes[level])
2887 break;
2888 eb = path->nodes[level];
2889 if (!btrfs_block_can_be_shared(root, eb))
2890 continue;
2891 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2892 &refs, NULL);
2893 if (refs > 1)
2894 return 1;
2895 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002896 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002897}
2898
2899/*
2900 * helper to start transaction for unlink and rmdir.
2901 *
2902 * unlink and rmdir are special in btrfs, they do not always free space.
2903 * so in enospc case, we should make sure they will free space before
2904 * allowing them to use the global metadata reservation.
2905 */
2906static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2907 struct dentry *dentry)
2908{
2909 struct btrfs_trans_handle *trans;
2910 struct btrfs_root *root = BTRFS_I(dir)->root;
2911 struct btrfs_path *path;
2912 struct btrfs_inode_ref *ref;
2913 struct btrfs_dir_item *di;
2914 struct inode *inode = dentry->d_inode;
2915 u64 index;
2916 int check_link = 1;
2917 int err = -ENOSPC;
2918 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002919 u64 ino = btrfs_ino(inode);
2920 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002921
Josef Bacike70bea52011-10-11 14:18:24 -04002922 /*
2923 * 1 for the possible orphan item
2924 * 1 for the dir item
2925 * 1 for the dir index
2926 * 1 for the inode ref
2927 * 1 for the inode ref in the tree log
2928 * 2 for the dir entries in the log
2929 * 1 for the inode
2930 */
2931 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002932 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2933 return trans;
2934
Li Zefan33345d012011-04-20 10:31:50 +08002935 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002936 return ERR_PTR(-ENOSPC);
2937
2938 /* check if there is someone else holds reference */
2939 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2940 return ERR_PTR(-ENOSPC);
2941
2942 if (atomic_read(&inode->i_count) > 2)
2943 return ERR_PTR(-ENOSPC);
2944
2945 if (xchg(&root->fs_info->enospc_unlink, 1))
2946 return ERR_PTR(-ENOSPC);
2947
2948 path = btrfs_alloc_path();
2949 if (!path) {
2950 root->fs_info->enospc_unlink = 0;
2951 return ERR_PTR(-ENOMEM);
2952 }
2953
Josef Bacik3880a1b2011-10-14 14:46:51 -04002954 /* 1 for the orphan item */
2955 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002956 if (IS_ERR(trans)) {
2957 btrfs_free_path(path);
2958 root->fs_info->enospc_unlink = 0;
2959 return trans;
2960 }
2961
2962 path->skip_locking = 1;
2963 path->search_commit_root = 1;
2964
2965 ret = btrfs_lookup_inode(trans, root, path,
2966 &BTRFS_I(dir)->location, 0);
2967 if (ret < 0) {
2968 err = ret;
2969 goto out;
2970 }
2971 if (ret == 0) {
2972 if (check_path_shared(root, path))
2973 goto out;
2974 } else {
2975 check_link = 0;
2976 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002977 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002978
2979 ret = btrfs_lookup_inode(trans, root, path,
2980 &BTRFS_I(inode)->location, 0);
2981 if (ret < 0) {
2982 err = ret;
2983 goto out;
2984 }
2985 if (ret == 0) {
2986 if (check_path_shared(root, path))
2987 goto out;
2988 } else {
2989 check_link = 0;
2990 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002991 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002992
2993 if (ret == 0 && S_ISREG(inode->i_mode)) {
2994 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002995 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002996 if (ret < 0) {
2997 err = ret;
2998 goto out;
2999 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003000 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003001 if (check_path_shared(root, path))
3002 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003003 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003004 }
3005
3006 if (!check_link) {
3007 err = 0;
3008 goto out;
3009 }
3010
Li Zefan33345d012011-04-20 10:31:50 +08003011 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003012 dentry->d_name.name, dentry->d_name.len, 0);
3013 if (IS_ERR(di)) {
3014 err = PTR_ERR(di);
3015 goto out;
3016 }
3017 if (di) {
3018 if (check_path_shared(root, path))
3019 goto out;
3020 } else {
3021 err = 0;
3022 goto out;
3023 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003024 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003025
3026 ref = btrfs_lookup_inode_ref(trans, root, path,
3027 dentry->d_name.name, dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08003028 ino, dir_ino, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003029 if (IS_ERR(ref)) {
3030 err = PTR_ERR(ref);
3031 goto out;
3032 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003033 BUG_ON(!ref); /* Logic error */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003034 if (check_path_shared(root, path))
3035 goto out;
3036 index = btrfs_inode_ref_index(path->nodes[0], ref);
David Sterbab3b4aa72011-04-21 01:20:15 +02003037 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003038
Miao Xie16cdcec2011-04-22 18:12:22 +08003039 /*
3040 * This is a commit root search, if we can lookup inode item and other
3041 * relative items in the commit root, it means the transaction of
3042 * dir/file creation has been committed, and the dir index item that we
3043 * delay to insert has also been inserted into the commit root. So
3044 * we needn't worry about the delayed insertion of the dir index item
3045 * here.
3046 */
Li Zefan33345d012011-04-20 10:31:50 +08003047 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003048 dentry->d_name.name, dentry->d_name.len, 0);
3049 if (IS_ERR(di)) {
3050 err = PTR_ERR(di);
3051 goto out;
3052 }
3053 BUG_ON(ret == -ENOENT);
3054 if (check_path_shared(root, path))
3055 goto out;
3056
3057 err = 0;
3058out:
3059 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003060 /* Migrate the orphan reservation over */
3061 if (!err)
3062 err = btrfs_block_rsv_migrate(trans->block_rsv,
3063 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003064 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003065
Yan, Zhenga22285a2010-05-16 10:48:46 -04003066 if (err) {
3067 btrfs_end_transaction(trans, root);
3068 root->fs_info->enospc_unlink = 0;
3069 return ERR_PTR(err);
3070 }
3071
3072 trans->block_rsv = &root->fs_info->global_block_rsv;
3073 return trans;
3074}
3075
3076static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3077 struct btrfs_root *root)
3078{
3079 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003080 btrfs_block_rsv_release(root, trans->block_rsv,
3081 trans->bytes_reserved);
3082 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003083 BUG_ON(!root->fs_info->enospc_unlink);
3084 root->fs_info->enospc_unlink = 0;
3085 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003086 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003087}
3088
Chris Mason39279cc2007-06-12 06:35:45 -04003089static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3090{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003091 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003092 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003093 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003094 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05003095 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003096
Yan, Zhenga22285a2010-05-16 10:48:46 -04003097 trans = __unlink_start_trans(dir, dentry);
3098 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003099 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003100
Chris Mason12fcfd22009-03-24 10:24:20 -04003101 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3102
Chris Masone02119d2008-09-05 16:13:11 -04003103 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3104 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003105 if (ret)
3106 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003107
Yan, Zhenga22285a2010-05-16 10:48:46 -04003108 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003109 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003110 if (ret)
3111 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003112 }
Josef Bacik7b128762008-07-24 12:17:14 -04003113
Tsutomu Itohb5324022011-07-19 07:27:20 +00003114out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003115 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003116 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003117 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003118 return ret;
3119}
3120
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003121int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3122 struct btrfs_root *root,
3123 struct inode *dir, u64 objectid,
3124 const char *name, int name_len)
3125{
3126 struct btrfs_path *path;
3127 struct extent_buffer *leaf;
3128 struct btrfs_dir_item *di;
3129 struct btrfs_key key;
3130 u64 index;
3131 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003132 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003133
3134 path = btrfs_alloc_path();
3135 if (!path)
3136 return -ENOMEM;
3137
Li Zefan33345d012011-04-20 10:31:50 +08003138 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003139 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003140 if (IS_ERR_OR_NULL(di)) {
3141 if (!di)
3142 ret = -ENOENT;
3143 else
3144 ret = PTR_ERR(di);
3145 goto out;
3146 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003147
3148 leaf = path->nodes[0];
3149 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3150 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3151 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003152 if (ret) {
3153 btrfs_abort_transaction(trans, root, ret);
3154 goto out;
3155 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003156 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003157
3158 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3159 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003160 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003161 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003162 if (ret != -ENOENT) {
3163 btrfs_abort_transaction(trans, root, ret);
3164 goto out;
3165 }
Li Zefan33345d012011-04-20 10:31:50 +08003166 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003167 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003168 if (IS_ERR_OR_NULL(di)) {
3169 if (!di)
3170 ret = -ENOENT;
3171 else
3172 ret = PTR_ERR(di);
3173 btrfs_abort_transaction(trans, root, ret);
3174 goto out;
3175 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003176
3177 leaf = path->nodes[0];
3178 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003179 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003180 index = key.offset;
3181 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003182 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003183
Miao Xie16cdcec2011-04-22 18:12:22 +08003184 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003185 if (ret) {
3186 btrfs_abort_transaction(trans, root, ret);
3187 goto out;
3188 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003189
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003190 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003191 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003192 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003193 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003194 if (ret)
3195 btrfs_abort_transaction(trans, root, ret);
3196out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003197 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003198 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003199}
3200
Chris Mason39279cc2007-06-12 06:35:45 -04003201static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3202{
3203 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003204 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003205 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003206 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003207 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003208
Chris Mason3394e162008-11-17 20:42:26 -05003209 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Li Zefan33345d012011-04-20 10:31:50 +08003210 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003211 return -ENOTEMPTY;
3212
Yan, Zhenga22285a2010-05-16 10:48:46 -04003213 trans = __unlink_start_trans(dir, dentry);
3214 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003215 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003216
Li Zefan33345d012011-04-20 10:31:50 +08003217 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003218 err = btrfs_unlink_subvol(trans, root, dir,
3219 BTRFS_I(inode)->location.objectid,
3220 dentry->d_name.name,
3221 dentry->d_name.len);
3222 goto out;
3223 }
3224
Josef Bacik7b128762008-07-24 12:17:14 -04003225 err = btrfs_orphan_add(trans, inode);
3226 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003227 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003228
Chris Mason39279cc2007-06-12 06:35:45 -04003229 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003230 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3231 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003232 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003233 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003234out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003235 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003236 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003237 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003238
Chris Mason39279cc2007-06-12 06:35:45 -04003239 return err;
3240}
3241
Chris Mason323ac952008-10-01 19:05:46 -04003242/*
Chris Mason39279cc2007-06-12 06:35:45 -04003243 * this can truncate away extent items, csum items and directory items.
3244 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003245 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003246 *
3247 * csum items that cross the new i_size are truncated to the new size
3248 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003249 *
3250 * min_type is the minimum key type to truncate down to. If set to 0, this
3251 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003252 */
Yan, Zheng80825102009-11-12 09:35:36 +00003253int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3254 struct btrfs_root *root,
3255 struct inode *inode,
3256 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003257{
Chris Mason39279cc2007-06-12 06:35:45 -04003258 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003259 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003260 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003261 struct btrfs_key key;
3262 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003263 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003264 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003265 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003266 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003267 u64 mask = root->sectorsize - 1;
3268 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003269 int found_extent;
3270 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003271 int pending_del_nr = 0;
3272 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003273 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003274 int ret;
3275 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003276 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003277
3278 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003279
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003280 path = btrfs_alloc_path();
3281 if (!path)
3282 return -ENOMEM;
3283 path->reada = -1;
3284
Josef Bacik5dc562c2012-08-17 13:14:17 -04003285 /*
3286 * We want to drop from the next block forward in case this new size is
3287 * not block aligned since we will be keeping the last block of the
3288 * extent just the way it is.
3289 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003290 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003291 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003292
Miao Xie16cdcec2011-04-22 18:12:22 +08003293 /*
3294 * This function is also used to drop the items in the log tree before
3295 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3296 * it is used to drop the loged items. So we shouldn't kill the delayed
3297 * items.
3298 */
3299 if (min_type == 0 && root == BTRFS_I(inode)->root)
3300 btrfs_kill_delayed_inode_items(inode);
3301
Li Zefan33345d012011-04-20 10:31:50 +08003302 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003303 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003304 key.type = (u8)-1;
3305
Chris Mason85e21ba2008-01-29 15:11:36 -05003306search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003307 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003308 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003309 if (ret < 0) {
3310 err = ret;
3311 goto out;
3312 }
Chris Masond3977122009-01-05 21:25:51 -05003313
Chris Mason85e21ba2008-01-29 15:11:36 -05003314 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003315 /* there are no items in the tree for us to truncate, we're
3316 * done
3317 */
Yan, Zheng80825102009-11-12 09:35:36 +00003318 if (path->slots[0] == 0)
3319 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003320 path->slots[0]--;
3321 }
3322
Chris Masond3977122009-01-05 21:25:51 -05003323 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003324 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003325 leaf = path->nodes[0];
3326 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3327 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003328
Li Zefan33345d012011-04-20 10:31:50 +08003329 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003330 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003331
Chris Mason85e21ba2008-01-29 15:11:36 -05003332 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003333 break;
3334
Chris Mason5f39d392007-10-15 16:14:19 -04003335 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003336 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003337 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003338 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003339 extent_type = btrfs_file_extent_type(leaf, fi);
3340 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003341 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003342 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003343 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003344 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003345 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003346 }
Yan008630c2007-11-07 13:31:09 -05003347 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003348 }
Yan, Zheng80825102009-11-12 09:35:36 +00003349 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003350 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003351 } else {
3352 if (item_end < new_size)
3353 break;
3354 if (found_key.offset >= new_size)
3355 del_item = 1;
3356 else
3357 del_item = 0;
3358 }
Chris Mason39279cc2007-06-12 06:35:45 -04003359 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003360 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003361 if (found_type != BTRFS_EXTENT_DATA_KEY)
3362 goto delete;
3363
3364 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003365 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003366 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003367 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003368 u64 orig_num_bytes =
3369 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003370 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003371 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003372 extent_num_bytes = extent_num_bytes &
3373 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003374 btrfs_set_file_extent_num_bytes(leaf, fi,
3375 extent_num_bytes);
3376 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003377 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003378 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003379 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003380 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003381 } else {
Chris Masondb945352007-10-15 16:15:53 -04003382 extent_num_bytes =
3383 btrfs_file_extent_disk_num_bytes(leaf,
3384 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003385 extent_offset = found_key.offset -
3386 btrfs_file_extent_offset(leaf, fi);
3387
Chris Mason39279cc2007-06-12 06:35:45 -04003388 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003389 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003390 if (extent_start != 0) {
3391 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003392 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003393 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003394 }
3395 }
Chris Mason90692182008-02-08 13:49:28 -05003396 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003397 /*
3398 * we can't truncate inline items that have had
3399 * special encodings
3400 */
3401 if (!del_item &&
3402 btrfs_file_extent_compression(leaf, fi) == 0 &&
3403 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3404 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003405 u32 size = new_size - found_key.offset;
3406
3407 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003408 inode_sub_bytes(inode, item_end + 1 -
3409 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003410 }
3411 size =
3412 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003413 btrfs_truncate_item(trans, root, path,
3414 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003415 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003416 inode_sub_bytes(inode, item_end + 1 -
3417 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003418 }
Chris Mason39279cc2007-06-12 06:35:45 -04003419 }
Chris Mason179e29e2007-11-01 11:28:41 -04003420delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003421 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003422 if (!pending_del_nr) {
3423 /* no pending yet, add ourselves */
3424 pending_del_slot = path->slots[0];
3425 pending_del_nr = 1;
3426 } else if (pending_del_nr &&
3427 path->slots[0] + 1 == pending_del_slot) {
3428 /* hop on the pending chunk */
3429 pending_del_nr++;
3430 pending_del_slot = path->slots[0];
3431 } else {
Chris Masond3977122009-01-05 21:25:51 -05003432 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003433 }
Chris Mason39279cc2007-06-12 06:35:45 -04003434 } else {
3435 break;
3436 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003437 if (found_extent && (root->ref_cows ||
3438 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003439 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003440 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003441 extent_num_bytes, 0,
3442 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003443 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003444 BUG_ON(ret);
3445 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003446
Yan, Zheng80825102009-11-12 09:35:36 +00003447 if (found_type == BTRFS_INODE_ITEM_KEY)
3448 break;
3449
3450 if (path->slots[0] == 0 ||
3451 path->slots[0] != pending_del_slot) {
Yan, Zheng80825102009-11-12 09:35:36 +00003452 if (pending_del_nr) {
3453 ret = btrfs_del_items(trans, root, path,
3454 pending_del_slot,
3455 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003456 if (ret) {
3457 btrfs_abort_transaction(trans,
3458 root, ret);
3459 goto error;
3460 }
Yan, Zheng80825102009-11-12 09:35:36 +00003461 pending_del_nr = 0;
3462 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003463 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003464 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003465 } else {
3466 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003467 }
Chris Mason39279cc2007-06-12 06:35:45 -04003468 }
Yan, Zheng80825102009-11-12 09:35:36 +00003469out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003470 if (pending_del_nr) {
3471 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3472 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003473 if (ret)
3474 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003475 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003476error:
Chris Mason39279cc2007-06-12 06:35:45 -04003477 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003478 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003479}
3480
Chris Masona52d9a82007-08-27 16:49:44 -04003481/*
3482 * taken from block_truncate_page, but does cow as it zeros out
3483 * any bytes left in the last page in the file.
3484 */
3485static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3486{
3487 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003488 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003489 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3490 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003491 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003492 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003493 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003494 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3495 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3496 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003497 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003498 int ret = 0;
3499 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003500 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003501
3502 if ((offset & (blocksize - 1)) == 0)
3503 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003504 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003505 if (ret)
3506 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003507
3508 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003509again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003510 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003511 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003512 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003513 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003514 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003515
3516 page_start = page_offset(page);
3517 page_end = page_start + PAGE_CACHE_SIZE - 1;
3518
Chris Masona52d9a82007-08-27 16:49:44 -04003519 if (!PageUptodate(page)) {
3520 ret = btrfs_readpage(NULL, page);
3521 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003522 if (page->mapping != mapping) {
3523 unlock_page(page);
3524 page_cache_release(page);
3525 goto again;
3526 }
Chris Masona52d9a82007-08-27 16:49:44 -04003527 if (!PageUptodate(page)) {
3528 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003529 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003530 }
3531 }
Chris Mason211c17f2008-05-15 09:13:45 -04003532 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003533
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003534 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003535 set_page_extent_mapped(page);
3536
3537 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3538 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003539 unlock_extent_cached(io_tree, page_start, page_end,
3540 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003541 unlock_page(page);
3542 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003543 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003544 btrfs_put_ordered_extent(ordered);
3545 goto again;
3546 }
3547
Josef Bacik2ac55d42010-02-03 19:33:23 +00003548 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003549 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003550 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003551
Josef Bacik2ac55d42010-02-03 19:33:23 +00003552 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3553 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003554 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003555 unlock_extent_cached(io_tree, page_start, page_end,
3556 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003557 goto out_unlock;
3558 }
3559
Chris Masone6dcd2d2008-07-17 12:53:50 -04003560 ret = 0;
3561 if (offset != PAGE_CACHE_SIZE) {
3562 kaddr = kmap(page);
3563 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3564 flush_dcache_page(page);
3565 kunmap(page);
3566 }
Chris Mason247e7432008-07-17 12:53:51 -04003567 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003568 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003569 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3570 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003571
Chris Mason89642222008-07-24 09:41:53 -04003572out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003573 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003574 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003575 unlock_page(page);
3576 page_cache_release(page);
3577out:
3578 return ret;
3579}
3580
Josef Bacik695a0d02011-03-04 15:46:53 -05003581/*
3582 * This function puts in dummy file extents for the area we're creating a hole
3583 * for. So if we are truncating this file to a larger size we need to insert
3584 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3585 * the range between oldsize and size
3586 */
Josef Bacika41ad392011-01-31 15:30:16 -05003587int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003588{
3589 struct btrfs_trans_handle *trans;
3590 struct btrfs_root *root = BTRFS_I(inode)->root;
3591 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003592 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003593 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003594 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003595 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003596 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003597 u64 block_end = (size + mask) & ~mask;
3598 u64 last_byte;
3599 u64 cur_offset;
3600 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003601 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003602
3603 if (size <= hole_start)
3604 return 0;
3605
Yan Zheng9036c102008-10-30 14:19:41 -04003606 while (1) {
3607 struct btrfs_ordered_extent *ordered;
3608 btrfs_wait_ordered_range(inode, hole_start,
3609 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003610 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003611 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003612 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3613 if (!ordered)
3614 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003615 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3616 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003617 btrfs_put_ordered_extent(ordered);
3618 }
3619
Yan Zheng9036c102008-10-30 14:19:41 -04003620 cur_offset = hole_start;
3621 while (1) {
3622 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3623 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003624 if (IS_ERR(em)) {
3625 err = PTR_ERR(em);
3626 break;
3627 }
Yan Zheng9036c102008-10-30 14:19:41 -04003628 last_byte = min(extent_map_end(em), block_end);
3629 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003630 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003631 struct extent_map *hole_em;
Chris Mason771ed682008-11-06 22:02:51 -05003632 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003633 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003634
Miao Xie36423202011-12-14 20:12:02 -05003635 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003636 if (IS_ERR(trans)) {
3637 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003638 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003639 }
Yan, Zheng80825102009-11-12 09:35:36 +00003640
Josef Bacik5dc562c2012-08-17 13:14:17 -04003641 err = btrfs_drop_extents(trans, root, inode,
3642 cur_offset,
Yan, Zheng80825102009-11-12 09:35:36 +00003643 cur_offset + hole_size,
3644 &hint_byte, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003645 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003646 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003647 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003648 break;
Miao Xie5b397372011-09-11 10:52:24 -04003649 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003650
Yan Zheng9036c102008-10-30 14:19:41 -04003651 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003652 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003653 0, hole_size, 0, hole_size,
3654 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003655 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003656 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003657 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003658 break;
Miao Xie5b397372011-09-11 10:52:24 -04003659 }
Yan, Zheng80825102009-11-12 09:35:36 +00003660
Josef Bacik5dc562c2012-08-17 13:14:17 -04003661 btrfs_drop_extent_cache(inode, cur_offset,
3662 cur_offset + hole_size - 1, 0);
3663 hole_em = alloc_extent_map();
3664 if (!hole_em) {
3665 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3666 &BTRFS_I(inode)->runtime_flags);
3667 goto next;
3668 }
3669 hole_em->start = cur_offset;
3670 hole_em->len = hole_size;
3671 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003672
Josef Bacik5dc562c2012-08-17 13:14:17 -04003673 hole_em->block_start = EXTENT_MAP_HOLE;
3674 hole_em->block_len = 0;
3675 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3676 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3677 hole_em->generation = trans->transid;
3678
3679 while (1) {
3680 write_lock(&em_tree->lock);
3681 err = add_extent_mapping(em_tree, hole_em);
3682 if (!err)
3683 list_move(&hole_em->list,
3684 &em_tree->modified_extents);
3685 write_unlock(&em_tree->lock);
3686 if (err != -EEXIST)
3687 break;
3688 btrfs_drop_extent_cache(inode, cur_offset,
3689 cur_offset +
3690 hole_size - 1, 0);
3691 }
3692 free_extent_map(hole_em);
3693next:
Miao Xie36423202011-12-14 20:12:02 -05003694 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003695 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003696 }
3697 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003698 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003699 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003700 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003701 break;
3702 }
3703
Yan, Zhenga22285a2010-05-16 10:48:46 -04003704 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003705 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3706 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003707 return err;
3708}
3709
Josef Bacika41ad392011-01-31 15:30:16 -05003710static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003711{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003712 struct btrfs_root *root = BTRFS_I(inode)->root;
3713 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003714 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003715 int ret;
3716
Josef Bacika41ad392011-01-31 15:30:16 -05003717 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003718 return 0;
3719
Josef Bacika41ad392011-01-31 15:30:16 -05003720 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003721 truncate_pagecache(inode, oldsize, newsize);
3722 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003723 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003724 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003725
Miao Xief4a2f4c2011-12-14 20:12:01 -05003726 trans = btrfs_start_transaction(root, 1);
3727 if (IS_ERR(trans))
3728 return PTR_ERR(trans);
3729
3730 i_size_write(inode, newsize);
3731 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3732 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003733 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003734 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003735
Josef Bacika41ad392011-01-31 15:30:16 -05003736 /*
3737 * We're truncating a file that used to have good data down to
3738 * zero. Make sure it gets into the ordered flush list so that
3739 * any new writes get down to disk quickly.
3740 */
3741 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003742 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3743 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003744
Josef Bacika41ad392011-01-31 15:30:16 -05003745 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3746 truncate_setsize(inode, newsize);
3747 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003748 }
3749
Josef Bacika41ad392011-01-31 15:30:16 -05003750 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003751}
3752
Chris Mason39279cc2007-06-12 06:35:45 -04003753static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3754{
3755 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003756 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003757 int err;
3758
Li Zefanb83cc962010-12-20 16:04:08 +08003759 if (btrfs_root_readonly(root))
3760 return -EROFS;
3761
Chris Mason39279cc2007-06-12 06:35:45 -04003762 err = inode_change_ok(inode, attr);
3763 if (err)
3764 return err;
3765
Chris Mason5a3f23d2009-03-31 13:27:11 -04003766 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003767 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003768 if (err)
3769 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003770 }
Yan Zheng9036c102008-10-30 14:19:41 -04003771
Christoph Hellwig10257742010-06-04 11:30:02 +02003772 if (attr->ia_valid) {
3773 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003774 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003775 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003776
Josef Bacik22c44fe2011-11-30 10:45:38 -05003777 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003778 err = btrfs_acl_chmod(inode);
3779 }
3780
Chris Mason39279cc2007-06-12 06:35:45 -04003781 return err;
3782}
Chris Mason61295eb2008-01-14 16:24:38 -05003783
Al Virobd555972010-06-07 11:35:40 -04003784void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003785{
3786 struct btrfs_trans_handle *trans;
3787 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003788 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003789 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003790 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003791 int ret;
3792
liubo1abe9b82011-03-24 11:18:59 +00003793 trace_btrfs_inode_evict(inode);
3794
Chris Mason39279cc2007-06-12 06:35:45 -04003795 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003796 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003797 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003798 goto no_delete;
3799
Chris Mason39279cc2007-06-12 06:35:45 -04003800 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003801 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003802 goto no_delete;
3803 }
Al Virobd555972010-06-07 11:35:40 -04003804 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003805 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003806
Yan, Zhengc71bf092009-11-12 09:34:40 +00003807 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003808 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003809 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003810 goto no_delete;
3811 }
3812
Yan, Zheng76dda932009-09-21 16:00:26 -04003813 if (inode->i_nlink > 0) {
3814 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3815 goto no_delete;
3816 }
3817
Josef Bacik4289a662011-08-05 13:22:24 -04003818 rsv = btrfs_alloc_block_rsv(root);
3819 if (!rsv) {
3820 btrfs_orphan_del(NULL, inode);
3821 goto no_delete;
3822 }
Josef Bacik4a338542011-08-29 11:01:31 -04003823 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04003824 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04003825 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003826
Chris Masondbe674a2008-07-17 12:54:05 -04003827 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003828
Josef Bacik4289a662011-08-05 13:22:24 -04003829 /*
3830 * This is a bit simpler than btrfs_truncate since
3831 *
3832 * 1) We've already reserved our space for our orphan item in the
3833 * unlink.
3834 * 2) We're going to delete the inode item, so we don't need to update
3835 * it at all.
3836 *
3837 * So we just need to reserve some slack space in case we add bytes when
3838 * doing the truncate.
3839 */
Yan, Zheng80825102009-11-12 09:35:36 +00003840 while (1) {
Miao Xieaa38a712011-11-18 17:43:00 +08003841 ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003842
Josef Bacik726c35f2011-09-26 15:46:06 -04003843 /*
3844 * Try and steal from the global reserve since we will
3845 * likely not use this space anyway, we want to try as
3846 * hard as possible to get this to work.
3847 */
3848 if (ret)
3849 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3850
Yan, Zhengd68fc572010-05-16 10:49:58 -04003851 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003852 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003853 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003854 btrfs_orphan_del(NULL, inode);
3855 btrfs_free_block_rsv(root, rsv);
3856 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003857 }
3858
Josef Bacik4289a662011-08-05 13:22:24 -04003859 trans = btrfs_start_transaction(root, 0);
3860 if (IS_ERR(trans)) {
3861 btrfs_orphan_del(NULL, inode);
3862 btrfs_free_block_rsv(root, rsv);
3863 goto no_delete;
3864 }
3865
3866 trans->block_rsv = rsv;
3867
Yan, Zhengd68fc572010-05-16 10:49:58 -04003868 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04003869 if (ret != -ENOSPC)
Yan, Zheng80825102009-11-12 09:35:36 +00003870 break;
3871
3872 nr = trans->blocks_used;
3873 btrfs_end_transaction(trans, root);
3874 trans = NULL;
3875 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003876 }
3877
Josef Bacik4289a662011-08-05 13:22:24 -04003878 btrfs_free_block_rsv(root, rsv);
3879
Yan, Zheng80825102009-11-12 09:35:36 +00003880 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003881 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003882 ret = btrfs_orphan_del(trans, inode);
3883 BUG_ON(ret);
3884 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003885
Josef Bacik4289a662011-08-05 13:22:24 -04003886 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003887 if (!(root == root->fs_info->tree_root ||
3888 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003889 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003890
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003891 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003892 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003893 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003894no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02003895 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003896 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003897}
3898
3899/*
3900 * this returns the key found in the dir entry in the location pointer.
3901 * If no dir entries were found, location->objectid is 0.
3902 */
3903static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3904 struct btrfs_key *location)
3905{
3906 const char *name = dentry->d_name.name;
3907 int namelen = dentry->d_name.len;
3908 struct btrfs_dir_item *di;
3909 struct btrfs_path *path;
3910 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003911 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003912
3913 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003914 if (!path)
3915 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003916
Li Zefan33345d012011-04-20 10:31:50 +08003917 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003918 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003919 if (IS_ERR(di))
3920 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003921
David Sterbac7040052011-04-19 18:00:01 +02003922 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003923 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003924
Chris Mason5f39d392007-10-15 16:14:19 -04003925 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003926out:
Chris Mason39279cc2007-06-12 06:35:45 -04003927 btrfs_free_path(path);
3928 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003929out_err:
3930 location->objectid = 0;
3931 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003932}
3933
3934/*
3935 * when we hit a tree root in a directory, the btrfs part of the inode
3936 * needs to be changed to reflect the root directory of the tree root. This
3937 * is kind of like crossing a mount point.
3938 */
3939static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003940 struct inode *dir,
3941 struct dentry *dentry,
3942 struct btrfs_key *location,
3943 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003944{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003945 struct btrfs_path *path;
3946 struct btrfs_root *new_root;
3947 struct btrfs_root_ref *ref;
3948 struct extent_buffer *leaf;
3949 int ret;
3950 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003951
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003952 path = btrfs_alloc_path();
3953 if (!path) {
3954 err = -ENOMEM;
3955 goto out;
3956 }
Chris Mason39279cc2007-06-12 06:35:45 -04003957
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003958 err = -ENOENT;
3959 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3960 BTRFS_I(dir)->root->root_key.objectid,
3961 location->objectid);
3962 if (ret) {
3963 if (ret < 0)
3964 err = ret;
3965 goto out;
3966 }
Chris Mason39279cc2007-06-12 06:35:45 -04003967
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003968 leaf = path->nodes[0];
3969 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003970 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003971 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3972 goto out;
3973
3974 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3975 (unsigned long)(ref + 1),
3976 dentry->d_name.len);
3977 if (ret)
3978 goto out;
3979
David Sterbab3b4aa72011-04-21 01:20:15 +02003980 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003981
3982 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3983 if (IS_ERR(new_root)) {
3984 err = PTR_ERR(new_root);
3985 goto out;
3986 }
3987
3988 if (btrfs_root_refs(&new_root->root_item) == 0) {
3989 err = -ENOENT;
3990 goto out;
3991 }
3992
3993 *sub_root = new_root;
3994 location->objectid = btrfs_root_dirid(&new_root->root_item);
3995 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003996 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003997 err = 0;
3998out:
3999 btrfs_free_path(path);
4000 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004001}
4002
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004003static void inode_tree_add(struct inode *inode)
4004{
4005 struct btrfs_root *root = BTRFS_I(inode)->root;
4006 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004007 struct rb_node **p;
4008 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004009 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004010again:
4011 p = &root->inode_tree.rb_node;
4012 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004013
Al Viro1d3382cb2010-10-23 15:19:20 -04004014 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004015 return;
4016
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004017 spin_lock(&root->inode_lock);
4018 while (*p) {
4019 parent = *p;
4020 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4021
Li Zefan33345d012011-04-20 10:31:50 +08004022 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004023 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004024 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004025 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004026 else {
4027 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004028 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004029 rb_erase(parent, &root->inode_tree);
4030 RB_CLEAR_NODE(parent);
4031 spin_unlock(&root->inode_lock);
4032 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004033 }
4034 }
4035 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4036 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4037 spin_unlock(&root->inode_lock);
4038}
4039
4040static void inode_tree_del(struct inode *inode)
4041{
4042 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004043 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004044
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004045 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004046 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004047 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004048 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004049 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004050 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004051 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004052
Josef Bacik0af3d002010-06-21 14:48:16 -04004053 /*
4054 * Free space cache has inodes in the tree root, but the tree root has a
4055 * root_refs of 0, so this could end up dropping the tree root as a
4056 * snapshot, so we need the extra !root->fs_info->tree_root check to
4057 * make sure we don't drop it.
4058 */
4059 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4060 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004061 synchronize_srcu(&root->fs_info->subvol_srcu);
4062 spin_lock(&root->inode_lock);
4063 empty = RB_EMPTY_ROOT(&root->inode_tree);
4064 spin_unlock(&root->inode_lock);
4065 if (empty)
4066 btrfs_add_dead_root(root);
4067 }
4068}
4069
Jeff Mahoney143bede2012-03-01 14:56:26 +01004070void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004071{
4072 struct rb_node *node;
4073 struct rb_node *prev;
4074 struct btrfs_inode *entry;
4075 struct inode *inode;
4076 u64 objectid = 0;
4077
4078 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4079
4080 spin_lock(&root->inode_lock);
4081again:
4082 node = root->inode_tree.rb_node;
4083 prev = NULL;
4084 while (node) {
4085 prev = node;
4086 entry = rb_entry(node, struct btrfs_inode, rb_node);
4087
Li Zefan33345d012011-04-20 10:31:50 +08004088 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004089 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004090 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004091 node = node->rb_right;
4092 else
4093 break;
4094 }
4095 if (!node) {
4096 while (prev) {
4097 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004098 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004099 node = prev;
4100 break;
4101 }
4102 prev = rb_next(prev);
4103 }
4104 }
4105 while (node) {
4106 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004107 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004108 inode = igrab(&entry->vfs_inode);
4109 if (inode) {
4110 spin_unlock(&root->inode_lock);
4111 if (atomic_read(&inode->i_count) > 1)
4112 d_prune_aliases(inode);
4113 /*
Al Viro45321ac2010-06-07 13:43:19 -04004114 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004115 * the inode cache when its usage count
4116 * hits zero.
4117 */
4118 iput(inode);
4119 cond_resched();
4120 spin_lock(&root->inode_lock);
4121 goto again;
4122 }
4123
4124 if (cond_resched_lock(&root->inode_lock))
4125 goto again;
4126
4127 node = rb_next(node);
4128 }
4129 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004130}
4131
Chris Masone02119d2008-09-05 16:13:11 -04004132static int btrfs_init_locked_inode(struct inode *inode, void *p)
4133{
4134 struct btrfs_iget_args *args = p;
4135 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004136 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004137 return 0;
4138}
4139
4140static int btrfs_find_actor(struct inode *inode, void *opaque)
4141{
4142 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004143 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004144 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004145}
4146
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004147static struct inode *btrfs_iget_locked(struct super_block *s,
4148 u64 objectid,
4149 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004150{
4151 struct inode *inode;
4152 struct btrfs_iget_args args;
4153 args.ino = objectid;
4154 args.root = root;
4155
4156 inode = iget5_locked(s, objectid, btrfs_find_actor,
4157 btrfs_init_locked_inode,
4158 (void *)&args);
4159 return inode;
4160}
4161
Balaji Rao1a54ef82008-07-21 02:01:04 +05304162/* Get an inode object given its location and corresponding root.
4163 * Returns in *is_new if the inode was read from disk
4164 */
4165struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004166 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304167{
4168 struct inode *inode;
4169
4170 inode = btrfs_iget_locked(s, location->objectid, root);
4171 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004172 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304173
4174 if (inode->i_state & I_NEW) {
4175 BTRFS_I(inode)->root = root;
4176 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4177 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004178 if (!is_bad_inode(inode)) {
4179 inode_tree_add(inode);
4180 unlock_new_inode(inode);
4181 if (new)
4182 *new = 1;
4183 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004184 unlock_new_inode(inode);
4185 iput(inode);
4186 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004187 }
4188 }
4189
Balaji Rao1a54ef82008-07-21 02:01:04 +05304190 return inode;
4191}
4192
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004193static struct inode *new_simple_dir(struct super_block *s,
4194 struct btrfs_key *key,
4195 struct btrfs_root *root)
4196{
4197 struct inode *inode = new_inode(s);
4198
4199 if (!inode)
4200 return ERR_PTR(-ENOMEM);
4201
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004202 BTRFS_I(inode)->root = root;
4203 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004204 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004205
4206 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004207 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004208 inode->i_fop = &simple_dir_operations;
4209 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4210 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4211
4212 return inode;
4213}
4214
Chris Mason3de45862008-11-17 21:02:50 -05004215struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004216{
Chris Masond3977122009-01-05 21:25:51 -05004217 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004218 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004219 struct btrfs_root *sub_root = root;
4220 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004221 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004222 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004223
4224 if (dentry->d_name.len > BTRFS_NAME_LEN)
4225 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004226
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004227 if (unlikely(d_need_lookup(dentry))) {
4228 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4229 kfree(dentry->d_fsdata);
4230 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004231 /* This thing is hashed, drop it for now */
4232 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004233 } else {
4234 ret = btrfs_inode_by_name(dir, dentry, &location);
4235 }
Chris Mason5f39d392007-10-15 16:14:19 -04004236
Chris Mason39279cc2007-06-12 06:35:45 -04004237 if (ret < 0)
4238 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004239
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004240 if (location.objectid == 0)
4241 return NULL;
4242
4243 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004244 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004245 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004246 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004247
4248 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4249
Yan, Zheng76dda932009-09-21 16:00:26 -04004250 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004251 ret = fixup_tree_root_location(root, dir, dentry,
4252 &location, &sub_root);
4253 if (ret < 0) {
4254 if (ret != -ENOENT)
4255 inode = ERR_PTR(ret);
4256 else
4257 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4258 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004259 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004260 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004261 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4262
Julia Lawall34d19ba2011-01-24 19:55:19 +00004263 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004264 down_read(&root->fs_info->cleanup_work_sem);
4265 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004266 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004267 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004268 if (ret)
4269 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004270 }
4271
Chris Mason3de45862008-11-17 21:02:50 -05004272 return inode;
4273}
4274
Nick Pigginfe15ce42011-01-07 17:49:23 +11004275static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004276{
4277 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004278 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004279
Li Zefan848cce02012-02-21 17:04:28 +08004280 if (!inode && !IS_ROOT(dentry))
4281 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004282
Li Zefan848cce02012-02-21 17:04:28 +08004283 if (inode) {
4284 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004285 if (btrfs_root_refs(&root->root_item) == 0)
4286 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004287
4288 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4289 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004290 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004291 return 0;
4292}
4293
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004294static void btrfs_dentry_release(struct dentry *dentry)
4295{
4296 if (dentry->d_fsdata)
4297 kfree(dentry->d_fsdata);
4298}
4299
Chris Mason3de45862008-11-17 21:02:50 -05004300static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004301 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004302{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004303 struct dentry *ret;
4304
4305 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4306 if (unlikely(d_need_lookup(dentry))) {
4307 spin_lock(&dentry->d_lock);
4308 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4309 spin_unlock(&dentry->d_lock);
4310 }
4311 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004312}
4313
Miao Xie16cdcec2011-04-22 18:12:22 +08004314unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004315 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4316};
4317
David Woodhousecbdf5a22008-08-06 19:42:33 +01004318static int btrfs_real_readdir(struct file *filp, void *dirent,
4319 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004320{
Chris Mason6da6aba2007-12-18 16:15:09 -05004321 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004322 struct btrfs_root *root = BTRFS_I(inode)->root;
4323 struct btrfs_item *item;
4324 struct btrfs_dir_item *di;
4325 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004326 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004327 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004328 struct list_head ins_list;
4329 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004330 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004331 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004332 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004333 unsigned char d_type;
4334 int over = 0;
4335 u32 di_cur;
4336 u32 di_total;
4337 u32 di_len;
4338 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004339 char tmp_name[32];
4340 char *name_ptr;
4341 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004342 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004343
4344 /* FIXME, use a real flag for deciding about the key type */
4345 if (root->fs_info->tree_root == root)
4346 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004347
Chris Mason39544012007-12-12 14:38:19 -05004348 /* special case for "." */
4349 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004350 over = filldir(dirent, ".", 1,
4351 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004352 if (over)
4353 return 0;
4354 filp->f_pos = 1;
4355 }
Chris Mason39544012007-12-12 14:38:19 -05004356 /* special case for .., just use the back ref */
4357 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004358 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004359 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004360 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004361 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004362 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004363 filp->f_pos = 2;
4364 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004365 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004366 if (!path)
4367 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004368
Josef Bacik026fd312011-05-13 10:32:11 -04004369 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004370
Miao Xie16cdcec2011-04-22 18:12:22 +08004371 if (key_type == BTRFS_DIR_INDEX_KEY) {
4372 INIT_LIST_HEAD(&ins_list);
4373 INIT_LIST_HEAD(&del_list);
4374 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4375 }
4376
Chris Mason39279cc2007-06-12 06:35:45 -04004377 btrfs_set_key_type(&key, key_type);
4378 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004379 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004380
Chris Mason39279cc2007-06-12 06:35:45 -04004381 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4382 if (ret < 0)
4383 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004384
4385 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004386 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004387 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004388 if (slot >= btrfs_header_nritems(leaf)) {
4389 ret = btrfs_next_leaf(root, path);
4390 if (ret < 0)
4391 goto err;
4392 else if (ret > 0)
4393 break;
4394 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004395 }
Chris Mason3de45862008-11-17 21:02:50 -05004396
Chris Mason5f39d392007-10-15 16:14:19 -04004397 item = btrfs_item_nr(leaf, slot);
4398 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4399
4400 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004401 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004402 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004403 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004404 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004405 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004406 if (key_type == BTRFS_DIR_INDEX_KEY &&
4407 btrfs_should_delete_dir_index(&del_list,
4408 found_key.offset))
4409 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004410
4411 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004412 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004413
Chris Mason39279cc2007-06-12 06:35:45 -04004414 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4415 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004416 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004417
4418 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004419 struct btrfs_key location;
4420
Josef Bacik22a94d42011-03-16 16:47:17 -04004421 if (verify_dir_item(root, leaf, di))
4422 break;
4423
Chris Mason5f39d392007-10-15 16:14:19 -04004424 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004425 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004426 name_ptr = tmp_name;
4427 } else {
4428 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004429 if (!name_ptr) {
4430 ret = -ENOMEM;
4431 goto err;
4432 }
Chris Mason5f39d392007-10-15 16:14:19 -04004433 }
4434 read_extent_buffer(leaf, name_ptr,
4435 (unsigned long)(di + 1), name_len);
4436
4437 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4438 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004439
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004440
Chris Mason3de45862008-11-17 21:02:50 -05004441 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004442 * skip it.
4443 *
4444 * In contrast to old kernels, we insert the snapshot's
4445 * dir item and dir index after it has been created, so
4446 * we won't find a reference to our own snapshot. We
4447 * still keep the following code for backward
4448 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004449 */
4450 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4451 location.objectid == root->root_key.objectid) {
4452 over = 0;
4453 goto skip;
4454 }
Chris Mason5f39d392007-10-15 16:14:19 -04004455 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004456 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004457 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004458
Chris Mason3de45862008-11-17 21:02:50 -05004459skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004460 if (name_ptr != tmp_name)
4461 kfree(name_ptr);
4462
Chris Mason39279cc2007-06-12 06:35:45 -04004463 if (over)
4464 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004465 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004466 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004467 di_cur += di_len;
4468 di = (struct btrfs_dir_item *)((char *)di + di_len);
4469 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004470next:
4471 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004472 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004473
Miao Xie16cdcec2011-04-22 18:12:22 +08004474 if (key_type == BTRFS_DIR_INDEX_KEY) {
4475 if (is_curr)
4476 filp->f_pos++;
4477 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4478 &ins_list);
4479 if (ret)
4480 goto nopos;
4481 }
4482
David Woodhouse49593bf2008-08-17 17:08:36 +01004483 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004484 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004485 /*
4486 * 32-bit glibc will use getdents64, but then strtol -
4487 * so the last number we can serve is this.
4488 */
4489 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004490 else
4491 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004492nopos:
4493 ret = 0;
4494err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004495 if (key_type == BTRFS_DIR_INDEX_KEY)
4496 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004497 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004498 return ret;
4499}
4500
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004501int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004502{
4503 struct btrfs_root *root = BTRFS_I(inode)->root;
4504 struct btrfs_trans_handle *trans;
4505 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004506 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004507
Josef Bacik72ac3c02012-05-23 14:13:11 -04004508 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004509 return 0;
4510
Liu Bo83eea1f2012-07-10 05:28:39 -06004511 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004512 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004513
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004514 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004515 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004516 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004517 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004518 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004519 if (IS_ERR(trans))
4520 return PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04004521 if (nolock)
4522 ret = btrfs_end_transaction_nolock(trans, root);
4523 else
4524 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004525 }
4526 return ret;
4527}
4528
4529/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004530 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004531 * inode changes. But, it is most likely to find the inode in cache.
4532 * FIXME, needs more benchmarking...there are no reasons other than performance
4533 * to keep or drop this code.
4534 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004535int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004536{
4537 struct btrfs_root *root = BTRFS_I(inode)->root;
4538 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004539 int ret;
4540
Josef Bacik72ac3c02012-05-23 14:13:11 -04004541 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004542 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004543
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004544 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004545 if (IS_ERR(trans))
4546 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004547
4548 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004549 if (ret && ret == -ENOSPC) {
4550 /* whoops, lets try again with the full transaction */
4551 btrfs_end_transaction(trans, root);
4552 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004553 if (IS_ERR(trans))
4554 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004555
Chris Mason94b60442010-05-26 11:02:00 -04004556 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004557 }
Chris Mason39279cc2007-06-12 06:35:45 -04004558 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004559 if (BTRFS_I(inode)->delayed_node)
4560 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004561
4562 return ret;
4563}
4564
4565/*
4566 * This is a copy of file_update_time. We need this so we can return error on
4567 * ENOSPC for updating the inode in the case of file write and mmap writes.
4568 */
Josef Bacike41f9412012-03-26 09:46:47 -04004569static int btrfs_update_time(struct inode *inode, struct timespec *now,
4570 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004571{
Alexander Block2bc5565282012-06-15 09:49:33 +02004572 struct btrfs_root *root = BTRFS_I(inode)->root;
4573
4574 if (btrfs_root_readonly(root))
4575 return -EROFS;
4576
Josef Bacike41f9412012-03-26 09:46:47 -04004577 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004578 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004579 if (flags & S_CTIME)
4580 inode->i_ctime = *now;
4581 if (flags & S_MTIME)
4582 inode->i_mtime = *now;
4583 if (flags & S_ATIME)
4584 inode->i_atime = *now;
4585 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004586}
4587
Chris Masond352ac62008-09-29 15:18:18 -04004588/*
4589 * find the highest existing sequence number in a directory
4590 * and then set the in-memory index_cnt variable to reflect
4591 * free sequence numbers
4592 */
Josef Bacikaec74772008-07-24 12:12:38 -04004593static int btrfs_set_inode_index_count(struct inode *inode)
4594{
4595 struct btrfs_root *root = BTRFS_I(inode)->root;
4596 struct btrfs_key key, found_key;
4597 struct btrfs_path *path;
4598 struct extent_buffer *leaf;
4599 int ret;
4600
Li Zefan33345d012011-04-20 10:31:50 +08004601 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004602 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4603 key.offset = (u64)-1;
4604
4605 path = btrfs_alloc_path();
4606 if (!path)
4607 return -ENOMEM;
4608
4609 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4610 if (ret < 0)
4611 goto out;
4612 /* FIXME: we should be able to handle this */
4613 if (ret == 0)
4614 goto out;
4615 ret = 0;
4616
4617 /*
4618 * MAGIC NUMBER EXPLANATION:
4619 * since we search a directory based on f_pos we have to start at 2
4620 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4621 * else has to start at 2
4622 */
4623 if (path->slots[0] == 0) {
4624 BTRFS_I(inode)->index_cnt = 2;
4625 goto out;
4626 }
4627
4628 path->slots[0]--;
4629
4630 leaf = path->nodes[0];
4631 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4632
Li Zefan33345d012011-04-20 10:31:50 +08004633 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004634 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4635 BTRFS_I(inode)->index_cnt = 2;
4636 goto out;
4637 }
4638
4639 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4640out:
4641 btrfs_free_path(path);
4642 return ret;
4643}
4644
Chris Masond352ac62008-09-29 15:18:18 -04004645/*
4646 * helper to find a free sequence number in a given directory. This current
4647 * code is very simple, later versions will do smarter things in the btree
4648 */
Chris Mason3de45862008-11-17 21:02:50 -05004649int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004650{
4651 int ret = 0;
4652
4653 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004654 ret = btrfs_inode_delayed_dir_index_count(dir);
4655 if (ret) {
4656 ret = btrfs_set_inode_index_count(dir);
4657 if (ret)
4658 return ret;
4659 }
Josef Bacikaec74772008-07-24 12:12:38 -04004660 }
4661
Chris Mason00e4e6b2008-08-05 11:18:09 -04004662 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004663 BTRFS_I(dir)->index_cnt++;
4664
4665 return ret;
4666}
4667
Chris Mason39279cc2007-06-12 06:35:45 -04004668static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4669 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004670 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004671 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004672 u64 ref_objectid, u64 objectid,
4673 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004674{
4675 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004676 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004677 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004678 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004679 struct btrfs_inode_ref *ref;
4680 struct btrfs_key key[2];
4681 u32 sizes[2];
4682 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004683 int ret;
4684 int owner;
4685
Chris Mason5f39d392007-10-15 16:14:19 -04004686 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004687 if (!path)
4688 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004689
Chris Mason39279cc2007-06-12 06:35:45 -04004690 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004691 if (!inode) {
4692 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004693 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004694 }
Chris Mason39279cc2007-06-12 06:35:45 -04004695
Li Zefan581bb052011-04-20 10:06:11 +08004696 /*
4697 * we have to initialize this early, so we can reclaim the inode
4698 * number if we fail afterwards in this function.
4699 */
4700 inode->i_ino = objectid;
4701
Josef Bacikaec74772008-07-24 12:12:38 -04004702 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004703 trace_btrfs_inode_request(dir);
4704
Chris Mason3de45862008-11-17 21:02:50 -05004705 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004706 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004707 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004708 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004709 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004710 }
Josef Bacikaec74772008-07-24 12:12:38 -04004711 }
4712 /*
4713 * index_cnt is ignored for everything but a dir,
4714 * btrfs_get_inode_index_count has an explanation for the magic
4715 * number
4716 */
4717 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004718 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004719 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004720 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04004721
Josef Bacik5dc562c2012-08-17 13:14:17 -04004722 /*
4723 * We could have gotten an inode number from somebody who was fsynced
4724 * and then removed in this same transaction, so let's just set full
4725 * sync since it will be a full sync anyway and this will blow away the
4726 * old info in the log.
4727 */
4728 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4729
Al Viro569254b2011-07-24 17:08:40 -04004730 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004731 owner = 0;
4732 else
4733 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004734
4735 key[0].objectid = objectid;
4736 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4737 key[0].offset = 0;
4738
4739 key[1].objectid = objectid;
4740 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4741 key[1].offset = ref_objectid;
4742
4743 sizes[0] = sizeof(struct btrfs_inode_item);
4744 sizes[1] = name_len + sizeof(*ref);
4745
Chris Masonb9473432009-03-13 11:00:37 -04004746 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004747 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4748 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004749 goto fail;
4750
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004751 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004752 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004753 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004754 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4755 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004756 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4757 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004758 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004759
4760 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4761 struct btrfs_inode_ref);
4762 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004763 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004764 ptr = (unsigned long)(ref + 1);
4765 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4766
Chris Mason5f39d392007-10-15 16:14:19 -04004767 btrfs_mark_buffer_dirty(path->nodes[0]);
4768 btrfs_free_path(path);
4769
Chris Mason39279cc2007-06-12 06:35:45 -04004770 location = &BTRFS_I(inode)->location;
4771 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004772 location->offset = 0;
4773 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4774
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004775 btrfs_inherit_iflags(inode, dir);
4776
Al Viro569254b2011-07-24 17:08:40 -04004777 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004778 if (btrfs_test_opt(root, NODATASUM))
4779 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004780 if (btrfs_test_opt(root, NODATACOW) ||
4781 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004782 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4783 }
4784
Chris Mason39279cc2007-06-12 06:35:45 -04004785 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004786 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004787
4788 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004789 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004790
Alexander Block8ea05e32012-07-25 17:35:53 +02004791 btrfs_update_root_times(trans, root);
4792
Chris Mason39279cc2007-06-12 06:35:45 -04004793 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004794fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004795 if (dir)
4796 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004797 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004798 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004799 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004800}
4801
4802static inline u8 btrfs_inode_type(struct inode *inode)
4803{
4804 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4805}
4806
Chris Masond352ac62008-09-29 15:18:18 -04004807/*
4808 * utility function to add 'inode' into 'parent_inode' with
4809 * a give name and a given sequence number.
4810 * if 'add_backref' is true, also insert a backref from the
4811 * inode to the parent directory.
4812 */
Chris Masone02119d2008-09-05 16:13:11 -04004813int btrfs_add_link(struct btrfs_trans_handle *trans,
4814 struct inode *parent_inode, struct inode *inode,
4815 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004816{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004817 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004818 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004819 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004820 u64 ino = btrfs_ino(inode);
4821 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004822
Li Zefan33345d012011-04-20 10:31:50 +08004823 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004824 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4825 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004826 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004827 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4828 key.offset = 0;
4829 }
Chris Mason39279cc2007-06-12 06:35:45 -04004830
Li Zefan33345d012011-04-20 10:31:50 +08004831 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004832 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4833 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004834 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004835 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004836 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4837 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004838 }
4839
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004840 /* Nothing to clean up yet */
4841 if (ret)
4842 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004843
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004844 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4845 parent_inode, &key,
4846 btrfs_inode_type(inode), index);
4847 if (ret == -EEXIST)
4848 goto fail_dir_item;
4849 else if (ret) {
4850 btrfs_abort_transaction(trans, root, ret);
4851 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004852 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004853
4854 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4855 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004856 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004857 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4858 ret = btrfs_update_inode(trans, root, parent_inode);
4859 if (ret)
4860 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004861 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004862
4863fail_dir_item:
4864 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4865 u64 local_index;
4866 int err;
4867 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4868 key.objectid, root->root_key.objectid,
4869 parent_ino, &local_index, name, name_len);
4870
4871 } else if (add_backref) {
4872 u64 local_index;
4873 int err;
4874
4875 err = btrfs_del_inode_ref(trans, root, name, name_len,
4876 ino, parent_ino, &local_index);
4877 }
4878 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004879}
4880
4881static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004882 struct inode *dir, struct dentry *dentry,
4883 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004884{
Josef Bacika1b075d2010-11-19 20:36:11 +00004885 int err = btrfs_add_link(trans, dir, inode,
4886 dentry->d_name.name, dentry->d_name.len,
4887 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004888 if (err > 0)
4889 err = -EEXIST;
4890 return err;
4891}
4892
Josef Bacik618e21d2007-07-11 10:18:17 -04004893static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004894 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004895{
4896 struct btrfs_trans_handle *trans;
4897 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004898 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004899 int err;
4900 int drop_inode = 0;
4901 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004902 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004903 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004904
4905 if (!new_valid_dev(rdev))
4906 return -EINVAL;
4907
Josef Bacik9ed74f22009-09-11 16:12:44 -04004908 /*
4909 * 2 for inode item and ref
4910 * 2 for dir items
4911 * 1 for xattr if selinux is on
4912 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004913 trans = btrfs_start_transaction(root, 5);
4914 if (IS_ERR(trans))
4915 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004916
Li Zefan581bb052011-04-20 10:06:11 +08004917 err = btrfs_find_free_ino(root, &objectid);
4918 if (err)
4919 goto out_unlock;
4920
Josef Bacikaec74772008-07-24 12:12:38 -04004921 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004922 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004923 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004924 if (IS_ERR(inode)) {
4925 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004926 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004927 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004928
Eric Paris2a7dba32011-02-01 11:05:39 -05004929 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004930 if (err) {
4931 drop_inode = 1;
4932 goto out_unlock;
4933 }
4934
Casey Schauflerad19db72011-12-15 10:09:07 -05004935 /*
4936 * If the active LSM wants to access the inode during
4937 * d_instantiate it needs these. Smack checks to see
4938 * if the filesystem supports xattrs by looking at the
4939 * ops vector.
4940 */
4941
4942 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004943 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004944 if (err)
4945 drop_inode = 1;
4946 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004947 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004948 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05004949 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004950 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004951out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004952 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004953 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004954 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004955 if (drop_inode) {
4956 inode_dec_link_count(inode);
4957 iput(inode);
4958 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004959 return err;
4960}
4961
Chris Mason39279cc2007-06-12 06:35:45 -04004962static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04004963 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04004964{
4965 struct btrfs_trans_handle *trans;
4966 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004967 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004968 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004969 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004970 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004971 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004972 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004973
Josef Bacik9ed74f22009-09-11 16:12:44 -04004974 /*
4975 * 2 for inode item and ref
4976 * 2 for dir items
4977 * 1 for xattr if selinux is on
4978 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004979 trans = btrfs_start_transaction(root, 5);
4980 if (IS_ERR(trans))
4981 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004982
Li Zefan581bb052011-04-20 10:06:11 +08004983 err = btrfs_find_free_ino(root, &objectid);
4984 if (err)
4985 goto out_unlock;
4986
Josef Bacikaec74772008-07-24 12:12:38 -04004987 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004988 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004989 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004990 if (IS_ERR(inode)) {
4991 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004992 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004993 }
Chris Mason39279cc2007-06-12 06:35:45 -04004994
Eric Paris2a7dba32011-02-01 11:05:39 -05004995 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004996 if (err) {
4997 drop_inode = 1;
4998 goto out_unlock;
4999 }
5000
Casey Schauflerad19db72011-12-15 10:09:07 -05005001 /*
5002 * If the active LSM wants to access the inode during
5003 * d_instantiate it needs these. Smack checks to see
5004 * if the filesystem supports xattrs by looking at the
5005 * ops vector.
5006 */
5007 inode->i_fop = &btrfs_file_operations;
5008 inode->i_op = &btrfs_file_inode_operations;
5009
Josef Bacika1b075d2010-11-19 20:36:11 +00005010 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005011 if (err)
5012 drop_inode = 1;
5013 else {
5014 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005015 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05005016 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro08c422c2011-12-23 07:58:13 -05005017 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005018 }
Chris Mason39279cc2007-06-12 06:35:45 -04005019out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005020 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005021 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005022 if (drop_inode) {
5023 inode_dec_link_count(inode);
5024 iput(inode);
5025 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005026 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005027 return err;
5028}
5029
5030static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5031 struct dentry *dentry)
5032{
5033 struct btrfs_trans_handle *trans;
5034 struct btrfs_root *root = BTRFS_I(dir)->root;
5035 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005036 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05005037 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005038 int err;
5039 int drop_inode = 0;
5040
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005041 /* do not allow sys_link's with other subvols of the same device */
5042 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005043 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005044
Al Viroc055e992011-03-04 17:15:18 +00005045 if (inode->i_nlink == ~0U)
5046 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005047
Chris Mason3de45862008-11-17 21:02:50 -05005048 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005049 if (err)
5050 goto fail;
5051
Yan, Zhenga22285a2010-05-16 10:48:46 -04005052 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005053 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005054 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005055 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005056 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005057 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005058 if (IS_ERR(trans)) {
5059 err = PTR_ERR(trans);
5060 goto fail;
5061 }
Chris Mason5f39d392007-10-15 16:14:19 -04005062
Miao Xie31534952011-04-13 13:19:21 +08005063 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005064 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005065 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005066 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005067
Josef Bacika1b075d2010-11-19 20:36:11 +00005068 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005069
Yan, Zhenga5719522009-09-24 09:17:31 -04005070 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005071 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005072 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005073 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005074 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005075 if (err)
5076 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005077 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005078 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005079 }
Chris Mason39279cc2007-06-12 06:35:45 -04005080
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005081 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005082 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005083fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005084 if (drop_inode) {
5085 inode_dec_link_count(inode);
5086 iput(inode);
5087 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005088 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005089 return err;
5090}
5091
Al Viro18bb1db2011-07-26 01:41:39 -04005092static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005093{
Chris Masonb9d86662008-05-02 16:13:49 -04005094 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005095 struct btrfs_trans_handle *trans;
5096 struct btrfs_root *root = BTRFS_I(dir)->root;
5097 int err = 0;
5098 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005099 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005100 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005101 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005102
Josef Bacik9ed74f22009-09-11 16:12:44 -04005103 /*
5104 * 2 items for inode and ref
5105 * 2 items for dir items
5106 * 1 for xattr if selinux is on
5107 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005108 trans = btrfs_start_transaction(root, 5);
5109 if (IS_ERR(trans))
5110 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005111
Li Zefan581bb052011-04-20 10:06:11 +08005112 err = btrfs_find_free_ino(root, &objectid);
5113 if (err)
5114 goto out_fail;
5115
Josef Bacikaec74772008-07-24 12:12:38 -04005116 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005117 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005118 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005119 if (IS_ERR(inode)) {
5120 err = PTR_ERR(inode);
5121 goto out_fail;
5122 }
Chris Mason5f39d392007-10-15 16:14:19 -04005123
Chris Mason39279cc2007-06-12 06:35:45 -04005124 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005125
Eric Paris2a7dba32011-02-01 11:05:39 -05005126 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005127 if (err)
5128 goto out_fail;
5129
Chris Mason39279cc2007-06-12 06:35:45 -04005130 inode->i_op = &btrfs_dir_inode_operations;
5131 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005132
Chris Masondbe674a2008-07-17 12:54:05 -04005133 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005134 err = btrfs_update_inode(trans, root, inode);
5135 if (err)
5136 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005137
Josef Bacika1b075d2010-11-19 20:36:11 +00005138 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5139 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005140 if (err)
5141 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005142
Chris Mason39279cc2007-06-12 06:35:45 -04005143 d_instantiate(dentry, inode);
5144 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005145
5146out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005147 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005148 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005149 if (drop_on_err)
5150 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005151 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005152 return err;
5153}
5154
Chris Masond352ac62008-09-29 15:18:18 -04005155/* helper for btfs_get_extent. Given an existing extent in the tree,
5156 * and an extent that you want to insert, deal with overlap and insert
5157 * the new extent into the tree.
5158 */
Chris Mason3b951512008-04-17 11:29:12 -04005159static int merge_extent_mapping(struct extent_map_tree *em_tree,
5160 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005161 struct extent_map *em,
5162 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005163{
5164 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005165
Chris Masone6dcd2d2008-07-17 12:53:50 -04005166 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5167 start_diff = map_start - em->start;
5168 em->start = map_start;
5169 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005170 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5171 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005172 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005173 em->block_len -= start_diff;
5174 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005175 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005176}
5177
Chris Masonc8b97812008-10-29 14:49:59 -04005178static noinline int uncompress_inline(struct btrfs_path *path,
5179 struct inode *inode, struct page *page,
5180 size_t pg_offset, u64 extent_offset,
5181 struct btrfs_file_extent_item *item)
5182{
5183 int ret;
5184 struct extent_buffer *leaf = path->nodes[0];
5185 char *tmp;
5186 size_t max_size;
5187 unsigned long inline_size;
5188 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005189 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005190
5191 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005192 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005193 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5194 inline_size = btrfs_file_extent_inline_item_len(leaf,
5195 btrfs_item_nr(leaf, path->slots[0]));
5196 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005197 if (!tmp)
5198 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005199 ptr = btrfs_file_extent_inline_start(item);
5200
5201 read_extent_buffer(leaf, tmp, ptr, inline_size);
5202
Chris Mason5b050f02008-11-11 09:34:41 -05005203 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005204 ret = btrfs_decompress(compress_type, tmp, page,
5205 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005206 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005207 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005208 unsigned long copy_size = min_t(u64,
5209 PAGE_CACHE_SIZE - pg_offset,
5210 max_size - extent_offset);
5211 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005212 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005213 }
5214 kfree(tmp);
5215 return 0;
5216}
5217
Chris Masond352ac62008-09-29 15:18:18 -04005218/*
5219 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005220 * the ugly parts come from merging extents from the disk with the in-ram
5221 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005222 * where the in-ram extents might be locked pending data=ordered completion.
5223 *
5224 * This also copies inline extents directly into the page.
5225 */
Chris Masond3977122009-01-05 21:25:51 -05005226
Chris Masona52d9a82007-08-27 16:49:44 -04005227struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005228 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005229 int create)
5230{
5231 int ret;
5232 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005233 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005234 u64 extent_start = 0;
5235 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005236 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005237 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005238 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005239 struct btrfs_root *root = BTRFS_I(inode)->root;
5240 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005241 struct extent_buffer *leaf;
5242 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005243 struct extent_map *em = NULL;
5244 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005245 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005246 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005247 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005248
Chris Masona52d9a82007-08-27 16:49:44 -04005249again:
Chris Mason890871b2009-09-02 16:24:52 -04005250 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005251 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005252 if (em)
5253 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005254 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005255
Chris Masona52d9a82007-08-27 16:49:44 -04005256 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005257 if (em->start > start || em->start + em->len <= start)
5258 free_extent_map(em);
5259 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005260 free_extent_map(em);
5261 else
5262 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005263 }
David Sterba172ddd62011-04-21 00:48:27 +02005264 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005265 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005266 err = -ENOMEM;
5267 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005268 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005269 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005270 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005271 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005272 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005273 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005274
5275 if (!path) {
5276 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005277 if (!path) {
5278 err = -ENOMEM;
5279 goto out;
5280 }
5281 /*
5282 * Chances are we'll be called again, so go ahead and do
5283 * readahead
5284 */
5285 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005286 }
5287
Chris Mason179e29e2007-11-01 11:28:41 -04005288 ret = btrfs_lookup_file_extent(trans, root, path,
5289 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005290 if (ret < 0) {
5291 err = ret;
5292 goto out;
5293 }
5294
5295 if (ret != 0) {
5296 if (path->slots[0] == 0)
5297 goto not_found;
5298 path->slots[0]--;
5299 }
5300
Chris Mason5f39d392007-10-15 16:14:19 -04005301 leaf = path->nodes[0];
5302 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005303 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005304 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005305 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5306 found_type = btrfs_key_type(&found_key);
5307 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005308 found_type != BTRFS_EXTENT_DATA_KEY) {
5309 goto not_found;
5310 }
5311
Chris Mason5f39d392007-10-15 16:14:19 -04005312 found_type = btrfs_file_extent_type(leaf, item);
5313 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005314 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005315 if (found_type == BTRFS_FILE_EXTENT_REG ||
5316 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005317 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005318 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005319 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5320 size_t size;
5321 size = btrfs_file_extent_inline_len(leaf, item);
5322 extent_end = (extent_start + size + root->sectorsize - 1) &
5323 ~((u64)root->sectorsize - 1);
5324 }
5325
5326 if (start >= extent_end) {
5327 path->slots[0]++;
5328 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5329 ret = btrfs_next_leaf(root, path);
5330 if (ret < 0) {
5331 err = ret;
5332 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005333 }
Yan Zheng9036c102008-10-30 14:19:41 -04005334 if (ret > 0)
5335 goto not_found;
5336 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005337 }
Yan Zheng9036c102008-10-30 14:19:41 -04005338 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5339 if (found_key.objectid != objectid ||
5340 found_key.type != BTRFS_EXTENT_DATA_KEY)
5341 goto not_found;
5342 if (start + len <= found_key.offset)
5343 goto not_found;
5344 em->start = start;
5345 em->len = found_key.offset - start;
5346 goto not_found_em;
5347 }
5348
Yan Zhengd899e052008-10-30 14:25:28 -04005349 if (found_type == BTRFS_FILE_EXTENT_REG ||
5350 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005351 em->start = extent_start;
5352 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005353 em->orig_start = extent_start -
5354 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005355 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5356 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005357 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005358 goto insert;
5359 }
Li Zefan261507a02010-12-17 14:21:50 +08005360 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005361 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005362 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005363 em->block_start = bytenr;
5364 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5365 item);
5366 } else {
5367 bytenr += btrfs_file_extent_offset(leaf, item);
5368 em->block_start = bytenr;
5369 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005370 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5371 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005372 }
Chris Masona52d9a82007-08-27 16:49:44 -04005373 goto insert;
5374 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005375 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005376 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005377 size_t size;
5378 size_t extent_offset;
5379 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005380
Chris Masona52d9a82007-08-27 16:49:44 -04005381 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005382 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005383 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005384 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005385 goto out;
5386 }
5387
Yan Zheng9036c102008-10-30 14:19:41 -04005388 size = btrfs_file_extent_inline_len(leaf, item);
5389 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005390 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005391 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005392 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005393 em->len = (copy_size + root->sectorsize - 1) &
5394 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005395 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005396 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005397 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005398 em->compress_type = compress_type;
5399 }
Yan689f9342007-10-29 11:41:07 -04005400 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005401 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005402 if (btrfs_file_extent_compression(leaf, item) !=
5403 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005404 ret = uncompress_inline(path, inode, page,
5405 pg_offset,
5406 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005407 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005408 } else {
5409 map = kmap(page);
5410 read_extent_buffer(leaf, map + pg_offset, ptr,
5411 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005412 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5413 memset(map + pg_offset + copy_size, 0,
5414 PAGE_CACHE_SIZE - pg_offset -
5415 copy_size);
5416 }
Chris Masonc8b97812008-10-29 14:49:59 -04005417 kunmap(page);
5418 }
Chris Mason179e29e2007-11-01 11:28:41 -04005419 flush_dcache_page(page);
5420 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005421 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005422 if (!trans) {
5423 kunmap(page);
5424 free_extent_map(em);
5425 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005426
David Sterbab3b4aa72011-04-21 01:20:15 +02005427 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005428 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005429
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005430 if (IS_ERR(trans))
5431 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005432 goto again;
5433 }
Chris Masonc8b97812008-10-29 14:49:59 -04005434 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005435 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005436 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005437 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005438 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005439 }
Chris Masond1310b22008-01-24 16:13:08 -05005440 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005441 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005442 goto insert;
5443 } else {
Chris Masond3977122009-01-05 21:25:51 -05005444 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005445 WARN_ON(1);
5446 }
5447not_found:
5448 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005449 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005450not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005451 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005452 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005453insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005454 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005455 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005456 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5457 "[%llu %llu]\n", (unsigned long long)em->start,
5458 (unsigned long long)em->len,
5459 (unsigned long long)start,
5460 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005461 err = -EIO;
5462 goto out;
5463 }
Chris Masond1310b22008-01-24 16:13:08 -05005464
5465 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005466 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005467 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005468 /* it is possible that someone inserted the extent into the tree
5469 * while we had the lock dropped. It is also possible that
5470 * an overlapping map exists in the tree
5471 */
Chris Masona52d9a82007-08-27 16:49:44 -04005472 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005473 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005474
5475 ret = 0;
5476
Chris Mason3b951512008-04-17 11:29:12 -04005477 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005478 if (existing && (existing->start > start ||
5479 existing->start + existing->len <= start)) {
5480 free_extent_map(existing);
5481 existing = NULL;
5482 }
Chris Mason3b951512008-04-17 11:29:12 -04005483 if (!existing) {
5484 existing = lookup_extent_mapping(em_tree, em->start,
5485 em->len);
5486 if (existing) {
5487 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005488 em, start,
5489 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005490 free_extent_map(existing);
5491 if (err) {
5492 free_extent_map(em);
5493 em = NULL;
5494 }
5495 } else {
5496 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005497 free_extent_map(em);
5498 em = NULL;
5499 }
5500 } else {
5501 free_extent_map(em);
5502 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005503 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005504 }
Chris Masona52d9a82007-08-27 16:49:44 -04005505 }
Chris Mason890871b2009-09-02 16:24:52 -04005506 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005507out:
liubo1abe9b82011-03-24 11:18:59 +00005508
5509 trace_btrfs_get_extent(root, em);
5510
Chris Masonf4219502008-07-22 11:18:09 -04005511 if (path)
5512 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005513 if (trans) {
5514 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005515 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005516 err = ret;
5517 }
Chris Masona52d9a82007-08-27 16:49:44 -04005518 if (err) {
5519 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005520 return ERR_PTR(err);
5521 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005522 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005523 return em;
5524}
5525
Chris Masonec29ed52011-02-23 16:23:20 -05005526struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5527 size_t pg_offset, u64 start, u64 len,
5528 int create)
5529{
5530 struct extent_map *em;
5531 struct extent_map *hole_em = NULL;
5532 u64 range_start = start;
5533 u64 end;
5534 u64 found;
5535 u64 found_end;
5536 int err = 0;
5537
5538 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5539 if (IS_ERR(em))
5540 return em;
5541 if (em) {
5542 /*
5543 * if our em maps to a hole, there might
5544 * actually be delalloc bytes behind it
5545 */
5546 if (em->block_start != EXTENT_MAP_HOLE)
5547 return em;
5548 else
5549 hole_em = em;
5550 }
5551
5552 /* check to see if we've wrapped (len == -1 or similar) */
5553 end = start + len;
5554 if (end < start)
5555 end = (u64)-1;
5556 else
5557 end -= 1;
5558
5559 em = NULL;
5560
5561 /* ok, we didn't find anything, lets look for delalloc */
5562 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5563 end, len, EXTENT_DELALLOC, 1);
5564 found_end = range_start + found;
5565 if (found_end < range_start)
5566 found_end = (u64)-1;
5567
5568 /*
5569 * we didn't find anything useful, return
5570 * the original results from get_extent()
5571 */
5572 if (range_start > end || found_end <= start) {
5573 em = hole_em;
5574 hole_em = NULL;
5575 goto out;
5576 }
5577
5578 /* adjust the range_start to make sure it doesn't
5579 * go backwards from the start they passed in
5580 */
5581 range_start = max(start,range_start);
5582 found = found_end - range_start;
5583
5584 if (found > 0) {
5585 u64 hole_start = start;
5586 u64 hole_len = len;
5587
David Sterba172ddd62011-04-21 00:48:27 +02005588 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005589 if (!em) {
5590 err = -ENOMEM;
5591 goto out;
5592 }
5593 /*
5594 * when btrfs_get_extent can't find anything it
5595 * returns one huge hole
5596 *
5597 * make sure what it found really fits our range, and
5598 * adjust to make sure it is based on the start from
5599 * the caller
5600 */
5601 if (hole_em) {
5602 u64 calc_end = extent_map_end(hole_em);
5603
5604 if (calc_end <= start || (hole_em->start > end)) {
5605 free_extent_map(hole_em);
5606 hole_em = NULL;
5607 } else {
5608 hole_start = max(hole_em->start, start);
5609 hole_len = calc_end - hole_start;
5610 }
5611 }
5612 em->bdev = NULL;
5613 if (hole_em && range_start > hole_start) {
5614 /* our hole starts before our delalloc, so we
5615 * have to return just the parts of the hole
5616 * that go until the delalloc starts
5617 */
5618 em->len = min(hole_len,
5619 range_start - hole_start);
5620 em->start = hole_start;
5621 em->orig_start = hole_start;
5622 /*
5623 * don't adjust block start at all,
5624 * it is fixed at EXTENT_MAP_HOLE
5625 */
5626 em->block_start = hole_em->block_start;
5627 em->block_len = hole_len;
5628 } else {
5629 em->start = range_start;
5630 em->len = found;
5631 em->orig_start = range_start;
5632 em->block_start = EXTENT_MAP_DELALLOC;
5633 em->block_len = found;
5634 }
5635 } else if (hole_em) {
5636 return hole_em;
5637 }
5638out:
5639
5640 free_extent_map(hole_em);
5641 if (err) {
5642 free_extent_map(em);
5643 return ERR_PTR(err);
5644 }
5645 return em;
5646}
5647
Josef Bacik4b46fce2010-05-23 11:00:55 -04005648static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005649 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005650 u64 start, u64 len)
5651{
5652 struct btrfs_root *root = BTRFS_I(inode)->root;
5653 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005654 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5655 struct btrfs_key ins;
5656 u64 alloc_hint;
5657 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005658 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005659
Josef Bacik16d299a2011-04-06 14:53:07 -04005660 /*
5661 * Ok if the extent map we looked up is a hole and is for the exact
5662 * range we want, there is no reason to allocate a new one, however if
5663 * it is not right then we need to free this one and drop the cache for
5664 * our range.
5665 */
5666 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5667 em->len != len) {
5668 free_extent_map(em);
5669 em = NULL;
5670 insert = true;
5671 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5672 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005673
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005674 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005675 if (IS_ERR(trans))
5676 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005677
Chris Mason4cb53002011-05-24 15:35:30 -04005678 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5679 btrfs_add_inode_defrag(trans, inode);
5680
Josef Bacik4b46fce2010-05-23 11:00:55 -04005681 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5682
5683 alloc_hint = get_extent_allocation_hint(inode, start, len);
5684 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005685 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005686 if (ret) {
5687 em = ERR_PTR(ret);
5688 goto out;
5689 }
5690
Josef Bacik4b46fce2010-05-23 11:00:55 -04005691 if (!em) {
David Sterba172ddd62011-04-21 00:48:27 +02005692 em = alloc_extent_map();
Josef Bacik16d299a2011-04-06 14:53:07 -04005693 if (!em) {
5694 em = ERR_PTR(-ENOMEM);
5695 goto out;
5696 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005697 }
5698
5699 em->start = start;
5700 em->orig_start = em->start;
5701 em->len = ins.offset;
5702
5703 em->block_start = ins.objectid;
5704 em->block_len = ins.offset;
5705 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005706
5707 /*
5708 * We need to do this because if we're using the original em we searched
5709 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5710 */
5711 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005712 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5713
Josef Bacik16d299a2011-04-06 14:53:07 -04005714 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005715 write_lock(&em_tree->lock);
5716 ret = add_extent_mapping(em_tree, em);
5717 write_unlock(&em_tree->lock);
5718 if (ret != -EEXIST)
5719 break;
5720 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5721 }
5722
5723 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5724 ins.offset, ins.offset, 0);
5725 if (ret) {
5726 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5727 em = ERR_PTR(ret);
5728 }
5729out:
5730 btrfs_end_transaction(trans, root);
5731 return em;
5732}
5733
Chris Mason46bfbb52010-05-26 11:04:10 -04005734/*
5735 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5736 * block must be cow'd
5737 */
5738static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5739 struct inode *inode, u64 offset, u64 len)
5740{
5741 struct btrfs_path *path;
5742 int ret;
5743 struct extent_buffer *leaf;
5744 struct btrfs_root *root = BTRFS_I(inode)->root;
5745 struct btrfs_file_extent_item *fi;
5746 struct btrfs_key key;
5747 u64 disk_bytenr;
5748 u64 backref_offset;
5749 u64 extent_end;
5750 u64 num_bytes;
5751 int slot;
5752 int found_type;
5753
5754 path = btrfs_alloc_path();
5755 if (!path)
5756 return -ENOMEM;
5757
Li Zefan33345d012011-04-20 10:31:50 +08005758 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005759 offset, 0);
5760 if (ret < 0)
5761 goto out;
5762
5763 slot = path->slots[0];
5764 if (ret == 1) {
5765 if (slot == 0) {
5766 /* can't find the item, must cow */
5767 ret = 0;
5768 goto out;
5769 }
5770 slot--;
5771 }
5772 ret = 0;
5773 leaf = path->nodes[0];
5774 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005775 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005776 key.type != BTRFS_EXTENT_DATA_KEY) {
5777 /* not our file or wrong item type, must cow */
5778 goto out;
5779 }
5780
5781 if (key.offset > offset) {
5782 /* Wrong offset, must cow */
5783 goto out;
5784 }
5785
5786 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5787 found_type = btrfs_file_extent_type(leaf, fi);
5788 if (found_type != BTRFS_FILE_EXTENT_REG &&
5789 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5790 /* not a regular extent, must cow */
5791 goto out;
5792 }
5793 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5794 backref_offset = btrfs_file_extent_offset(leaf, fi);
5795
5796 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5797 if (extent_end < offset + len) {
5798 /* extent doesn't include our full range, must cow */
5799 goto out;
5800 }
5801
5802 if (btrfs_extent_readonly(root, disk_bytenr))
5803 goto out;
5804
5805 /*
5806 * look for other files referencing this extent, if we
5807 * find any we must cow
5808 */
Li Zefan33345d012011-04-20 10:31:50 +08005809 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005810 key.offset - backref_offset, disk_bytenr))
5811 goto out;
5812
5813 /*
5814 * adjust disk_bytenr and num_bytes to cover just the bytes
5815 * in this extent we are about to write. If there
5816 * are any csums in that range we have to cow in order
5817 * to keep the csums correct
5818 */
5819 disk_bytenr += backref_offset;
5820 disk_bytenr += offset - key.offset;
5821 num_bytes = min(offset + len, extent_end) - offset;
5822 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5823 goto out;
5824 /*
5825 * all of the above have passed, it is safe to overwrite this extent
5826 * without cow
5827 */
5828 ret = 1;
5829out:
5830 btrfs_free_path(path);
5831 return ret;
5832}
5833
Josef Bacikeb838e72012-07-31 16:28:48 -04005834static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5835 struct extent_state **cached_state, int writing)
5836{
5837 struct btrfs_ordered_extent *ordered;
5838 int ret = 0;
5839
5840 while (1) {
5841 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5842 0, cached_state);
5843 /*
5844 * We're concerned with the entire range that we're going to be
5845 * doing DIO to, so we need to make sure theres no ordered
5846 * extents in this range.
5847 */
5848 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5849 lockend - lockstart + 1);
5850
5851 /*
5852 * We need to make sure there are no buffered pages in this
5853 * range either, we could have raced between the invalidate in
5854 * generic_file_direct_write and locking the extent. The
5855 * invalidate needs to happen so that reads after a write do not
5856 * get stale data.
5857 */
5858 if (!ordered && (!writing ||
5859 !test_range_bit(&BTRFS_I(inode)->io_tree,
5860 lockstart, lockend, EXTENT_UPTODATE, 0,
5861 *cached_state)))
5862 break;
5863
5864 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5865 cached_state, GFP_NOFS);
5866
5867 if (ordered) {
5868 btrfs_start_ordered_extent(inode, ordered, 1);
5869 btrfs_put_ordered_extent(ordered);
5870 } else {
5871 /* Screw you mmap */
5872 ret = filemap_write_and_wait_range(inode->i_mapping,
5873 lockstart,
5874 lockend);
5875 if (ret)
5876 break;
5877
5878 /*
5879 * If we found a page that couldn't be invalidated just
5880 * fall back to buffered.
5881 */
5882 ret = invalidate_inode_pages2_range(inode->i_mapping,
5883 lockstart >> PAGE_CACHE_SHIFT,
5884 lockend >> PAGE_CACHE_SHIFT);
5885 if (ret)
5886 break;
5887 }
5888
5889 cond_resched();
5890 }
5891
5892 return ret;
5893}
5894
Josef Bacik4b46fce2010-05-23 11:00:55 -04005895static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5896 struct buffer_head *bh_result, int create)
5897{
5898 struct extent_map *em;
5899 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04005900 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005901 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04005902 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005903 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005904 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04005905 int unlock_bits = EXTENT_LOCKED;
5906 int ret;
5907
Josef Bacikeb838e72012-07-31 16:28:48 -04005908 if (create) {
5909 ret = btrfs_delalloc_reserve_space(inode, len);
5910 if (ret)
5911 return ret;
5912 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04005913 } else {
5914 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04005915 }
5916
Josef Bacikc3298612012-08-03 16:49:19 -04005917 lockstart = start;
5918 lockend = start + len - 1;
5919
Josef Bacikeb838e72012-07-31 16:28:48 -04005920 /*
5921 * If this errors out it's because we couldn't invalidate pagecache for
5922 * this range and we need to fallback to buffered.
5923 */
5924 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
5925 return -ENOTBLK;
5926
5927 if (create) {
5928 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
5929 lockend, EXTENT_DELALLOC, NULL,
5930 &cached_state, GFP_NOFS);
5931 if (ret)
5932 goto unlock_err;
5933 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005934
5935 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04005936 if (IS_ERR(em)) {
5937 ret = PTR_ERR(em);
5938 goto unlock_err;
5939 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005940
5941 /*
5942 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5943 * io. INLINE is special, and we could probably kludge it in here, but
5944 * it's still buffered so for safety lets just fall back to the generic
5945 * buffered path.
5946 *
5947 * For COMPRESSED we _have_ to read the entire extent in so we can
5948 * decompress it, so there will be buffering required no matter what we
5949 * do, so go ahead and fallback to buffered.
5950 *
5951 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5952 * to buffered IO. Don't blame me, this is the price we pay for using
5953 * the generic code.
5954 */
5955 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5956 em->block_start == EXTENT_MAP_INLINE) {
5957 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04005958 ret = -ENOTBLK;
5959 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005960 }
5961
5962 /* Just a good old fashioned hole, return */
5963 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5964 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5965 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04005966 ret = 0;
5967 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005968 }
5969
5970 /*
5971 * We don't allocate a new extent in the following cases
5972 *
5973 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5974 * existing extent.
5975 * 2) The extent is marked as PREALLOC. We're good to go here and can
5976 * just use the extent.
5977 *
5978 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005979 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04005980 len = min(len, em->len - (start - em->start));
5981 lockstart = start + len;
5982 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04005983 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005984
5985 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5986 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5987 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005988 int type;
5989 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005990 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005991
5992 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5993 type = BTRFS_ORDERED_PREALLOC;
5994 else
5995 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005996 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005997 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005998
5999 /*
6000 * we're not going to log anything, but we do need
6001 * to make sure the current transaction stays open
6002 * while we look for nocow cross refs
6003 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006004 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006005 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006006 goto must_cow;
6007
6008 if (can_nocow_odirect(trans, inode, start, len) == 1) {
6009 ret = btrfs_add_ordered_extent_dio(inode, start,
6010 block_start, len, len, type);
6011 btrfs_end_transaction(trans, root);
6012 if (ret) {
6013 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006014 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006015 }
6016 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006017 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006018 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006019 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006020must_cow:
6021 /*
6022 * this will cow the extent, reset the len in case we changed
6023 * it above
6024 */
6025 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04006026 em = btrfs_new_extent_direct(inode, em, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006027 if (IS_ERR(em)) {
6028 ret = PTR_ERR(em);
6029 goto unlock_err;
6030 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006031 len = min(len, em->len - (start - em->start));
6032unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006033 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6034 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006035 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006036 bh_result->b_bdev = em->bdev;
6037 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006038 if (create) {
6039 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6040 set_buffer_new(bh_result);
6041
6042 /*
6043 * Need to update the i_size under the extent lock so buffered
6044 * readers will get the updated i_size when we unlock.
6045 */
6046 if (start + len > i_size_read(inode))
6047 i_size_write(inode, start + len);
6048 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006049
Josef Bacikeb838e72012-07-31 16:28:48 -04006050 /*
6051 * In the case of write we need to clear and unlock the entire range,
6052 * in the case of read we need to unlock only the end area that we
6053 * aren't using if there is any left over space.
6054 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006055 if (lockstart < lockend) {
6056 if (create && len < lockend - lockstart) {
6057 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6058 lockstart + len - 1, unlock_bits, 1, 0,
6059 &cached_state, GFP_NOFS);
6060 /*
6061 * Beside unlock, we also need to cleanup reserved space
6062 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6063 */
6064 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6065 lockstart + len, lockend,
6066 unlock_bits | EXTENT_DO_ACCOUNTING,
6067 1, 0, NULL, GFP_NOFS);
6068 } else {
6069 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6070 lockend, unlock_bits, 1, 0,
6071 &cached_state, GFP_NOFS);
6072 }
6073 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006074 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006075 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006076
Josef Bacik4b46fce2010-05-23 11:00:55 -04006077 free_extent_map(em);
6078
6079 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006080
6081unlock_err:
6082 if (create)
6083 unlock_bits |= EXTENT_DO_ACCOUNTING;
6084
6085 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6086 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6087 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006088}
6089
6090struct btrfs_dio_private {
6091 struct inode *inode;
6092 u64 logical_offset;
6093 u64 disk_bytenr;
6094 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006095 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006096
6097 /* number of bios pending for this dio */
6098 atomic_t pending_bios;
6099
6100 /* IO errors */
6101 int errors;
6102
6103 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006104};
6105
6106static void btrfs_endio_direct_read(struct bio *bio, int err)
6107{
Miao Xiee65e1532010-11-22 03:04:43 +00006108 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006109 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6110 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006111 struct inode *inode = dip->inode;
6112 struct btrfs_root *root = BTRFS_I(inode)->root;
6113 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006114
6115 start = dip->logical_offset;
6116 do {
6117 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6118 struct page *page = bvec->bv_page;
6119 char *kaddr;
6120 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006121 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006122 unsigned long flags;
6123
Josef Bacikc3298612012-08-03 16:49:19 -04006124 if (get_state_private(&BTRFS_I(inode)->io_tree,
6125 start, &private))
6126 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006127 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006128 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006129 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6130 csum, bvec->bv_len);
6131 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006132 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006133 local_irq_restore(flags);
6134
6135 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006136 if (csum != private) {
6137failed:
Li Zefan33345d012011-04-20 10:31:50 +08006138 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006139 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006140 (unsigned long long)btrfs_ino(inode),
6141 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006142 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006143 err = -EIO;
6144 }
6145 }
6146
6147 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006148 bvec++;
6149 } while (bvec <= bvec_end);
6150
6151 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006152 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006153 bio->bi_private = dip->private;
6154
Josef Bacik4b46fce2010-05-23 11:00:55 -04006155 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006156
6157 /* If we had a csum failure make sure to clear the uptodate flag */
6158 if (err)
6159 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006160 dio_end_io(bio, err);
6161}
6162
6163static void btrfs_endio_direct_write(struct bio *bio, int err)
6164{
6165 struct btrfs_dio_private *dip = bio->bi_private;
6166 struct inode *inode = dip->inode;
6167 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006168 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006169 u64 ordered_offset = dip->logical_offset;
6170 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006171 int ret;
6172
6173 if (err)
6174 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006175again:
6176 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6177 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006178 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006179 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006180 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006181
Josef Bacik5fd02042012-05-02 14:00:54 -04006182 ordered->work.func = finish_ordered_fn;
6183 ordered->work.flags = 0;
6184 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6185 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006186out_test:
6187 /*
6188 * our bio might span multiple ordered extents. If we haven't
6189 * completed the accounting for the whole dio, go back and try again
6190 */
6191 if (ordered_offset < dip->logical_offset + dip->bytes) {
6192 ordered_bytes = dip->logical_offset + dip->bytes -
6193 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006194 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006195 goto again;
6196 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006197out_done:
6198 bio->bi_private = dip->private;
6199
Josef Bacik4b46fce2010-05-23 11:00:55 -04006200 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006201
6202 /* If we had an error make sure to clear the uptodate flag */
6203 if (err)
6204 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006205 dio_end_io(bio, err);
6206}
6207
Chris Masoneaf25d92010-05-25 09:48:28 -04006208static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6209 struct bio *bio, int mirror_num,
6210 unsigned long bio_flags, u64 offset)
6211{
6212 int ret;
6213 struct btrfs_root *root = BTRFS_I(inode)->root;
6214 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006215 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006216 return 0;
6217}
6218
Miao Xiee65e1532010-11-22 03:04:43 +00006219static void btrfs_end_dio_bio(struct bio *bio, int err)
6220{
6221 struct btrfs_dio_private *dip = bio->bi_private;
6222
6223 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006224 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006225 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006226 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006227 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006228 dip->errors = 1;
6229
6230 /*
6231 * before atomic variable goto zero, we must make sure
6232 * dip->errors is perceived to be set.
6233 */
6234 smp_mb__before_atomic_dec();
6235 }
6236
6237 /* if there are more bios still pending for this dio, just exit */
6238 if (!atomic_dec_and_test(&dip->pending_bios))
6239 goto out;
6240
6241 if (dip->errors)
6242 bio_io_error(dip->orig_bio);
6243 else {
6244 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6245 bio_endio(dip->orig_bio, 0);
6246 }
6247out:
6248 bio_put(bio);
6249}
6250
6251static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6252 u64 first_sector, gfp_t gfp_flags)
6253{
6254 int nr_vecs = bio_get_nr_vecs(bdev);
6255 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6256}
6257
6258static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6259 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006260 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006261{
6262 int write = rw & REQ_WRITE;
6263 struct btrfs_root *root = BTRFS_I(inode)->root;
6264 int ret;
6265
6266 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006267
6268 if (!write) {
6269 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6270 if (ret)
6271 goto err;
6272 }
Miao Xiee65e1532010-11-22 03:04:43 +00006273
Josef Bacik1ae39932011-04-06 14:41:34 -04006274 if (skip_sum)
6275 goto map;
6276
6277 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006278 ret = btrfs_wq_submit_bio(root->fs_info,
6279 inode, rw, bio, 0, 0,
6280 file_offset,
6281 __btrfs_submit_bio_start_direct_io,
6282 __btrfs_submit_bio_done);
6283 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006284 } else if (write) {
6285 /*
6286 * If we aren't doing async submit, calculate the csum of the
6287 * bio now.
6288 */
6289 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6290 if (ret)
6291 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006292 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006293 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006294 if (ret)
6295 goto err;
6296 }
Miao Xiee65e1532010-11-22 03:04:43 +00006297
Josef Bacik1ae39932011-04-06 14:41:34 -04006298map:
6299 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006300err:
6301 bio_put(bio);
6302 return ret;
6303}
6304
6305static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6306 int skip_sum)
6307{
6308 struct inode *inode = dip->inode;
6309 struct btrfs_root *root = BTRFS_I(inode)->root;
6310 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6311 struct bio *bio;
6312 struct bio *orig_bio = dip->orig_bio;
6313 struct bio_vec *bvec = orig_bio->bi_io_vec;
6314 u64 start_sector = orig_bio->bi_sector;
6315 u64 file_offset = dip->logical_offset;
6316 u64 submit_len = 0;
6317 u64 map_length;
6318 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006319 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006320 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006321
Miao Xiee65e1532010-11-22 03:04:43 +00006322 map_length = orig_bio->bi_size;
6323 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6324 &map_length, NULL, 0);
6325 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006326 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006327 return -EIO;
6328 }
6329
Josef Bacik02f57c72011-04-06 14:25:44 -04006330 if (map_length >= orig_bio->bi_size) {
6331 bio = orig_bio;
6332 goto submit;
6333 }
6334
Josef Bacik1ae39932011-04-06 14:41:34 -04006335 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006336 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6337 if (!bio)
6338 return -ENOMEM;
6339 bio->bi_private = dip;
6340 bio->bi_end_io = btrfs_end_dio_bio;
6341 atomic_inc(&dip->pending_bios);
6342
Miao Xiee65e1532010-11-22 03:04:43 +00006343 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6344 if (unlikely(map_length < submit_len + bvec->bv_len ||
6345 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6346 bvec->bv_offset) < bvec->bv_len)) {
6347 /*
6348 * inc the count before we submit the bio so
6349 * we know the end IO handler won't happen before
6350 * we inc the count. Otherwise, the dip might get freed
6351 * before we're done setting it up
6352 */
6353 atomic_inc(&dip->pending_bios);
6354 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6355 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006356 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006357 if (ret) {
6358 bio_put(bio);
6359 atomic_dec(&dip->pending_bios);
6360 goto out_err;
6361 }
6362
Miao Xiee65e1532010-11-22 03:04:43 +00006363 start_sector += submit_len >> 9;
6364 file_offset += submit_len;
6365
6366 submit_len = 0;
6367 nr_pages = 0;
6368
6369 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6370 start_sector, GFP_NOFS);
6371 if (!bio)
6372 goto out_err;
6373 bio->bi_private = dip;
6374 bio->bi_end_io = btrfs_end_dio_bio;
6375
6376 map_length = orig_bio->bi_size;
6377 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6378 &map_length, NULL, 0);
6379 if (ret) {
6380 bio_put(bio);
6381 goto out_err;
6382 }
6383 } else {
6384 submit_len += bvec->bv_len;
6385 nr_pages ++;
6386 bvec++;
6387 }
6388 }
6389
Josef Bacik02f57c72011-04-06 14:25:44 -04006390submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006391 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006392 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006393 if (!ret)
6394 return 0;
6395
6396 bio_put(bio);
6397out_err:
6398 dip->errors = 1;
6399 /*
6400 * before atomic variable goto zero, we must
6401 * make sure dip->errors is perceived to be set.
6402 */
6403 smp_mb__before_atomic_dec();
6404 if (atomic_dec_and_test(&dip->pending_bios))
6405 bio_io_error(dip->orig_bio);
6406
6407 /* bio_end_io() will handle error, so we needn't return it */
6408 return 0;
6409}
6410
Josef Bacik4b46fce2010-05-23 11:00:55 -04006411static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6412 loff_t file_offset)
6413{
6414 struct btrfs_root *root = BTRFS_I(inode)->root;
6415 struct btrfs_dio_private *dip;
6416 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006417 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006418 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006419 int ret = 0;
6420
6421 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6422
6423 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6424 if (!dip) {
6425 ret = -ENOMEM;
6426 goto free_ordered;
6427 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006428
6429 dip->private = bio->bi_private;
6430 dip->inode = inode;
6431 dip->logical_offset = file_offset;
6432
Josef Bacik4b46fce2010-05-23 11:00:55 -04006433 dip->bytes = 0;
6434 do {
6435 dip->bytes += bvec->bv_len;
6436 bvec++;
6437 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6438
Chris Mason46bfbb52010-05-26 11:04:10 -04006439 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006440 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006441 dip->errors = 0;
6442 dip->orig_bio = bio;
6443 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006444
6445 if (write)
6446 bio->bi_end_io = btrfs_endio_direct_write;
6447 else
6448 bio->bi_end_io = btrfs_endio_direct_read;
6449
Miao Xiee65e1532010-11-22 03:04:43 +00006450 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6451 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006452 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006453free_ordered:
6454 /*
6455 * If this is a write, we need to clean up the reserved space and kill
6456 * the ordered extent.
6457 */
6458 if (write) {
6459 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006460 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006461 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6462 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6463 btrfs_free_reserved_extent(root, ordered->start,
6464 ordered->disk_len);
6465 btrfs_put_ordered_extent(ordered);
6466 btrfs_put_ordered_extent(ordered);
6467 }
6468 bio_endio(bio, ret);
6469}
6470
Chris Mason5a5f79b2010-05-26 21:33:37 -04006471static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6472 const struct iovec *iov, loff_t offset,
6473 unsigned long nr_segs)
6474{
6475 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006476 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006477 size_t size;
6478 unsigned long addr;
6479 unsigned blocksize_mask = root->sectorsize - 1;
6480 ssize_t retval = -EINVAL;
6481 loff_t end = offset;
6482
6483 if (offset & blocksize_mask)
6484 goto out;
6485
6486 /* Check the memory alignment. Blocks cannot straddle pages */
6487 for (seg = 0; seg < nr_segs; seg++) {
6488 addr = (unsigned long)iov[seg].iov_base;
6489 size = iov[seg].iov_len;
6490 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006491 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006492 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006493
6494 /* If this is a write we don't need to check anymore */
6495 if (rw & WRITE)
6496 continue;
6497
6498 /*
6499 * Check to make sure we don't have duplicate iov_base's in this
6500 * iovec, if so return EINVAL, otherwise we'll get csum errors
6501 * when reading back.
6502 */
6503 for (i = seg + 1; i < nr_segs; i++) {
6504 if (iov[seg].iov_base == iov[i].iov_base)
6505 goto out;
6506 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006507 }
6508 retval = 0;
6509out:
6510 return retval;
6511}
Josef Bacikeb838e72012-07-31 16:28:48 -04006512
Chris Mason16432982008-04-10 10:23:21 -04006513static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6514 const struct iovec *iov, loff_t offset,
6515 unsigned long nr_segs)
6516{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006517 struct file *file = iocb->ki_filp;
6518 struct inode *inode = file->f_mapping->host;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006519
Chris Mason5a5f79b2010-05-26 21:33:37 -04006520 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006521 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006522 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006523
Josef Bacikeb838e72012-07-31 16:28:48 -04006524 return __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006525 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6526 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6527 btrfs_submit_direct, 0);
Chris Mason16432982008-04-10 10:23:21 -04006528}
6529
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006530static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6531 __u64 start, __u64 len)
6532{
Chris Masonec29ed52011-02-23 16:23:20 -05006533 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006534}
6535
Chris Mason9ebefb182007-06-15 13:50:00 -04006536int btrfs_readpage(struct file *file, struct page *page)
6537{
Chris Masond1310b22008-01-24 16:13:08 -05006538 struct extent_io_tree *tree;
6539 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006540 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006541}
Chris Mason1832a6d2007-12-21 16:27:21 -05006542
Chris Mason39279cc2007-06-12 06:35:45 -04006543static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6544{
Chris Masond1310b22008-01-24 16:13:08 -05006545 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006546
6547
6548 if (current->flags & PF_MEMALLOC) {
6549 redirty_page_for_writepage(wbc, page);
6550 unlock_page(page);
6551 return 0;
6552 }
Chris Masond1310b22008-01-24 16:13:08 -05006553 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006554 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6555}
Chris Mason39279cc2007-06-12 06:35:45 -04006556
Chris Masonf4219502008-07-22 11:18:09 -04006557int btrfs_writepages(struct address_space *mapping,
6558 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006559{
Chris Masond1310b22008-01-24 16:13:08 -05006560 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006561
Chris Masond1310b22008-01-24 16:13:08 -05006562 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006563 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6564}
6565
Chris Mason3ab2fb52007-11-08 10:59:22 -05006566static int
6567btrfs_readpages(struct file *file, struct address_space *mapping,
6568 struct list_head *pages, unsigned nr_pages)
6569{
Chris Masond1310b22008-01-24 16:13:08 -05006570 struct extent_io_tree *tree;
6571 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006572 return extent_readpages(tree, mapping, pages, nr_pages,
6573 btrfs_get_extent);
6574}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006575static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006576{
Chris Masond1310b22008-01-24 16:13:08 -05006577 struct extent_io_tree *tree;
6578 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006579 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006580
Chris Masond1310b22008-01-24 16:13:08 -05006581 tree = &BTRFS_I(page->mapping->host)->io_tree;
6582 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006583 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006584 if (ret == 1) {
6585 ClearPagePrivate(page);
6586 set_page_private(page, 0);
6587 page_cache_release(page);
6588 }
6589 return ret;
6590}
Chris Mason39279cc2007-06-12 06:35:45 -04006591
Chris Masone6dcd2d2008-07-17 12:53:50 -04006592static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6593{
Chris Mason98509cf2008-09-11 15:51:43 -04006594 if (PageWriteback(page) || PageDirty(page))
6595 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006596 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006597}
6598
Chris Masona52d9a82007-08-27 16:49:44 -04006599static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6600{
Josef Bacik5fd02042012-05-02 14:00:54 -04006601 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006602 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006603 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006604 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006605 u64 page_start = page_offset(page);
6606 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006607
Chris Mason8b62b722009-09-02 16:53:46 -04006608 /*
6609 * we have the page locked, so new writeback can't start,
6610 * and the dirty bit won't be cleared while we are here.
6611 *
6612 * Wait for IO on this page so that we can safely clear
6613 * the PagePrivate2 bit and do ordered accounting
6614 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006615 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006616
Josef Bacik5fd02042012-05-02 14:00:54 -04006617 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006618 if (offset) {
6619 btrfs_releasepage(page, GFP_NOFS);
6620 return;
6621 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006622 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Josef Bacik5fd02042012-05-02 14:00:54 -04006623 ordered = btrfs_lookup_ordered_extent(inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006624 page_offset(page));
6625 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006626 /*
6627 * IO on this page will never be started, so we need
6628 * to account for any ordered extents now
6629 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006630 clear_extent_bit(tree, page_start, page_end,
6631 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006632 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006633 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006634 /*
6635 * whoever cleared the private bit is responsible
6636 * for the finish_ordered_io
6637 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006638 if (TestClearPagePrivate2(page) &&
6639 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6640 PAGE_CACHE_SIZE, 1)) {
6641 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006642 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006643 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006644 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006645 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006646 }
6647 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006648 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006649 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006650 __btrfs_releasepage(page, GFP_NOFS);
6651
Chris Mason4a096752008-07-21 10:29:44 -04006652 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006653 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006654 ClearPagePrivate(page);
6655 set_page_private(page, 0);
6656 page_cache_release(page);
6657 }
Chris Mason39279cc2007-06-12 06:35:45 -04006658}
6659
Chris Mason9ebefb182007-06-15 13:50:00 -04006660/*
6661 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6662 * called from a page fault handler when a page is first dirtied. Hence we must
6663 * be careful to check for EOF conditions here. We set the page up correctly
6664 * for a written page which means we get ENOSPC checking when writing into
6665 * holes and correct delalloc and unwritten extent mapping on filesystems that
6666 * support these features.
6667 *
6668 * We are not allowed to take the i_mutex here so we have to play games to
6669 * protect against truncate races as the page could now be beyond EOF. Because
6670 * vmtruncate() writes the inode size before removing pages, once we have the
6671 * page lock we can determine safely if the page is beyond EOF. If it is not
6672 * beyond EOF, then the page is guaranteed safe against truncation until we
6673 * unlock the page.
6674 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006675int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006676{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006677 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006678 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006679 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006680 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6681 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006682 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006683 char *kaddr;
6684 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006685 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006686 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006687 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006688 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006689 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006690
Jan Karab2b5ef52012-06-12 16:20:45 +02006691 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006692 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006693 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006694 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006695 reserved = 1;
6696 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006697 if (ret) {
6698 if (ret == -ENOMEM)
6699 ret = VM_FAULT_OOM;
6700 else /* -ENOSPC, -EIO, etc */
6701 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006702 if (reserved)
6703 goto out;
6704 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006705 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006706
Nick Piggin56a76f82009-03-31 15:23:23 -07006707 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006708again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006709 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006710 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006711 page_start = page_offset(page);
6712 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006713
Chris Mason9ebefb182007-06-15 13:50:00 -04006714 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006715 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006716 /* page got truncated out from underneath us */
6717 goto out_unlock;
6718 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006719 wait_on_page_writeback(page);
6720
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006721 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006722 set_page_extent_mapped(page);
6723
Chris Masoneb84ae02008-07-17 13:53:27 -04006724 /*
6725 * we can't set the delalloc bits if there are pending ordered
6726 * extents. Drop our locks and wait for them to finish
6727 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006728 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6729 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006730 unlock_extent_cached(io_tree, page_start, page_end,
6731 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006732 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006733 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006734 btrfs_put_ordered_extent(ordered);
6735 goto again;
6736 }
6737
Josef Bacikfbf19082009-10-01 17:10:23 -04006738 /*
6739 * XXX - page_mkwrite gets called every time the page is dirtied, even
6740 * if it was already dirty, so for space accounting reasons we need to
6741 * clear any delalloc bits for the range we are fixing to save. There
6742 * is probably a better way to do this, but for now keep consistent with
6743 * prepare_pages in the normal write path.
6744 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006745 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006746 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006747 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006748
Josef Bacik2ac55d42010-02-03 19:33:23 +00006749 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6750 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006751 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006752 unlock_extent_cached(io_tree, page_start, page_end,
6753 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006754 ret = VM_FAULT_SIGBUS;
6755 goto out_unlock;
6756 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006757 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006758
6759 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006760 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006761 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006762 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006763 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006764
Chris Masone6dcd2d2008-07-17 12:53:50 -04006765 if (zero_start != PAGE_CACHE_SIZE) {
6766 kaddr = kmap(page);
6767 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6768 flush_dcache_page(page);
6769 kunmap(page);
6770 }
Chris Mason247e7432008-07-17 12:53:51 -04006771 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006772 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006773 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006774
Chris Mason257c62e2009-10-13 13:21:08 -04006775 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6776 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6777
Josef Bacik2ac55d42010-02-03 19:33:23 +00006778 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006779
6780out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006781 if (!ret) {
6782 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006783 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006784 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006785 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006786out:
Josef Bacikec39e182012-01-12 19:10:12 -05006787 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006788out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006789 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006790 return ret;
6791}
6792
Josef Bacika41ad392011-01-31 15:30:16 -05006793static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006794{
6795 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006796 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006797 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006798 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006799 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006800 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006801 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006802 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006803
Josef Bacik5d5e1032009-10-13 16:46:49 -04006804 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6805 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006806 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006807
Chris Mason4a096752008-07-21 10:29:44 -04006808 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006809 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006810
Josef Bacikfcb80c22011-05-03 10:40:22 -04006811 /*
6812 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6813 * 3 things going on here
6814 *
6815 * 1) We need to reserve space for our orphan item and the space to
6816 * delete our orphan item. Lord knows we don't want to have a dangling
6817 * orphan item because we didn't reserve space to remove it.
6818 *
6819 * 2) We need to reserve space to update our inode.
6820 *
6821 * 3) We need to have something to cache all the space that is going to
6822 * be free'd up by the truncate operation, but also have some slack
6823 * space reserved in case it uses space during the truncate (thank you
6824 * very much snapshotting).
6825 *
6826 * And we need these to all be seperate. The fact is we can use alot of
6827 * space doing the truncate, and we have no earthly idea how much space
6828 * we will use, so we need the truncate reservation to be seperate so it
6829 * doesn't end up using space reserved for updating the inode or
6830 * removing the orphan item. We also need to be able to stop the
6831 * transaction and start a new one, which means we need to be able to
6832 * update the inode several times, and we have no idea of knowing how
6833 * many times that will be, so we can't just reserve 1 item for the
6834 * entirety of the opration, so that has to be done seperately as well.
6835 * Then there is the orphan item, which does indeed need to be held on
6836 * to for the whole operation, and we need nobody to touch this reserved
6837 * space except the orphan code.
6838 *
6839 * So that leaves us with
6840 *
6841 * 1) root->orphan_block_rsv - for the orphan deletion.
6842 * 2) rsv - for the truncate reservation, which we will steal from the
6843 * transaction reservation.
6844 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6845 * updating the inode.
6846 */
6847 rsv = btrfs_alloc_block_rsv(root);
6848 if (!rsv)
6849 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006850 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04006851 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006852
Josef Bacik907cbce2011-08-08 13:46:15 -04006853 /*
Josef Bacik07127182011-08-19 10:29:59 -04006854 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006855 * 1 for the orphan item we're going to add
6856 * 1 for the orphan item deletion
6857 * 1 for updating the inode.
6858 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04006859 trans = btrfs_start_transaction(root, 4);
6860 if (IS_ERR(trans)) {
6861 err = PTR_ERR(trans);
6862 goto out;
6863 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006864
Josef Bacik907cbce2011-08-08 13:46:15 -04006865 /* Migrate the slack space for the truncate to our reserve */
6866 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6867 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006868 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006869
6870 ret = btrfs_orphan_add(trans, inode);
6871 if (ret) {
6872 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006873 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006874 }
6875
Chris Mason5a3f23d2009-03-31 13:27:11 -04006876 /*
6877 * setattr is responsible for setting the ordered_data_close flag,
6878 * but that is only tested during the last file release. That
6879 * could happen well after the next commit, leaving a great big
6880 * window where new writes may get lost if someone chooses to write
6881 * to this file after truncating to zero
6882 *
6883 * The inode doesn't have any dirty data here, and so if we commit
6884 * this is a noop. If someone immediately starts writing to the inode
6885 * it is very likely we'll catch some of their writes in this
6886 * transaction, and the commit will find this file on the ordered
6887 * data list with good things to send down.
6888 *
6889 * This is a best effort solution, there is still a window where
6890 * using truncate to replace the contents of the file will
6891 * end up with a zero length file after a crash.
6892 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04006893 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
6894 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04006895 btrfs_add_ordered_operation(trans, root, inode);
6896
Josef Bacik5dc562c2012-08-17 13:14:17 -04006897 /*
6898 * So if we truncate and then write and fsync we normally would just
6899 * write the extents that changed, which is a problem if we need to
6900 * first truncate that entire inode. So set this flag so we write out
6901 * all of the extents in the inode to the sync log so we're completely
6902 * safe.
6903 */
6904 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006905 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04006906
Yan, Zheng80825102009-11-12 09:35:36 +00006907 while (1) {
6908 ret = btrfs_truncate_inode_items(trans, root, inode,
6909 inode->i_size,
6910 BTRFS_EXTENT_DATA_KEY);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006911 if (ret != -ENOSPC) {
Josef Bacik3893e332011-01-31 16:03:11 -05006912 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006913 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006914 }
Chris Mason39279cc2007-06-12 06:35:45 -04006915
Josef Bacikfcb80c22011-05-03 10:40:22 -04006916 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006917 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006918 if (ret) {
6919 err = ret;
6920 break;
6921 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04006922
Yan, Zheng80825102009-11-12 09:35:36 +00006923 nr = trans->blocks_used;
6924 btrfs_end_transaction(trans, root);
6925 btrfs_btree_balance_dirty(root, nr);
Josef Bacikca7e70f2012-08-27 17:48:15 -04006926
6927 trans = btrfs_start_transaction(root, 2);
6928 if (IS_ERR(trans)) {
6929 ret = err = PTR_ERR(trans);
6930 trans = NULL;
6931 break;
6932 }
6933
6934 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
6935 rsv, min_size);
6936 BUG_ON(ret); /* shouldn't happen */
6937 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006938 }
6939
6940 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04006941 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006942 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006943 if (ret)
6944 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006945 } else if (ret && inode->i_nlink > 0) {
6946 /*
6947 * Failed to do the truncate, remove us from the in memory
6948 * orphan list.
6949 */
6950 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006951 }
6952
Chris Mason917c16b2011-11-08 14:49:59 -05006953 if (trans) {
6954 trans->block_rsv = &root->fs_info->trans_block_rsv;
6955 ret = btrfs_update_inode(trans, root, inode);
6956 if (ret && !err)
6957 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006958
Chris Mason917c16b2011-11-08 14:49:59 -05006959 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006960 ret = btrfs_end_transaction(trans, root);
Chris Mason917c16b2011-11-08 14:49:59 -05006961 btrfs_btree_balance_dirty(root, nr);
6962 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04006963
6964out:
6965 btrfs_free_block_rsv(root, rsv);
6966
Josef Bacik3893e332011-01-31 16:03:11 -05006967 if (ret && !err)
6968 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05006969
Josef Bacik3893e332011-01-31 16:03:11 -05006970 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006971}
6972
Sven Wegener3b963622008-06-09 21:57:42 -04006973/*
Chris Masond352ac62008-09-29 15:18:18 -04006974 * create a new subvolume directory/inode (helper for the ioctl).
6975 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006976int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006977 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04006978{
Chris Mason39279cc2007-06-12 06:35:45 -04006979 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006980 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006981 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006982
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01006983 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
6984 new_dirid, new_dirid,
6985 S_IFDIR | (~current_umask() & S_IRWXUGO),
6986 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006987 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006988 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006989 inode->i_op = &btrfs_dir_inode_operations;
6990 inode->i_fop = &btrfs_dir_file_operations;
6991
Miklos Szeredibfe86842011-10-28 14:13:29 +02006992 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04006993 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006994
Yan, Zheng76dda932009-09-21 16:00:26 -04006995 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006996
Yan, Zheng76dda932009-09-21 16:00:26 -04006997 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07006998 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006999}
7000
Chris Mason39279cc2007-06-12 06:35:45 -04007001struct inode *btrfs_alloc_inode(struct super_block *sb)
7002{
7003 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007004 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007005
7006 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7007 if (!ei)
7008 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007009
7010 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007011 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007012 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007013 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007014 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007015 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007016 ei->disk_i_size = 0;
7017 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007018 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007019 ei->index_cnt = (u64)-1;
7020 ei->last_unlink_trans = 0;
7021
Josef Bacik9e0baf62011-07-15 15:16:44 +00007022 spin_lock_init(&ei->lock);
7023 ei->outstanding_extents = 0;
7024 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007025
Josef Bacik72ac3c02012-05-23 14:13:11 -04007026 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007027 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007028
Miao Xie16cdcec2011-04-22 18:12:22 +08007029 ei->delayed_node = NULL;
7030
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007031 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007032 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007033 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7034 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007035 ei->io_tree.track_uptodate = 1;
7036 ei->io_failure_tree.track_uptodate = 1;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007037 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007038 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007039 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007040 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007041 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007042 RB_CLEAR_NODE(&ei->rb_node);
7043
7044 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007045}
7046
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007047static void btrfs_i_callback(struct rcu_head *head)
7048{
7049 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007050 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7051}
7052
Chris Mason39279cc2007-06-12 06:35:45 -04007053void btrfs_destroy_inode(struct inode *inode)
7054{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007055 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007056 struct btrfs_root *root = BTRFS_I(inode)->root;
7057
Al Virob3d9b7a2012-06-09 13:51:19 -04007058 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007059 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007060 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7061 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007062 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7063 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007064
Chris Mason5a3f23d2009-03-31 13:27:11 -04007065 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007066 * This can happen where we create an inode, but somebody else also
7067 * created the same inode and we need to destroy the one we already
7068 * created.
7069 */
7070 if (!root)
7071 goto free;
7072
7073 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007074 * Make sure we're properly removed from the ordered operation
7075 * lists.
7076 */
7077 smp_mb();
7078 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7079 spin_lock(&root->fs_info->ordered_extent_lock);
7080 list_del_init(&BTRFS_I(inode)->ordered_operations);
7081 spin_unlock(&root->fs_info->ordered_extent_lock);
7082 }
7083
Josef Bacik8a35d952012-05-23 14:26:42 -04007084 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7085 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007086 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7087 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007088 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007089 }
Josef Bacik7b128762008-07-24 12:17:14 -04007090
Chris Masond3977122009-01-05 21:25:51 -05007091 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007092 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7093 if (!ordered)
7094 break;
7095 else {
Chris Masond3977122009-01-05 21:25:51 -05007096 printk(KERN_ERR "btrfs found ordered "
7097 "extent %llu %llu on inode cleanup\n",
7098 (unsigned long long)ordered->file_offset,
7099 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007100 btrfs_remove_ordered_extent(inode, ordered);
7101 btrfs_put_ordered_extent(ordered);
7102 btrfs_put_ordered_extent(ordered);
7103 }
7104 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007105 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007106 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007107free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007108 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007109 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007110}
7111
Al Viro45321ac2010-06-07 13:43:19 -04007112int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007113{
7114 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007115
Josef Bacik0af3d002010-06-21 14:48:16 -04007116 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007117 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007118 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007119 else
Al Viro45321ac2010-06-07 13:43:19 -04007120 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007121}
7122
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007123static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007124{
7125 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7126
7127 inode_init_once(&ei->vfs_inode);
7128}
7129
7130void btrfs_destroy_cachep(void)
7131{
7132 if (btrfs_inode_cachep)
7133 kmem_cache_destroy(btrfs_inode_cachep);
7134 if (btrfs_trans_handle_cachep)
7135 kmem_cache_destroy(btrfs_trans_handle_cachep);
7136 if (btrfs_transaction_cachep)
7137 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007138 if (btrfs_path_cachep)
7139 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007140 if (btrfs_free_space_cachep)
7141 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007142}
7143
7144int btrfs_init_cachep(void)
7145{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007146 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
7147 sizeof(struct btrfs_inode), 0,
7148 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007149 if (!btrfs_inode_cachep)
7150 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007151
7152 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
7153 sizeof(struct btrfs_trans_handle), 0,
7154 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007155 if (!btrfs_trans_handle_cachep)
7156 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007157
7158 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
7159 sizeof(struct btrfs_transaction), 0,
7160 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007161 if (!btrfs_transaction_cachep)
7162 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007163
7164 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
7165 sizeof(struct btrfs_path), 0,
7166 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007167 if (!btrfs_path_cachep)
7168 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007169
Josef Bacikdc89e982011-01-28 17:05:48 -05007170 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
7171 sizeof(struct btrfs_free_space), 0,
7172 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7173 if (!btrfs_free_space_cachep)
7174 goto fail;
7175
Chris Mason39279cc2007-06-12 06:35:45 -04007176 return 0;
7177fail:
7178 btrfs_destroy_cachep();
7179 return -ENOMEM;
7180}
7181
7182static int btrfs_getattr(struct vfsmount *mnt,
7183 struct dentry *dentry, struct kstat *stat)
7184{
7185 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007186 u32 blocksize = inode->i_sb->s_blocksize;
7187
Chris Mason39279cc2007-06-12 06:35:45 -04007188 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007189 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007190 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007191 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7192 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007193 return 0;
7194}
7195
Liu Bo75e7cb72011-03-22 10:12:20 +00007196/*
7197 * If a file is moved, it will inherit the cow and compression flags of the new
7198 * directory.
7199 */
7200static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7201{
7202 struct btrfs_inode *b_dir = BTRFS_I(dir);
7203 struct btrfs_inode *b_inode = BTRFS_I(inode);
7204
7205 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7206 b_inode->flags |= BTRFS_INODE_NODATACOW;
7207 else
7208 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7209
Liu Bobc178232012-06-14 02:23:18 -06007210 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007211 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007212 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7213 } else {
7214 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7215 BTRFS_INODE_NOCOMPRESS);
7216 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007217}
7218
Chris Masond3977122009-01-05 21:25:51 -05007219static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7220 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007221{
7222 struct btrfs_trans_handle *trans;
7223 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007224 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007225 struct inode *new_inode = new_dentry->d_inode;
7226 struct inode *old_inode = old_dentry->d_inode;
7227 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007228 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007229 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007230 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007231 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007232
Li Zefan33345d012011-04-20 10:31:50 +08007233 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007234 return -EPERM;
7235
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007236 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007237 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007238 return -EXDEV;
7239
Li Zefan33345d012011-04-20 10:31:50 +08007240 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7241 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007242 return -ENOTEMPTY;
7243
Chris Mason39279cc2007-06-12 06:35:45 -04007244 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007245 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007246 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007247 /*
7248 * we're using rename to replace one file with another.
7249 * and the replacement file is large. Start IO on it now so
7250 * we don't add too much work to the end of the transaction
7251 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007252 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007253 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7254 filemap_flush(old_inode->i_mapping);
7255
Yan, Zheng76dda932009-09-21 16:00:26 -04007256 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007257 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007258 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007259 /*
7260 * We want to reserve the absolute worst case amount of items. So if
7261 * both inodes are subvols and we need to unlink them then that would
7262 * require 4 item modifications, but if they are both normal inodes it
7263 * would require 5 item modifications, so we'll assume their normal
7264 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7265 * should cover the worst case number of items we'll modify.
7266 */
7267 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007268 if (IS_ERR(trans)) {
7269 ret = PTR_ERR(trans);
7270 goto out_notrans;
7271 }
Chris Mason5f39d392007-10-15 16:14:19 -04007272
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007273 if (dest != root)
7274 btrfs_record_root_in_trans(trans, dest);
7275
Yan, Zhenga5719522009-09-24 09:17:31 -04007276 ret = btrfs_set_inode_index(new_dir, &index);
7277 if (ret)
7278 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007279
Li Zefan33345d012011-04-20 10:31:50 +08007280 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007281 /* force full log commit if subvolume involved. */
7282 root->fs_info->last_trans_log_full_commit = trans->transid;
7283 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007284 ret = btrfs_insert_inode_ref(trans, dest,
7285 new_dentry->d_name.name,
7286 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007287 old_ino,
7288 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007289 if (ret)
7290 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007291 /*
7292 * this is an ugly little race, but the rename is required
7293 * to make sure that if we crash, the inode is either at the
7294 * old name or the new one. pinning the log transaction lets
7295 * us make sure we don't allow a log commit to come in after
7296 * we unlink the name but before we add the new name back in.
7297 */
7298 btrfs_pin_log_trans(root);
7299 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007300 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007301 * make sure the inode gets flushed if it is replacing
7302 * something.
7303 */
Li Zefan33345d012011-04-20 10:31:50 +08007304 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007305 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007306
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007307 inode_inc_iversion(old_dir);
7308 inode_inc_iversion(new_dir);
7309 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007310 old_dir->i_ctime = old_dir->i_mtime = ctime;
7311 new_dir->i_ctime = new_dir->i_mtime = ctime;
7312 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007313
Chris Mason12fcfd22009-03-24 10:24:20 -04007314 if (old_dentry->d_parent != new_dentry->d_parent)
7315 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7316
Li Zefan33345d012011-04-20 10:31:50 +08007317 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007318 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7319 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7320 old_dentry->d_name.name,
7321 old_dentry->d_name.len);
7322 } else {
Al Viro92986792011-03-04 17:14:37 +00007323 ret = __btrfs_unlink_inode(trans, root, old_dir,
7324 old_dentry->d_inode,
7325 old_dentry->d_name.name,
7326 old_dentry->d_name.len);
7327 if (!ret)
7328 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007329 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007330 if (ret) {
7331 btrfs_abort_transaction(trans, root, ret);
7332 goto out_fail;
7333 }
Chris Mason39279cc2007-06-12 06:35:45 -04007334
7335 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007336 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007337 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007338 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007339 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7340 root_objectid = BTRFS_I(new_inode)->location.objectid;
7341 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7342 root_objectid,
7343 new_dentry->d_name.name,
7344 new_dentry->d_name.len);
7345 BUG_ON(new_inode->i_nlink == 0);
7346 } else {
7347 ret = btrfs_unlink_inode(trans, dest, new_dir,
7348 new_dentry->d_inode,
7349 new_dentry->d_name.name,
7350 new_dentry->d_name.len);
7351 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007352 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007353 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007354 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007355 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007356 if (ret) {
7357 btrfs_abort_transaction(trans, root, ret);
7358 goto out_fail;
7359 }
Chris Mason39279cc2007-06-12 06:35:45 -04007360 }
Josef Bacikaec74772008-07-24 12:12:38 -04007361
Liu Bo75e7cb72011-03-22 10:12:20 +00007362 fixup_inode_flags(new_dir, old_inode);
7363
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007364 ret = btrfs_add_link(trans, new_dir, old_inode,
7365 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007366 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007367 if (ret) {
7368 btrfs_abort_transaction(trans, root, ret);
7369 goto out_fail;
7370 }
Chris Mason39279cc2007-06-12 06:35:45 -04007371
Li Zefan33345d012011-04-20 10:31:50 +08007372 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007373 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007374 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007375 btrfs_end_log_trans(root);
7376 }
Chris Mason39279cc2007-06-12 06:35:45 -04007377out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007378 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007379out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007380 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007381 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007382
Chris Mason39279cc2007-06-12 06:35:45 -04007383 return ret;
7384}
7385
Chris Masond352ac62008-09-29 15:18:18 -04007386/*
7387 * some fairly slow code that needs optimization. This walks the list
7388 * of all the inodes with pending delalloc and forces them to disk.
7389 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007390int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007391{
7392 struct list_head *head = &root->fs_info->delalloc_inodes;
7393 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007394 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007395
Yan Zhengc146afa2008-11-12 14:34:12 -05007396 if (root->fs_info->sb->s_flags & MS_RDONLY)
7397 return -EROFS;
7398
Chris Mason75eff682008-12-15 15:54:40 -05007399 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007400 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007401 binode = list_entry(head->next, struct btrfs_inode,
7402 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007403 inode = igrab(&binode->vfs_inode);
7404 if (!inode)
7405 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007406 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007407 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007408 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007409 if (delay_iput)
7410 btrfs_add_delayed_iput(inode);
7411 else
7412 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007413 }
7414 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007415 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007416 }
Chris Mason75eff682008-12-15 15:54:40 -05007417 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007418
7419 /* the filemap_flush will queue IO into the worker threads, but
7420 * we have to make sure the IO is actually started and that
7421 * ordered extents get created before we return
7422 */
7423 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007424 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007425 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007426 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007427 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7428 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007429 }
7430 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007431 return 0;
7432}
7433
Chris Mason39279cc2007-06-12 06:35:45 -04007434static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7435 const char *symname)
7436{
7437 struct btrfs_trans_handle *trans;
7438 struct btrfs_root *root = BTRFS_I(dir)->root;
7439 struct btrfs_path *path;
7440 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007441 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007442 int err;
7443 int drop_inode = 0;
7444 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007445 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007446 int name_len;
7447 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007448 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007449 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007450 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007451 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007452
7453 name_len = strlen(symname) + 1;
7454 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7455 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007456
Josef Bacik9ed74f22009-09-11 16:12:44 -04007457 /*
7458 * 2 items for inode item and ref
7459 * 2 items for dir items
7460 * 1 item for xattr if selinux is on
7461 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007462 trans = btrfs_start_transaction(root, 5);
7463 if (IS_ERR(trans))
7464 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007465
Li Zefan581bb052011-04-20 10:06:11 +08007466 err = btrfs_find_free_ino(root, &objectid);
7467 if (err)
7468 goto out_unlock;
7469
Josef Bacikaec74772008-07-24 12:12:38 -04007470 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007471 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007472 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007473 if (IS_ERR(inode)) {
7474 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007475 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007476 }
Chris Mason39279cc2007-06-12 06:35:45 -04007477
Eric Paris2a7dba32011-02-01 11:05:39 -05007478 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007479 if (err) {
7480 drop_inode = 1;
7481 goto out_unlock;
7482 }
7483
Casey Schauflerad19db72011-12-15 10:09:07 -05007484 /*
7485 * If the active LSM wants to access the inode during
7486 * d_instantiate it needs these. Smack checks to see
7487 * if the filesystem supports xattrs by looking at the
7488 * ops vector.
7489 */
7490 inode->i_fop = &btrfs_file_operations;
7491 inode->i_op = &btrfs_file_inode_operations;
7492
Josef Bacika1b075d2010-11-19 20:36:11 +00007493 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007494 if (err)
7495 drop_inode = 1;
7496 else {
7497 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007498 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007499 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007500 }
Chris Mason39279cc2007-06-12 06:35:45 -04007501 if (drop_inode)
7502 goto out_unlock;
7503
7504 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007505 if (!path) {
7506 err = -ENOMEM;
7507 drop_inode = 1;
7508 goto out_unlock;
7509 }
Li Zefan33345d012011-04-20 10:31:50 +08007510 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007511 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007512 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7513 datasize = btrfs_file_extent_calc_inline_size(name_len);
7514 err = btrfs_insert_empty_item(trans, root, path, &key,
7515 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007516 if (err) {
7517 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007518 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007519 goto out_unlock;
7520 }
Chris Mason5f39d392007-10-15 16:14:19 -04007521 leaf = path->nodes[0];
7522 ei = btrfs_item_ptr(leaf, path->slots[0],
7523 struct btrfs_file_extent_item);
7524 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7525 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007526 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007527 btrfs_set_file_extent_encryption(leaf, ei, 0);
7528 btrfs_set_file_extent_compression(leaf, ei, 0);
7529 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7530 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7531
Chris Mason39279cc2007-06-12 06:35:45 -04007532 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007533 write_extent_buffer(leaf, symname, ptr, name_len);
7534 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007535 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007536
Chris Mason39279cc2007-06-12 06:35:45 -04007537 inode->i_op = &btrfs_symlink_inode_operations;
7538 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007539 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007540 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007541 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007542 err = btrfs_update_inode(trans, root, inode);
7543 if (err)
7544 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007545
7546out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007547 if (!err)
7548 d_instantiate(dentry, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007549 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007550 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007551 if (drop_inode) {
7552 inode_dec_link_count(inode);
7553 iput(inode);
7554 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007555 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007556 return err;
7557}
Chris Mason16432982008-04-10 10:23:21 -04007558
Josef Bacik0af3d002010-06-21 14:48:16 -04007559static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7560 u64 start, u64 num_bytes, u64 min_size,
7561 loff_t actual_len, u64 *alloc_hint,
7562 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007563{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007564 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7565 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007566 struct btrfs_root *root = BTRFS_I(inode)->root;
7567 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007568 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007569 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007570 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007571 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007572
Josef Bacik0af3d002010-06-21 14:48:16 -04007573 if (trans)
7574 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007575 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007576 if (own_trans) {
7577 trans = btrfs_start_transaction(root, 3);
7578 if (IS_ERR(trans)) {
7579 ret = PTR_ERR(trans);
7580 break;
7581 }
Yan Zhengd899e052008-10-30 14:25:28 -04007582 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007583
Yan, Zhengefa56462010-05-16 10:49:59 -04007584 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007585 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007586 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007587 if (own_trans)
7588 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007589 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007590 }
7591
Yan Zhengd899e052008-10-30 14:25:28 -04007592 ret = insert_reserved_file_extent(trans, inode,
7593 cur_offset, ins.objectid,
7594 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007595 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007596 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007597 if (ret) {
7598 btrfs_abort_transaction(trans, root, ret);
7599 if (own_trans)
7600 btrfs_end_transaction(trans, root);
7601 break;
7602 }
Chris Masona1ed8352009-09-11 12:27:37 -04007603 btrfs_drop_extent_cache(inode, cur_offset,
7604 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007605
Josef Bacik5dc562c2012-08-17 13:14:17 -04007606 em = alloc_extent_map();
7607 if (!em) {
7608 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7609 &BTRFS_I(inode)->runtime_flags);
7610 goto next;
7611 }
7612
7613 em->start = cur_offset;
7614 em->orig_start = cur_offset;
7615 em->len = ins.offset;
7616 em->block_start = ins.objectid;
7617 em->block_len = ins.offset;
7618 em->bdev = root->fs_info->fs_devices->latest_bdev;
7619 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7620 em->generation = trans->transid;
7621
7622 while (1) {
7623 write_lock(&em_tree->lock);
7624 ret = add_extent_mapping(em_tree, em);
7625 if (!ret)
7626 list_move(&em->list,
7627 &em_tree->modified_extents);
7628 write_unlock(&em_tree->lock);
7629 if (ret != -EEXIST)
7630 break;
7631 btrfs_drop_extent_cache(inode, cur_offset,
7632 cur_offset + ins.offset - 1,
7633 0);
7634 }
7635 free_extent_map(em);
7636next:
Yan Zhengd899e052008-10-30 14:25:28 -04007637 num_bytes -= ins.offset;
7638 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007639 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007640
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007641 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007642 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007643 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007644 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007645 (actual_len > inode->i_size) &&
7646 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007647 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007648 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007649 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007650 i_size = cur_offset;
7651 i_size_write(inode, i_size);
7652 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007653 }
7654
Yan Zhengd899e052008-10-30 14:25:28 -04007655 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007656
7657 if (ret) {
7658 btrfs_abort_transaction(trans, root, ret);
7659 if (own_trans)
7660 btrfs_end_transaction(trans, root);
7661 break;
7662 }
Yan Zhengd899e052008-10-30 14:25:28 -04007663
Josef Bacik0af3d002010-06-21 14:48:16 -04007664 if (own_trans)
7665 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007666 }
Yan Zhengd899e052008-10-30 14:25:28 -04007667 return ret;
7668}
7669
Josef Bacik0af3d002010-06-21 14:48:16 -04007670int btrfs_prealloc_file_range(struct inode *inode, int mode,
7671 u64 start, u64 num_bytes, u64 min_size,
7672 loff_t actual_len, u64 *alloc_hint)
7673{
7674 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7675 min_size, actual_len, alloc_hint,
7676 NULL);
7677}
7678
7679int btrfs_prealloc_file_range_trans(struct inode *inode,
7680 struct btrfs_trans_handle *trans, int mode,
7681 u64 start, u64 num_bytes, u64 min_size,
7682 loff_t actual_len, u64 *alloc_hint)
7683{
7684 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7685 min_size, actual_len, alloc_hint, trans);
7686}
7687
Chris Masone6dcd2d2008-07-17 12:53:50 -04007688static int btrfs_set_page_dirty(struct page *page)
7689{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007690 return __set_page_dirty_nobuffers(page);
7691}
7692
Al Viro10556cb2011-06-20 19:28:19 -04007693static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007694{
Li Zefanb83cc962010-12-20 16:04:08 +08007695 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007696 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007697
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007698 if (mask & MAY_WRITE &&
7699 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7700 if (btrfs_root_readonly(root))
7701 return -EROFS;
7702 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7703 return -EACCES;
7704 }
Al Viro2830ba72011-06-20 19:16:29 -04007705 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007706}
Chris Mason39279cc2007-06-12 06:35:45 -04007707
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007708static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007709 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007710 .lookup = btrfs_lookup,
7711 .create = btrfs_create,
7712 .unlink = btrfs_unlink,
7713 .link = btrfs_link,
7714 .mkdir = btrfs_mkdir,
7715 .rmdir = btrfs_rmdir,
7716 .rename = btrfs_rename,
7717 .symlink = btrfs_symlink,
7718 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007719 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007720 .setxattr = btrfs_setxattr,
7721 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007722 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007723 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007724 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007725 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007726};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007727static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007728 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007729 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007730 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007731};
Yan, Zheng76dda932009-09-21 16:00:26 -04007732
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007733static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007734 .llseek = generic_file_llseek,
7735 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007736 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007737 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007738#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007739 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007740#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007741 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007742 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007743};
7744
Chris Masond1310b22008-01-24 16:13:08 -05007745static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007746 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007747 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007748 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007749 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007750 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007751 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007752 .set_bit_hook = btrfs_set_bit_hook,
7753 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007754 .merge_extent_hook = btrfs_merge_extent_hook,
7755 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007756};
7757
Chris Mason35054392009-01-21 13:11:13 -05007758/*
7759 * btrfs doesn't support the bmap operation because swapfiles
7760 * use bmap to make a mapping of extents in the file. They assume
7761 * these extents won't change over the life of the file and they
7762 * use the bmap result to do IO directly to the drive.
7763 *
7764 * the btrfs bmap call would return logical addresses that aren't
7765 * suitable for IO and they also will change frequently as COW
7766 * operations happen. So, swapfile + btrfs == corruption.
7767 *
7768 * For now we're avoiding this by dropping bmap.
7769 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007770static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007771 .readpage = btrfs_readpage,
7772 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007773 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007774 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007775 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007776 .invalidatepage = btrfs_invalidatepage,
7777 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007778 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007779 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007780};
7781
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007782static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007783 .readpage = btrfs_readpage,
7784 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007785 .invalidatepage = btrfs_invalidatepage,
7786 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007787};
7788
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007789static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007790 .getattr = btrfs_getattr,
7791 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007792 .setxattr = btrfs_setxattr,
7793 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007794 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007795 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007796 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007797 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007798 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007799 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007800};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007801static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007802 .getattr = btrfs_getattr,
7803 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007804 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007805 .setxattr = btrfs_setxattr,
7806 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007807 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007808 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007809 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007810 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04007811};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007812static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007813 .readlink = generic_readlink,
7814 .follow_link = page_follow_link_light,
7815 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007816 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05007817 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007818 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007819 .setxattr = btrfs_setxattr,
7820 .getxattr = btrfs_getxattr,
7821 .listxattr = btrfs_listxattr,
7822 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007823 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007824 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007825};
Yan, Zheng76dda932009-09-21 16:00:26 -04007826
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007827const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007828 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007829 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04007830};