blob: 1b99fe8a129d8c0132e2b042618fa3dc37a0caf2 [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);
1311 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001312 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001313 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001314 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001315 if (ret != -EEXIST) {
1316 free_extent_map(em);
1317 break;
1318 }
1319 btrfs_drop_extent_cache(inode, em->start,
1320 em->start + em->len - 1, 0);
1321 }
1322 type = BTRFS_ORDERED_PREALLOC;
1323 } else {
1324 type = BTRFS_ORDERED_NOCOW;
1325 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001326
1327 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001328 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001329 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001330
Yan, Zhengefa56462010-05-16 10:49:59 -04001331 if (root->root_key.objectid ==
1332 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1333 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1334 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001335 if (ret) {
1336 btrfs_abort_transaction(trans, root, ret);
1337 goto error;
1338 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001339 }
1340
Yan Zhengd899e052008-10-30 14:25:28 -04001341 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001342 cur_offset, cur_offset + num_bytes - 1,
1343 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1344 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1345 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 cur_offset = extent_end;
1347 if (cur_offset > end)
1348 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001349 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001350 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001351
Josef Bacik17ca04a2012-05-31 15:58:55 -04001352 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001354 cur_offset = end;
1355 }
1356
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 if (cow_start != (u64)-1) {
1358 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001359 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001360 if (ret) {
1361 btrfs_abort_transaction(trans, root, ret);
1362 goto error;
1363 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 }
1365
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001366error:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001367 if (nolock) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001368 err = btrfs_end_transaction_nolock(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001369 } else {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001370 err = btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001371 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001372 if (!ret)
1373 ret = err;
1374
Josef Bacik17ca04a2012-05-31 15:58:55 -04001375 if (ret && cur_offset < end)
1376 extent_clear_unlock_delalloc(inode,
1377 &BTRFS_I(inode)->io_tree,
1378 cur_offset, end, locked_page,
1379 EXTENT_CLEAR_UNLOCK_PAGE |
1380 EXTENT_CLEAR_UNLOCK |
1381 EXTENT_CLEAR_DELALLOC |
1382 EXTENT_CLEAR_DIRTY |
1383 EXTENT_SET_WRITEBACK |
1384 EXTENT_END_WRITEBACK);
1385
Yan Zheng7ea394f2008-08-05 13:05:02 -04001386 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001387 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001388}
1389
Chris Masond352ac62008-09-29 15:18:18 -04001390/*
1391 * extent_io.c call back to do delayed allocation processing
1392 */
Chris Masonc8b97812008-10-29 14:49:59 -04001393static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001394 u64 start, u64 end, int *page_started,
1395 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001396{
Chris Masonbe20aa92007-12-17 20:14:01 -05001397 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001398 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001399
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001400 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
Chris Masonc8b97812008-10-29 14:49:59 -04001401 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001402 page_started, 1, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001403 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001404 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001405 page_started, 0, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001406 } else if (!btrfs_test_opt(root, COMPRESS) &&
1407 !(BTRFS_I(inode)->force_compress) &&
1408 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
Chris Mason7f366cf2009-03-12 20:12:45 -04001409 ret = cow_file_range(inode, locked_page, start, end,
1410 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001411 } else {
1412 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1413 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001414 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001415 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001416 }
Chris Masonb888db22007-08-27 16:49:44 -04001417 return ret;
1418}
1419
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001420static void btrfs_split_extent_hook(struct inode *inode,
1421 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001422{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001423 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001424 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001425 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001426
Josef Bacik9e0baf62011-07-15 15:16:44 +00001427 spin_lock(&BTRFS_I(inode)->lock);
1428 BTRFS_I(inode)->outstanding_extents++;
1429 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001430}
1431
1432/*
1433 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1434 * extents so we can keep track of new extents that are just merged onto old
1435 * extents, such as when we are doing sequential writes, so we can properly
1436 * account for the metadata space we'll need.
1437 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001438static void btrfs_merge_extent_hook(struct inode *inode,
1439 struct extent_state *new,
1440 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001441{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001442 /* not delalloc, ignore it */
1443 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001444 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001445
Josef Bacik9e0baf62011-07-15 15:16:44 +00001446 spin_lock(&BTRFS_I(inode)->lock);
1447 BTRFS_I(inode)->outstanding_extents--;
1448 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001449}
1450
Chris Masond352ac62008-09-29 15:18:18 -04001451/*
1452 * extent_io.c set_bit_hook, used to track delayed allocation
1453 * bytes in this file, and to maintain the list of inodes that
1454 * have pending delalloc work to be done.
1455 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001456static void btrfs_set_bit_hook(struct inode *inode,
1457 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001458{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001459
Chris Mason75eff682008-12-15 15:54:40 -05001460 /*
1461 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001462 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001463 * bit, which is only set or cleared with irqs on
1464 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001465 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001466 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001467 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001468 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001469
Josef Bacik9e0baf62011-07-15 15:16:44 +00001470 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001471 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001472 } else {
1473 spin_lock(&BTRFS_I(inode)->lock);
1474 BTRFS_I(inode)->outstanding_extents++;
1475 spin_unlock(&BTRFS_I(inode)->lock);
1476 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001477
Chris Mason75eff682008-12-15 15:54:40 -05001478 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001479 BTRFS_I(inode)->delalloc_bytes += len;
1480 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001481 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001482 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1483 &root->fs_info->delalloc_inodes);
1484 }
Chris Mason75eff682008-12-15 15:54:40 -05001485 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001486 }
Chris Mason291d6732008-01-29 15:55:23 -05001487}
1488
Chris Masond352ac62008-09-29 15:18:18 -04001489/*
1490 * extent_io.c clear_bit_hook, see set_bit_hook for why
1491 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001492static void btrfs_clear_bit_hook(struct inode *inode,
1493 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001494{
Chris Mason75eff682008-12-15 15:54:40 -05001495 /*
1496 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001497 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001498 * bit, which is only set or cleared with irqs on
1499 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001500 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001501 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001502 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001503 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001504
Josef Bacik9e0baf62011-07-15 15:16:44 +00001505 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001506 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001507 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1508 spin_lock(&BTRFS_I(inode)->lock);
1509 BTRFS_I(inode)->outstanding_extents--;
1510 spin_unlock(&BTRFS_I(inode)->lock);
1511 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001512
1513 if (*bits & EXTENT_DO_ACCOUNTING)
1514 btrfs_delalloc_release_metadata(inode, len);
1515
Josef Bacik0cb59c92010-07-02 12:14:14 -04001516 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1517 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001518 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001519
Chris Mason75eff682008-12-15 15:54:40 -05001520 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001521 root->fs_info->delalloc_bytes -= len;
1522 BTRFS_I(inode)->delalloc_bytes -= len;
1523
Josef Bacik0cb59c92010-07-02 12:14:14 -04001524 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001525 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1526 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1527 }
Chris Mason75eff682008-12-15 15:54:40 -05001528 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001529 }
Chris Mason291d6732008-01-29 15:55:23 -05001530}
1531
Chris Masond352ac62008-09-29 15:18:18 -04001532/*
1533 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1534 * we don't create bios that span stripes or chunks
1535 */
Chris Mason239b14b2008-03-24 15:02:07 -04001536int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001537 size_t size, struct bio *bio,
1538 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001539{
1540 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1541 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001542 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001543 u64 length = 0;
1544 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001545 int ret;
1546
Chris Mason771ed682008-11-06 22:02:51 -05001547 if (bio_flags & EXTENT_BIO_COMPRESSED)
1548 return 0;
1549
Chris Masonf2d8d742008-04-21 10:03:05 -04001550 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001551 map_tree = &root->fs_info->mapping_tree;
1552 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001553 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001554 &map_length, NULL, 0);
Jeff Mahoney3444a972011-10-03 23:23:13 -04001555 /* Will always return 0 or 1 with map_multi == NULL */
1556 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001557 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001558 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001559 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001560}
1561
Chris Masond352ac62008-09-29 15:18:18 -04001562/*
1563 * in order to insert checksums into the metadata in large chunks,
1564 * we wait until bio submission time. All the pages in the bio are
1565 * checksummed and sums are attached onto the ordered extent record.
1566 *
1567 * At IO completion time the cums attached on the ordered extent record
1568 * are inserted into the btree
1569 */
Chris Masond3977122009-01-05 21:25:51 -05001570static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1571 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001572 unsigned long bio_flags,
1573 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001574{
Chris Mason065631f2008-02-20 12:07:25 -05001575 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001576 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001577
Chris Masond20f7042008-12-08 16:58:54 -05001578 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001579 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001580 return 0;
1581}
Chris Masone0156402008-04-16 11:15:20 -04001582
Chris Mason4a69a412008-11-06 22:03:00 -05001583/*
1584 * in order to insert checksums into the metadata in large chunks,
1585 * we wait until bio submission time. All the pages in the bio are
1586 * checksummed and sums are attached onto the ordered extent record.
1587 *
1588 * At IO completion time the cums attached on the ordered extent record
1589 * are inserted into the btree
1590 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001591static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001592 int mirror_num, unsigned long bio_flags,
1593 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001594{
1595 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001596 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001597}
1598
Chris Masond352ac62008-09-29 15:18:18 -04001599/*
Chris Masoncad321a2008-12-17 14:51:42 -05001600 * extent_io.c submission hook. This does the right thing for csum calculation
1601 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001602 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001603static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001604 int mirror_num, unsigned long bio_flags,
1605 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001606{
1607 struct btrfs_root *root = BTRFS_I(inode)->root;
1608 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001609 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001610 int metadata = 0;
Chris Mason44b8bd72008-04-16 11:14:51 -04001611
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001612 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001613
Liu Bo83eea1f2012-07-10 05:28:39 -06001614 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001615 metadata = 2;
1616
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001617 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001618 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1619 if (ret)
1620 return ret;
1621
Chris Masond20f7042008-12-08 16:58:54 -05001622 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001623 return btrfs_submit_compressed_read(inode, bio,
1624 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001625 } else if (!skip_sum) {
1626 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1627 if (ret)
1628 return ret;
1629 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001630 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001631 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001632 /* csum items have already been cloned */
1633 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1634 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001635 /* we're doing a write, do the async checksumming */
1636 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001637 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001638 bio_flags, bio_offset,
1639 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001640 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001641 }
1642
Chris Mason0b86a832008-03-24 15:01:56 -04001643mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001644 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001645}
Chris Mason6885f302008-02-20 16:11:05 -05001646
Chris Masond352ac62008-09-29 15:18:18 -04001647/*
1648 * given a list of ordered sums record them in the inode. This happens
1649 * at IO completion time based on sums calculated at bio submission time.
1650 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001651static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001652 struct inode *inode, u64 file_offset,
1653 struct list_head *list)
1654{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001655 struct btrfs_ordered_sum *sum;
1656
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001657 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001658 btrfs_csum_file_blocks(trans,
1659 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001660 }
1661 return 0;
1662}
1663
Josef Bacik2ac55d42010-02-03 19:33:23 +00001664int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1665 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001666{
Chris Masond3977122009-01-05 21:25:51 -05001667 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001668 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001669 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001670 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001671}
1672
Chris Masond352ac62008-09-29 15:18:18 -04001673/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001674struct btrfs_writepage_fixup {
1675 struct page *page;
1676 struct btrfs_work work;
1677};
1678
Christoph Hellwigb2950862008-12-02 09:54:17 -05001679static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001680{
1681 struct btrfs_writepage_fixup *fixup;
1682 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001683 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001684 struct page *page;
1685 struct inode *inode;
1686 u64 page_start;
1687 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001688 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001689
1690 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1691 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001692again:
Chris Mason247e7432008-07-17 12:53:51 -04001693 lock_page(page);
1694 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1695 ClearPageChecked(page);
1696 goto out_page;
1697 }
1698
1699 inode = page->mapping->host;
1700 page_start = page_offset(page);
1701 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1702
Josef Bacik2ac55d42010-02-03 19:33:23 +00001703 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001704 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001705
1706 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001707 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001708 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001709
1710 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1711 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001712 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1713 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001714 unlock_page(page);
1715 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001716 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001717 goto again;
1718 }
Chris Mason247e7432008-07-17 12:53:51 -04001719
Jeff Mahoney87826df2012-02-15 16:23:57 +01001720 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1721 if (ret) {
1722 mapping_set_error(page->mapping, ret);
1723 end_extent_writepage(page, ret, page_start, page_end);
1724 ClearPageChecked(page);
1725 goto out;
1726 }
1727
Josef Bacik2ac55d42010-02-03 19:33:23 +00001728 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001729 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001730 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001731out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001732 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1733 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001734out_page:
1735 unlock_page(page);
1736 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001737 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001738}
1739
1740/*
1741 * There are a few paths in the higher layers of the kernel that directly
1742 * set the page dirty bit without asking the filesystem if it is a
1743 * good idea. This causes problems because we want to make sure COW
1744 * properly happens and the data=ordered rules are followed.
1745 *
Chris Masonc8b97812008-10-29 14:49:59 -04001746 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001747 * hasn't been properly setup for IO. We kick off an async process
1748 * to fix it up. The async helper will wait for ordered extents, set
1749 * the delalloc bit and make it safe to write the page.
1750 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001751static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001752{
1753 struct inode *inode = page->mapping->host;
1754 struct btrfs_writepage_fixup *fixup;
1755 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001756
Chris Mason8b62b722009-09-02 16:53:46 -04001757 /* this page is properly in the ordered list */
1758 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001759 return 0;
1760
1761 if (PageChecked(page))
1762 return -EAGAIN;
1763
1764 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1765 if (!fixup)
1766 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001767
Chris Mason247e7432008-07-17 12:53:51 -04001768 SetPageChecked(page);
1769 page_cache_get(page);
1770 fixup->work.func = btrfs_writepage_fixup_worker;
1771 fixup->page = page;
1772 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001773 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001774}
1775
Yan Zhengd899e052008-10-30 14:25:28 -04001776static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1777 struct inode *inode, u64 file_pos,
1778 u64 disk_bytenr, u64 disk_num_bytes,
1779 u64 num_bytes, u64 ram_bytes,
1780 u8 compression, u8 encryption,
1781 u16 other_encoding, int extent_type)
1782{
1783 struct btrfs_root *root = BTRFS_I(inode)->root;
1784 struct btrfs_file_extent_item *fi;
1785 struct btrfs_path *path;
1786 struct extent_buffer *leaf;
1787 struct btrfs_key ins;
1788 u64 hint;
1789 int ret;
1790
1791 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001792 if (!path)
1793 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001794
Chris Masonb9473432009-03-13 11:00:37 -04001795 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001796
1797 /*
1798 * we may be replacing one extent in the tree with another.
1799 * The new extent is pinned in the extent map, and we don't want
1800 * to drop it from the cache until it is completely in the btree.
1801 *
1802 * So, tell btrfs_drop_extents to leave this extent in the cache.
1803 * the caller is expected to unpin it and allow it to be merged
1804 * with the others.
1805 */
Josef Bacik5dc562c2012-08-17 13:14:17 -04001806 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1807 file_pos + num_bytes,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001808 &hint, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001809 if (ret)
1810 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001811
Li Zefan33345d012011-04-20 10:31:50 +08001812 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001813 ins.offset = file_pos;
1814 ins.type = BTRFS_EXTENT_DATA_KEY;
1815 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001816 if (ret)
1817 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001818 leaf = path->nodes[0];
1819 fi = btrfs_item_ptr(leaf, path->slots[0],
1820 struct btrfs_file_extent_item);
1821 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1822 btrfs_set_file_extent_type(leaf, fi, extent_type);
1823 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1824 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1825 btrfs_set_file_extent_offset(leaf, fi, 0);
1826 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1827 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1828 btrfs_set_file_extent_compression(leaf, fi, compression);
1829 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1830 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001831
1832 btrfs_unlock_up_safe(path, 1);
1833 btrfs_set_lock_blocking(leaf);
1834
Yan Zhengd899e052008-10-30 14:25:28 -04001835 btrfs_mark_buffer_dirty(leaf);
1836
1837 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001838
1839 ins.objectid = disk_bytenr;
1840 ins.offset = disk_num_bytes;
1841 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001842 ret = btrfs_alloc_reserved_file_extent(trans, root,
1843 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001844 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001845out:
Yan Zhengd899e052008-10-30 14:25:28 -04001846 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001847
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001848 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001849}
1850
Chris Mason5d13a982009-03-13 11:41:46 -04001851/*
1852 * helper function for btrfs_finish_ordered_io, this
1853 * just reads in some of the csum leaves to prime them into ram
1854 * before we start the transaction. It limits the amount of btree
1855 * reads required while inside the transaction.
1856 */
Chris Masond352ac62008-09-29 15:18:18 -04001857/* as ordered data IO finishes, this gets called so we can finish
1858 * an ordered extent if the range of bytes in the file it covers are
1859 * fully written.
1860 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001861static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001862{
Josef Bacik5fd02042012-05-02 14:00:54 -04001863 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001864 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001865 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001866 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001867 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001868 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001869 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001870 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001871
Liu Bo83eea1f2012-07-10 05:28:39 -06001872 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001873
Josef Bacik5fd02042012-05-02 14:00:54 -04001874 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1875 ret = -EIO;
1876 goto out;
1877 }
1878
Yan, Zhengc2167752009-11-12 09:34:21 +00001879 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001880 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Yan, Zhengc2167752009-11-12 09:34:21 +00001881 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1882 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001883 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001884 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001885 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001886 trans = btrfs_join_transaction(root);
Liu Bod280e5b2012-08-21 21:13:25 -06001887 if (IS_ERR(trans)) {
1888 ret = PTR_ERR(trans);
1889 trans = NULL;
1890 goto out;
1891 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001892 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason21151332011-11-10 20:39:08 -05001893 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001894 if (ret) /* -ENOMEM or corruption */
1895 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001896 }
1897 goto out;
1898 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001899
Josef Bacik2ac55d42010-02-03 19:33:23 +00001900 lock_extent_bits(io_tree, ordered_extent->file_offset,
1901 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001902 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001903
Josef Bacik0cb59c92010-07-02 12:14:14 -04001904 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001905 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001906 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001907 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001908 if (IS_ERR(trans)) {
1909 ret = PTR_ERR(trans);
1910 trans = NULL;
1911 goto out_unlock;
1912 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001913 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001914
Chris Masonc8b97812008-10-29 14:49:59 -04001915 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001916 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001917 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001918 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001919 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001920 ordered_extent->file_offset,
1921 ordered_extent->file_offset +
1922 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001923 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001924 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001925 ret = insert_reserved_file_extent(trans, inode,
1926 ordered_extent->file_offset,
1927 ordered_extent->start,
1928 ordered_extent->disk_len,
1929 ordered_extent->len,
1930 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001931 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001932 BTRFS_FILE_EXTENT_REG);
Yan Zhengd899e052008-10-30 14:25:28 -04001933 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04001934 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1935 ordered_extent->file_offset, ordered_extent->len,
1936 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001937 if (ret < 0) {
1938 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001939 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001940 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001941
Chris Masone6dcd2d2008-07-17 12:53:50 -04001942 add_pending_csums(trans, inode, ordered_extent->file_offset,
1943 &ordered_extent->list);
1944
Josef Bacik1ef30be2011-04-05 19:25:36 -04001945 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Miao Xiea39f7522011-09-11 10:52:25 -04001946 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Chris Mason21151332011-11-10 20:39:08 -05001947 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001948 if (ret) { /* -ENOMEM or corruption */
1949 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001950 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001951 }
Josef Bacik7c735312012-08-13 15:43:26 -04001952 } else {
1953 btrfs_set_inode_last_trans(trans, inode);
Josef Bacik1ef30be2011-04-05 19:25:36 -04001954 }
1955 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04001956out_unlock:
1957 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1958 ordered_extent->file_offset +
1959 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00001960out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001961 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001962 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001963 if (trans) {
1964 if (nolock)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001965 btrfs_end_transaction_nolock(trans, root);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001966 else
Josef Bacik0cb59c92010-07-02 12:14:14 -04001967 btrfs_end_transaction(trans, root);
1968 }
1969
Josef Bacik5fd02042012-05-02 14:00:54 -04001970 if (ret)
1971 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
1972 ordered_extent->file_offset +
1973 ordered_extent->len - 1, NULL, GFP_NOFS);
1974
1975 /*
1976 * This needs to be dont to make sure anybody waiting knows we are done
1977 * upating everything for this ordered extent.
1978 */
1979 btrfs_remove_ordered_extent(inode, ordered_extent);
1980
Chris Masone6dcd2d2008-07-17 12:53:50 -04001981 /* once for us */
1982 btrfs_put_ordered_extent(ordered_extent);
1983 /* once for the tree */
1984 btrfs_put_ordered_extent(ordered_extent);
1985
Josef Bacik5fd02042012-05-02 14:00:54 -04001986 return ret;
1987}
1988
1989static void finish_ordered_fn(struct btrfs_work *work)
1990{
1991 struct btrfs_ordered_extent *ordered_extent;
1992 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
1993 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001994}
1995
Christoph Hellwigb2950862008-12-02 09:54:17 -05001996static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001997 struct extent_state *state, int uptodate)
1998{
Josef Bacik5fd02042012-05-02 14:00:54 -04001999 struct inode *inode = page->mapping->host;
2000 struct btrfs_root *root = BTRFS_I(inode)->root;
2001 struct btrfs_ordered_extent *ordered_extent = NULL;
2002 struct btrfs_workers *workers;
2003
liubo1abe9b82011-03-24 11:18:59 +00002004 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2005
Chris Mason8b62b722009-09-02 16:53:46 -04002006 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002007 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2008 end - start + 1, uptodate))
2009 return 0;
2010
2011 ordered_extent->work.func = finish_ordered_fn;
2012 ordered_extent->work.flags = 0;
2013
Liu Bo83eea1f2012-07-10 05:28:39 -06002014 if (btrfs_is_free_space_inode(inode))
Josef Bacik5fd02042012-05-02 14:00:54 -04002015 workers = &root->fs_info->endio_freespace_worker;
2016 else
2017 workers = &root->fs_info->endio_write_workers;
2018 btrfs_queue_worker(workers, &ordered_extent->work);
2019
2020 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04002021}
2022
Chris Masond352ac62008-09-29 15:18:18 -04002023/*
Chris Masond352ac62008-09-29 15:18:18 -04002024 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002025 * if there's a match, we allow the bio to finish. If not, the code in
2026 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002027 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002028static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002029 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002030{
Chris Mason35ebb932007-10-30 16:56:53 -04002031 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04002032 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002033 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04002034 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05002035 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04002036 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04002037 struct btrfs_root *root = BTRFS_I(inode)->root;
2038 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05002039
Chris Masond20f7042008-12-08 16:58:54 -05002040 if (PageChecked(page)) {
2041 ClearPageChecked(page);
2042 goto good;
2043 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002044
2045 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02002046 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002047
Yan Zheng17d217f2008-12-12 10:03:38 -05002048 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002049 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002050 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2051 GFP_NOFS);
2052 return 0;
2053 }
2054
Yanc2e639f2008-02-04 08:57:25 -05002055 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002056 private = state->private;
2057 ret = 0;
2058 } else {
2059 ret = get_state_private(io_tree, start, &private);
2060 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002061 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002062 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002063 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002064
Chris Masonff79f812007-10-15 16:22:25 -04002065 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2066 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002067 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002068 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002069
Cong Wang7ac687d2011-11-25 23:14:28 +08002070 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002071good:
Chris Mason07157aa2007-08-30 08:50:51 -04002072 return 0;
2073
2074zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002075 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002076 "private %llu\n",
2077 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002078 (unsigned long long)start, csum,
2079 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002080 memset(kaddr + offset, 1, end - start + 1);
2081 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002082 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002083 if (private == 0)
2084 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002085 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002086}
Chris Masonb888db22007-08-27 16:49:44 -04002087
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002088struct delayed_iput {
2089 struct list_head list;
2090 struct inode *inode;
2091};
2092
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002093/* JDM: If this is fs-wide, why can't we add a pointer to
2094 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002095void btrfs_add_delayed_iput(struct inode *inode)
2096{
2097 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2098 struct delayed_iput *delayed;
2099
2100 if (atomic_add_unless(&inode->i_count, -1, 1))
2101 return;
2102
2103 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2104 delayed->inode = inode;
2105
2106 spin_lock(&fs_info->delayed_iput_lock);
2107 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2108 spin_unlock(&fs_info->delayed_iput_lock);
2109}
2110
2111void btrfs_run_delayed_iputs(struct btrfs_root *root)
2112{
2113 LIST_HEAD(list);
2114 struct btrfs_fs_info *fs_info = root->fs_info;
2115 struct delayed_iput *delayed;
2116 int empty;
2117
2118 spin_lock(&fs_info->delayed_iput_lock);
2119 empty = list_empty(&fs_info->delayed_iputs);
2120 spin_unlock(&fs_info->delayed_iput_lock);
2121 if (empty)
2122 return;
2123
2124 down_read(&root->fs_info->cleanup_work_sem);
2125 spin_lock(&fs_info->delayed_iput_lock);
2126 list_splice_init(&fs_info->delayed_iputs, &list);
2127 spin_unlock(&fs_info->delayed_iput_lock);
2128
2129 while (!list_empty(&list)) {
2130 delayed = list_entry(list.next, struct delayed_iput, list);
2131 list_del(&delayed->list);
2132 iput(delayed->inode);
2133 kfree(delayed);
2134 }
2135 up_read(&root->fs_info->cleanup_work_sem);
2136}
2137
Yan, Zhengd68fc572010-05-16 10:49:58 -04002138enum btrfs_orphan_cleanup_state {
2139 ORPHAN_CLEANUP_STARTED = 1,
2140 ORPHAN_CLEANUP_DONE = 2,
2141};
2142
2143/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002144 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002145 * files in the subvolume, it removes orphan item and frees block_rsv
2146 * structure.
2147 */
2148void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2149 struct btrfs_root *root)
2150{
Josef Bacik90290e12011-12-02 15:44:12 -05002151 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002152 int ret;
2153
Josef Bacik8a35d952012-05-23 14:26:42 -04002154 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002155 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2156 return;
2157
Josef Bacik90290e12011-12-02 15:44:12 -05002158 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002159 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002160 spin_unlock(&root->orphan_lock);
2161 return;
2162 }
2163
2164 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2165 spin_unlock(&root->orphan_lock);
2166 return;
2167 }
2168
2169 block_rsv = root->orphan_block_rsv;
2170 root->orphan_block_rsv = NULL;
2171 spin_unlock(&root->orphan_lock);
2172
Yan, Zhengd68fc572010-05-16 10:49:58 -04002173 if (root->orphan_item_inserted &&
2174 btrfs_root_refs(&root->root_item) > 0) {
2175 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2176 root->root_key.objectid);
2177 BUG_ON(ret);
2178 root->orphan_item_inserted = 0;
2179 }
2180
Josef Bacik90290e12011-12-02 15:44:12 -05002181 if (block_rsv) {
2182 WARN_ON(block_rsv->size > 0);
2183 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002184 }
2185}
2186
2187/*
Josef Bacik7b128762008-07-24 12:17:14 -04002188 * This creates an orphan entry for the given inode in case something goes
2189 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002190 *
2191 * NOTE: caller of this function should reserve 5 units of metadata for
2192 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002193 */
2194int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2195{
2196 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002197 struct btrfs_block_rsv *block_rsv = NULL;
2198 int reserve = 0;
2199 int insert = 0;
2200 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002201
Yan, Zhengd68fc572010-05-16 10:49:58 -04002202 if (!root->orphan_block_rsv) {
2203 block_rsv = btrfs_alloc_block_rsv(root);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002204 if (!block_rsv)
2205 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002206 }
2207
Yan, Zhengd68fc572010-05-16 10:49:58 -04002208 spin_lock(&root->orphan_lock);
2209 if (!root->orphan_block_rsv) {
2210 root->orphan_block_rsv = block_rsv;
2211 } else if (block_rsv) {
2212 btrfs_free_block_rsv(root, block_rsv);
2213 block_rsv = NULL;
2214 }
Josef Bacik7b128762008-07-24 12:17:14 -04002215
Josef Bacik8a35d952012-05-23 14:26:42 -04002216 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2217 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002218#if 0
2219 /*
2220 * For proper ENOSPC handling, we should do orphan
2221 * cleanup when mounting. But this introduces backward
2222 * compatibility issue.
2223 */
2224 if (!xchg(&root->orphan_item_inserted, 1))
2225 insert = 2;
2226 else
2227 insert = 1;
2228#endif
2229 insert = 1;
Josef Bacik8a35d952012-05-23 14:26:42 -04002230 atomic_dec(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002231 }
Josef Bacik7b128762008-07-24 12:17:14 -04002232
Josef Bacik72ac3c02012-05-23 14:13:11 -04002233 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2234 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002235 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002236 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002237
Yan, Zhengd68fc572010-05-16 10:49:58 -04002238 /* grab metadata reservation from transaction handle */
2239 if (reserve) {
2240 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002241 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002242 }
2243
2244 /* insert an orphan item to track this unlinked/truncated file */
2245 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002246 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002247 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002248 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2249 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002250 btrfs_abort_transaction(trans, root, ret);
2251 return ret;
2252 }
2253 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002254 }
2255
2256 /* insert an orphan item to track subvolume contains orphan files */
2257 if (insert >= 2) {
2258 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2259 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002260 if (ret && ret != -EEXIST) {
2261 btrfs_abort_transaction(trans, root, ret);
2262 return ret;
2263 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002264 }
2265 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002266}
2267
2268/*
2269 * We have done the truncate/delete so we can go ahead and remove the orphan
2270 * item for this particular inode.
2271 */
2272int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2273{
2274 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002275 int delete_item = 0;
2276 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002277 int ret = 0;
2278
Yan, Zhengd68fc572010-05-16 10:49:58 -04002279 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002280 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2281 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002282 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002283
Josef Bacik72ac3c02012-05-23 14:13:11 -04002284 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2285 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002286 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002287 spin_unlock(&root->orphan_lock);
2288
2289 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002290 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002291 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002292 }
2293
Josef Bacik8a35d952012-05-23 14:26:42 -04002294 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002295 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002296 atomic_dec(&root->orphan_inodes);
2297 }
Josef Bacik7b128762008-07-24 12:17:14 -04002298
Yan, Zhengd68fc572010-05-16 10:49:58 -04002299 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002300}
2301
2302/*
2303 * this cleans up any orphans that may be left on the list from the last use
2304 * of this root.
2305 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002306int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002307{
2308 struct btrfs_path *path;
2309 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002310 struct btrfs_key key, found_key;
2311 struct btrfs_trans_handle *trans;
2312 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002313 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002314 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2315
Yan, Zhengd68fc572010-05-16 10:49:58 -04002316 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002317 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002318
2319 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002320 if (!path) {
2321 ret = -ENOMEM;
2322 goto out;
2323 }
Josef Bacik7b128762008-07-24 12:17:14 -04002324 path->reada = -1;
2325
2326 key.objectid = BTRFS_ORPHAN_OBJECTID;
2327 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2328 key.offset = (u64)-1;
2329
Josef Bacik7b128762008-07-24 12:17:14 -04002330 while (1) {
2331 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002332 if (ret < 0)
2333 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002334
2335 /*
2336 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002337 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002338 * find the key and see if we have stuff that matches
2339 */
2340 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002341 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002342 if (path->slots[0] == 0)
2343 break;
2344 path->slots[0]--;
2345 }
2346
2347 /* pull out the item */
2348 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002349 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2350
2351 /* make sure the item matches what we want */
2352 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2353 break;
2354 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2355 break;
2356
2357 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002358 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002359
2360 /*
2361 * this is where we are basically btrfs_lookup, without the
2362 * crossing root thing. we store the inode number in the
2363 * offset of the orphan item.
2364 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002365
2366 if (found_key.offset == last_objectid) {
2367 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2368 "stopping orphan cleanup\n");
2369 ret = -EINVAL;
2370 goto out;
2371 }
2372
2373 last_objectid = found_key.offset;
2374
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002375 found_key.objectid = found_key.offset;
2376 found_key.type = BTRFS_INODE_ITEM_KEY;
2377 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002378 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002379 ret = PTR_RET(inode);
2380 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002381 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002382
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002383 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2384 struct btrfs_root *dead_root;
2385 struct btrfs_fs_info *fs_info = root->fs_info;
2386 int is_dead_root = 0;
2387
2388 /*
2389 * this is an orphan in the tree root. Currently these
2390 * could come from 2 sources:
2391 * a) a snapshot deletion in progress
2392 * b) a free space cache inode
2393 * We need to distinguish those two, as the snapshot
2394 * orphan must not get deleted.
2395 * find_dead_roots already ran before us, so if this
2396 * is a snapshot deletion, we should find the root
2397 * in the dead_roots list
2398 */
2399 spin_lock(&fs_info->trans_lock);
2400 list_for_each_entry(dead_root, &fs_info->dead_roots,
2401 root_list) {
2402 if (dead_root->root_key.objectid ==
2403 found_key.objectid) {
2404 is_dead_root = 1;
2405 break;
2406 }
2407 }
2408 spin_unlock(&fs_info->trans_lock);
2409 if (is_dead_root) {
2410 /* prevent this orphan from being found again */
2411 key.offset = found_key.objectid - 1;
2412 continue;
2413 }
2414 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002415 /*
2416 * Inode is already gone but the orphan item is still there,
2417 * kill the orphan item.
2418 */
2419 if (ret == -ESTALE) {
2420 trans = btrfs_start_transaction(root, 1);
2421 if (IS_ERR(trans)) {
2422 ret = PTR_ERR(trans);
2423 goto out;
2424 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002425 printk(KERN_ERR "auto deleting %Lu\n",
2426 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002427 ret = btrfs_del_orphan_item(trans, root,
2428 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002429 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002430 btrfs_end_transaction(trans, root);
2431 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002432 }
Josef Bacik7b128762008-07-24 12:17:14 -04002433
Josef Bacik7b128762008-07-24 12:17:14 -04002434 /*
2435 * add this inode to the orphan list so btrfs_orphan_del does
2436 * the proper thing when we hit it
2437 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002438 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2439 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002440
Josef Bacik7b128762008-07-24 12:17:14 -04002441 /* if we have links, this was a truncate, lets do that */
2442 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002443 if (!S_ISREG(inode->i_mode)) {
2444 WARN_ON(1);
2445 iput(inode);
2446 continue;
2447 }
Josef Bacik7b128762008-07-24 12:17:14 -04002448 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002449 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002450 } else {
2451 nr_unlink++;
2452 }
2453
2454 /* this will do delete_inode and everything for us */
2455 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002456 if (ret)
2457 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002458 }
Miao Xie3254c872011-11-10 20:45:05 -05002459 /* release the path since we're done with it */
2460 btrfs_release_path(path);
2461
Yan, Zhengd68fc572010-05-16 10:49:58 -04002462 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2463
2464 if (root->orphan_block_rsv)
2465 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2466 (u64)-1);
2467
2468 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002469 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002470 if (!IS_ERR(trans))
2471 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002472 }
Josef Bacik7b128762008-07-24 12:17:14 -04002473
2474 if (nr_unlink)
2475 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2476 if (nr_truncate)
2477 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002478
2479out:
2480 if (ret)
2481 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2482 btrfs_free_path(path);
2483 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002484}
2485
Chris Masond352ac62008-09-29 15:18:18 -04002486/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002487 * very simple check to peek ahead in the leaf looking for xattrs. If we
2488 * don't find any xattrs, we know there can't be any acls.
2489 *
2490 * slot is the slot the inode is in, objectid is the objectid of the inode
2491 */
2492static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2493 int slot, u64 objectid)
2494{
2495 u32 nritems = btrfs_header_nritems(leaf);
2496 struct btrfs_key found_key;
2497 int scanned = 0;
2498
2499 slot++;
2500 while (slot < nritems) {
2501 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2502
2503 /* we found a different objectid, there must not be acls */
2504 if (found_key.objectid != objectid)
2505 return 0;
2506
2507 /* we found an xattr, assume we've got an acl */
2508 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2509 return 1;
2510
2511 /*
2512 * we found a key greater than an xattr key, there can't
2513 * be any acls later on
2514 */
2515 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2516 return 0;
2517
2518 slot++;
2519 scanned++;
2520
2521 /*
2522 * it goes inode, inode backrefs, xattrs, extents,
2523 * so if there are a ton of hard links to an inode there can
2524 * be a lot of backrefs. Don't waste time searching too hard,
2525 * this is just an optimization
2526 */
2527 if (scanned >= 8)
2528 break;
2529 }
2530 /* we hit the end of the leaf before we found an xattr or
2531 * something larger than an xattr. We have to assume the inode
2532 * has acls
2533 */
2534 return 1;
2535}
2536
2537/*
Chris Masond352ac62008-09-29 15:18:18 -04002538 * read an inode from the btree into the in-memory inode
2539 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002540static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002541{
2542 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002543 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002544 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002545 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002546 struct btrfs_root *root = BTRFS_I(inode)->root;
2547 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002548 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002549 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002550 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002551 bool filled = false;
2552
2553 ret = btrfs_fill_inode(inode, &rdev);
2554 if (!ret)
2555 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002556
2557 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002558 if (!path)
2559 goto make_bad;
2560
Josef Bacikd90c7322011-05-17 09:50:54 -04002561 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002562 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002563
Chris Mason39279cc2007-06-12 06:35:45 -04002564 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002565 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002566 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002567
Chris Mason5f39d392007-10-15 16:14:19 -04002568 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002569
2570 if (filled)
2571 goto cache_acl;
2572
Chris Mason5f39d392007-10-15 16:14:19 -04002573 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2574 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002575 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002576 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002577 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2578 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002579 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002580
2581 tspec = btrfs_inode_atime(inode_item);
2582 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2583 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2584
2585 tspec = btrfs_inode_mtime(inode_item);
2586 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2587 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2588
2589 tspec = btrfs_inode_ctime(inode_item);
2590 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2591 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2592
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002593 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002594 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04002595 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2596
2597 /*
2598 * If we were modified in the current generation and evicted from memory
2599 * and then re-read we need to do a full sync since we don't have any
2600 * idea about which extents were modified before we were evicted from
2601 * cache.
2602 */
2603 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2604 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2605 &BTRFS_I(inode)->runtime_flags);
2606
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002607 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002608 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002609 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002610 rdev = btrfs_inode_rdev(leaf, inode_item);
2611
Josef Bacikaec74772008-07-24 12:12:38 -04002612 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002613 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002614cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002615 /*
2616 * try to precache a NULL acl entry for files that don't have
2617 * any xattrs or acls
2618 */
Li Zefan33345d012011-04-20 10:31:50 +08002619 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2620 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002621 if (!maybe_acls)
2622 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002623
Chris Mason39279cc2007-06-12 06:35:45 -04002624 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002625
Chris Mason39279cc2007-06-12 06:35:45 -04002626 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002627 case S_IFREG:
2628 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002629 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002630 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002631 inode->i_fop = &btrfs_file_operations;
2632 inode->i_op = &btrfs_file_inode_operations;
2633 break;
2634 case S_IFDIR:
2635 inode->i_fop = &btrfs_dir_file_operations;
2636 if (root == root->fs_info->tree_root)
2637 inode->i_op = &btrfs_dir_ro_inode_operations;
2638 else
2639 inode->i_op = &btrfs_dir_inode_operations;
2640 break;
2641 case S_IFLNK:
2642 inode->i_op = &btrfs_symlink_inode_operations;
2643 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002644 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002645 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002646 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002647 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002648 init_special_inode(inode, inode->i_mode, rdev);
2649 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002650 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002651
2652 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002653 return;
2654
2655make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002656 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002657 make_bad_inode(inode);
2658}
2659
Chris Masond352ac62008-09-29 15:18:18 -04002660/*
2661 * given a leaf and an inode, copy the inode fields into the leaf
2662 */
Chris Masone02119d2008-09-05 16:13:11 -04002663static void fill_inode_item(struct btrfs_trans_handle *trans,
2664 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002665 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002666 struct inode *inode)
2667{
Chris Mason5f39d392007-10-15 16:14:19 -04002668 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2669 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002670 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002671 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2672 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2673
2674 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2675 inode->i_atime.tv_sec);
2676 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2677 inode->i_atime.tv_nsec);
2678
2679 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2680 inode->i_mtime.tv_sec);
2681 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2682 inode->i_mtime.tv_nsec);
2683
2684 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2685 inode->i_ctime.tv_sec);
2686 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2687 inode->i_ctime.tv_nsec);
2688
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002689 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002690 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002691 btrfs_set_inode_sequence(leaf, item, inode->i_version);
Chris Masone02119d2008-09-05 16:13:11 -04002692 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002693 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002694 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04002695 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002696}
2697
Chris Masond352ac62008-09-29 15:18:18 -04002698/*
2699 * copy everything in the in-memory inode into the btree.
2700 */
Chris Mason21151332011-11-10 20:39:08 -05002701static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002702 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002703{
2704 struct btrfs_inode_item *inode_item;
2705 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002706 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002707 int ret;
2708
2709 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002710 if (!path)
2711 return -ENOMEM;
2712
Chris Masonb9473432009-03-13 11:00:37 -04002713 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002714 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2715 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002716 if (ret) {
2717 if (ret > 0)
2718 ret = -ENOENT;
2719 goto failed;
2720 }
2721
Chris Masonb4ce94d2009-02-04 09:25:08 -05002722 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002723 leaf = path->nodes[0];
2724 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002725 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002726
Chris Masone02119d2008-09-05 16:13:11 -04002727 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002728 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002729 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002730 ret = 0;
2731failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002732 btrfs_free_path(path);
2733 return ret;
2734}
2735
Chris Masond352ac62008-09-29 15:18:18 -04002736/*
Chris Mason21151332011-11-10 20:39:08 -05002737 * copy everything in the in-memory inode into the btree.
2738 */
2739noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2740 struct btrfs_root *root, struct inode *inode)
2741{
2742 int ret;
2743
2744 /*
2745 * If the inode is a free space inode, we can deadlock during commit
2746 * if we put it into the delayed code.
2747 *
2748 * The data relocation inode should also be directly updated
2749 * without delay
2750 */
Liu Bo83eea1f2012-07-10 05:28:39 -06002751 if (!btrfs_is_free_space_inode(inode)
Chris Mason21151332011-11-10 20:39:08 -05002752 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02002753 btrfs_update_root_times(trans, root);
2754
Chris Mason21151332011-11-10 20:39:08 -05002755 ret = btrfs_delayed_update_inode(trans, root, inode);
2756 if (!ret)
2757 btrfs_set_inode_last_trans(trans, inode);
2758 return ret;
2759 }
2760
2761 return btrfs_update_inode_item(trans, root, inode);
2762}
2763
2764static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2765 struct btrfs_root *root, struct inode *inode)
2766{
2767 int ret;
2768
2769 ret = btrfs_update_inode(trans, root, inode);
2770 if (ret == -ENOSPC)
2771 return btrfs_update_inode_item(trans, root, inode);
2772 return ret;
2773}
2774
2775/*
Chris Masond352ac62008-09-29 15:18:18 -04002776 * unlink helper that gets used here in inode.c and in the tree logging
2777 * recovery code. It remove a link in a directory with a given name, and
2778 * also drops the back refs in the inode to the directory
2779 */
Al Viro92986792011-03-04 17:14:37 +00002780static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2781 struct btrfs_root *root,
2782 struct inode *dir, struct inode *inode,
2783 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002784{
2785 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002786 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002787 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002788 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002789 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002790 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002791 u64 ino = btrfs_ino(inode);
2792 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002793
2794 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002795 if (!path) {
2796 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002797 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002798 }
2799
Chris Masonb9473432009-03-13 11:00:37 -04002800 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002801 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002802 name, name_len, -1);
2803 if (IS_ERR(di)) {
2804 ret = PTR_ERR(di);
2805 goto err;
2806 }
2807 if (!di) {
2808 ret = -ENOENT;
2809 goto err;
2810 }
Chris Mason5f39d392007-10-15 16:14:19 -04002811 leaf = path->nodes[0];
2812 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002813 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002814 if (ret)
2815 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002816 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002817
Li Zefan33345d012011-04-20 10:31:50 +08002818 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2819 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002820 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002821 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002822 "inode %llu parent %llu\n", name_len, name,
2823 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002824 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002825 goto err;
2826 }
2827
Miao Xie16cdcec2011-04-22 18:12:22 +08002828 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002829 if (ret) {
2830 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002831 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002832 }
Chris Mason39279cc2007-06-12 06:35:45 -04002833
Chris Masone02119d2008-09-05 16:13:11 -04002834 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002835 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002836 if (ret != 0 && ret != -ENOENT) {
2837 btrfs_abort_transaction(trans, root, ret);
2838 goto err;
2839 }
Chris Masone02119d2008-09-05 16:13:11 -04002840
2841 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2842 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002843 if (ret == -ENOENT)
2844 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002845err:
2846 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002847 if (ret)
2848 goto out;
2849
2850 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002851 inode_inc_iversion(inode);
2852 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002853 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Tsutomu Itohb9959292012-06-25 21:25:22 -06002854 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002855out:
Chris Mason39279cc2007-06-12 06:35:45 -04002856 return ret;
2857}
2858
Al Viro92986792011-03-04 17:14:37 +00002859int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2860 struct btrfs_root *root,
2861 struct inode *dir, struct inode *inode,
2862 const char *name, int name_len)
2863{
2864 int ret;
2865 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2866 if (!ret) {
2867 btrfs_drop_nlink(inode);
2868 ret = btrfs_update_inode(trans, root, inode);
2869 }
2870 return ret;
2871}
2872
2873
Yan, Zhenga22285a2010-05-16 10:48:46 -04002874/* helper to check if there is any shared block in the path */
2875static int check_path_shared(struct btrfs_root *root,
2876 struct btrfs_path *path)
2877{
2878 struct extent_buffer *eb;
2879 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002880 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002881
2882 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002883 int ret;
2884
Yan, Zhenga22285a2010-05-16 10:48:46 -04002885 if (!path->nodes[level])
2886 break;
2887 eb = path->nodes[level];
2888 if (!btrfs_block_can_be_shared(root, eb))
2889 continue;
2890 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2891 &refs, NULL);
2892 if (refs > 1)
2893 return 1;
2894 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002895 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002896}
2897
2898/*
2899 * helper to start transaction for unlink and rmdir.
2900 *
2901 * unlink and rmdir are special in btrfs, they do not always free space.
2902 * so in enospc case, we should make sure they will free space before
2903 * allowing them to use the global metadata reservation.
2904 */
2905static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2906 struct dentry *dentry)
2907{
2908 struct btrfs_trans_handle *trans;
2909 struct btrfs_root *root = BTRFS_I(dir)->root;
2910 struct btrfs_path *path;
2911 struct btrfs_inode_ref *ref;
2912 struct btrfs_dir_item *di;
2913 struct inode *inode = dentry->d_inode;
2914 u64 index;
2915 int check_link = 1;
2916 int err = -ENOSPC;
2917 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002918 u64 ino = btrfs_ino(inode);
2919 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002920
Josef Bacike70bea52011-10-11 14:18:24 -04002921 /*
2922 * 1 for the possible orphan item
2923 * 1 for the dir item
2924 * 1 for the dir index
2925 * 1 for the inode ref
2926 * 1 for the inode ref in the tree log
2927 * 2 for the dir entries in the log
2928 * 1 for the inode
2929 */
2930 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002931 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2932 return trans;
2933
Li Zefan33345d012011-04-20 10:31:50 +08002934 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002935 return ERR_PTR(-ENOSPC);
2936
2937 /* check if there is someone else holds reference */
2938 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2939 return ERR_PTR(-ENOSPC);
2940
2941 if (atomic_read(&inode->i_count) > 2)
2942 return ERR_PTR(-ENOSPC);
2943
2944 if (xchg(&root->fs_info->enospc_unlink, 1))
2945 return ERR_PTR(-ENOSPC);
2946
2947 path = btrfs_alloc_path();
2948 if (!path) {
2949 root->fs_info->enospc_unlink = 0;
2950 return ERR_PTR(-ENOMEM);
2951 }
2952
Josef Bacik3880a1b2011-10-14 14:46:51 -04002953 /* 1 for the orphan item */
2954 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002955 if (IS_ERR(trans)) {
2956 btrfs_free_path(path);
2957 root->fs_info->enospc_unlink = 0;
2958 return trans;
2959 }
2960
2961 path->skip_locking = 1;
2962 path->search_commit_root = 1;
2963
2964 ret = btrfs_lookup_inode(trans, root, path,
2965 &BTRFS_I(dir)->location, 0);
2966 if (ret < 0) {
2967 err = ret;
2968 goto out;
2969 }
2970 if (ret == 0) {
2971 if (check_path_shared(root, path))
2972 goto out;
2973 } else {
2974 check_link = 0;
2975 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002976 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002977
2978 ret = btrfs_lookup_inode(trans, root, path,
2979 &BTRFS_I(inode)->location, 0);
2980 if (ret < 0) {
2981 err = ret;
2982 goto out;
2983 }
2984 if (ret == 0) {
2985 if (check_path_shared(root, path))
2986 goto out;
2987 } else {
2988 check_link = 0;
2989 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002990 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002991
2992 if (ret == 0 && S_ISREG(inode->i_mode)) {
2993 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002994 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002995 if (ret < 0) {
2996 err = ret;
2997 goto out;
2998 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002999 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003000 if (check_path_shared(root, path))
3001 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02003002 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003003 }
3004
3005 if (!check_link) {
3006 err = 0;
3007 goto out;
3008 }
3009
Li Zefan33345d012011-04-20 10:31:50 +08003010 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003011 dentry->d_name.name, dentry->d_name.len, 0);
3012 if (IS_ERR(di)) {
3013 err = PTR_ERR(di);
3014 goto out;
3015 }
3016 if (di) {
3017 if (check_path_shared(root, path))
3018 goto out;
3019 } else {
3020 err = 0;
3021 goto out;
3022 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003023 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003024
3025 ref = btrfs_lookup_inode_ref(trans, root, path,
3026 dentry->d_name.name, dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08003027 ino, dir_ino, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003028 if (IS_ERR(ref)) {
3029 err = PTR_ERR(ref);
3030 goto out;
3031 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003032 BUG_ON(!ref); /* Logic error */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003033 if (check_path_shared(root, path))
3034 goto out;
3035 index = btrfs_inode_ref_index(path->nodes[0], ref);
David Sterbab3b4aa72011-04-21 01:20:15 +02003036 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003037
Miao Xie16cdcec2011-04-22 18:12:22 +08003038 /*
3039 * This is a commit root search, if we can lookup inode item and other
3040 * relative items in the commit root, it means the transaction of
3041 * dir/file creation has been committed, and the dir index item that we
3042 * delay to insert has also been inserted into the commit root. So
3043 * we needn't worry about the delayed insertion of the dir index item
3044 * here.
3045 */
Li Zefan33345d012011-04-20 10:31:50 +08003046 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003047 dentry->d_name.name, dentry->d_name.len, 0);
3048 if (IS_ERR(di)) {
3049 err = PTR_ERR(di);
3050 goto out;
3051 }
3052 BUG_ON(ret == -ENOENT);
3053 if (check_path_shared(root, path))
3054 goto out;
3055
3056 err = 0;
3057out:
3058 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003059 /* Migrate the orphan reservation over */
3060 if (!err)
3061 err = btrfs_block_rsv_migrate(trans->block_rsv,
3062 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003063 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003064
Yan, Zhenga22285a2010-05-16 10:48:46 -04003065 if (err) {
3066 btrfs_end_transaction(trans, root);
3067 root->fs_info->enospc_unlink = 0;
3068 return ERR_PTR(err);
3069 }
3070
3071 trans->block_rsv = &root->fs_info->global_block_rsv;
3072 return trans;
3073}
3074
3075static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3076 struct btrfs_root *root)
3077{
3078 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003079 btrfs_block_rsv_release(root, trans->block_rsv,
3080 trans->bytes_reserved);
3081 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003082 BUG_ON(!root->fs_info->enospc_unlink);
3083 root->fs_info->enospc_unlink = 0;
3084 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003085 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003086}
3087
Chris Mason39279cc2007-06-12 06:35:45 -04003088static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3089{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003090 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003091 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003092 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003093 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05003094 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003095
Yan, Zhenga22285a2010-05-16 10:48:46 -04003096 trans = __unlink_start_trans(dir, dentry);
3097 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003098 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003099
Chris Mason12fcfd22009-03-24 10:24:20 -04003100 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3101
Chris Masone02119d2008-09-05 16:13:11 -04003102 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3103 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003104 if (ret)
3105 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003106
Yan, Zhenga22285a2010-05-16 10:48:46 -04003107 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003108 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003109 if (ret)
3110 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003111 }
Josef Bacik7b128762008-07-24 12:17:14 -04003112
Tsutomu Itohb5324022011-07-19 07:27:20 +00003113out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003114 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003115 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003116 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003117 return ret;
3118}
3119
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003120int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3121 struct btrfs_root *root,
3122 struct inode *dir, u64 objectid,
3123 const char *name, int name_len)
3124{
3125 struct btrfs_path *path;
3126 struct extent_buffer *leaf;
3127 struct btrfs_dir_item *di;
3128 struct btrfs_key key;
3129 u64 index;
3130 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003131 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003132
3133 path = btrfs_alloc_path();
3134 if (!path)
3135 return -ENOMEM;
3136
Li Zefan33345d012011-04-20 10:31:50 +08003137 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003138 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003139 if (IS_ERR_OR_NULL(di)) {
3140 if (!di)
3141 ret = -ENOENT;
3142 else
3143 ret = PTR_ERR(di);
3144 goto out;
3145 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003146
3147 leaf = path->nodes[0];
3148 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3149 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3150 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003151 if (ret) {
3152 btrfs_abort_transaction(trans, root, ret);
3153 goto out;
3154 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003155 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003156
3157 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3158 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003159 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003160 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003161 if (ret != -ENOENT) {
3162 btrfs_abort_transaction(trans, root, ret);
3163 goto out;
3164 }
Li Zefan33345d012011-04-20 10:31:50 +08003165 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003166 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003167 if (IS_ERR_OR_NULL(di)) {
3168 if (!di)
3169 ret = -ENOENT;
3170 else
3171 ret = PTR_ERR(di);
3172 btrfs_abort_transaction(trans, root, ret);
3173 goto out;
3174 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003175
3176 leaf = path->nodes[0];
3177 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003178 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003179 index = key.offset;
3180 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003181 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003182
Miao Xie16cdcec2011-04-22 18:12:22 +08003183 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003184 if (ret) {
3185 btrfs_abort_transaction(trans, root, ret);
3186 goto out;
3187 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003188
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003189 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003190 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003191 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Josef Bacik5a24e842012-08-08 10:12:59 -06003192 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003193 if (ret)
3194 btrfs_abort_transaction(trans, root, ret);
3195out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003196 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003197 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003198}
3199
Chris Mason39279cc2007-06-12 06:35:45 -04003200static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3201{
3202 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003203 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003204 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003205 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003206 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003207
Chris Mason3394e162008-11-17 20:42:26 -05003208 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Li Zefan33345d012011-04-20 10:31:50 +08003209 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003210 return -ENOTEMPTY;
3211
Yan, Zhenga22285a2010-05-16 10:48:46 -04003212 trans = __unlink_start_trans(dir, dentry);
3213 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003214 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003215
Li Zefan33345d012011-04-20 10:31:50 +08003216 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003217 err = btrfs_unlink_subvol(trans, root, dir,
3218 BTRFS_I(inode)->location.objectid,
3219 dentry->d_name.name,
3220 dentry->d_name.len);
3221 goto out;
3222 }
3223
Josef Bacik7b128762008-07-24 12:17:14 -04003224 err = btrfs_orphan_add(trans, inode);
3225 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003226 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003227
Chris Mason39279cc2007-06-12 06:35:45 -04003228 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003229 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3230 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003231 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003232 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003233out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003234 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003235 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003236 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003237
Chris Mason39279cc2007-06-12 06:35:45 -04003238 return err;
3239}
3240
Chris Mason323ac952008-10-01 19:05:46 -04003241/*
Chris Mason39279cc2007-06-12 06:35:45 -04003242 * this can truncate away extent items, csum items and directory items.
3243 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003244 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003245 *
3246 * csum items that cross the new i_size are truncated to the new size
3247 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003248 *
3249 * min_type is the minimum key type to truncate down to. If set to 0, this
3250 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003251 */
Yan, Zheng80825102009-11-12 09:35:36 +00003252int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3253 struct btrfs_root *root,
3254 struct inode *inode,
3255 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003256{
Chris Mason39279cc2007-06-12 06:35:45 -04003257 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003258 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003259 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003260 struct btrfs_key key;
3261 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003262 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003263 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003264 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003265 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003266 u64 mask = root->sectorsize - 1;
3267 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003268 int found_extent;
3269 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003270 int pending_del_nr = 0;
3271 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003272 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003273 int ret;
3274 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003275 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003276
3277 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003278
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003279 path = btrfs_alloc_path();
3280 if (!path)
3281 return -ENOMEM;
3282 path->reada = -1;
3283
Josef Bacik5dc562c2012-08-17 13:14:17 -04003284 /*
3285 * We want to drop from the next block forward in case this new size is
3286 * not block aligned since we will be keeping the last block of the
3287 * extent just the way it is.
3288 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003289 if (root->ref_cows || root == root->fs_info->tree_root)
Josef Bacik5dc562c2012-08-17 13:14:17 -04003290 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003291
Miao Xie16cdcec2011-04-22 18:12:22 +08003292 /*
3293 * This function is also used to drop the items in the log tree before
3294 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3295 * it is used to drop the loged items. So we shouldn't kill the delayed
3296 * items.
3297 */
3298 if (min_type == 0 && root == BTRFS_I(inode)->root)
3299 btrfs_kill_delayed_inode_items(inode);
3300
Li Zefan33345d012011-04-20 10:31:50 +08003301 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003302 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003303 key.type = (u8)-1;
3304
Chris Mason85e21ba2008-01-29 15:11:36 -05003305search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003306 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003307 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003308 if (ret < 0) {
3309 err = ret;
3310 goto out;
3311 }
Chris Masond3977122009-01-05 21:25:51 -05003312
Chris Mason85e21ba2008-01-29 15:11:36 -05003313 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003314 /* there are no items in the tree for us to truncate, we're
3315 * done
3316 */
Yan, Zheng80825102009-11-12 09:35:36 +00003317 if (path->slots[0] == 0)
3318 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003319 path->slots[0]--;
3320 }
3321
Chris Masond3977122009-01-05 21:25:51 -05003322 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003323 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003324 leaf = path->nodes[0];
3325 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3326 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003327
Li Zefan33345d012011-04-20 10:31:50 +08003328 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003329 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003330
Chris Mason85e21ba2008-01-29 15:11:36 -05003331 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003332 break;
3333
Chris Mason5f39d392007-10-15 16:14:19 -04003334 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003335 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003336 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003337 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003338 extent_type = btrfs_file_extent_type(leaf, fi);
3339 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003340 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003341 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003342 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003343 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003344 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003345 }
Yan008630c2007-11-07 13:31:09 -05003346 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003347 }
Yan, Zheng80825102009-11-12 09:35:36 +00003348 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003349 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003350 } else {
3351 if (item_end < new_size)
3352 break;
3353 if (found_key.offset >= new_size)
3354 del_item = 1;
3355 else
3356 del_item = 0;
3357 }
Chris Mason39279cc2007-06-12 06:35:45 -04003358 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003359 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003360 if (found_type != BTRFS_EXTENT_DATA_KEY)
3361 goto delete;
3362
3363 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003364 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003365 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003366 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003367 u64 orig_num_bytes =
3368 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003369 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003370 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003371 extent_num_bytes = extent_num_bytes &
3372 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003373 btrfs_set_file_extent_num_bytes(leaf, fi,
3374 extent_num_bytes);
3375 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003376 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003377 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003378 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003379 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003380 } else {
Chris Masondb945352007-10-15 16:15:53 -04003381 extent_num_bytes =
3382 btrfs_file_extent_disk_num_bytes(leaf,
3383 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003384 extent_offset = found_key.offset -
3385 btrfs_file_extent_offset(leaf, fi);
3386
Chris Mason39279cc2007-06-12 06:35:45 -04003387 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003388 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003389 if (extent_start != 0) {
3390 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003391 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003392 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003393 }
3394 }
Chris Mason90692182008-02-08 13:49:28 -05003395 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003396 /*
3397 * we can't truncate inline items that have had
3398 * special encodings
3399 */
3400 if (!del_item &&
3401 btrfs_file_extent_compression(leaf, fi) == 0 &&
3402 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3403 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003404 u32 size = new_size - found_key.offset;
3405
3406 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003407 inode_sub_bytes(inode, item_end + 1 -
3408 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003409 }
3410 size =
3411 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003412 btrfs_truncate_item(trans, root, path,
3413 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003414 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003415 inode_sub_bytes(inode, item_end + 1 -
3416 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003417 }
Chris Mason39279cc2007-06-12 06:35:45 -04003418 }
Chris Mason179e29e2007-11-01 11:28:41 -04003419delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003420 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003421 if (!pending_del_nr) {
3422 /* no pending yet, add ourselves */
3423 pending_del_slot = path->slots[0];
3424 pending_del_nr = 1;
3425 } else if (pending_del_nr &&
3426 path->slots[0] + 1 == pending_del_slot) {
3427 /* hop on the pending chunk */
3428 pending_del_nr++;
3429 pending_del_slot = path->slots[0];
3430 } else {
Chris Masond3977122009-01-05 21:25:51 -05003431 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003432 }
Chris Mason39279cc2007-06-12 06:35:45 -04003433 } else {
3434 break;
3435 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003436 if (found_extent && (root->ref_cows ||
3437 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003438 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003439 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003440 extent_num_bytes, 0,
3441 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003442 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003443 BUG_ON(ret);
3444 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003445
Yan, Zheng80825102009-11-12 09:35:36 +00003446 if (found_type == BTRFS_INODE_ITEM_KEY)
3447 break;
3448
3449 if (path->slots[0] == 0 ||
3450 path->slots[0] != pending_del_slot) {
Li Zefan82d59022011-04-20 10:33:24 +08003451 if (root->ref_cows &&
3452 BTRFS_I(inode)->location.objectid !=
3453 BTRFS_FREE_INO_OBJECTID) {
Yan, Zheng80825102009-11-12 09:35:36 +00003454 err = -EAGAIN;
3455 goto out;
3456 }
3457 if (pending_del_nr) {
3458 ret = btrfs_del_items(trans, root, path,
3459 pending_del_slot,
3460 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003461 if (ret) {
3462 btrfs_abort_transaction(trans,
3463 root, ret);
3464 goto error;
3465 }
Yan, Zheng80825102009-11-12 09:35:36 +00003466 pending_del_nr = 0;
3467 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003468 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003469 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003470 } else {
3471 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003472 }
Chris Mason39279cc2007-06-12 06:35:45 -04003473 }
Yan, Zheng80825102009-11-12 09:35:36 +00003474out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003475 if (pending_del_nr) {
3476 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3477 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003478 if (ret)
3479 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003480 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003481error:
Chris Mason39279cc2007-06-12 06:35:45 -04003482 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003483 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003484}
3485
Chris Masona52d9a82007-08-27 16:49:44 -04003486/*
3487 * taken from block_truncate_page, but does cow as it zeros out
3488 * any bytes left in the last page in the file.
3489 */
3490static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3491{
3492 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003493 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003494 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3495 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003496 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003497 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003498 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003499 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3500 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3501 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003502 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003503 int ret = 0;
3504 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003505 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003506
3507 if ((offset & (blocksize - 1)) == 0)
3508 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003509 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003510 if (ret)
3511 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003512
3513 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003514again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003515 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003516 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003517 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003518 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003519 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003520
3521 page_start = page_offset(page);
3522 page_end = page_start + PAGE_CACHE_SIZE - 1;
3523
Chris Masona52d9a82007-08-27 16:49:44 -04003524 if (!PageUptodate(page)) {
3525 ret = btrfs_readpage(NULL, page);
3526 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003527 if (page->mapping != mapping) {
3528 unlock_page(page);
3529 page_cache_release(page);
3530 goto again;
3531 }
Chris Masona52d9a82007-08-27 16:49:44 -04003532 if (!PageUptodate(page)) {
3533 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003534 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003535 }
3536 }
Chris Mason211c17f2008-05-15 09:13:45 -04003537 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003538
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003539 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003540 set_page_extent_mapped(page);
3541
3542 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3543 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003544 unlock_extent_cached(io_tree, page_start, page_end,
3545 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003546 unlock_page(page);
3547 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003548 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003549 btrfs_put_ordered_extent(ordered);
3550 goto again;
3551 }
3552
Josef Bacik2ac55d42010-02-03 19:33:23 +00003553 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003554 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003555 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003556
Josef Bacik2ac55d42010-02-03 19:33:23 +00003557 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3558 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003559 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003560 unlock_extent_cached(io_tree, page_start, page_end,
3561 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003562 goto out_unlock;
3563 }
3564
Chris Masone6dcd2d2008-07-17 12:53:50 -04003565 ret = 0;
3566 if (offset != PAGE_CACHE_SIZE) {
3567 kaddr = kmap(page);
3568 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3569 flush_dcache_page(page);
3570 kunmap(page);
3571 }
Chris Mason247e7432008-07-17 12:53:51 -04003572 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003573 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003574 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3575 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003576
Chris Mason89642222008-07-24 09:41:53 -04003577out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003578 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003579 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003580 unlock_page(page);
3581 page_cache_release(page);
3582out:
3583 return ret;
3584}
3585
Josef Bacik695a0d02011-03-04 15:46:53 -05003586/*
3587 * This function puts in dummy file extents for the area we're creating a hole
3588 * for. So if we are truncating this file to a larger size we need to insert
3589 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3590 * the range between oldsize and size
3591 */
Josef Bacika41ad392011-01-31 15:30:16 -05003592int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003593{
3594 struct btrfs_trans_handle *trans;
3595 struct btrfs_root *root = BTRFS_I(inode)->root;
3596 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003597 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003598 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04003599 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan Zheng9036c102008-10-30 14:19:41 -04003600 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003601 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003602 u64 block_end = (size + mask) & ~mask;
3603 u64 last_byte;
3604 u64 cur_offset;
3605 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003606 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003607
3608 if (size <= hole_start)
3609 return 0;
3610
Yan Zheng9036c102008-10-30 14:19:41 -04003611 while (1) {
3612 struct btrfs_ordered_extent *ordered;
3613 btrfs_wait_ordered_range(inode, hole_start,
3614 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003615 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003616 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003617 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3618 if (!ordered)
3619 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003620 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3621 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003622 btrfs_put_ordered_extent(ordered);
3623 }
3624
Yan Zheng9036c102008-10-30 14:19:41 -04003625 cur_offset = hole_start;
3626 while (1) {
3627 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3628 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003629 if (IS_ERR(em)) {
3630 err = PTR_ERR(em);
3631 break;
3632 }
Yan Zheng9036c102008-10-30 14:19:41 -04003633 last_byte = min(extent_map_end(em), block_end);
3634 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003635 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04003636 struct extent_map *hole_em;
Chris Mason771ed682008-11-06 22:02:51 -05003637 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003638 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003639
Miao Xie36423202011-12-14 20:12:02 -05003640 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003641 if (IS_ERR(trans)) {
3642 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003643 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003644 }
Yan, Zheng80825102009-11-12 09:35:36 +00003645
Josef Bacik5dc562c2012-08-17 13:14:17 -04003646 err = btrfs_drop_extents(trans, root, inode,
3647 cur_offset,
Yan, Zheng80825102009-11-12 09:35:36 +00003648 cur_offset + hole_size,
3649 &hint_byte, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003650 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003651 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003652 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003653 break;
Miao Xie5b397372011-09-11 10:52:24 -04003654 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003655
Yan Zheng9036c102008-10-30 14:19:41 -04003656 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003657 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003658 0, hole_size, 0, hole_size,
3659 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003660 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003661 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003662 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003663 break;
Miao Xie5b397372011-09-11 10:52:24 -04003664 }
Yan, Zheng80825102009-11-12 09:35:36 +00003665
Josef Bacik5dc562c2012-08-17 13:14:17 -04003666 btrfs_drop_extent_cache(inode, cur_offset,
3667 cur_offset + hole_size - 1, 0);
3668 hole_em = alloc_extent_map();
3669 if (!hole_em) {
3670 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3671 &BTRFS_I(inode)->runtime_flags);
3672 goto next;
3673 }
3674 hole_em->start = cur_offset;
3675 hole_em->len = hole_size;
3676 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003677
Josef Bacik5dc562c2012-08-17 13:14:17 -04003678 hole_em->block_start = EXTENT_MAP_HOLE;
3679 hole_em->block_len = 0;
3680 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3681 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3682 hole_em->generation = trans->transid;
3683
3684 while (1) {
3685 write_lock(&em_tree->lock);
3686 err = add_extent_mapping(em_tree, hole_em);
3687 if (!err)
3688 list_move(&hole_em->list,
3689 &em_tree->modified_extents);
3690 write_unlock(&em_tree->lock);
3691 if (err != -EEXIST)
3692 break;
3693 btrfs_drop_extent_cache(inode, cur_offset,
3694 cur_offset +
3695 hole_size - 1, 0);
3696 }
3697 free_extent_map(hole_em);
3698next:
Miao Xie36423202011-12-14 20:12:02 -05003699 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003700 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003701 }
3702 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003703 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003704 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003705 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003706 break;
3707 }
3708
Yan, Zhenga22285a2010-05-16 10:48:46 -04003709 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003710 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3711 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003712 return err;
3713}
3714
Josef Bacika41ad392011-01-31 15:30:16 -05003715static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003716{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003717 struct btrfs_root *root = BTRFS_I(inode)->root;
3718 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003719 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003720 int ret;
3721
Josef Bacika41ad392011-01-31 15:30:16 -05003722 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003723 return 0;
3724
Josef Bacika41ad392011-01-31 15:30:16 -05003725 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003726 truncate_pagecache(inode, oldsize, newsize);
3727 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003728 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003729 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003730
Miao Xief4a2f4c2011-12-14 20:12:01 -05003731 trans = btrfs_start_transaction(root, 1);
3732 if (IS_ERR(trans))
3733 return PTR_ERR(trans);
3734
3735 i_size_write(inode, newsize);
3736 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3737 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003738 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003739 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003740
Josef Bacika41ad392011-01-31 15:30:16 -05003741 /*
3742 * We're truncating a file that used to have good data down to
3743 * zero. Make sure it gets into the ordered flush list so that
3744 * any new writes get down to disk quickly.
3745 */
3746 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003747 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3748 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003749
Josef Bacika41ad392011-01-31 15:30:16 -05003750 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3751 truncate_setsize(inode, newsize);
3752 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003753 }
3754
Josef Bacika41ad392011-01-31 15:30:16 -05003755 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003756}
3757
Chris Mason39279cc2007-06-12 06:35:45 -04003758static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3759{
3760 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003761 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003762 int err;
3763
Li Zefanb83cc962010-12-20 16:04:08 +08003764 if (btrfs_root_readonly(root))
3765 return -EROFS;
3766
Chris Mason39279cc2007-06-12 06:35:45 -04003767 err = inode_change_ok(inode, attr);
3768 if (err)
3769 return err;
3770
Chris Mason5a3f23d2009-03-31 13:27:11 -04003771 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003772 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003773 if (err)
3774 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003775 }
Yan Zheng9036c102008-10-30 14:19:41 -04003776
Christoph Hellwig10257742010-06-04 11:30:02 +02003777 if (attr->ia_valid) {
3778 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003779 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003780 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003781
Josef Bacik22c44fe2011-11-30 10:45:38 -05003782 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003783 err = btrfs_acl_chmod(inode);
3784 }
3785
Chris Mason39279cc2007-06-12 06:35:45 -04003786 return err;
3787}
Chris Mason61295eb2008-01-14 16:24:38 -05003788
Al Virobd555972010-06-07 11:35:40 -04003789void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003790{
3791 struct btrfs_trans_handle *trans;
3792 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003793 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003794 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003795 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003796 int ret;
3797
liubo1abe9b82011-03-24 11:18:59 +00003798 trace_btrfs_inode_evict(inode);
3799
Chris Mason39279cc2007-06-12 06:35:45 -04003800 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003801 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Liu Bo83eea1f2012-07-10 05:28:39 -06003802 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04003803 goto no_delete;
3804
Chris Mason39279cc2007-06-12 06:35:45 -04003805 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003806 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003807 goto no_delete;
3808 }
Al Virobd555972010-06-07 11:35:40 -04003809 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003810 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003811
Yan, Zhengc71bf092009-11-12 09:34:40 +00003812 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06003813 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04003814 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003815 goto no_delete;
3816 }
3817
Yan, Zheng76dda932009-09-21 16:00:26 -04003818 if (inode->i_nlink > 0) {
3819 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3820 goto no_delete;
3821 }
3822
Josef Bacik4289a662011-08-05 13:22:24 -04003823 rsv = btrfs_alloc_block_rsv(root);
3824 if (!rsv) {
3825 btrfs_orphan_del(NULL, inode);
3826 goto no_delete;
3827 }
Josef Bacik4a338542011-08-29 11:01:31 -04003828 rsv->size = min_size;
Josef Bacik726c35f2011-09-26 15:46:06 -04003829 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003830
Chris Masondbe674a2008-07-17 12:54:05 -04003831 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003832
Josef Bacik4289a662011-08-05 13:22:24 -04003833 /*
3834 * This is a bit simpler than btrfs_truncate since
3835 *
3836 * 1) We've already reserved our space for our orphan item in the
3837 * unlink.
3838 * 2) We're going to delete the inode item, so we don't need to update
3839 * it at all.
3840 *
3841 * So we just need to reserve some slack space in case we add bytes when
3842 * doing the truncate.
3843 */
Yan, Zheng80825102009-11-12 09:35:36 +00003844 while (1) {
Miao Xieaa38a712011-11-18 17:43:00 +08003845 ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003846
Josef Bacik726c35f2011-09-26 15:46:06 -04003847 /*
3848 * Try and steal from the global reserve since we will
3849 * likely not use this space anyway, we want to try as
3850 * hard as possible to get this to work.
3851 */
3852 if (ret)
3853 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3854
Yan, Zhengd68fc572010-05-16 10:49:58 -04003855 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003856 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003857 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003858 btrfs_orphan_del(NULL, inode);
3859 btrfs_free_block_rsv(root, rsv);
3860 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003861 }
3862
Josef Bacik4289a662011-08-05 13:22:24 -04003863 trans = btrfs_start_transaction(root, 0);
3864 if (IS_ERR(trans)) {
3865 btrfs_orphan_del(NULL, inode);
3866 btrfs_free_block_rsv(root, rsv);
3867 goto no_delete;
3868 }
3869
3870 trans->block_rsv = rsv;
3871
Yan, Zhengd68fc572010-05-16 10:49:58 -04003872 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003873 if (ret != -EAGAIN)
3874 break;
3875
3876 nr = trans->blocks_used;
3877 btrfs_end_transaction(trans, root);
3878 trans = NULL;
3879 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003880 }
3881
Josef Bacik4289a662011-08-05 13:22:24 -04003882 btrfs_free_block_rsv(root, rsv);
3883
Yan, Zheng80825102009-11-12 09:35:36 +00003884 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003885 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003886 ret = btrfs_orphan_del(trans, inode);
3887 BUG_ON(ret);
3888 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003889
Josef Bacik4289a662011-08-05 13:22:24 -04003890 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003891 if (!(root == root->fs_info->tree_root ||
3892 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003893 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003894
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003895 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003896 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003897 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003898no_delete:
Jan Karadbd57682012-05-03 14:48:02 +02003899 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003900 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003901}
3902
3903/*
3904 * this returns the key found in the dir entry in the location pointer.
3905 * If no dir entries were found, location->objectid is 0.
3906 */
3907static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3908 struct btrfs_key *location)
3909{
3910 const char *name = dentry->d_name.name;
3911 int namelen = dentry->d_name.len;
3912 struct btrfs_dir_item *di;
3913 struct btrfs_path *path;
3914 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003915 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003916
3917 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003918 if (!path)
3919 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003920
Li Zefan33345d012011-04-20 10:31:50 +08003921 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003922 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003923 if (IS_ERR(di))
3924 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003925
David Sterbac7040052011-04-19 18:00:01 +02003926 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003927 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003928
Chris Mason5f39d392007-10-15 16:14:19 -04003929 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003930out:
Chris Mason39279cc2007-06-12 06:35:45 -04003931 btrfs_free_path(path);
3932 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003933out_err:
3934 location->objectid = 0;
3935 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003936}
3937
3938/*
3939 * when we hit a tree root in a directory, the btrfs part of the inode
3940 * needs to be changed to reflect the root directory of the tree root. This
3941 * is kind of like crossing a mount point.
3942 */
3943static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003944 struct inode *dir,
3945 struct dentry *dentry,
3946 struct btrfs_key *location,
3947 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003948{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003949 struct btrfs_path *path;
3950 struct btrfs_root *new_root;
3951 struct btrfs_root_ref *ref;
3952 struct extent_buffer *leaf;
3953 int ret;
3954 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003955
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003956 path = btrfs_alloc_path();
3957 if (!path) {
3958 err = -ENOMEM;
3959 goto out;
3960 }
Chris Mason39279cc2007-06-12 06:35:45 -04003961
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003962 err = -ENOENT;
3963 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3964 BTRFS_I(dir)->root->root_key.objectid,
3965 location->objectid);
3966 if (ret) {
3967 if (ret < 0)
3968 err = ret;
3969 goto out;
3970 }
Chris Mason39279cc2007-06-12 06:35:45 -04003971
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003972 leaf = path->nodes[0];
3973 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003974 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003975 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3976 goto out;
3977
3978 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3979 (unsigned long)(ref + 1),
3980 dentry->d_name.len);
3981 if (ret)
3982 goto out;
3983
David Sterbab3b4aa72011-04-21 01:20:15 +02003984 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003985
3986 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3987 if (IS_ERR(new_root)) {
3988 err = PTR_ERR(new_root);
3989 goto out;
3990 }
3991
3992 if (btrfs_root_refs(&new_root->root_item) == 0) {
3993 err = -ENOENT;
3994 goto out;
3995 }
3996
3997 *sub_root = new_root;
3998 location->objectid = btrfs_root_dirid(&new_root->root_item);
3999 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04004000 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004001 err = 0;
4002out:
4003 btrfs_free_path(path);
4004 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004005}
4006
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004007static void inode_tree_add(struct inode *inode)
4008{
4009 struct btrfs_root *root = BTRFS_I(inode)->root;
4010 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004011 struct rb_node **p;
4012 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08004013 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004014again:
4015 p = &root->inode_tree.rb_node;
4016 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004017
Al Viro1d3382cb2010-10-23 15:19:20 -04004018 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004019 return;
4020
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004021 spin_lock(&root->inode_lock);
4022 while (*p) {
4023 parent = *p;
4024 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4025
Li Zefan33345d012011-04-20 10:31:50 +08004026 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004027 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004028 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004029 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004030 else {
4031 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04004032 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004033 rb_erase(parent, &root->inode_tree);
4034 RB_CLEAR_NODE(parent);
4035 spin_unlock(&root->inode_lock);
4036 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004037 }
4038 }
4039 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4040 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4041 spin_unlock(&root->inode_lock);
4042}
4043
4044static void inode_tree_del(struct inode *inode)
4045{
4046 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04004047 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004048
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004049 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004050 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004051 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004052 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04004053 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004054 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02004055 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04004056
Josef Bacik0af3d002010-06-21 14:48:16 -04004057 /*
4058 * Free space cache has inodes in the tree root, but the tree root has a
4059 * root_refs of 0, so this could end up dropping the tree root as a
4060 * snapshot, so we need the extra !root->fs_info->tree_root check to
4061 * make sure we don't drop it.
4062 */
4063 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4064 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004065 synchronize_srcu(&root->fs_info->subvol_srcu);
4066 spin_lock(&root->inode_lock);
4067 empty = RB_EMPTY_ROOT(&root->inode_tree);
4068 spin_unlock(&root->inode_lock);
4069 if (empty)
4070 btrfs_add_dead_root(root);
4071 }
4072}
4073
Jeff Mahoney143bede2012-03-01 14:56:26 +01004074void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04004075{
4076 struct rb_node *node;
4077 struct rb_node *prev;
4078 struct btrfs_inode *entry;
4079 struct inode *inode;
4080 u64 objectid = 0;
4081
4082 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4083
4084 spin_lock(&root->inode_lock);
4085again:
4086 node = root->inode_tree.rb_node;
4087 prev = NULL;
4088 while (node) {
4089 prev = node;
4090 entry = rb_entry(node, struct btrfs_inode, rb_node);
4091
Li Zefan33345d012011-04-20 10:31:50 +08004092 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004093 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08004094 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04004095 node = node->rb_right;
4096 else
4097 break;
4098 }
4099 if (!node) {
4100 while (prev) {
4101 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004102 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004103 node = prev;
4104 break;
4105 }
4106 prev = rb_next(prev);
4107 }
4108 }
4109 while (node) {
4110 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004111 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004112 inode = igrab(&entry->vfs_inode);
4113 if (inode) {
4114 spin_unlock(&root->inode_lock);
4115 if (atomic_read(&inode->i_count) > 1)
4116 d_prune_aliases(inode);
4117 /*
Al Viro45321ac2010-06-07 13:43:19 -04004118 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004119 * the inode cache when its usage count
4120 * hits zero.
4121 */
4122 iput(inode);
4123 cond_resched();
4124 spin_lock(&root->inode_lock);
4125 goto again;
4126 }
4127
4128 if (cond_resched_lock(&root->inode_lock))
4129 goto again;
4130
4131 node = rb_next(node);
4132 }
4133 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004134}
4135
Chris Masone02119d2008-09-05 16:13:11 -04004136static int btrfs_init_locked_inode(struct inode *inode, void *p)
4137{
4138 struct btrfs_iget_args *args = p;
4139 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004140 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004141 return 0;
4142}
4143
4144static int btrfs_find_actor(struct inode *inode, void *opaque)
4145{
4146 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004147 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004148 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004149}
4150
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004151static struct inode *btrfs_iget_locked(struct super_block *s,
4152 u64 objectid,
4153 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004154{
4155 struct inode *inode;
4156 struct btrfs_iget_args args;
4157 args.ino = objectid;
4158 args.root = root;
4159
4160 inode = iget5_locked(s, objectid, btrfs_find_actor,
4161 btrfs_init_locked_inode,
4162 (void *)&args);
4163 return inode;
4164}
4165
Balaji Rao1a54ef82008-07-21 02:01:04 +05304166/* Get an inode object given its location and corresponding root.
4167 * Returns in *is_new if the inode was read from disk
4168 */
4169struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004170 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304171{
4172 struct inode *inode;
4173
4174 inode = btrfs_iget_locked(s, location->objectid, root);
4175 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004176 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304177
4178 if (inode->i_state & I_NEW) {
4179 BTRFS_I(inode)->root = root;
4180 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4181 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004182 if (!is_bad_inode(inode)) {
4183 inode_tree_add(inode);
4184 unlock_new_inode(inode);
4185 if (new)
4186 *new = 1;
4187 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004188 unlock_new_inode(inode);
4189 iput(inode);
4190 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004191 }
4192 }
4193
Balaji Rao1a54ef82008-07-21 02:01:04 +05304194 return inode;
4195}
4196
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004197static struct inode *new_simple_dir(struct super_block *s,
4198 struct btrfs_key *key,
4199 struct btrfs_root *root)
4200{
4201 struct inode *inode = new_inode(s);
4202
4203 if (!inode)
4204 return ERR_PTR(-ENOMEM);
4205
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004206 BTRFS_I(inode)->root = root;
4207 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004208 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004209
4210 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004211 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004212 inode->i_fop = &simple_dir_operations;
4213 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4214 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4215
4216 return inode;
4217}
4218
Chris Mason3de45862008-11-17 21:02:50 -05004219struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004220{
Chris Masond3977122009-01-05 21:25:51 -05004221 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004222 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004223 struct btrfs_root *sub_root = root;
4224 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004225 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004226 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004227
4228 if (dentry->d_name.len > BTRFS_NAME_LEN)
4229 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004230
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004231 if (unlikely(d_need_lookup(dentry))) {
4232 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4233 kfree(dentry->d_fsdata);
4234 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004235 /* This thing is hashed, drop it for now */
4236 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004237 } else {
4238 ret = btrfs_inode_by_name(dir, dentry, &location);
4239 }
Chris Mason5f39d392007-10-15 16:14:19 -04004240
Chris Mason39279cc2007-06-12 06:35:45 -04004241 if (ret < 0)
4242 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004243
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004244 if (location.objectid == 0)
4245 return NULL;
4246
4247 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004248 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004249 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004250 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004251
4252 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4253
Yan, Zheng76dda932009-09-21 16:00:26 -04004254 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004255 ret = fixup_tree_root_location(root, dir, dentry,
4256 &location, &sub_root);
4257 if (ret < 0) {
4258 if (ret != -ENOENT)
4259 inode = ERR_PTR(ret);
4260 else
4261 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4262 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004263 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004264 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004265 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4266
Julia Lawall34d19ba2011-01-24 19:55:19 +00004267 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004268 down_read(&root->fs_info->cleanup_work_sem);
4269 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004270 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004271 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004272 if (ret)
4273 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004274 }
4275
Chris Mason3de45862008-11-17 21:02:50 -05004276 return inode;
4277}
4278
Nick Pigginfe15ce42011-01-07 17:49:23 +11004279static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004280{
4281 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004282 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004283
Li Zefan848cce02012-02-21 17:04:28 +08004284 if (!inode && !IS_ROOT(dentry))
4285 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004286
Li Zefan848cce02012-02-21 17:04:28 +08004287 if (inode) {
4288 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004289 if (btrfs_root_refs(&root->root_item) == 0)
4290 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004291
4292 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4293 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004294 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004295 return 0;
4296}
4297
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004298static void btrfs_dentry_release(struct dentry *dentry)
4299{
4300 if (dentry->d_fsdata)
4301 kfree(dentry->d_fsdata);
4302}
4303
Chris Mason3de45862008-11-17 21:02:50 -05004304static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04004305 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05004306{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004307 struct dentry *ret;
4308
4309 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4310 if (unlikely(d_need_lookup(dentry))) {
4311 spin_lock(&dentry->d_lock);
4312 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4313 spin_unlock(&dentry->d_lock);
4314 }
4315 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004316}
4317
Miao Xie16cdcec2011-04-22 18:12:22 +08004318unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004319 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4320};
4321
David Woodhousecbdf5a22008-08-06 19:42:33 +01004322static int btrfs_real_readdir(struct file *filp, void *dirent,
4323 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004324{
Chris Mason6da6aba2007-12-18 16:15:09 -05004325 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004326 struct btrfs_root *root = BTRFS_I(inode)->root;
4327 struct btrfs_item *item;
4328 struct btrfs_dir_item *di;
4329 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004330 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004331 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004332 struct list_head ins_list;
4333 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004334 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004335 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004336 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004337 unsigned char d_type;
4338 int over = 0;
4339 u32 di_cur;
4340 u32 di_total;
4341 u32 di_len;
4342 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004343 char tmp_name[32];
4344 char *name_ptr;
4345 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004346 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004347
4348 /* FIXME, use a real flag for deciding about the key type */
4349 if (root->fs_info->tree_root == root)
4350 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004351
Chris Mason39544012007-12-12 14:38:19 -05004352 /* special case for "." */
4353 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004354 over = filldir(dirent, ".", 1,
4355 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004356 if (over)
4357 return 0;
4358 filp->f_pos = 1;
4359 }
Chris Mason39544012007-12-12 14:38:19 -05004360 /* special case for .., just use the back ref */
4361 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004362 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004363 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004364 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004365 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004366 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004367 filp->f_pos = 2;
4368 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004369 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004370 if (!path)
4371 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004372
Josef Bacik026fd312011-05-13 10:32:11 -04004373 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004374
Miao Xie16cdcec2011-04-22 18:12:22 +08004375 if (key_type == BTRFS_DIR_INDEX_KEY) {
4376 INIT_LIST_HEAD(&ins_list);
4377 INIT_LIST_HEAD(&del_list);
4378 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4379 }
4380
Chris Mason39279cc2007-06-12 06:35:45 -04004381 btrfs_set_key_type(&key, key_type);
4382 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004383 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004384
Chris Mason39279cc2007-06-12 06:35:45 -04004385 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4386 if (ret < 0)
4387 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004388
4389 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004390 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004391 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004392 if (slot >= btrfs_header_nritems(leaf)) {
4393 ret = btrfs_next_leaf(root, path);
4394 if (ret < 0)
4395 goto err;
4396 else if (ret > 0)
4397 break;
4398 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004399 }
Chris Mason3de45862008-11-17 21:02:50 -05004400
Chris Mason5f39d392007-10-15 16:14:19 -04004401 item = btrfs_item_nr(leaf, slot);
4402 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4403
4404 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004405 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004406 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004407 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004408 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004409 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004410 if (key_type == BTRFS_DIR_INDEX_KEY &&
4411 btrfs_should_delete_dir_index(&del_list,
4412 found_key.offset))
4413 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004414
4415 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004416 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004417
Chris Mason39279cc2007-06-12 06:35:45 -04004418 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4419 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004420 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004421
4422 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004423 struct btrfs_key location;
4424
Josef Bacik22a94d42011-03-16 16:47:17 -04004425 if (verify_dir_item(root, leaf, di))
4426 break;
4427
Chris Mason5f39d392007-10-15 16:14:19 -04004428 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004429 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004430 name_ptr = tmp_name;
4431 } else {
4432 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004433 if (!name_ptr) {
4434 ret = -ENOMEM;
4435 goto err;
4436 }
Chris Mason5f39d392007-10-15 16:14:19 -04004437 }
4438 read_extent_buffer(leaf, name_ptr,
4439 (unsigned long)(di + 1), name_len);
4440
4441 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4442 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004443
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004444
Chris Mason3de45862008-11-17 21:02:50 -05004445 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004446 * skip it.
4447 *
4448 * In contrast to old kernels, we insert the snapshot's
4449 * dir item and dir index after it has been created, so
4450 * we won't find a reference to our own snapshot. We
4451 * still keep the following code for backward
4452 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004453 */
4454 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4455 location.objectid == root->root_key.objectid) {
4456 over = 0;
4457 goto skip;
4458 }
Chris Mason5f39d392007-10-15 16:14:19 -04004459 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004460 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004461 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004462
Chris Mason3de45862008-11-17 21:02:50 -05004463skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004464 if (name_ptr != tmp_name)
4465 kfree(name_ptr);
4466
Chris Mason39279cc2007-06-12 06:35:45 -04004467 if (over)
4468 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004469 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004470 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004471 di_cur += di_len;
4472 di = (struct btrfs_dir_item *)((char *)di + di_len);
4473 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004474next:
4475 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004476 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004477
Miao Xie16cdcec2011-04-22 18:12:22 +08004478 if (key_type == BTRFS_DIR_INDEX_KEY) {
4479 if (is_curr)
4480 filp->f_pos++;
4481 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4482 &ins_list);
4483 if (ret)
4484 goto nopos;
4485 }
4486
David Woodhouse49593bf2008-08-17 17:08:36 +01004487 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004488 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004489 /*
4490 * 32-bit glibc will use getdents64, but then strtol -
4491 * so the last number we can serve is this.
4492 */
4493 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004494 else
4495 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004496nopos:
4497 ret = 0;
4498err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004499 if (key_type == BTRFS_DIR_INDEX_KEY)
4500 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004501 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004502 return ret;
4503}
4504
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004505int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004506{
4507 struct btrfs_root *root = BTRFS_I(inode)->root;
4508 struct btrfs_trans_handle *trans;
4509 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004510 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004511
Josef Bacik72ac3c02012-05-23 14:13:11 -04004512 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004513 return 0;
4514
Liu Bo83eea1f2012-07-10 05:28:39 -06004515 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08004516 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004517
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004518 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004519 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004520 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004521 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004522 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004523 if (IS_ERR(trans))
4524 return PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04004525 if (nolock)
4526 ret = btrfs_end_transaction_nolock(trans, root);
4527 else
4528 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004529 }
4530 return ret;
4531}
4532
4533/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004534 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004535 * inode changes. But, it is most likely to find the inode in cache.
4536 * FIXME, needs more benchmarking...there are no reasons other than performance
4537 * to keep or drop this code.
4538 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004539int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004540{
4541 struct btrfs_root *root = BTRFS_I(inode)->root;
4542 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004543 int ret;
4544
Josef Bacik72ac3c02012-05-23 14:13:11 -04004545 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004546 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004547
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004548 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004549 if (IS_ERR(trans))
4550 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004551
4552 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004553 if (ret && ret == -ENOSPC) {
4554 /* whoops, lets try again with the full transaction */
4555 btrfs_end_transaction(trans, root);
4556 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004557 if (IS_ERR(trans))
4558 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004559
Chris Mason94b60442010-05-26 11:02:00 -04004560 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004561 }
Chris Mason39279cc2007-06-12 06:35:45 -04004562 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004563 if (BTRFS_I(inode)->delayed_node)
4564 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004565
4566 return ret;
4567}
4568
4569/*
4570 * This is a copy of file_update_time. We need this so we can return error on
4571 * ENOSPC for updating the inode in the case of file write and mmap writes.
4572 */
Josef Bacike41f9412012-03-26 09:46:47 -04004573static int btrfs_update_time(struct inode *inode, struct timespec *now,
4574 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004575{
Alexander Block2bc5565282012-06-15 09:49:33 +02004576 struct btrfs_root *root = BTRFS_I(inode)->root;
4577
4578 if (btrfs_root_readonly(root))
4579 return -EROFS;
4580
Josef Bacike41f9412012-03-26 09:46:47 -04004581 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05004582 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04004583 if (flags & S_CTIME)
4584 inode->i_ctime = *now;
4585 if (flags & S_MTIME)
4586 inode->i_mtime = *now;
4587 if (flags & S_ATIME)
4588 inode->i_atime = *now;
4589 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004590}
4591
Chris Masond352ac62008-09-29 15:18:18 -04004592/*
4593 * find the highest existing sequence number in a directory
4594 * and then set the in-memory index_cnt variable to reflect
4595 * free sequence numbers
4596 */
Josef Bacikaec74772008-07-24 12:12:38 -04004597static int btrfs_set_inode_index_count(struct inode *inode)
4598{
4599 struct btrfs_root *root = BTRFS_I(inode)->root;
4600 struct btrfs_key key, found_key;
4601 struct btrfs_path *path;
4602 struct extent_buffer *leaf;
4603 int ret;
4604
Li Zefan33345d012011-04-20 10:31:50 +08004605 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004606 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4607 key.offset = (u64)-1;
4608
4609 path = btrfs_alloc_path();
4610 if (!path)
4611 return -ENOMEM;
4612
4613 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4614 if (ret < 0)
4615 goto out;
4616 /* FIXME: we should be able to handle this */
4617 if (ret == 0)
4618 goto out;
4619 ret = 0;
4620
4621 /*
4622 * MAGIC NUMBER EXPLANATION:
4623 * since we search a directory based on f_pos we have to start at 2
4624 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4625 * else has to start at 2
4626 */
4627 if (path->slots[0] == 0) {
4628 BTRFS_I(inode)->index_cnt = 2;
4629 goto out;
4630 }
4631
4632 path->slots[0]--;
4633
4634 leaf = path->nodes[0];
4635 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4636
Li Zefan33345d012011-04-20 10:31:50 +08004637 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004638 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4639 BTRFS_I(inode)->index_cnt = 2;
4640 goto out;
4641 }
4642
4643 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4644out:
4645 btrfs_free_path(path);
4646 return ret;
4647}
4648
Chris Masond352ac62008-09-29 15:18:18 -04004649/*
4650 * helper to find a free sequence number in a given directory. This current
4651 * code is very simple, later versions will do smarter things in the btree
4652 */
Chris Mason3de45862008-11-17 21:02:50 -05004653int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004654{
4655 int ret = 0;
4656
4657 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004658 ret = btrfs_inode_delayed_dir_index_count(dir);
4659 if (ret) {
4660 ret = btrfs_set_inode_index_count(dir);
4661 if (ret)
4662 return ret;
4663 }
Josef Bacikaec74772008-07-24 12:12:38 -04004664 }
4665
Chris Mason00e4e6b2008-08-05 11:18:09 -04004666 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004667 BTRFS_I(dir)->index_cnt++;
4668
4669 return ret;
4670}
4671
Chris Mason39279cc2007-06-12 06:35:45 -04004672static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4673 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004674 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004675 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004676 u64 ref_objectid, u64 objectid,
4677 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004678{
4679 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004680 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004681 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004682 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004683 struct btrfs_inode_ref *ref;
4684 struct btrfs_key key[2];
4685 u32 sizes[2];
4686 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004687 int ret;
4688 int owner;
4689
Chris Mason5f39d392007-10-15 16:14:19 -04004690 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004691 if (!path)
4692 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004693
Chris Mason39279cc2007-06-12 06:35:45 -04004694 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004695 if (!inode) {
4696 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004697 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004698 }
Chris Mason39279cc2007-06-12 06:35:45 -04004699
Li Zefan581bb052011-04-20 10:06:11 +08004700 /*
4701 * we have to initialize this early, so we can reclaim the inode
4702 * number if we fail afterwards in this function.
4703 */
4704 inode->i_ino = objectid;
4705
Josef Bacikaec74772008-07-24 12:12:38 -04004706 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004707 trace_btrfs_inode_request(dir);
4708
Chris Mason3de45862008-11-17 21:02:50 -05004709 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004710 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004711 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004712 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004713 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004714 }
Josef Bacikaec74772008-07-24 12:12:38 -04004715 }
4716 /*
4717 * index_cnt is ignored for everything but a dir,
4718 * btrfs_get_inode_index_count has an explanation for the magic
4719 * number
4720 */
4721 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004722 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004723 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004724 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04004725
Josef Bacik5dc562c2012-08-17 13:14:17 -04004726 /*
4727 * We could have gotten an inode number from somebody who was fsynced
4728 * and then removed in this same transaction, so let's just set full
4729 * sync since it will be a full sync anyway and this will blow away the
4730 * old info in the log.
4731 */
4732 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4733
Al Viro569254b2011-07-24 17:08:40 -04004734 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004735 owner = 0;
4736 else
4737 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004738
4739 key[0].objectid = objectid;
4740 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4741 key[0].offset = 0;
4742
4743 key[1].objectid = objectid;
4744 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4745 key[1].offset = ref_objectid;
4746
4747 sizes[0] = sizeof(struct btrfs_inode_item);
4748 sizes[1] = name_len + sizeof(*ref);
4749
Chris Masonb9473432009-03-13 11:00:37 -04004750 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004751 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4752 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004753 goto fail;
4754
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004755 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004756 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004757 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004758 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4759 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06004760 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4761 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04004762 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004763
4764 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4765 struct btrfs_inode_ref);
4766 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004767 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004768 ptr = (unsigned long)(ref + 1);
4769 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4770
Chris Mason5f39d392007-10-15 16:14:19 -04004771 btrfs_mark_buffer_dirty(path->nodes[0]);
4772 btrfs_free_path(path);
4773
Chris Mason39279cc2007-06-12 06:35:45 -04004774 location = &BTRFS_I(inode)->location;
4775 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004776 location->offset = 0;
4777 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4778
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004779 btrfs_inherit_iflags(inode, dir);
4780
Al Viro569254b2011-07-24 17:08:40 -04004781 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004782 if (btrfs_test_opt(root, NODATASUM))
4783 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004784 if (btrfs_test_opt(root, NODATACOW) ||
4785 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004786 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4787 }
4788
Chris Mason39279cc2007-06-12 06:35:45 -04004789 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004790 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004791
4792 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004793 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004794
Alexander Block8ea05e32012-07-25 17:35:53 +02004795 btrfs_update_root_times(trans, root);
4796
Chris Mason39279cc2007-06-12 06:35:45 -04004797 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004798fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004799 if (dir)
4800 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004801 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004802 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004803 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004804}
4805
4806static inline u8 btrfs_inode_type(struct inode *inode)
4807{
4808 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4809}
4810
Chris Masond352ac62008-09-29 15:18:18 -04004811/*
4812 * utility function to add 'inode' into 'parent_inode' with
4813 * a give name and a given sequence number.
4814 * if 'add_backref' is true, also insert a backref from the
4815 * inode to the parent directory.
4816 */
Chris Masone02119d2008-09-05 16:13:11 -04004817int btrfs_add_link(struct btrfs_trans_handle *trans,
4818 struct inode *parent_inode, struct inode *inode,
4819 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004820{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004821 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004822 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004823 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004824 u64 ino = btrfs_ino(inode);
4825 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004826
Li Zefan33345d012011-04-20 10:31:50 +08004827 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004828 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4829 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004830 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004831 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4832 key.offset = 0;
4833 }
Chris Mason39279cc2007-06-12 06:35:45 -04004834
Li Zefan33345d012011-04-20 10:31:50 +08004835 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004836 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4837 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004838 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004839 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004840 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4841 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004842 }
4843
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004844 /* Nothing to clean up yet */
4845 if (ret)
4846 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004847
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004848 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4849 parent_inode, &key,
4850 btrfs_inode_type(inode), index);
4851 if (ret == -EEXIST)
4852 goto fail_dir_item;
4853 else if (ret) {
4854 btrfs_abort_transaction(trans, root, ret);
4855 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004856 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004857
4858 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4859 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004860 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004861 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4862 ret = btrfs_update_inode(trans, root, parent_inode);
4863 if (ret)
4864 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004865 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004866
4867fail_dir_item:
4868 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4869 u64 local_index;
4870 int err;
4871 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4872 key.objectid, root->root_key.objectid,
4873 parent_ino, &local_index, name, name_len);
4874
4875 } else if (add_backref) {
4876 u64 local_index;
4877 int err;
4878
4879 err = btrfs_del_inode_ref(trans, root, name, name_len,
4880 ino, parent_ino, &local_index);
4881 }
4882 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004883}
4884
4885static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004886 struct inode *dir, struct dentry *dentry,
4887 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004888{
Josef Bacika1b075d2010-11-19 20:36:11 +00004889 int err = btrfs_add_link(trans, dir, inode,
4890 dentry->d_name.name, dentry->d_name.len,
4891 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004892 if (err > 0)
4893 err = -EEXIST;
4894 return err;
4895}
4896
Josef Bacik618e21d2007-07-11 10:18:17 -04004897static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004898 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004899{
4900 struct btrfs_trans_handle *trans;
4901 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004902 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004903 int err;
4904 int drop_inode = 0;
4905 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004906 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004907 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004908
4909 if (!new_valid_dev(rdev))
4910 return -EINVAL;
4911
Josef Bacik9ed74f22009-09-11 16:12:44 -04004912 /*
4913 * 2 for inode item and ref
4914 * 2 for dir items
4915 * 1 for xattr if selinux is on
4916 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004917 trans = btrfs_start_transaction(root, 5);
4918 if (IS_ERR(trans))
4919 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004920
Li Zefan581bb052011-04-20 10:06:11 +08004921 err = btrfs_find_free_ino(root, &objectid);
4922 if (err)
4923 goto out_unlock;
4924
Josef Bacikaec74772008-07-24 12:12:38 -04004925 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004926 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04004927 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004928 if (IS_ERR(inode)) {
4929 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004930 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004931 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004932
Eric Paris2a7dba32011-02-01 11:05:39 -05004933 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004934 if (err) {
4935 drop_inode = 1;
4936 goto out_unlock;
4937 }
4938
Casey Schauflerad19db72011-12-15 10:09:07 -05004939 /*
4940 * If the active LSM wants to access the inode during
4941 * d_instantiate it needs these. Smack checks to see
4942 * if the filesystem supports xattrs by looking at the
4943 * ops vector.
4944 */
4945
4946 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004947 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004948 if (err)
4949 drop_inode = 1;
4950 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004951 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004952 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05004953 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004954 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004955out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004956 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004957 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004958 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004959 if (drop_inode) {
4960 inode_dec_link_count(inode);
4961 iput(inode);
4962 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004963 return err;
4964}
4965
Chris Mason39279cc2007-06-12 06:35:45 -04004966static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04004967 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04004968{
4969 struct btrfs_trans_handle *trans;
4970 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004971 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004972 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004973 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004974 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004975 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004976 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004977
Josef Bacik9ed74f22009-09-11 16:12:44 -04004978 /*
4979 * 2 for inode item and ref
4980 * 2 for dir items
4981 * 1 for xattr if selinux is on
4982 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004983 trans = btrfs_start_transaction(root, 5);
4984 if (IS_ERR(trans))
4985 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004986
Li Zefan581bb052011-04-20 10:06:11 +08004987 err = btrfs_find_free_ino(root, &objectid);
4988 if (err)
4989 goto out_unlock;
4990
Josef Bacikaec74772008-07-24 12:12:38 -04004991 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004992 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04004993 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004994 if (IS_ERR(inode)) {
4995 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004996 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004997 }
Chris Mason39279cc2007-06-12 06:35:45 -04004998
Eric Paris2a7dba32011-02-01 11:05:39 -05004999 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005000 if (err) {
5001 drop_inode = 1;
5002 goto out_unlock;
5003 }
5004
Casey Schauflerad19db72011-12-15 10:09:07 -05005005 /*
5006 * If the active LSM wants to access the inode during
5007 * d_instantiate it needs these. Smack checks to see
5008 * if the filesystem supports xattrs by looking at the
5009 * ops vector.
5010 */
5011 inode->i_fop = &btrfs_file_operations;
5012 inode->i_op = &btrfs_file_inode_operations;
5013
Josef Bacika1b075d2010-11-19 20:36:11 +00005014 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005015 if (err)
5016 drop_inode = 1;
5017 else {
5018 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005019 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05005020 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro08c422c2011-12-23 07:58:13 -05005021 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005022 }
Chris Mason39279cc2007-06-12 06:35:45 -04005023out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005024 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005025 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005026 if (drop_inode) {
5027 inode_dec_link_count(inode);
5028 iput(inode);
5029 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005030 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005031 return err;
5032}
5033
5034static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5035 struct dentry *dentry)
5036{
5037 struct btrfs_trans_handle *trans;
5038 struct btrfs_root *root = BTRFS_I(dir)->root;
5039 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005040 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05005041 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005042 int err;
5043 int drop_inode = 0;
5044
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005045 /* do not allow sys_link's with other subvols of the same device */
5046 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00005047 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00005048
Al Viroc055e992011-03-04 17:15:18 +00005049 if (inode->i_nlink == ~0U)
5050 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005051
Chris Mason3de45862008-11-17 21:02:50 -05005052 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04005053 if (err)
5054 goto fail;
5055
Yan, Zhenga22285a2010-05-16 10:48:46 -04005056 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00005057 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04005058 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00005059 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04005060 */
Miao Xie7e6b6462011-02-18 09:21:17 +00005061 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005062 if (IS_ERR(trans)) {
5063 err = PTR_ERR(trans);
5064 goto fail;
5065 }
Chris Mason5f39d392007-10-15 16:14:19 -04005066
Miao Xie31534952011-04-13 13:19:21 +08005067 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005068 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08005069 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04005070 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005071
Josef Bacika1b075d2010-11-19 20:36:11 +00005072 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04005073
Yan, Zhenga5719522009-09-24 09:17:31 -04005074 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04005075 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04005076 } else {
Al Viro10d9f302011-07-16 23:09:10 -04005077 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04005078 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005079 if (err)
5080 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05005081 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00005082 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04005083 }
Chris Mason39279cc2007-06-12 06:35:45 -04005084
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005085 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005086 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005087fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005088 if (drop_inode) {
5089 inode_dec_link_count(inode);
5090 iput(inode);
5091 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005092 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005093 return err;
5094}
5095
Al Viro18bb1db2011-07-26 01:41:39 -04005096static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005097{
Chris Masonb9d86662008-05-02 16:13:49 -04005098 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005099 struct btrfs_trans_handle *trans;
5100 struct btrfs_root *root = BTRFS_I(dir)->root;
5101 int err = 0;
5102 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005103 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005104 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005105 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005106
Josef Bacik9ed74f22009-09-11 16:12:44 -04005107 /*
5108 * 2 items for inode and ref
5109 * 2 items for dir items
5110 * 1 for xattr if selinux is on
5111 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005112 trans = btrfs_start_transaction(root, 5);
5113 if (IS_ERR(trans))
5114 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005115
Li Zefan581bb052011-04-20 10:06:11 +08005116 err = btrfs_find_free_ino(root, &objectid);
5117 if (err)
5118 goto out_fail;
5119
Josef Bacikaec74772008-07-24 12:12:38 -04005120 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005121 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04005122 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005123 if (IS_ERR(inode)) {
5124 err = PTR_ERR(inode);
5125 goto out_fail;
5126 }
Chris Mason5f39d392007-10-15 16:14:19 -04005127
Chris Mason39279cc2007-06-12 06:35:45 -04005128 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005129
Eric Paris2a7dba32011-02-01 11:05:39 -05005130 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005131 if (err)
5132 goto out_fail;
5133
Chris Mason39279cc2007-06-12 06:35:45 -04005134 inode->i_op = &btrfs_dir_inode_operations;
5135 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005136
Chris Masondbe674a2008-07-17 12:54:05 -04005137 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005138 err = btrfs_update_inode(trans, root, inode);
5139 if (err)
5140 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005141
Josef Bacika1b075d2010-11-19 20:36:11 +00005142 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5143 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005144 if (err)
5145 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005146
Chris Mason39279cc2007-06-12 06:35:45 -04005147 d_instantiate(dentry, inode);
5148 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005149
5150out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005151 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005152 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005153 if (drop_on_err)
5154 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005155 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005156 return err;
5157}
5158
Chris Masond352ac62008-09-29 15:18:18 -04005159/* helper for btfs_get_extent. Given an existing extent in the tree,
5160 * and an extent that you want to insert, deal with overlap and insert
5161 * the new extent into the tree.
5162 */
Chris Mason3b951512008-04-17 11:29:12 -04005163static int merge_extent_mapping(struct extent_map_tree *em_tree,
5164 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005165 struct extent_map *em,
5166 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005167{
5168 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005169
Chris Masone6dcd2d2008-07-17 12:53:50 -04005170 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5171 start_diff = map_start - em->start;
5172 em->start = map_start;
5173 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005174 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5175 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005176 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005177 em->block_len -= start_diff;
5178 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005179 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005180}
5181
Chris Masonc8b97812008-10-29 14:49:59 -04005182static noinline int uncompress_inline(struct btrfs_path *path,
5183 struct inode *inode, struct page *page,
5184 size_t pg_offset, u64 extent_offset,
5185 struct btrfs_file_extent_item *item)
5186{
5187 int ret;
5188 struct extent_buffer *leaf = path->nodes[0];
5189 char *tmp;
5190 size_t max_size;
5191 unsigned long inline_size;
5192 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005193 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005194
5195 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005196 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005197 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5198 inline_size = btrfs_file_extent_inline_item_len(leaf,
5199 btrfs_item_nr(leaf, path->slots[0]));
5200 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005201 if (!tmp)
5202 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005203 ptr = btrfs_file_extent_inline_start(item);
5204
5205 read_extent_buffer(leaf, tmp, ptr, inline_size);
5206
Chris Mason5b050f02008-11-11 09:34:41 -05005207 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005208 ret = btrfs_decompress(compress_type, tmp, page,
5209 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005210 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005211 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005212 unsigned long copy_size = min_t(u64,
5213 PAGE_CACHE_SIZE - pg_offset,
5214 max_size - extent_offset);
5215 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005216 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005217 }
5218 kfree(tmp);
5219 return 0;
5220}
5221
Chris Masond352ac62008-09-29 15:18:18 -04005222/*
5223 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005224 * the ugly parts come from merging extents from the disk with the in-ram
5225 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005226 * where the in-ram extents might be locked pending data=ordered completion.
5227 *
5228 * This also copies inline extents directly into the page.
5229 */
Chris Masond3977122009-01-05 21:25:51 -05005230
Chris Masona52d9a82007-08-27 16:49:44 -04005231struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005232 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005233 int create)
5234{
5235 int ret;
5236 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005237 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005238 u64 extent_start = 0;
5239 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005240 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005241 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005242 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005243 struct btrfs_root *root = BTRFS_I(inode)->root;
5244 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005245 struct extent_buffer *leaf;
5246 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005247 struct extent_map *em = NULL;
5248 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005249 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005250 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005251 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005252
Chris Masona52d9a82007-08-27 16:49:44 -04005253again:
Chris Mason890871b2009-09-02 16:24:52 -04005254 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005255 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005256 if (em)
5257 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005258 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005259
Chris Masona52d9a82007-08-27 16:49:44 -04005260 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005261 if (em->start > start || em->start + em->len <= start)
5262 free_extent_map(em);
5263 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005264 free_extent_map(em);
5265 else
5266 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005267 }
David Sterba172ddd62011-04-21 00:48:27 +02005268 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005269 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005270 err = -ENOMEM;
5271 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005272 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005273 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005274 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005275 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005276 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005277 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005278
5279 if (!path) {
5280 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005281 if (!path) {
5282 err = -ENOMEM;
5283 goto out;
5284 }
5285 /*
5286 * Chances are we'll be called again, so go ahead and do
5287 * readahead
5288 */
5289 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005290 }
5291
Chris Mason179e29e2007-11-01 11:28:41 -04005292 ret = btrfs_lookup_file_extent(trans, root, path,
5293 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005294 if (ret < 0) {
5295 err = ret;
5296 goto out;
5297 }
5298
5299 if (ret != 0) {
5300 if (path->slots[0] == 0)
5301 goto not_found;
5302 path->slots[0]--;
5303 }
5304
Chris Mason5f39d392007-10-15 16:14:19 -04005305 leaf = path->nodes[0];
5306 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005307 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005308 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005309 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5310 found_type = btrfs_key_type(&found_key);
5311 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005312 found_type != BTRFS_EXTENT_DATA_KEY) {
5313 goto not_found;
5314 }
5315
Chris Mason5f39d392007-10-15 16:14:19 -04005316 found_type = btrfs_file_extent_type(leaf, item);
5317 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005318 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005319 if (found_type == BTRFS_FILE_EXTENT_REG ||
5320 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005321 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005322 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005323 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5324 size_t size;
5325 size = btrfs_file_extent_inline_len(leaf, item);
5326 extent_end = (extent_start + size + root->sectorsize - 1) &
5327 ~((u64)root->sectorsize - 1);
5328 }
5329
5330 if (start >= extent_end) {
5331 path->slots[0]++;
5332 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5333 ret = btrfs_next_leaf(root, path);
5334 if (ret < 0) {
5335 err = ret;
5336 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005337 }
Yan Zheng9036c102008-10-30 14:19:41 -04005338 if (ret > 0)
5339 goto not_found;
5340 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005341 }
Yan Zheng9036c102008-10-30 14:19:41 -04005342 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5343 if (found_key.objectid != objectid ||
5344 found_key.type != BTRFS_EXTENT_DATA_KEY)
5345 goto not_found;
5346 if (start + len <= found_key.offset)
5347 goto not_found;
5348 em->start = start;
5349 em->len = found_key.offset - start;
5350 goto not_found_em;
5351 }
5352
Yan Zhengd899e052008-10-30 14:25:28 -04005353 if (found_type == BTRFS_FILE_EXTENT_REG ||
5354 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005355 em->start = extent_start;
5356 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005357 em->orig_start = extent_start -
5358 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005359 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5360 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005361 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005362 goto insert;
5363 }
Li Zefan261507a02010-12-17 14:21:50 +08005364 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005365 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005366 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005367 em->block_start = bytenr;
5368 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5369 item);
5370 } else {
5371 bytenr += btrfs_file_extent_offset(leaf, item);
5372 em->block_start = bytenr;
5373 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005374 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5375 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005376 }
Chris Masona52d9a82007-08-27 16:49:44 -04005377 goto insert;
5378 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005379 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005380 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005381 size_t size;
5382 size_t extent_offset;
5383 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005384
Chris Masona52d9a82007-08-27 16:49:44 -04005385 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005386 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005387 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005388 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005389 goto out;
5390 }
5391
Yan Zheng9036c102008-10-30 14:19:41 -04005392 size = btrfs_file_extent_inline_len(leaf, item);
5393 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005394 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005395 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005396 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005397 em->len = (copy_size + root->sectorsize - 1) &
5398 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005399 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005400 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005401 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005402 em->compress_type = compress_type;
5403 }
Yan689f9342007-10-29 11:41:07 -04005404 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005405 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005406 if (btrfs_file_extent_compression(leaf, item) !=
5407 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005408 ret = uncompress_inline(path, inode, page,
5409 pg_offset,
5410 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005411 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005412 } else {
5413 map = kmap(page);
5414 read_extent_buffer(leaf, map + pg_offset, ptr,
5415 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005416 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5417 memset(map + pg_offset + copy_size, 0,
5418 PAGE_CACHE_SIZE - pg_offset -
5419 copy_size);
5420 }
Chris Masonc8b97812008-10-29 14:49:59 -04005421 kunmap(page);
5422 }
Chris Mason179e29e2007-11-01 11:28:41 -04005423 flush_dcache_page(page);
5424 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005425 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005426 if (!trans) {
5427 kunmap(page);
5428 free_extent_map(em);
5429 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005430
David Sterbab3b4aa72011-04-21 01:20:15 +02005431 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005432 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005433
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005434 if (IS_ERR(trans))
5435 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005436 goto again;
5437 }
Chris Masonc8b97812008-10-29 14:49:59 -04005438 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005439 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005440 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005441 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005442 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005443 }
Chris Masond1310b22008-01-24 16:13:08 -05005444 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005445 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005446 goto insert;
5447 } else {
Chris Masond3977122009-01-05 21:25:51 -05005448 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005449 WARN_ON(1);
5450 }
5451not_found:
5452 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005453 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005454not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005455 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005456 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005457insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005458 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005459 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005460 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5461 "[%llu %llu]\n", (unsigned long long)em->start,
5462 (unsigned long long)em->len,
5463 (unsigned long long)start,
5464 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005465 err = -EIO;
5466 goto out;
5467 }
Chris Masond1310b22008-01-24 16:13:08 -05005468
5469 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005470 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005471 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005472 /* it is possible that someone inserted the extent into the tree
5473 * while we had the lock dropped. It is also possible that
5474 * an overlapping map exists in the tree
5475 */
Chris Masona52d9a82007-08-27 16:49:44 -04005476 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005477 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005478
5479 ret = 0;
5480
Chris Mason3b951512008-04-17 11:29:12 -04005481 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005482 if (existing && (existing->start > start ||
5483 existing->start + existing->len <= start)) {
5484 free_extent_map(existing);
5485 existing = NULL;
5486 }
Chris Mason3b951512008-04-17 11:29:12 -04005487 if (!existing) {
5488 existing = lookup_extent_mapping(em_tree, em->start,
5489 em->len);
5490 if (existing) {
5491 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005492 em, start,
5493 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005494 free_extent_map(existing);
5495 if (err) {
5496 free_extent_map(em);
5497 em = NULL;
5498 }
5499 } else {
5500 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005501 free_extent_map(em);
5502 em = NULL;
5503 }
5504 } else {
5505 free_extent_map(em);
5506 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005507 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005508 }
Chris Masona52d9a82007-08-27 16:49:44 -04005509 }
Chris Mason890871b2009-09-02 16:24:52 -04005510 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005511out:
liubo1abe9b82011-03-24 11:18:59 +00005512
5513 trace_btrfs_get_extent(root, em);
5514
Chris Masonf4219502008-07-22 11:18:09 -04005515 if (path)
5516 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005517 if (trans) {
5518 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005519 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005520 err = ret;
5521 }
Chris Masona52d9a82007-08-27 16:49:44 -04005522 if (err) {
5523 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005524 return ERR_PTR(err);
5525 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005526 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005527 return em;
5528}
5529
Chris Masonec29ed52011-02-23 16:23:20 -05005530struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5531 size_t pg_offset, u64 start, u64 len,
5532 int create)
5533{
5534 struct extent_map *em;
5535 struct extent_map *hole_em = NULL;
5536 u64 range_start = start;
5537 u64 end;
5538 u64 found;
5539 u64 found_end;
5540 int err = 0;
5541
5542 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5543 if (IS_ERR(em))
5544 return em;
5545 if (em) {
5546 /*
5547 * if our em maps to a hole, there might
5548 * actually be delalloc bytes behind it
5549 */
5550 if (em->block_start != EXTENT_MAP_HOLE)
5551 return em;
5552 else
5553 hole_em = em;
5554 }
5555
5556 /* check to see if we've wrapped (len == -1 or similar) */
5557 end = start + len;
5558 if (end < start)
5559 end = (u64)-1;
5560 else
5561 end -= 1;
5562
5563 em = NULL;
5564
5565 /* ok, we didn't find anything, lets look for delalloc */
5566 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5567 end, len, EXTENT_DELALLOC, 1);
5568 found_end = range_start + found;
5569 if (found_end < range_start)
5570 found_end = (u64)-1;
5571
5572 /*
5573 * we didn't find anything useful, return
5574 * the original results from get_extent()
5575 */
5576 if (range_start > end || found_end <= start) {
5577 em = hole_em;
5578 hole_em = NULL;
5579 goto out;
5580 }
5581
5582 /* adjust the range_start to make sure it doesn't
5583 * go backwards from the start they passed in
5584 */
5585 range_start = max(start,range_start);
5586 found = found_end - range_start;
5587
5588 if (found > 0) {
5589 u64 hole_start = start;
5590 u64 hole_len = len;
5591
David Sterba172ddd62011-04-21 00:48:27 +02005592 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005593 if (!em) {
5594 err = -ENOMEM;
5595 goto out;
5596 }
5597 /*
5598 * when btrfs_get_extent can't find anything it
5599 * returns one huge hole
5600 *
5601 * make sure what it found really fits our range, and
5602 * adjust to make sure it is based on the start from
5603 * the caller
5604 */
5605 if (hole_em) {
5606 u64 calc_end = extent_map_end(hole_em);
5607
5608 if (calc_end <= start || (hole_em->start > end)) {
5609 free_extent_map(hole_em);
5610 hole_em = NULL;
5611 } else {
5612 hole_start = max(hole_em->start, start);
5613 hole_len = calc_end - hole_start;
5614 }
5615 }
5616 em->bdev = NULL;
5617 if (hole_em && range_start > hole_start) {
5618 /* our hole starts before our delalloc, so we
5619 * have to return just the parts of the hole
5620 * that go until the delalloc starts
5621 */
5622 em->len = min(hole_len,
5623 range_start - hole_start);
5624 em->start = hole_start;
5625 em->orig_start = hole_start;
5626 /*
5627 * don't adjust block start at all,
5628 * it is fixed at EXTENT_MAP_HOLE
5629 */
5630 em->block_start = hole_em->block_start;
5631 em->block_len = hole_len;
5632 } else {
5633 em->start = range_start;
5634 em->len = found;
5635 em->orig_start = range_start;
5636 em->block_start = EXTENT_MAP_DELALLOC;
5637 em->block_len = found;
5638 }
5639 } else if (hole_em) {
5640 return hole_em;
5641 }
5642out:
5643
5644 free_extent_map(hole_em);
5645 if (err) {
5646 free_extent_map(em);
5647 return ERR_PTR(err);
5648 }
5649 return em;
5650}
5651
Josef Bacik4b46fce2010-05-23 11:00:55 -04005652static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005653 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005654 u64 start, u64 len)
5655{
5656 struct btrfs_root *root = BTRFS_I(inode)->root;
5657 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005658 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5659 struct btrfs_key ins;
5660 u64 alloc_hint;
5661 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005662 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005663
Josef Bacik16d299a2011-04-06 14:53:07 -04005664 /*
5665 * Ok if the extent map we looked up is a hole and is for the exact
5666 * range we want, there is no reason to allocate a new one, however if
5667 * it is not right then we need to free this one and drop the cache for
5668 * our range.
5669 */
5670 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5671 em->len != len) {
5672 free_extent_map(em);
5673 em = NULL;
5674 insert = true;
5675 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5676 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005677
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005678 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005679 if (IS_ERR(trans))
5680 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005681
Chris Mason4cb53002011-05-24 15:35:30 -04005682 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5683 btrfs_add_inode_defrag(trans, inode);
5684
Josef Bacik4b46fce2010-05-23 11:00:55 -04005685 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5686
5687 alloc_hint = get_extent_allocation_hint(inode, start, len);
5688 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005689 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005690 if (ret) {
5691 em = ERR_PTR(ret);
5692 goto out;
5693 }
5694
Josef Bacik4b46fce2010-05-23 11:00:55 -04005695 if (!em) {
David Sterba172ddd62011-04-21 00:48:27 +02005696 em = alloc_extent_map();
Josef Bacik16d299a2011-04-06 14:53:07 -04005697 if (!em) {
5698 em = ERR_PTR(-ENOMEM);
5699 goto out;
5700 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005701 }
5702
5703 em->start = start;
5704 em->orig_start = em->start;
5705 em->len = ins.offset;
5706
5707 em->block_start = ins.objectid;
5708 em->block_len = ins.offset;
5709 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005710
5711 /*
5712 * We need to do this because if we're using the original em we searched
5713 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5714 */
5715 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005716 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5717
Josef Bacik16d299a2011-04-06 14:53:07 -04005718 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005719 write_lock(&em_tree->lock);
5720 ret = add_extent_mapping(em_tree, em);
5721 write_unlock(&em_tree->lock);
5722 if (ret != -EEXIST)
5723 break;
5724 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5725 }
5726
5727 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5728 ins.offset, ins.offset, 0);
5729 if (ret) {
5730 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5731 em = ERR_PTR(ret);
5732 }
5733out:
5734 btrfs_end_transaction(trans, root);
5735 return em;
5736}
5737
Chris Mason46bfbb52010-05-26 11:04:10 -04005738/*
5739 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5740 * block must be cow'd
5741 */
5742static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5743 struct inode *inode, u64 offset, u64 len)
5744{
5745 struct btrfs_path *path;
5746 int ret;
5747 struct extent_buffer *leaf;
5748 struct btrfs_root *root = BTRFS_I(inode)->root;
5749 struct btrfs_file_extent_item *fi;
5750 struct btrfs_key key;
5751 u64 disk_bytenr;
5752 u64 backref_offset;
5753 u64 extent_end;
5754 u64 num_bytes;
5755 int slot;
5756 int found_type;
5757
5758 path = btrfs_alloc_path();
5759 if (!path)
5760 return -ENOMEM;
5761
Li Zefan33345d012011-04-20 10:31:50 +08005762 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005763 offset, 0);
5764 if (ret < 0)
5765 goto out;
5766
5767 slot = path->slots[0];
5768 if (ret == 1) {
5769 if (slot == 0) {
5770 /* can't find the item, must cow */
5771 ret = 0;
5772 goto out;
5773 }
5774 slot--;
5775 }
5776 ret = 0;
5777 leaf = path->nodes[0];
5778 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005779 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005780 key.type != BTRFS_EXTENT_DATA_KEY) {
5781 /* not our file or wrong item type, must cow */
5782 goto out;
5783 }
5784
5785 if (key.offset > offset) {
5786 /* Wrong offset, must cow */
5787 goto out;
5788 }
5789
5790 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5791 found_type = btrfs_file_extent_type(leaf, fi);
5792 if (found_type != BTRFS_FILE_EXTENT_REG &&
5793 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5794 /* not a regular extent, must cow */
5795 goto out;
5796 }
5797 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5798 backref_offset = btrfs_file_extent_offset(leaf, fi);
5799
5800 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5801 if (extent_end < offset + len) {
5802 /* extent doesn't include our full range, must cow */
5803 goto out;
5804 }
5805
5806 if (btrfs_extent_readonly(root, disk_bytenr))
5807 goto out;
5808
5809 /*
5810 * look for other files referencing this extent, if we
5811 * find any we must cow
5812 */
Li Zefan33345d012011-04-20 10:31:50 +08005813 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005814 key.offset - backref_offset, disk_bytenr))
5815 goto out;
5816
5817 /*
5818 * adjust disk_bytenr and num_bytes to cover just the bytes
5819 * in this extent we are about to write. If there
5820 * are any csums in that range we have to cow in order
5821 * to keep the csums correct
5822 */
5823 disk_bytenr += backref_offset;
5824 disk_bytenr += offset - key.offset;
5825 num_bytes = min(offset + len, extent_end) - offset;
5826 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5827 goto out;
5828 /*
5829 * all of the above have passed, it is safe to overwrite this extent
5830 * without cow
5831 */
5832 ret = 1;
5833out:
5834 btrfs_free_path(path);
5835 return ret;
5836}
5837
Josef Bacikeb838e72012-07-31 16:28:48 -04005838static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5839 struct extent_state **cached_state, int writing)
5840{
5841 struct btrfs_ordered_extent *ordered;
5842 int ret = 0;
5843
5844 while (1) {
5845 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5846 0, cached_state);
5847 /*
5848 * We're concerned with the entire range that we're going to be
5849 * doing DIO to, so we need to make sure theres no ordered
5850 * extents in this range.
5851 */
5852 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5853 lockend - lockstart + 1);
5854
5855 /*
5856 * We need to make sure there are no buffered pages in this
5857 * range either, we could have raced between the invalidate in
5858 * generic_file_direct_write and locking the extent. The
5859 * invalidate needs to happen so that reads after a write do not
5860 * get stale data.
5861 */
5862 if (!ordered && (!writing ||
5863 !test_range_bit(&BTRFS_I(inode)->io_tree,
5864 lockstart, lockend, EXTENT_UPTODATE, 0,
5865 *cached_state)))
5866 break;
5867
5868 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5869 cached_state, GFP_NOFS);
5870
5871 if (ordered) {
5872 btrfs_start_ordered_extent(inode, ordered, 1);
5873 btrfs_put_ordered_extent(ordered);
5874 } else {
5875 /* Screw you mmap */
5876 ret = filemap_write_and_wait_range(inode->i_mapping,
5877 lockstart,
5878 lockend);
5879 if (ret)
5880 break;
5881
5882 /*
5883 * If we found a page that couldn't be invalidated just
5884 * fall back to buffered.
5885 */
5886 ret = invalidate_inode_pages2_range(inode->i_mapping,
5887 lockstart >> PAGE_CACHE_SHIFT,
5888 lockend >> PAGE_CACHE_SHIFT);
5889 if (ret)
5890 break;
5891 }
5892
5893 cond_resched();
5894 }
5895
5896 return ret;
5897}
5898
Josef Bacik4b46fce2010-05-23 11:00:55 -04005899static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5900 struct buffer_head *bh_result, int create)
5901{
5902 struct extent_map *em;
5903 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04005904 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005905 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04005906 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005907 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005908 struct btrfs_trans_handle *trans;
Josef Bacikeb838e72012-07-31 16:28:48 -04005909 int unlock_bits = EXTENT_LOCKED;
5910 int ret;
5911
Josef Bacikeb838e72012-07-31 16:28:48 -04005912 if (create) {
5913 ret = btrfs_delalloc_reserve_space(inode, len);
5914 if (ret)
5915 return ret;
5916 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
Josef Bacikc3298612012-08-03 16:49:19 -04005917 } else {
5918 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04005919 }
5920
Josef Bacikc3298612012-08-03 16:49:19 -04005921 lockstart = start;
5922 lockend = start + len - 1;
5923
Josef Bacikeb838e72012-07-31 16:28:48 -04005924 /*
5925 * If this errors out it's because we couldn't invalidate pagecache for
5926 * this range and we need to fallback to buffered.
5927 */
5928 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
5929 return -ENOTBLK;
5930
5931 if (create) {
5932 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
5933 lockend, EXTENT_DELALLOC, NULL,
5934 &cached_state, GFP_NOFS);
5935 if (ret)
5936 goto unlock_err;
5937 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005938
5939 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04005940 if (IS_ERR(em)) {
5941 ret = PTR_ERR(em);
5942 goto unlock_err;
5943 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005944
5945 /*
5946 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5947 * io. INLINE is special, and we could probably kludge it in here, but
5948 * it's still buffered so for safety lets just fall back to the generic
5949 * buffered path.
5950 *
5951 * For COMPRESSED we _have_ to read the entire extent in so we can
5952 * decompress it, so there will be buffering required no matter what we
5953 * do, so go ahead and fallback to buffered.
5954 *
5955 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5956 * to buffered IO. Don't blame me, this is the price we pay for using
5957 * the generic code.
5958 */
5959 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5960 em->block_start == EXTENT_MAP_INLINE) {
5961 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04005962 ret = -ENOTBLK;
5963 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005964 }
5965
5966 /* Just a good old fashioned hole, return */
5967 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5968 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5969 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04005970 ret = 0;
5971 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005972 }
5973
5974 /*
5975 * We don't allocate a new extent in the following cases
5976 *
5977 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5978 * existing extent.
5979 * 2) The extent is marked as PREALLOC. We're good to go here and can
5980 * just use the extent.
5981 *
5982 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005983 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04005984 len = min(len, em->len - (start - em->start));
5985 lockstart = start + len;
5986 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04005987 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005988
5989 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5990 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5991 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005992 int type;
5993 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005994 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005995
5996 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5997 type = BTRFS_ORDERED_PREALLOC;
5998 else
5999 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04006000 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04006001 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04006002
6003 /*
6004 * we're not going to log anything, but we do need
6005 * to make sure the current transaction stays open
6006 * while we look for nocow cross refs
6007 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006008 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006009 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04006010 goto must_cow;
6011
6012 if (can_nocow_odirect(trans, inode, start, len) == 1) {
6013 ret = btrfs_add_ordered_extent_dio(inode, start,
6014 block_start, len, len, type);
6015 btrfs_end_transaction(trans, root);
6016 if (ret) {
6017 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04006018 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04006019 }
6020 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006021 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006022 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006023 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006024must_cow:
6025 /*
6026 * this will cow the extent, reset the len in case we changed
6027 * it above
6028 */
6029 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04006030 em = btrfs_new_extent_direct(inode, em, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04006031 if (IS_ERR(em)) {
6032 ret = PTR_ERR(em);
6033 goto unlock_err;
6034 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006035 len = min(len, em->len - (start - em->start));
6036unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04006037 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6038 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04006039 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006040 bh_result->b_bdev = em->bdev;
6041 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04006042 if (create) {
6043 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6044 set_buffer_new(bh_result);
6045
6046 /*
6047 * Need to update the i_size under the extent lock so buffered
6048 * readers will get the updated i_size when we unlock.
6049 */
6050 if (start + len > i_size_read(inode))
6051 i_size_write(inode, start + len);
6052 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006053
Josef Bacikeb838e72012-07-31 16:28:48 -04006054 /*
6055 * In the case of write we need to clear and unlock the entire range,
6056 * in the case of read we need to unlock only the end area that we
6057 * aren't using if there is any left over space.
6058 */
Liu Bo24c03fa2012-08-22 20:10:38 -06006059 if (lockstart < lockend) {
6060 if (create && len < lockend - lockstart) {
6061 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6062 lockstart + len - 1, unlock_bits, 1, 0,
6063 &cached_state, GFP_NOFS);
6064 /*
6065 * Beside unlock, we also need to cleanup reserved space
6066 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6067 */
6068 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6069 lockstart + len, lockend,
6070 unlock_bits | EXTENT_DO_ACCOUNTING,
6071 1, 0, NULL, GFP_NOFS);
6072 } else {
6073 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6074 lockend, unlock_bits, 1, 0,
6075 &cached_state, GFP_NOFS);
6076 }
6077 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04006078 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06006079 }
Josef Bacikeb838e72012-07-31 16:28:48 -04006080
Josef Bacik4b46fce2010-05-23 11:00:55 -04006081 free_extent_map(em);
6082
6083 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04006084
6085unlock_err:
6086 if (create)
6087 unlock_bits |= EXTENT_DO_ACCOUNTING;
6088
6089 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6090 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6091 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006092}
6093
6094struct btrfs_dio_private {
6095 struct inode *inode;
6096 u64 logical_offset;
6097 u64 disk_bytenr;
6098 u64 bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006099 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00006100
6101 /* number of bios pending for this dio */
6102 atomic_t pending_bios;
6103
6104 /* IO errors */
6105 int errors;
6106
6107 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006108};
6109
6110static void btrfs_endio_direct_read(struct bio *bio, int err)
6111{
Miao Xiee65e1532010-11-22 03:04:43 +00006112 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006113 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6114 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006115 struct inode *inode = dip->inode;
6116 struct btrfs_root *root = BTRFS_I(inode)->root;
6117 u64 start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006118
6119 start = dip->logical_offset;
6120 do {
6121 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6122 struct page *page = bvec->bv_page;
6123 char *kaddr;
6124 u32 csum = ~(u32)0;
Josef Bacikc3298612012-08-03 16:49:19 -04006125 u64 private = ~(u32)0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006126 unsigned long flags;
6127
Josef Bacikc3298612012-08-03 16:49:19 -04006128 if (get_state_private(&BTRFS_I(inode)->io_tree,
6129 start, &private))
6130 goto failed;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006131 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08006132 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006133 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6134 csum, bvec->bv_len);
6135 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08006136 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006137 local_irq_restore(flags);
6138
6139 flush_dcache_page(bvec->bv_page);
Josef Bacikc3298612012-08-03 16:49:19 -04006140 if (csum != private) {
6141failed:
Li Zefan33345d012011-04-20 10:31:50 +08006142 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04006143 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08006144 (unsigned long long)btrfs_ino(inode),
6145 (unsigned long long)start,
Josef Bacikc3298612012-08-03 16:49:19 -04006146 csum, (unsigned)private);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006147 err = -EIO;
6148 }
6149 }
6150
6151 start += bvec->bv_len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006152 bvec++;
6153 } while (bvec <= bvec_end);
6154
6155 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006156 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006157 bio->bi_private = dip->private;
6158
Josef Bacik4b46fce2010-05-23 11:00:55 -04006159 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006160
6161 /* If we had a csum failure make sure to clear the uptodate flag */
6162 if (err)
6163 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006164 dio_end_io(bio, err);
6165}
6166
6167static void btrfs_endio_direct_write(struct bio *bio, int err)
6168{
6169 struct btrfs_dio_private *dip = bio->bi_private;
6170 struct inode *inode = dip->inode;
6171 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006172 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006173 u64 ordered_offset = dip->logical_offset;
6174 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006175 int ret;
6176
6177 if (err)
6178 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05006179again:
6180 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6181 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04006182 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006183 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05006184 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006185
Josef Bacik5fd02042012-05-02 14:00:54 -04006186 ordered->work.func = finish_ordered_fn;
6187 ordered->work.flags = 0;
6188 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6189 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05006190out_test:
6191 /*
6192 * our bio might span multiple ordered extents. If we haven't
6193 * completed the accounting for the whole dio, go back and try again
6194 */
6195 if (ordered_offset < dip->logical_offset + dip->bytes) {
6196 ordered_bytes = dip->logical_offset + dip->bytes -
6197 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04006198 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05006199 goto again;
6200 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006201out_done:
6202 bio->bi_private = dip->private;
6203
Josef Bacik4b46fce2010-05-23 11:00:55 -04006204 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04006205
6206 /* If we had an error make sure to clear the uptodate flag */
6207 if (err)
6208 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006209 dio_end_io(bio, err);
6210}
6211
Chris Masoneaf25d92010-05-25 09:48:28 -04006212static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6213 struct bio *bio, int mirror_num,
6214 unsigned long bio_flags, u64 offset)
6215{
6216 int ret;
6217 struct btrfs_root *root = BTRFS_I(inode)->root;
6218 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006219 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04006220 return 0;
6221}
6222
Miao Xiee65e1532010-11-22 03:04:43 +00006223static void btrfs_end_dio_bio(struct bio *bio, int err)
6224{
6225 struct btrfs_dio_private *dip = bio->bi_private;
6226
6227 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006228 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006229 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006230 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006231 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006232 dip->errors = 1;
6233
6234 /*
6235 * before atomic variable goto zero, we must make sure
6236 * dip->errors is perceived to be set.
6237 */
6238 smp_mb__before_atomic_dec();
6239 }
6240
6241 /* if there are more bios still pending for this dio, just exit */
6242 if (!atomic_dec_and_test(&dip->pending_bios))
6243 goto out;
6244
6245 if (dip->errors)
6246 bio_io_error(dip->orig_bio);
6247 else {
6248 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6249 bio_endio(dip->orig_bio, 0);
6250 }
6251out:
6252 bio_put(bio);
6253}
6254
6255static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6256 u64 first_sector, gfp_t gfp_flags)
6257{
6258 int nr_vecs = bio_get_nr_vecs(bdev);
6259 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6260}
6261
6262static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6263 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006264 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006265{
6266 int write = rw & REQ_WRITE;
6267 struct btrfs_root *root = BTRFS_I(inode)->root;
6268 int ret;
6269
6270 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006271
6272 if (!write) {
6273 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6274 if (ret)
6275 goto err;
6276 }
Miao Xiee65e1532010-11-22 03:04:43 +00006277
Josef Bacik1ae39932011-04-06 14:41:34 -04006278 if (skip_sum)
6279 goto map;
6280
6281 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006282 ret = btrfs_wq_submit_bio(root->fs_info,
6283 inode, rw, bio, 0, 0,
6284 file_offset,
6285 __btrfs_submit_bio_start_direct_io,
6286 __btrfs_submit_bio_done);
6287 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006288 } else if (write) {
6289 /*
6290 * If we aren't doing async submit, calculate the csum of the
6291 * bio now.
6292 */
6293 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6294 if (ret)
6295 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006296 } else if (!skip_sum) {
Josef Bacikc3298612012-08-03 16:49:19 -04006297 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006298 if (ret)
6299 goto err;
6300 }
Miao Xiee65e1532010-11-22 03:04:43 +00006301
Josef Bacik1ae39932011-04-06 14:41:34 -04006302map:
6303 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006304err:
6305 bio_put(bio);
6306 return ret;
6307}
6308
6309static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6310 int skip_sum)
6311{
6312 struct inode *inode = dip->inode;
6313 struct btrfs_root *root = BTRFS_I(inode)->root;
6314 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6315 struct bio *bio;
6316 struct bio *orig_bio = dip->orig_bio;
6317 struct bio_vec *bvec = orig_bio->bi_io_vec;
6318 u64 start_sector = orig_bio->bi_sector;
6319 u64 file_offset = dip->logical_offset;
6320 u64 submit_len = 0;
6321 u64 map_length;
6322 int nr_pages = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006323 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006324 int async_submit = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00006325
Miao Xiee65e1532010-11-22 03:04:43 +00006326 map_length = orig_bio->bi_size;
6327 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6328 &map_length, NULL, 0);
6329 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006330 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006331 return -EIO;
6332 }
6333
Josef Bacik02f57c72011-04-06 14:25:44 -04006334 if (map_length >= orig_bio->bi_size) {
6335 bio = orig_bio;
6336 goto submit;
6337 }
6338
Josef Bacik1ae39932011-04-06 14:41:34 -04006339 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006340 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6341 if (!bio)
6342 return -ENOMEM;
6343 bio->bi_private = dip;
6344 bio->bi_end_io = btrfs_end_dio_bio;
6345 atomic_inc(&dip->pending_bios);
6346
Miao Xiee65e1532010-11-22 03:04:43 +00006347 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6348 if (unlikely(map_length < submit_len + bvec->bv_len ||
6349 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6350 bvec->bv_offset) < bvec->bv_len)) {
6351 /*
6352 * inc the count before we submit the bio so
6353 * we know the end IO handler won't happen before
6354 * we inc the count. Otherwise, the dip might get freed
6355 * before we're done setting it up
6356 */
6357 atomic_inc(&dip->pending_bios);
6358 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6359 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006360 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006361 if (ret) {
6362 bio_put(bio);
6363 atomic_dec(&dip->pending_bios);
6364 goto out_err;
6365 }
6366
Miao Xiee65e1532010-11-22 03:04:43 +00006367 start_sector += submit_len >> 9;
6368 file_offset += submit_len;
6369
6370 submit_len = 0;
6371 nr_pages = 0;
6372
6373 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6374 start_sector, GFP_NOFS);
6375 if (!bio)
6376 goto out_err;
6377 bio->bi_private = dip;
6378 bio->bi_end_io = btrfs_end_dio_bio;
6379
6380 map_length = orig_bio->bi_size;
6381 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6382 &map_length, NULL, 0);
6383 if (ret) {
6384 bio_put(bio);
6385 goto out_err;
6386 }
6387 } else {
6388 submit_len += bvec->bv_len;
6389 nr_pages ++;
6390 bvec++;
6391 }
6392 }
6393
Josef Bacik02f57c72011-04-06 14:25:44 -04006394submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006395 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04006396 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006397 if (!ret)
6398 return 0;
6399
6400 bio_put(bio);
6401out_err:
6402 dip->errors = 1;
6403 /*
6404 * before atomic variable goto zero, we must
6405 * make sure dip->errors is perceived to be set.
6406 */
6407 smp_mb__before_atomic_dec();
6408 if (atomic_dec_and_test(&dip->pending_bios))
6409 bio_io_error(dip->orig_bio);
6410
6411 /* bio_end_io() will handle error, so we needn't return it */
6412 return 0;
6413}
6414
Josef Bacik4b46fce2010-05-23 11:00:55 -04006415static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6416 loff_t file_offset)
6417{
6418 struct btrfs_root *root = BTRFS_I(inode)->root;
6419 struct btrfs_dio_private *dip;
6420 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006421 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006422 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006423 int ret = 0;
6424
6425 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6426
6427 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6428 if (!dip) {
6429 ret = -ENOMEM;
6430 goto free_ordered;
6431 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04006432
6433 dip->private = bio->bi_private;
6434 dip->inode = inode;
6435 dip->logical_offset = file_offset;
6436
Josef Bacik4b46fce2010-05-23 11:00:55 -04006437 dip->bytes = 0;
6438 do {
6439 dip->bytes += bvec->bv_len;
6440 bvec++;
6441 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6442
Chris Mason46bfbb52010-05-26 11:04:10 -04006443 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006444 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006445 dip->errors = 0;
6446 dip->orig_bio = bio;
6447 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006448
6449 if (write)
6450 bio->bi_end_io = btrfs_endio_direct_write;
6451 else
6452 bio->bi_end_io = btrfs_endio_direct_read;
6453
Miao Xiee65e1532010-11-22 03:04:43 +00006454 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6455 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006456 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006457free_ordered:
6458 /*
6459 * If this is a write, we need to clean up the reserved space and kill
6460 * the ordered extent.
6461 */
6462 if (write) {
6463 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006464 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006465 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6466 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6467 btrfs_free_reserved_extent(root, ordered->start,
6468 ordered->disk_len);
6469 btrfs_put_ordered_extent(ordered);
6470 btrfs_put_ordered_extent(ordered);
6471 }
6472 bio_endio(bio, ret);
6473}
6474
Chris Mason5a5f79b2010-05-26 21:33:37 -04006475static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6476 const struct iovec *iov, loff_t offset,
6477 unsigned long nr_segs)
6478{
6479 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006480 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006481 size_t size;
6482 unsigned long addr;
6483 unsigned blocksize_mask = root->sectorsize - 1;
6484 ssize_t retval = -EINVAL;
6485 loff_t end = offset;
6486
6487 if (offset & blocksize_mask)
6488 goto out;
6489
6490 /* Check the memory alignment. Blocks cannot straddle pages */
6491 for (seg = 0; seg < nr_segs; seg++) {
6492 addr = (unsigned long)iov[seg].iov_base;
6493 size = iov[seg].iov_len;
6494 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006495 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006496 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006497
6498 /* If this is a write we don't need to check anymore */
6499 if (rw & WRITE)
6500 continue;
6501
6502 /*
6503 * Check to make sure we don't have duplicate iov_base's in this
6504 * iovec, if so return EINVAL, otherwise we'll get csum errors
6505 * when reading back.
6506 */
6507 for (i = seg + 1; i < nr_segs; i++) {
6508 if (iov[seg].iov_base == iov[i].iov_base)
6509 goto out;
6510 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006511 }
6512 retval = 0;
6513out:
6514 return retval;
6515}
Josef Bacikeb838e72012-07-31 16:28:48 -04006516
Chris Mason16432982008-04-10 10:23:21 -04006517static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6518 const struct iovec *iov, loff_t offset,
6519 unsigned long nr_segs)
6520{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006521 struct file *file = iocb->ki_filp;
6522 struct inode *inode = file->f_mapping->host;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006523
Chris Mason5a5f79b2010-05-26 21:33:37 -04006524 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
Josef Bacikeb838e72012-07-31 16:28:48 -04006525 offset, nr_segs))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006526 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006527
Josef Bacikeb838e72012-07-31 16:28:48 -04006528 return __blockdev_direct_IO(rw, iocb, inode,
Chris Mason5a5f79b2010-05-26 21:33:37 -04006529 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6530 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6531 btrfs_submit_direct, 0);
Chris Mason16432982008-04-10 10:23:21 -04006532}
6533
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006534static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6535 __u64 start, __u64 len)
6536{
Chris Masonec29ed52011-02-23 16:23:20 -05006537 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006538}
6539
Chris Mason9ebefb182007-06-15 13:50:00 -04006540int btrfs_readpage(struct file *file, struct page *page)
6541{
Chris Masond1310b22008-01-24 16:13:08 -05006542 struct extent_io_tree *tree;
6543 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006544 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006545}
Chris Mason1832a6d2007-12-21 16:27:21 -05006546
Chris Mason39279cc2007-06-12 06:35:45 -04006547static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6548{
Chris Masond1310b22008-01-24 16:13:08 -05006549 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006550
6551
6552 if (current->flags & PF_MEMALLOC) {
6553 redirty_page_for_writepage(wbc, page);
6554 unlock_page(page);
6555 return 0;
6556 }
Chris Masond1310b22008-01-24 16:13:08 -05006557 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006558 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6559}
Chris Mason39279cc2007-06-12 06:35:45 -04006560
Chris Masonf4219502008-07-22 11:18:09 -04006561int btrfs_writepages(struct address_space *mapping,
6562 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04006563{
Chris Masond1310b22008-01-24 16:13:08 -05006564 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006565
Chris Masond1310b22008-01-24 16:13:08 -05006566 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04006567 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6568}
6569
Chris Mason3ab2fb52007-11-08 10:59:22 -05006570static int
6571btrfs_readpages(struct file *file, struct address_space *mapping,
6572 struct list_head *pages, unsigned nr_pages)
6573{
Chris Masond1310b22008-01-24 16:13:08 -05006574 struct extent_io_tree *tree;
6575 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006576 return extent_readpages(tree, mapping, pages, nr_pages,
6577 btrfs_get_extent);
6578}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006579static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006580{
Chris Masond1310b22008-01-24 16:13:08 -05006581 struct extent_io_tree *tree;
6582 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006583 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006584
Chris Masond1310b22008-01-24 16:13:08 -05006585 tree = &BTRFS_I(page->mapping->host)->io_tree;
6586 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006587 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006588 if (ret == 1) {
6589 ClearPagePrivate(page);
6590 set_page_private(page, 0);
6591 page_cache_release(page);
6592 }
6593 return ret;
6594}
Chris Mason39279cc2007-06-12 06:35:45 -04006595
Chris Masone6dcd2d2008-07-17 12:53:50 -04006596static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6597{
Chris Mason98509cf2008-09-11 15:51:43 -04006598 if (PageWriteback(page) || PageDirty(page))
6599 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006600 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006601}
6602
Chris Masona52d9a82007-08-27 16:49:44 -04006603static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6604{
Josef Bacik5fd02042012-05-02 14:00:54 -04006605 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006606 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006607 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006608 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006609 u64 page_start = page_offset(page);
6610 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006611
Chris Mason8b62b722009-09-02 16:53:46 -04006612 /*
6613 * we have the page locked, so new writeback can't start,
6614 * and the dirty bit won't be cleared while we are here.
6615 *
6616 * Wait for IO on this page so that we can safely clear
6617 * the PagePrivate2 bit and do ordered accounting
6618 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006619 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006620
Josef Bacik5fd02042012-05-02 14:00:54 -04006621 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006622 if (offset) {
6623 btrfs_releasepage(page, GFP_NOFS);
6624 return;
6625 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006626 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Josef Bacik5fd02042012-05-02 14:00:54 -04006627 ordered = btrfs_lookup_ordered_extent(inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006628 page_offset(page));
6629 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006630 /*
6631 * IO on this page will never be started, so we need
6632 * to account for any ordered extents now
6633 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006634 clear_extent_bit(tree, page_start, page_end,
6635 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006636 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006637 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006638 /*
6639 * whoever cleared the private bit is responsible
6640 * for the finish_ordered_io
6641 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006642 if (TestClearPagePrivate2(page) &&
6643 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6644 PAGE_CACHE_SIZE, 1)) {
6645 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006646 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006647 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006648 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006649 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006650 }
6651 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006652 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006653 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006654 __btrfs_releasepage(page, GFP_NOFS);
6655
Chris Mason4a096752008-07-21 10:29:44 -04006656 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04006657 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04006658 ClearPagePrivate(page);
6659 set_page_private(page, 0);
6660 page_cache_release(page);
6661 }
Chris Mason39279cc2007-06-12 06:35:45 -04006662}
6663
Chris Mason9ebefb182007-06-15 13:50:00 -04006664/*
6665 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6666 * called from a page fault handler when a page is first dirtied. Hence we must
6667 * be careful to check for EOF conditions here. We set the page up correctly
6668 * for a written page which means we get ENOSPC checking when writing into
6669 * holes and correct delalloc and unwritten extent mapping on filesystems that
6670 * support these features.
6671 *
6672 * We are not allowed to take the i_mutex here so we have to play games to
6673 * protect against truncate races as the page could now be beyond EOF. Because
6674 * vmtruncate() writes the inode size before removing pages, once we have the
6675 * page lock we can determine safely if the page is beyond EOF. If it is not
6676 * beyond EOF, then the page is guaranteed safe against truncation until we
6677 * unlock the page.
6678 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006679int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006680{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006681 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006682 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006683 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006684 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6685 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006686 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006687 char *kaddr;
6688 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006689 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006690 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006691 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006692 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006693 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006694
Jan Karab2b5ef52012-06-12 16:20:45 +02006695 sb_start_pagefault(inode->i_sb);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006696 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006697 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04006698 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006699 reserved = 1;
6700 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006701 if (ret) {
6702 if (ret == -ENOMEM)
6703 ret = VM_FAULT_OOM;
6704 else /* -ENOSPC, -EIO, etc */
6705 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006706 if (reserved)
6707 goto out;
6708 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006709 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006710
Nick Piggin56a76f82009-03-31 15:23:23 -07006711 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006712again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006713 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006714 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006715 page_start = page_offset(page);
6716 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006717
Chris Mason9ebefb182007-06-15 13:50:00 -04006718 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006719 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006720 /* page got truncated out from underneath us */
6721 goto out_unlock;
6722 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006723 wait_on_page_writeback(page);
6724
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006725 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006726 set_page_extent_mapped(page);
6727
Chris Masoneb84ae02008-07-17 13:53:27 -04006728 /*
6729 * we can't set the delalloc bits if there are pending ordered
6730 * extents. Drop our locks and wait for them to finish
6731 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006732 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6733 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006734 unlock_extent_cached(io_tree, page_start, page_end,
6735 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006736 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006737 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006738 btrfs_put_ordered_extent(ordered);
6739 goto again;
6740 }
6741
Josef Bacikfbf19082009-10-01 17:10:23 -04006742 /*
6743 * XXX - page_mkwrite gets called every time the page is dirtied, even
6744 * if it was already dirty, so for space accounting reasons we need to
6745 * clear any delalloc bits for the range we are fixing to save. There
6746 * is probably a better way to do this, but for now keep consistent with
6747 * prepare_pages in the normal write path.
6748 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006749 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006750 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006751 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006752
Josef Bacik2ac55d42010-02-03 19:33:23 +00006753 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6754 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006755 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006756 unlock_extent_cached(io_tree, page_start, page_end,
6757 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006758 ret = VM_FAULT_SIGBUS;
6759 goto out_unlock;
6760 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006761 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006762
6763 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006764 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006765 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006766 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006767 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006768
Chris Masone6dcd2d2008-07-17 12:53:50 -04006769 if (zero_start != PAGE_CACHE_SIZE) {
6770 kaddr = kmap(page);
6771 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6772 flush_dcache_page(page);
6773 kunmap(page);
6774 }
Chris Mason247e7432008-07-17 12:53:51 -04006775 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006776 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006777 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006778
Chris Mason257c62e2009-10-13 13:21:08 -04006779 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6780 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6781
Josef Bacik2ac55d42010-02-03 19:33:23 +00006782 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006783
6784out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02006785 if (!ret) {
6786 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04006787 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02006788 }
Chris Mason9ebefb182007-06-15 13:50:00 -04006789 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006790out:
Josef Bacikec39e182012-01-12 19:10:12 -05006791 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006792out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02006793 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04006794 return ret;
6795}
6796
Josef Bacika41ad392011-01-31 15:30:16 -05006797static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006798{
6799 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006800 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006801 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006802 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006803 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006804 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006805 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006806 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006807
Josef Bacik5d5e1032009-10-13 16:46:49 -04006808 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6809 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006810 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006811
Chris Mason4a096752008-07-21 10:29:44 -04006812 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006813 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006814
Josef Bacikfcb80c22011-05-03 10:40:22 -04006815 /*
6816 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6817 * 3 things going on here
6818 *
6819 * 1) We need to reserve space for our orphan item and the space to
6820 * delete our orphan item. Lord knows we don't want to have a dangling
6821 * orphan item because we didn't reserve space to remove it.
6822 *
6823 * 2) We need to reserve space to update our inode.
6824 *
6825 * 3) We need to have something to cache all the space that is going to
6826 * be free'd up by the truncate operation, but also have some slack
6827 * space reserved in case it uses space during the truncate (thank you
6828 * very much snapshotting).
6829 *
6830 * And we need these to all be seperate. The fact is we can use alot of
6831 * space doing the truncate, and we have no earthly idea how much space
6832 * we will use, so we need the truncate reservation to be seperate so it
6833 * doesn't end up using space reserved for updating the inode or
6834 * removing the orphan item. We also need to be able to stop the
6835 * transaction and start a new one, which means we need to be able to
6836 * update the inode several times, and we have no idea of knowing how
6837 * many times that will be, so we can't just reserve 1 item for the
6838 * entirety of the opration, so that has to be done seperately as well.
6839 * Then there is the orphan item, which does indeed need to be held on
6840 * to for the whole operation, and we need nobody to touch this reserved
6841 * space except the orphan code.
6842 *
6843 * So that leaves us with
6844 *
6845 * 1) root->orphan_block_rsv - for the orphan deletion.
6846 * 2) rsv - for the truncate reservation, which we will steal from the
6847 * transaction reservation.
6848 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6849 * updating the inode.
6850 */
6851 rsv = btrfs_alloc_block_rsv(root);
6852 if (!rsv)
6853 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006854 rsv->size = min_size;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006855
Josef Bacik907cbce2011-08-08 13:46:15 -04006856 /*
Josef Bacik07127182011-08-19 10:29:59 -04006857 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006858 * 1 for the orphan item we're going to add
6859 * 1 for the orphan item deletion
6860 * 1 for updating the inode.
6861 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04006862 trans = btrfs_start_transaction(root, 4);
6863 if (IS_ERR(trans)) {
6864 err = PTR_ERR(trans);
6865 goto out;
6866 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006867
Josef Bacik907cbce2011-08-08 13:46:15 -04006868 /* Migrate the slack space for the truncate to our reserve */
6869 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6870 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006871 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006872
6873 ret = btrfs_orphan_add(trans, inode);
6874 if (ret) {
6875 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006876 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006877 }
6878
Chris Mason5a3f23d2009-03-31 13:27:11 -04006879 /*
6880 * setattr is responsible for setting the ordered_data_close flag,
6881 * but that is only tested during the last file release. That
6882 * could happen well after the next commit, leaving a great big
6883 * window where new writes may get lost if someone chooses to write
6884 * to this file after truncating to zero
6885 *
6886 * The inode doesn't have any dirty data here, and so if we commit
6887 * this is a noop. If someone immediately starts writing to the inode
6888 * it is very likely we'll catch some of their writes in this
6889 * transaction, and the commit will find this file on the ordered
6890 * data list with good things to send down.
6891 *
6892 * This is a best effort solution, there is still a window where
6893 * using truncate to replace the contents of the file will
6894 * end up with a zero length file after a crash.
6895 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04006896 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
6897 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04006898 btrfs_add_ordered_operation(trans, root, inode);
6899
Josef Bacik5dc562c2012-08-17 13:14:17 -04006900 /*
6901 * So if we truncate and then write and fsync we normally would just
6902 * write the extents that changed, which is a problem if we need to
6903 * first truncate that entire inode. So set this flag so we write out
6904 * all of the extents in the inode to the sync log so we're completely
6905 * safe.
6906 */
6907 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6908
Yan, Zheng80825102009-11-12 09:35:36 +00006909 while (1) {
Josef Bacik36ba0222011-10-18 12:15:48 -04006910 ret = btrfs_block_rsv_refill(root, rsv, min_size);
Josef Bacik907cbce2011-08-08 13:46:15 -04006911 if (ret) {
6912 /*
6913 * This can only happen with the original transaction we
6914 * started above, every other time we shouldn't have a
6915 * transaction started yet.
6916 */
6917 if (ret == -EAGAIN)
6918 goto end_trans;
6919 err = ret;
6920 break;
6921 }
6922
Yan, Zhengd68fc572010-05-16 10:49:58 -04006923 if (!trans) {
Josef Bacik907cbce2011-08-08 13:46:15 -04006924 /* Just need the 1 for updating the inode */
6925 trans = btrfs_start_transaction(root, 1);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006926 if (IS_ERR(trans)) {
Josef Bacik7041ee92011-12-09 13:26:22 -05006927 ret = err = PTR_ERR(trans);
6928 trans = NULL;
6929 break;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006930 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006931 }
6932
Josef Bacik907cbce2011-08-08 13:46:15 -04006933 trans->block_rsv = rsv;
6934
Yan, Zheng80825102009-11-12 09:35:36 +00006935 ret = btrfs_truncate_inode_items(trans, root, inode,
6936 inode->i_size,
6937 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006938 if (ret != -EAGAIN) {
6939 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006940 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006941 }
Chris Mason39279cc2007-06-12 06:35:45 -04006942
Josef Bacikfcb80c22011-05-03 10:40:22 -04006943 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006944 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006945 if (ret) {
6946 err = ret;
6947 break;
6948 }
Josef Bacik907cbce2011-08-08 13:46:15 -04006949end_trans:
Yan, Zheng80825102009-11-12 09:35:36 +00006950 nr = trans->blocks_used;
6951 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006952 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006953 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006954 }
6955
6956 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04006957 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006958 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006959 if (ret)
6960 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006961 } else if (ret && inode->i_nlink > 0) {
6962 /*
6963 * Failed to do the truncate, remove us from the in memory
6964 * orphan list.
6965 */
6966 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006967 }
6968
Chris Mason917c16b2011-11-08 14:49:59 -05006969 if (trans) {
6970 trans->block_rsv = &root->fs_info->trans_block_rsv;
6971 ret = btrfs_update_inode(trans, root, inode);
6972 if (ret && !err)
6973 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006974
Chris Mason917c16b2011-11-08 14:49:59 -05006975 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006976 ret = btrfs_end_transaction(trans, root);
Chris Mason917c16b2011-11-08 14:49:59 -05006977 btrfs_btree_balance_dirty(root, nr);
6978 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04006979
6980out:
6981 btrfs_free_block_rsv(root, rsv);
6982
Josef Bacik3893e332011-01-31 16:03:11 -05006983 if (ret && !err)
6984 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05006985
Josef Bacik3893e332011-01-31 16:03:11 -05006986 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006987}
6988
Sven Wegener3b963622008-06-09 21:57:42 -04006989/*
Chris Masond352ac62008-09-29 15:18:18 -04006990 * create a new subvolume directory/inode (helper for the ioctl).
6991 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006992int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006993 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04006994{
Chris Mason39279cc2007-06-12 06:35:45 -04006995 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006996 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006997 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006998
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01006999 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7000 new_dirid, new_dirid,
7001 S_IFDIR | (~current_umask() & S_IRWXUGO),
7002 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04007003 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04007004 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007005 inode->i_op = &btrfs_dir_inode_operations;
7006 inode->i_fop = &btrfs_dir_file_operations;
7007
Miklos Szeredibfe86842011-10-28 14:13:29 +02007008 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04007009 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04007010
Yan, Zheng76dda932009-09-21 16:00:26 -04007011 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04007012
Yan, Zheng76dda932009-09-21 16:00:26 -04007013 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07007014 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04007015}
7016
Chris Mason39279cc2007-06-12 06:35:45 -04007017struct inode *btrfs_alloc_inode(struct super_block *sb)
7018{
7019 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007020 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007021
7022 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7023 if (!ei)
7024 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007025
7026 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007027 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04007028 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04007029 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04007030 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007031 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007032 ei->disk_i_size = 0;
7033 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04007034 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007035 ei->index_cnt = (u64)-1;
7036 ei->last_unlink_trans = 0;
7037
Josef Bacik9e0baf62011-07-15 15:16:44 +00007038 spin_lock_init(&ei->lock);
7039 ei->outstanding_extents = 0;
7040 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007041
Josef Bacik72ac3c02012-05-23 14:13:11 -04007042 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08007043 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007044
Miao Xie16cdcec2011-04-22 18:12:22 +08007045 ei->delayed_node = NULL;
7046
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007047 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02007048 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02007049 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7050 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04007051 ei->io_tree.track_uptodate = 1;
7052 ei->io_failure_tree.track_uptodate = 1;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007053 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05007054 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007055 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007056 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007057 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04007058 RB_CLEAR_NODE(&ei->rb_node);
7059
7060 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04007061}
7062
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007063static void btrfs_i_callback(struct rcu_head *head)
7064{
7065 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007066 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7067}
7068
Chris Mason39279cc2007-06-12 06:35:45 -04007069void btrfs_destroy_inode(struct inode *inode)
7070{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007071 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007072 struct btrfs_root *root = BTRFS_I(inode)->root;
7073
Al Virob3d9b7a2012-06-09 13:51:19 -04007074 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04007075 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00007076 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7077 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04007078 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7079 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04007080
Chris Mason5a3f23d2009-03-31 13:27:11 -04007081 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05007082 * This can happen where we create an inode, but somebody else also
7083 * created the same inode and we need to destroy the one we already
7084 * created.
7085 */
7086 if (!root)
7087 goto free;
7088
7089 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007090 * Make sure we're properly removed from the ordered operation
7091 * lists.
7092 */
7093 smp_mb();
7094 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7095 spin_lock(&root->fs_info->ordered_extent_lock);
7096 list_del_init(&BTRFS_I(inode)->ordered_operations);
7097 spin_unlock(&root->fs_info->ordered_extent_lock);
7098 }
7099
Josef Bacik8a35d952012-05-23 14:26:42 -04007100 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7101 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08007102 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7103 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04007104 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04007105 }
Josef Bacik7b128762008-07-24 12:17:14 -04007106
Chris Masond3977122009-01-05 21:25:51 -05007107 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04007108 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7109 if (!ordered)
7110 break;
7111 else {
Chris Masond3977122009-01-05 21:25:51 -05007112 printk(KERN_ERR "btrfs found ordered "
7113 "extent %llu %llu on inode cleanup\n",
7114 (unsigned long long)ordered->file_offset,
7115 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007116 btrfs_remove_ordered_extent(inode, ordered);
7117 btrfs_put_ordered_extent(ordered);
7118 btrfs_put_ordered_extent(ordered);
7119 }
7120 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007121 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007122 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05007123free:
Miao Xie16cdcec2011-04-22 18:12:22 +08007124 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11007125 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04007126}
7127
Al Viro45321ac2010-06-07 13:43:19 -04007128int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04007129{
7130 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04007131
Josef Bacik0af3d002010-06-21 14:48:16 -04007132 if (btrfs_root_refs(&root->root_item) == 0 &&
Liu Bo83eea1f2012-07-10 05:28:39 -06007133 !btrfs_is_free_space_inode(inode))
Al Viro45321ac2010-06-07 13:43:19 -04007134 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04007135 else
Al Viro45321ac2010-06-07 13:43:19 -04007136 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04007137}
7138
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007139static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04007140{
7141 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7142
7143 inode_init_once(&ei->vfs_inode);
7144}
7145
7146void btrfs_destroy_cachep(void)
7147{
7148 if (btrfs_inode_cachep)
7149 kmem_cache_destroy(btrfs_inode_cachep);
7150 if (btrfs_trans_handle_cachep)
7151 kmem_cache_destroy(btrfs_trans_handle_cachep);
7152 if (btrfs_transaction_cachep)
7153 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007154 if (btrfs_path_cachep)
7155 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007156 if (btrfs_free_space_cachep)
7157 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007158}
7159
7160int btrfs_init_cachep(void)
7161{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007162 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
7163 sizeof(struct btrfs_inode), 0,
7164 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007165 if (!btrfs_inode_cachep)
7166 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007167
7168 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
7169 sizeof(struct btrfs_trans_handle), 0,
7170 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007171 if (!btrfs_trans_handle_cachep)
7172 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007173
7174 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
7175 sizeof(struct btrfs_transaction), 0,
7176 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007177 if (!btrfs_transaction_cachep)
7178 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007179
7180 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
7181 sizeof(struct btrfs_path), 0,
7182 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007183 if (!btrfs_path_cachep)
7184 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007185
Josef Bacikdc89e982011-01-28 17:05:48 -05007186 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
7187 sizeof(struct btrfs_free_space), 0,
7188 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7189 if (!btrfs_free_space_cachep)
7190 goto fail;
7191
Chris Mason39279cc2007-06-12 06:35:45 -04007192 return 0;
7193fail:
7194 btrfs_destroy_cachep();
7195 return -ENOMEM;
7196}
7197
7198static int btrfs_getattr(struct vfsmount *mnt,
7199 struct dentry *dentry, struct kstat *stat)
7200{
7201 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007202 u32 blocksize = inode->i_sb->s_blocksize;
7203
Chris Mason39279cc2007-06-12 06:35:45 -04007204 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007205 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007206 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007207 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7208 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007209 return 0;
7210}
7211
Liu Bo75e7cb72011-03-22 10:12:20 +00007212/*
7213 * If a file is moved, it will inherit the cow and compression flags of the new
7214 * directory.
7215 */
7216static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7217{
7218 struct btrfs_inode *b_dir = BTRFS_I(dir);
7219 struct btrfs_inode *b_inode = BTRFS_I(inode);
7220
7221 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7222 b_inode->flags |= BTRFS_INODE_NODATACOW;
7223 else
7224 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7225
Liu Bobc178232012-06-14 02:23:18 -06007226 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
Liu Bo75e7cb72011-03-22 10:12:20 +00007227 b_inode->flags |= BTRFS_INODE_COMPRESS;
Liu Bobc178232012-06-14 02:23:18 -06007228 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7229 } else {
7230 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7231 BTRFS_INODE_NOCOMPRESS);
7232 }
Liu Bo75e7cb72011-03-22 10:12:20 +00007233}
7234
Chris Masond3977122009-01-05 21:25:51 -05007235static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7236 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007237{
7238 struct btrfs_trans_handle *trans;
7239 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007240 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007241 struct inode *new_inode = new_dentry->d_inode;
7242 struct inode *old_inode = old_dentry->d_inode;
7243 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007244 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007245 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007246 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007247 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007248
Li Zefan33345d012011-04-20 10:31:50 +08007249 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007250 return -EPERM;
7251
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007252 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007253 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007254 return -EXDEV;
7255
Li Zefan33345d012011-04-20 10:31:50 +08007256 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7257 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007258 return -ENOTEMPTY;
7259
Chris Mason39279cc2007-06-12 06:35:45 -04007260 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007261 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007262 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007263 /*
7264 * we're using rename to replace one file with another.
7265 * and the replacement file is large. Start IO on it now so
7266 * we don't add too much work to the end of the transaction
7267 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007268 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007269 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7270 filemap_flush(old_inode->i_mapping);
7271
Yan, Zheng76dda932009-09-21 16:00:26 -04007272 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007273 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007274 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007275 /*
7276 * We want to reserve the absolute worst case amount of items. So if
7277 * both inodes are subvols and we need to unlink them then that would
7278 * require 4 item modifications, but if they are both normal inodes it
7279 * would require 5 item modifications, so we'll assume their normal
7280 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7281 * should cover the worst case number of items we'll modify.
7282 */
7283 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007284 if (IS_ERR(trans)) {
7285 ret = PTR_ERR(trans);
7286 goto out_notrans;
7287 }
Chris Mason5f39d392007-10-15 16:14:19 -04007288
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007289 if (dest != root)
7290 btrfs_record_root_in_trans(trans, dest);
7291
Yan, Zhenga5719522009-09-24 09:17:31 -04007292 ret = btrfs_set_inode_index(new_dir, &index);
7293 if (ret)
7294 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007295
Li Zefan33345d012011-04-20 10:31:50 +08007296 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007297 /* force full log commit if subvolume involved. */
7298 root->fs_info->last_trans_log_full_commit = trans->transid;
7299 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007300 ret = btrfs_insert_inode_ref(trans, dest,
7301 new_dentry->d_name.name,
7302 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007303 old_ino,
7304 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007305 if (ret)
7306 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007307 /*
7308 * this is an ugly little race, but the rename is required
7309 * to make sure that if we crash, the inode is either at the
7310 * old name or the new one. pinning the log transaction lets
7311 * us make sure we don't allow a log commit to come in after
7312 * we unlink the name but before we add the new name back in.
7313 */
7314 btrfs_pin_log_trans(root);
7315 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007316 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007317 * make sure the inode gets flushed if it is replacing
7318 * something.
7319 */
Li Zefan33345d012011-04-20 10:31:50 +08007320 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007321 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007322
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007323 inode_inc_iversion(old_dir);
7324 inode_inc_iversion(new_dir);
7325 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007326 old_dir->i_ctime = old_dir->i_mtime = ctime;
7327 new_dir->i_ctime = new_dir->i_mtime = ctime;
7328 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007329
Chris Mason12fcfd22009-03-24 10:24:20 -04007330 if (old_dentry->d_parent != new_dentry->d_parent)
7331 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7332
Li Zefan33345d012011-04-20 10:31:50 +08007333 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007334 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7335 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7336 old_dentry->d_name.name,
7337 old_dentry->d_name.len);
7338 } else {
Al Viro92986792011-03-04 17:14:37 +00007339 ret = __btrfs_unlink_inode(trans, root, old_dir,
7340 old_dentry->d_inode,
7341 old_dentry->d_name.name,
7342 old_dentry->d_name.len);
7343 if (!ret)
7344 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007345 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007346 if (ret) {
7347 btrfs_abort_transaction(trans, root, ret);
7348 goto out_fail;
7349 }
Chris Mason39279cc2007-06-12 06:35:45 -04007350
7351 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007352 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007353 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007354 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007355 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7356 root_objectid = BTRFS_I(new_inode)->location.objectid;
7357 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7358 root_objectid,
7359 new_dentry->d_name.name,
7360 new_dentry->d_name.len);
7361 BUG_ON(new_inode->i_nlink == 0);
7362 } else {
7363 ret = btrfs_unlink_inode(trans, dest, new_dir,
7364 new_dentry->d_inode,
7365 new_dentry->d_name.name,
7366 new_dentry->d_name.len);
7367 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007368 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007369 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007370 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007371 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007372 if (ret) {
7373 btrfs_abort_transaction(trans, root, ret);
7374 goto out_fail;
7375 }
Chris Mason39279cc2007-06-12 06:35:45 -04007376 }
Josef Bacikaec74772008-07-24 12:12:38 -04007377
Liu Bo75e7cb72011-03-22 10:12:20 +00007378 fixup_inode_flags(new_dir, old_inode);
7379
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007380 ret = btrfs_add_link(trans, new_dir, old_inode,
7381 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007382 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007383 if (ret) {
7384 btrfs_abort_transaction(trans, root, ret);
7385 goto out_fail;
7386 }
Chris Mason39279cc2007-06-12 06:35:45 -04007387
Li Zefan33345d012011-04-20 10:31:50 +08007388 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007389 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007390 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007391 btrfs_end_log_trans(root);
7392 }
Chris Mason39279cc2007-06-12 06:35:45 -04007393out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007394 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007395out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007396 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007397 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007398
Chris Mason39279cc2007-06-12 06:35:45 -04007399 return ret;
7400}
7401
Chris Masond352ac62008-09-29 15:18:18 -04007402/*
7403 * some fairly slow code that needs optimization. This walks the list
7404 * of all the inodes with pending delalloc and forces them to disk.
7405 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007406int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007407{
7408 struct list_head *head = &root->fs_info->delalloc_inodes;
7409 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007410 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007411
Yan Zhengc146afa2008-11-12 14:34:12 -05007412 if (root->fs_info->sb->s_flags & MS_RDONLY)
7413 return -EROFS;
7414
Chris Mason75eff682008-12-15 15:54:40 -05007415 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007416 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007417 binode = list_entry(head->next, struct btrfs_inode,
7418 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007419 inode = igrab(&binode->vfs_inode);
7420 if (!inode)
7421 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007422 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007423 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007424 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007425 if (delay_iput)
7426 btrfs_add_delayed_iput(inode);
7427 else
7428 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007429 }
7430 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007431 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007432 }
Chris Mason75eff682008-12-15 15:54:40 -05007433 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007434
7435 /* the filemap_flush will queue IO into the worker threads, but
7436 * we have to make sure the IO is actually started and that
7437 * ordered extents get created before we return
7438 */
7439 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007440 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007441 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007442 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007443 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7444 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007445 }
7446 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007447 return 0;
7448}
7449
Chris Mason39279cc2007-06-12 06:35:45 -04007450static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7451 const char *symname)
7452{
7453 struct btrfs_trans_handle *trans;
7454 struct btrfs_root *root = BTRFS_I(dir)->root;
7455 struct btrfs_path *path;
7456 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007457 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007458 int err;
7459 int drop_inode = 0;
7460 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007461 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007462 int name_len;
7463 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007464 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007465 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007466 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007467 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007468
7469 name_len = strlen(symname) + 1;
7470 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7471 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007472
Josef Bacik9ed74f22009-09-11 16:12:44 -04007473 /*
7474 * 2 items for inode item and ref
7475 * 2 items for dir items
7476 * 1 item for xattr if selinux is on
7477 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007478 trans = btrfs_start_transaction(root, 5);
7479 if (IS_ERR(trans))
7480 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007481
Li Zefan581bb052011-04-20 10:06:11 +08007482 err = btrfs_find_free_ino(root, &objectid);
7483 if (err)
7484 goto out_unlock;
7485
Josef Bacikaec74772008-07-24 12:12:38 -04007486 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007487 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04007488 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007489 if (IS_ERR(inode)) {
7490 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007491 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007492 }
Chris Mason39279cc2007-06-12 06:35:45 -04007493
Eric Paris2a7dba32011-02-01 11:05:39 -05007494 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007495 if (err) {
7496 drop_inode = 1;
7497 goto out_unlock;
7498 }
7499
Casey Schauflerad19db72011-12-15 10:09:07 -05007500 /*
7501 * If the active LSM wants to access the inode during
7502 * d_instantiate it needs these. Smack checks to see
7503 * if the filesystem supports xattrs by looking at the
7504 * ops vector.
7505 */
7506 inode->i_fop = &btrfs_file_operations;
7507 inode->i_op = &btrfs_file_inode_operations;
7508
Josef Bacika1b075d2010-11-19 20:36:11 +00007509 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007510 if (err)
7511 drop_inode = 1;
7512 else {
7513 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007514 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007515 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007516 }
Chris Mason39279cc2007-06-12 06:35:45 -04007517 if (drop_inode)
7518 goto out_unlock;
7519
7520 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007521 if (!path) {
7522 err = -ENOMEM;
7523 drop_inode = 1;
7524 goto out_unlock;
7525 }
Li Zefan33345d012011-04-20 10:31:50 +08007526 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007527 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007528 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7529 datasize = btrfs_file_extent_calc_inline_size(name_len);
7530 err = btrfs_insert_empty_item(trans, root, path, &key,
7531 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007532 if (err) {
7533 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007534 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007535 goto out_unlock;
7536 }
Chris Mason5f39d392007-10-15 16:14:19 -04007537 leaf = path->nodes[0];
7538 ei = btrfs_item_ptr(leaf, path->slots[0],
7539 struct btrfs_file_extent_item);
7540 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7541 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007542 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007543 btrfs_set_file_extent_encryption(leaf, ei, 0);
7544 btrfs_set_file_extent_compression(leaf, ei, 0);
7545 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7546 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7547
Chris Mason39279cc2007-06-12 06:35:45 -04007548 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007549 write_extent_buffer(leaf, symname, ptr, name_len);
7550 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007551 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007552
Chris Mason39279cc2007-06-12 06:35:45 -04007553 inode->i_op = &btrfs_symlink_inode_operations;
7554 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007555 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007556 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007557 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007558 err = btrfs_update_inode(trans, root, inode);
7559 if (err)
7560 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007561
7562out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007563 if (!err)
7564 d_instantiate(dentry, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007565 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007566 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007567 if (drop_inode) {
7568 inode_dec_link_count(inode);
7569 iput(inode);
7570 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007571 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007572 return err;
7573}
Chris Mason16432982008-04-10 10:23:21 -04007574
Josef Bacik0af3d002010-06-21 14:48:16 -04007575static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7576 u64 start, u64 num_bytes, u64 min_size,
7577 loff_t actual_len, u64 *alloc_hint,
7578 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007579{
Josef Bacik5dc562c2012-08-17 13:14:17 -04007580 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7581 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04007582 struct btrfs_root *root = BTRFS_I(inode)->root;
7583 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007584 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007585 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007586 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007587 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007588
Josef Bacik0af3d002010-06-21 14:48:16 -04007589 if (trans)
7590 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007591 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007592 if (own_trans) {
7593 trans = btrfs_start_transaction(root, 3);
7594 if (IS_ERR(trans)) {
7595 ret = PTR_ERR(trans);
7596 break;
7597 }
Yan Zhengd899e052008-10-30 14:25:28 -04007598 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007599
Yan, Zhengefa56462010-05-16 10:49:59 -04007600 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007601 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007602 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007603 if (own_trans)
7604 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007605 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007606 }
7607
Yan Zhengd899e052008-10-30 14:25:28 -04007608 ret = insert_reserved_file_extent(trans, inode,
7609 cur_offset, ins.objectid,
7610 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007611 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007612 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007613 if (ret) {
7614 btrfs_abort_transaction(trans, root, ret);
7615 if (own_trans)
7616 btrfs_end_transaction(trans, root);
7617 break;
7618 }
Chris Masona1ed8352009-09-11 12:27:37 -04007619 btrfs_drop_extent_cache(inode, cur_offset,
7620 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007621
Josef Bacik5dc562c2012-08-17 13:14:17 -04007622 em = alloc_extent_map();
7623 if (!em) {
7624 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7625 &BTRFS_I(inode)->runtime_flags);
7626 goto next;
7627 }
7628
7629 em->start = cur_offset;
7630 em->orig_start = cur_offset;
7631 em->len = ins.offset;
7632 em->block_start = ins.objectid;
7633 em->block_len = ins.offset;
7634 em->bdev = root->fs_info->fs_devices->latest_bdev;
7635 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7636 em->generation = trans->transid;
7637
7638 while (1) {
7639 write_lock(&em_tree->lock);
7640 ret = add_extent_mapping(em_tree, em);
7641 if (!ret)
7642 list_move(&em->list,
7643 &em_tree->modified_extents);
7644 write_unlock(&em_tree->lock);
7645 if (ret != -EEXIST)
7646 break;
7647 btrfs_drop_extent_cache(inode, cur_offset,
7648 cur_offset + ins.offset - 1,
7649 0);
7650 }
7651 free_extent_map(em);
7652next:
Yan Zhengd899e052008-10-30 14:25:28 -04007653 num_bytes -= ins.offset;
7654 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007655 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007656
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007657 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007658 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007659 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007660 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007661 (actual_len > inode->i_size) &&
7662 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007663 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007664 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007665 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007666 i_size = cur_offset;
7667 i_size_write(inode, i_size);
7668 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007669 }
7670
Yan Zhengd899e052008-10-30 14:25:28 -04007671 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007672
7673 if (ret) {
7674 btrfs_abort_transaction(trans, root, ret);
7675 if (own_trans)
7676 btrfs_end_transaction(trans, root);
7677 break;
7678 }
Yan Zhengd899e052008-10-30 14:25:28 -04007679
Josef Bacik0af3d002010-06-21 14:48:16 -04007680 if (own_trans)
7681 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007682 }
Yan Zhengd899e052008-10-30 14:25:28 -04007683 return ret;
7684}
7685
Josef Bacik0af3d002010-06-21 14:48:16 -04007686int btrfs_prealloc_file_range(struct inode *inode, int mode,
7687 u64 start, u64 num_bytes, u64 min_size,
7688 loff_t actual_len, u64 *alloc_hint)
7689{
7690 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7691 min_size, actual_len, alloc_hint,
7692 NULL);
7693}
7694
7695int btrfs_prealloc_file_range_trans(struct inode *inode,
7696 struct btrfs_trans_handle *trans, int mode,
7697 u64 start, u64 num_bytes, u64 min_size,
7698 loff_t actual_len, u64 *alloc_hint)
7699{
7700 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7701 min_size, actual_len, alloc_hint, trans);
7702}
7703
Chris Masone6dcd2d2008-07-17 12:53:50 -04007704static int btrfs_set_page_dirty(struct page *page)
7705{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007706 return __set_page_dirty_nobuffers(page);
7707}
7708
Al Viro10556cb22011-06-20 19:28:19 -04007709static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007710{
Li Zefanb83cc962010-12-20 16:04:08 +08007711 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007712 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007713
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007714 if (mask & MAY_WRITE &&
7715 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7716 if (btrfs_root_readonly(root))
7717 return -EROFS;
7718 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7719 return -EACCES;
7720 }
Al Viro2830ba72011-06-20 19:16:29 -04007721 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007722}
Chris Mason39279cc2007-06-12 06:35:45 -04007723
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007724static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007725 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007726 .lookup = btrfs_lookup,
7727 .create = btrfs_create,
7728 .unlink = btrfs_unlink,
7729 .link = btrfs_link,
7730 .mkdir = btrfs_mkdir,
7731 .rmdir = btrfs_rmdir,
7732 .rename = btrfs_rename,
7733 .symlink = btrfs_symlink,
7734 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007735 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007736 .setxattr = btrfs_setxattr,
7737 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007738 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007739 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007740 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007741 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007742};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007743static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007744 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007745 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007746 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007747};
Yan, Zheng76dda932009-09-21 16:00:26 -04007748
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007749static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007750 .llseek = generic_file_llseek,
7751 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007752 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007753 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007754#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007755 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007756#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007757 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007758 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007759};
7760
Chris Masond1310b22008-01-24 16:13:08 -05007761static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007762 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007763 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007764 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007765 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007766 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007767 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007768 .set_bit_hook = btrfs_set_bit_hook,
7769 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007770 .merge_extent_hook = btrfs_merge_extent_hook,
7771 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007772};
7773
Chris Mason35054392009-01-21 13:11:13 -05007774/*
7775 * btrfs doesn't support the bmap operation because swapfiles
7776 * use bmap to make a mapping of extents in the file. They assume
7777 * these extents won't change over the life of the file and they
7778 * use the bmap result to do IO directly to the drive.
7779 *
7780 * the btrfs bmap call would return logical addresses that aren't
7781 * suitable for IO and they also will change frequently as COW
7782 * operations happen. So, swapfile + btrfs == corruption.
7783 *
7784 * For now we're avoiding this by dropping bmap.
7785 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007786static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007787 .readpage = btrfs_readpage,
7788 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04007789 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007790 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007791 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007792 .invalidatepage = btrfs_invalidatepage,
7793 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007794 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007795 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007796};
7797
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007798static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007799 .readpage = btrfs_readpage,
7800 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007801 .invalidatepage = btrfs_invalidatepage,
7802 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007803};
7804
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007805static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007806 .getattr = btrfs_getattr,
7807 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007808 .setxattr = btrfs_setxattr,
7809 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007810 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007811 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007812 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007813 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007814 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007815 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007816};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007817static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007818 .getattr = btrfs_getattr,
7819 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007820 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007821 .setxattr = btrfs_setxattr,
7822 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007823 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007824 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007825 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007826 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -04007827};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007828static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007829 .readlink = generic_readlink,
7830 .follow_link = page_follow_link_light,
7831 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007832 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05007833 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007834 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007835 .setxattr = btrfs_setxattr,
7836 .getxattr = btrfs_getxattr,
7837 .listxattr = btrfs_listxattr,
7838 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007839 .get_acl = btrfs_get_acl,
Josef Bacike41f9412012-03-26 09:46:47 -04007840 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -04007841};
Yan, Zheng76dda932009-09-21 16:00:26 -04007842
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007843const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007844 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007845 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04007846};