blob: 029892887fc1f364bb53cbfbd273f4fda0aeb8f4 [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
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000250 ret = btrfs_drop_extents(trans, 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);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100260 if (ret) {
261 btrfs_abort_transaction(trans, root, ret);
262 return ret;
263 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400264 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400265 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400266 return 0;
267}
268
Chris Mason771ed682008-11-06 22:02:51 -0500269struct async_extent {
270 u64 start;
271 u64 ram_size;
272 u64 compressed_size;
273 struct page **pages;
274 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800275 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500276 struct list_head list;
277};
278
279struct async_cow {
280 struct inode *inode;
281 struct btrfs_root *root;
282 struct page *locked_page;
283 u64 start;
284 u64 end;
285 struct list_head extents;
286 struct btrfs_work work;
287};
288
289static noinline int add_async_extent(struct async_cow *cow,
290 u64 start, u64 ram_size,
291 u64 compressed_size,
292 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800293 unsigned long nr_pages,
294 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500295{
296 struct async_extent *async_extent;
297
298 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100299 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500300 async_extent->start = start;
301 async_extent->ram_size = ram_size;
302 async_extent->compressed_size = compressed_size;
303 async_extent->pages = pages;
304 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800305 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500306 list_add_tail(&async_extent->list, &cow->extents);
307 return 0;
308}
309
Chris Masonc8b97812008-10-29 14:49:59 -0400310/*
Chris Mason771ed682008-11-06 22:02:51 -0500311 * we create compressed extents in two phases. The first
312 * phase compresses a range of pages that have already been
313 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400314 *
Chris Mason771ed682008-11-06 22:02:51 -0500315 * This is done inside an ordered work queue, and the compression
316 * is spread across many cpus. The actual IO submission is step
317 * two, and the ordered work queue takes care of making sure that
318 * happens in the same order things were put onto the queue by
319 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400320 *
Chris Mason771ed682008-11-06 22:02:51 -0500321 * If this code finds it can't get good compression, it puts an
322 * entry onto the work queue to write the uncompressed bytes. This
323 * makes sure that both compressed inodes and uncompressed inodes
324 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400325 */
Chris Mason771ed682008-11-06 22:02:51 -0500326static noinline int compress_file_range(struct inode *inode,
327 struct page *locked_page,
328 u64 start, u64 end,
329 struct async_cow *async_cow,
330 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400331{
332 struct btrfs_root *root = BTRFS_I(inode)->root;
333 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400334 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400335 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400336 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500337 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400338 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400339 struct page **pages = NULL;
340 unsigned long nr_pages;
341 unsigned long nr_pages_ret = 0;
342 unsigned long total_compressed = 0;
343 unsigned long total_in = 0;
344 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500345 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400346 int i;
347 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800348 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400349
Liu Bo4cb13e52012-03-29 09:57:45 -0400350 /* if this is a small write inside eof, kick off a defrag */
351 if ((end - start + 1) < 16 * 1024 &&
352 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400353 btrfs_add_inode_defrag(NULL, inode);
354
Chris Mason42dc7ba2008-12-15 11:44:56 -0500355 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400356again:
357 will_compress = 0;
358 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
359 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
360
Chris Masonf03d9302009-02-04 09:31:06 -0500361 /*
362 * we don't want to send crud past the end of i_size through
363 * compression, that's just a waste of CPU time. So, if the
364 * end of the file is before the start of our current
365 * requested range of bytes, we bail out to the uncompressed
366 * cleanup code that can deal with all of this.
367 *
368 * It isn't really the fastest way to fix things, but this is a
369 * very uncommon corner.
370 */
371 if (actual_end <= start)
372 goto cleanup_and_bail_uncompressed;
373
Chris Masonc8b97812008-10-29 14:49:59 -0400374 total_compressed = actual_end - start;
375
376 /* we want to make sure that amount of ram required to uncompress
377 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500378 * of a compressed extent to 128k. This is a crucial number
379 * because it also controls how easily we can spread reads across
380 * cpus for decompression.
381 *
382 * We also want to make sure the amount of IO required to do
383 * a random read is reasonably small, so we limit the size of
384 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400385 */
386 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400387 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500388 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400389 total_in = 0;
390 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400391
Chris Mason771ed682008-11-06 22:02:51 -0500392 /*
393 * we do compression for mount -o compress and when the
394 * inode has not been flagged as nocompress. This flag can
395 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400396 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200397 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500398 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000399 (BTRFS_I(inode)->force_compress) ||
400 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400401 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400402 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800403 if (!pages) {
404 /* just bail out to the uncompressed code */
405 goto cont;
406 }
Chris Mason179e29e2007-11-01 11:28:41 -0400407
Li Zefan261507a02010-12-17 14:21:50 +0800408 if (BTRFS_I(inode)->force_compress)
409 compress_type = BTRFS_I(inode)->force_compress;
410
411 ret = btrfs_compress_pages(compress_type,
412 inode->i_mapping, start,
413 total_compressed, pages,
414 nr_pages, &nr_pages_ret,
415 &total_in,
416 &total_compressed,
417 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400418
419 if (!ret) {
420 unsigned long offset = total_compressed &
421 (PAGE_CACHE_SIZE - 1);
422 struct page *page = pages[nr_pages_ret - 1];
423 char *kaddr;
424
425 /* zero the tail end of the last page, we might be
426 * sending it down to disk
427 */
428 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800429 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400430 memset(kaddr + offset, 0,
431 PAGE_CACHE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800432 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400433 }
434 will_compress = 1;
435 }
436 }
Li Zefan560f7d72011-09-08 10:22:01 +0800437cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400438 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400439 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100440 if (IS_ERR(trans)) {
441 ret = PTR_ERR(trans);
442 trans = NULL;
443 goto cleanup_and_out;
444 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400445 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500446
Chris Masonc8b97812008-10-29 14:49:59 -0400447 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500448 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400449 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500450 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400451 */
452 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000453 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400454 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500455 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400456 ret = cow_file_range_inline(trans, root, inode,
457 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000458 total_compressed,
459 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400460 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100461 if (ret <= 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500462 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100463 * inline extent creation worked or returned error,
464 * we don't need to create any more async work items.
465 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500466 */
Chris Masonc8b97812008-10-29 14:49:59 -0400467 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400468 &BTRFS_I(inode)->io_tree,
469 start, end, NULL,
470 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400471 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400472 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000473
474 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400475 goto free_pages_out;
476 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000477 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400478 }
479
480 if (will_compress) {
481 /*
482 * we aren't doing an inline extent round the compressed size
483 * up to a block size boundary so the allocator does sane
484 * things
485 */
486 total_compressed = (total_compressed + blocksize - 1) &
487 ~(blocksize - 1);
488
489 /*
490 * one last check to make sure the compression is really a
491 * win, compare the page count read with the blocks on disk
492 */
493 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
494 ~(PAGE_CACHE_SIZE - 1);
495 if (total_compressed >= total_in) {
496 will_compress = 0;
497 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400498 num_bytes = total_in;
499 }
500 }
501 if (!will_compress && pages) {
502 /*
503 * the compression code ran but failed to make things smaller,
504 * free any pages it allocated and our page pointer array
505 */
506 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400507 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400508 page_cache_release(pages[i]);
509 }
510 kfree(pages);
511 pages = NULL;
512 total_compressed = 0;
513 nr_pages_ret = 0;
514
515 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500516 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
517 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500518 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500519 }
Chris Masonc8b97812008-10-29 14:49:59 -0400520 }
Chris Mason771ed682008-11-06 22:02:51 -0500521 if (will_compress) {
522 *num_added += 1;
523
524 /* the async work queues will take care of doing actual
525 * allocation on disk for these compressed pages,
526 * and will submit them to the elevator.
527 */
528 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800529 total_compressed, pages, nr_pages_ret,
530 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500531
Yan, Zheng24ae6362010-12-06 07:02:36 +0000532 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500533 start += num_bytes;
534 pages = NULL;
535 cond_resched();
536 goto again;
537 }
538 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500539cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500540 /*
541 * No compression, but we still need to write the pages in
542 * the file we've been given so far. redirty the locked
543 * page if it corresponds to our extent and set things up
544 * for the async work queue to run cow_file_range to do
545 * the normal delalloc dance
546 */
547 if (page_offset(locked_page) >= start &&
548 page_offset(locked_page) <= end) {
549 __set_page_dirty_nobuffers(locked_page);
550 /* unlocked later on in the async handlers */
551 }
Li Zefan261507a02010-12-17 14:21:50 +0800552 add_async_extent(async_cow, start, end - start + 1,
553 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500554 *num_added += 1;
555 }
556
557out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100558 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500559
560free_pages_out:
561 for (i = 0; i < nr_pages_ret; i++) {
562 WARN_ON(pages[i]->mapping);
563 page_cache_release(pages[i]);
564 }
Chris Masond3977122009-01-05 21:25:51 -0500565 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500566
567 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100568
569cleanup_and_out:
570 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
571 start, end, NULL,
572 EXTENT_CLEAR_UNLOCK_PAGE |
573 EXTENT_CLEAR_DIRTY |
574 EXTENT_CLEAR_DELALLOC |
575 EXTENT_SET_WRITEBACK |
576 EXTENT_END_WRITEBACK);
577 if (!trans || IS_ERR(trans))
578 btrfs_error(root->fs_info, ret, "Failed to join transaction");
579 else
580 btrfs_abort_transaction(trans, root, ret);
581 goto free_pages_out;
Chris Mason771ed682008-11-06 22:02:51 -0500582}
583
584/*
585 * phase two of compressed writeback. This is the ordered portion
586 * of the code, which only gets called in the order the work was
587 * queued. We walk all the async extents created by compress_file_range
588 * and send them down to the disk.
589 */
590static noinline int submit_compressed_extents(struct inode *inode,
591 struct async_cow *async_cow)
592{
593 struct async_extent *async_extent;
594 u64 alloc_hint = 0;
595 struct btrfs_trans_handle *trans;
596 struct btrfs_key ins;
597 struct extent_map *em;
598 struct btrfs_root *root = BTRFS_I(inode)->root;
599 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
600 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500601 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500602
603 if (list_empty(&async_cow->extents))
604 return 0;
605
Chris Mason771ed682008-11-06 22:02:51 -0500606
Chris Masond3977122009-01-05 21:25:51 -0500607 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500608 async_extent = list_entry(async_cow->extents.next,
609 struct async_extent, list);
610 list_del(&async_extent->list);
611
612 io_tree = &BTRFS_I(inode)->io_tree;
613
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500614retry:
Chris Mason771ed682008-11-06 22:02:51 -0500615 /* did the compression code fall back to uncompressed IO? */
616 if (!async_extent->pages) {
617 int page_started = 0;
618 unsigned long nr_written = 0;
619
620 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000621 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100622 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500623
624 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500625 ret = cow_file_range(inode, async_cow->locked_page,
626 async_extent->start,
627 async_extent->start +
628 async_extent->ram_size - 1,
629 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500630
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100631 /* JDM XXX */
632
Chris Mason771ed682008-11-06 22:02:51 -0500633 /*
634 * if page_started, cow_file_range inserted an
635 * inline extent and took care of all the unlocking
636 * and IO for us. Otherwise, we need to submit
637 * all those pages down to the drive.
638 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500639 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500640 extent_write_locked_range(io_tree,
641 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500642 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500643 async_extent->ram_size - 1,
644 btrfs_get_extent,
645 WB_SYNC_ALL);
646 kfree(async_extent);
647 cond_resched();
648 continue;
649 }
650
651 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100652 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500653
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400654 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100655 if (IS_ERR(trans)) {
656 ret = PTR_ERR(trans);
657 } else {
658 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
659 ret = btrfs_reserve_extent(trans, root,
Chris Mason771ed682008-11-06 22:02:51 -0500660 async_extent->compressed_size,
661 async_extent->compressed_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500662 0, alloc_hint, &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100663 if (ret)
664 btrfs_abort_transaction(trans, root, ret);
665 btrfs_end_transaction(trans, root);
666 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000667
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500668 if (ret) {
669 int i;
670 for (i = 0; i < async_extent->nr_pages; i++) {
671 WARN_ON(async_extent->pages[i]->mapping);
672 page_cache_release(async_extent->pages[i]);
673 }
674 kfree(async_extent->pages);
675 async_extent->nr_pages = 0;
676 async_extent->pages = NULL;
677 unlock_extent(io_tree, async_extent->start,
678 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100679 async_extent->ram_size - 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100680 if (ret == -ENOSPC)
681 goto retry;
682 goto out_free; /* JDM: Requeue? */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500683 }
684
Yan, Zhengc2167752009-11-12 09:34:21 +0000685 /*
686 * here we're doing allocation and writeback of the
687 * compressed pages
688 */
689 btrfs_drop_extent_cache(inode, async_extent->start,
690 async_extent->start +
691 async_extent->ram_size - 1, 0);
692
David Sterba172ddd62011-04-21 00:48:27 +0200693 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100694 BUG_ON(!em); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500695 em->start = async_extent->start;
696 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500697 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500698
699 em->block_start = ins.objectid;
700 em->block_len = ins.offset;
701 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800702 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500703 set_bit(EXTENT_FLAG_PINNED, &em->flags);
704 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
705
Chris Masond3977122009-01-05 21:25:51 -0500706 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400707 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500708 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400709 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500710 if (ret != -EEXIST) {
711 free_extent_map(em);
712 break;
713 }
714 btrfs_drop_extent_cache(inode, async_extent->start,
715 async_extent->start +
716 async_extent->ram_size - 1, 0);
717 }
718
Li Zefan261507a02010-12-17 14:21:50 +0800719 ret = btrfs_add_ordered_extent_compress(inode,
720 async_extent->start,
721 ins.objectid,
722 async_extent->ram_size,
723 ins.offset,
724 BTRFS_ORDERED_COMPRESSED,
725 async_extent->compress_type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100726 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500727
Chris Mason771ed682008-11-06 22:02:51 -0500728 /*
729 * clear dirty, set writeback and unlock the pages.
730 */
731 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400732 &BTRFS_I(inode)->io_tree,
733 async_extent->start,
734 async_extent->start +
735 async_extent->ram_size - 1,
736 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
737 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400738 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400739 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500740
741 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500742 async_extent->start,
743 async_extent->ram_size,
744 ins.objectid,
745 ins.offset, async_extent->pages,
746 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500747
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100748 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500749 alloc_hint = ins.objectid + ins.offset;
750 kfree(async_extent);
751 cond_resched();
752 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100753 ret = 0;
754out:
755 return ret;
756out_free:
757 kfree(async_extent);
758 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500759}
760
Josef Bacik4b46fce2010-05-23 11:00:55 -0400761static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
762 u64 num_bytes)
763{
764 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
765 struct extent_map *em;
766 u64 alloc_hint = 0;
767
768 read_lock(&em_tree->lock);
769 em = search_extent_mapping(em_tree, start, num_bytes);
770 if (em) {
771 /*
772 * if block start isn't an actual block number then find the
773 * first block in this inode and use that as a hint. If that
774 * block is also bogus then just don't worry about it.
775 */
776 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
777 free_extent_map(em);
778 em = search_extent_mapping(em_tree, 0, 0);
779 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
780 alloc_hint = em->block_start;
781 if (em)
782 free_extent_map(em);
783 } else {
784 alloc_hint = em->block_start;
785 free_extent_map(em);
786 }
787 }
788 read_unlock(&em_tree->lock);
789
790 return alloc_hint;
791}
792
Chris Mason771ed682008-11-06 22:02:51 -0500793/*
794 * when extent_io.c finds a delayed allocation range in the file,
795 * the call backs end up in this code. The basic idea is to
796 * allocate extents on disk for the range, and create ordered data structs
797 * in ram to track those extents.
798 *
799 * locked_page is the page that writepage had locked already. We use
800 * it to make sure we don't do extra locks or unlocks.
801 *
802 * *page_started is set to one if we unlock locked_page and do everything
803 * required to start IO on it. It may be clean and already done with
804 * IO when we return.
805 */
806static noinline int cow_file_range(struct inode *inode,
807 struct page *locked_page,
808 u64 start, u64 end, int *page_started,
809 unsigned long *nr_written,
810 int unlock)
811{
812 struct btrfs_root *root = BTRFS_I(inode)->root;
813 struct btrfs_trans_handle *trans;
814 u64 alloc_hint = 0;
815 u64 num_bytes;
816 unsigned long ram_size;
817 u64 disk_num_bytes;
818 u64 cur_alloc_size;
819 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500820 struct btrfs_key ins;
821 struct extent_map *em;
822 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
823 int ret = 0;
824
Chris Mason2cf85722011-07-26 15:35:09 -0400825 BUG_ON(btrfs_is_free_space_inode(root, inode));
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400826 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100827 if (IS_ERR(trans)) {
828 extent_clear_unlock_delalloc(inode,
829 &BTRFS_I(inode)->io_tree,
830 start, end, NULL,
831 EXTENT_CLEAR_UNLOCK_PAGE |
832 EXTENT_CLEAR_UNLOCK |
833 EXTENT_CLEAR_DELALLOC |
834 EXTENT_CLEAR_DIRTY |
835 EXTENT_SET_WRITEBACK |
836 EXTENT_END_WRITEBACK);
837 return PTR_ERR(trans);
838 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400839 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500840
Chris Mason771ed682008-11-06 22:02:51 -0500841 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
842 num_bytes = max(blocksize, num_bytes);
843 disk_num_bytes = num_bytes;
844 ret = 0;
845
Chris Mason4cb53002011-05-24 15:35:30 -0400846 /* if this is a small write inside eof, kick off defrag */
Liu Bo4cb13e52012-03-29 09:57:45 -0400847 if (num_bytes < 64 * 1024 &&
848 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400849 btrfs_add_inode_defrag(trans, inode);
850
Chris Mason771ed682008-11-06 22:02:51 -0500851 if (start == 0) {
852 /* lets try to make an inline extent */
853 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000854 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500855 if (ret == 0) {
856 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400857 &BTRFS_I(inode)->io_tree,
858 start, end, NULL,
859 EXTENT_CLEAR_UNLOCK_PAGE |
860 EXTENT_CLEAR_UNLOCK |
861 EXTENT_CLEAR_DELALLOC |
862 EXTENT_CLEAR_DIRTY |
863 EXTENT_SET_WRITEBACK |
864 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000865
Chris Mason771ed682008-11-06 22:02:51 -0500866 *nr_written = *nr_written +
867 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
868 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500869 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100870 } else if (ret < 0) {
871 btrfs_abort_transaction(trans, root, ret);
872 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500873 }
874 }
Chris Masonc8b97812008-10-29 14:49:59 -0400875
876 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200877 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400878
Josef Bacik4b46fce2010-05-23 11:00:55 -0400879 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400880 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400881
Chris Masond3977122009-01-05 21:25:51 -0500882 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400883 unsigned long op;
884
Josef Bacik287a0ab2010-03-19 18:07:23 +0000885 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400886 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500887 root->sectorsize, 0, alloc_hint,
Josef Bacik81c9ad22012-01-18 10:56:06 -0500888 &ins, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100889 if (ret < 0) {
890 btrfs_abort_transaction(trans, root, ret);
891 goto out_unlock;
892 }
Chris Masond3977122009-01-05 21:25:51 -0500893
David Sterba172ddd62011-04-21 00:48:27 +0200894 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100895 BUG_ON(!em); /* -ENOMEM */
Chris Masone6dcd2d2008-07-17 12:53:50 -0400896 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500897 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500898 ram_size = ins.offset;
899 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400900
Chris Masone6dcd2d2008-07-17 12:53:50 -0400901 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400902 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400903 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400904 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400905
Chris Masond3977122009-01-05 21:25:51 -0500906 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400907 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400908 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400909 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400910 if (ret != -EEXIST) {
911 free_extent_map(em);
912 break;
913 }
914 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400915 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400916 }
917
Chris Mason98d20f62008-04-14 09:46:10 -0400918 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400919 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500920 ram_size, cur_alloc_size, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100921 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -0400922
Yan Zheng17d217f2008-12-12 10:03:38 -0500923 if (root->root_key.objectid ==
924 BTRFS_DATA_RELOC_TREE_OBJECTID) {
925 ret = btrfs_reloc_clone_csums(inode, start,
926 cur_alloc_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100927 if (ret) {
928 btrfs_abort_transaction(trans, root, ret);
929 goto out_unlock;
930 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500931 }
932
Chris Masond3977122009-01-05 21:25:51 -0500933 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400934 break;
Chris Masond3977122009-01-05 21:25:51 -0500935
Chris Masonc8b97812008-10-29 14:49:59 -0400936 /* we're not doing compressed IO, don't unlock the first
937 * page (which the caller expects to stay locked), don't
938 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400939 *
940 * Do set the Private2 bit so we know this page was properly
941 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400942 */
Chris Masona791e352009-10-08 11:27:10 -0400943 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
944 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
945 EXTENT_SET_PRIVATE2;
946
Chris Masonc8b97812008-10-29 14:49:59 -0400947 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
948 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400949 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400950 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500951 num_bytes -= cur_alloc_size;
952 alloc_hint = ins.objectid + ins.offset;
953 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400954 }
Chris Mason771ed682008-11-06 22:02:51 -0500955 ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100956out:
Chris Masonb888db22007-08-27 16:49:44 -0400957 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400958
Chris Masonbe20aa92007-12-17 20:14:01 -0500959 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100960out_unlock:
961 extent_clear_unlock_delalloc(inode,
962 &BTRFS_I(inode)->io_tree,
963 start, end, NULL,
964 EXTENT_CLEAR_UNLOCK_PAGE |
965 EXTENT_CLEAR_UNLOCK |
966 EXTENT_CLEAR_DELALLOC |
967 EXTENT_CLEAR_DIRTY |
968 EXTENT_SET_WRITEBACK |
969 EXTENT_END_WRITEBACK);
970
971 goto out;
Chris Mason771ed682008-11-06 22:02:51 -0500972}
Chris Masonc8b97812008-10-29 14:49:59 -0400973
Chris Mason771ed682008-11-06 22:02:51 -0500974/*
975 * work queue call back to started compression on a file and pages
976 */
977static noinline void async_cow_start(struct btrfs_work *work)
978{
979 struct async_cow *async_cow;
980 int num_added = 0;
981 async_cow = container_of(work, struct async_cow, work);
982
983 compress_file_range(async_cow->inode, async_cow->locked_page,
984 async_cow->start, async_cow->end, async_cow,
985 &num_added);
986 if (num_added == 0)
987 async_cow->inode = NULL;
988}
989
990/*
991 * work queue call back to submit previously compressed pages
992 */
993static noinline void async_cow_submit(struct btrfs_work *work)
994{
995 struct async_cow *async_cow;
996 struct btrfs_root *root;
997 unsigned long nr_pages;
998
999 async_cow = container_of(work, struct async_cow, work);
1000
1001 root = async_cow->root;
1002 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1003 PAGE_CACHE_SHIFT;
1004
1005 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
1006
1007 if (atomic_read(&root->fs_info->async_delalloc_pages) <
1008 5 * 1042 * 1024 &&
1009 waitqueue_active(&root->fs_info->async_submit_wait))
1010 wake_up(&root->fs_info->async_submit_wait);
1011
Chris Masond3977122009-01-05 21:25:51 -05001012 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001013 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001014}
Chris Masonc8b97812008-10-29 14:49:59 -04001015
Chris Mason771ed682008-11-06 22:02:51 -05001016static noinline void async_cow_free(struct btrfs_work *work)
1017{
1018 struct async_cow *async_cow;
1019 async_cow = container_of(work, struct async_cow, work);
1020 kfree(async_cow);
1021}
1022
1023static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1024 u64 start, u64 end, int *page_started,
1025 unsigned long *nr_written)
1026{
1027 struct async_cow *async_cow;
1028 struct btrfs_root *root = BTRFS_I(inode)->root;
1029 unsigned long nr_pages;
1030 u64 cur_end;
1031 int limit = 10 * 1024 * 1042;
1032
Chris Masona3429ab2009-10-08 12:30:20 -04001033 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1034 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001035 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001036 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001037 BUG_ON(!async_cow); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001038 async_cow->inode = inode;
1039 async_cow->root = root;
1040 async_cow->locked_page = locked_page;
1041 async_cow->start = start;
1042
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001043 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -05001044 cur_end = end;
1045 else
1046 cur_end = min(end, start + 512 * 1024 - 1);
1047
1048 async_cow->end = cur_end;
1049 INIT_LIST_HEAD(&async_cow->extents);
1050
1051 async_cow->work.func = async_cow_start;
1052 async_cow->work.ordered_func = async_cow_submit;
1053 async_cow->work.ordered_free = async_cow_free;
1054 async_cow->work.flags = 0;
1055
Chris Mason771ed682008-11-06 22:02:51 -05001056 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1057 PAGE_CACHE_SHIFT;
1058 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1059
1060 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1061 &async_cow->work);
1062
1063 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1064 wait_event(root->fs_info->async_submit_wait,
1065 (atomic_read(&root->fs_info->async_delalloc_pages) <
1066 limit));
1067 }
1068
Chris Masond3977122009-01-05 21:25:51 -05001069 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001070 atomic_read(&root->fs_info->async_delalloc_pages)) {
1071 wait_event(root->fs_info->async_submit_wait,
1072 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1073 0));
1074 }
1075
1076 *nr_written += nr_pages;
1077 start = cur_end + 1;
1078 }
1079 *page_started = 1;
1080 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001081}
1082
Chris Masond3977122009-01-05 21:25:51 -05001083static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001084 u64 bytenr, u64 num_bytes)
1085{
1086 int ret;
1087 struct btrfs_ordered_sum *sums;
1088 LIST_HEAD(list);
1089
Yan Zheng07d400a2009-01-06 11:42:00 -05001090 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001091 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001092 if (ret == 0 && list_empty(&list))
1093 return 0;
1094
1095 while (!list_empty(&list)) {
1096 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1097 list_del(&sums->list);
1098 kfree(sums);
1099 }
1100 return 1;
1101}
1102
Chris Masond352ac62008-09-29 15:18:18 -04001103/*
1104 * when nowcow writeback call back. This checks for snapshots or COW copies
1105 * of the extents that exist in the file, and COWs the file as required.
1106 *
1107 * If no cow copies or snapshots exist, we write directly to the existing
1108 * blocks on disk
1109 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001110static noinline int run_delalloc_nocow(struct inode *inode,
1111 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001112 u64 start, u64 end, int *page_started, int force,
1113 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001114{
Chris Masonbe20aa92007-12-17 20:14:01 -05001115 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001116 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001117 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001118 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001119 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001120 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001121 u64 cow_start;
1122 u64 cur_offset;
1123 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001124 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001125 u64 disk_bytenr;
1126 u64 num_bytes;
1127 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001128 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001129 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001130 int nocow;
1131 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001132 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001133 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001134
1135 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001136 if (!path)
1137 return -ENOMEM;
Li Zefan82d59022011-04-20 10:33:24 +08001138
Chris Mason2cf85722011-07-26 15:35:09 -04001139 nolock = btrfs_is_free_space_inode(root, inode);
Li Zefan82d59022011-04-20 10:33:24 +08001140
1141 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001142 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001143 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001144 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001145
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001146 if (IS_ERR(trans)) {
1147 btrfs_free_path(path);
1148 return PTR_ERR(trans);
1149 }
1150
Josef Bacik74b21072011-04-13 12:02:53 -04001151 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001152
Yan Zheng80ff3852008-10-30 14:20:02 -04001153 cow_start = (u64)-1;
1154 cur_offset = start;
1155 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001156 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001157 cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001158 if (ret < 0) {
1159 btrfs_abort_transaction(trans, root, ret);
1160 goto error;
1161 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001162 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1163 leaf = path->nodes[0];
1164 btrfs_item_key_to_cpu(leaf, &found_key,
1165 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001166 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001167 found_key.type == BTRFS_EXTENT_DATA_KEY)
1168 path->slots[0]--;
1169 }
1170 check_prev = 0;
1171next_slot:
1172 leaf = path->nodes[0];
1173 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1174 ret = btrfs_next_leaf(root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001175 if (ret < 0) {
1176 btrfs_abort_transaction(trans, root, ret);
1177 goto error;
1178 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001179 if (ret > 0)
1180 break;
1181 leaf = path->nodes[0];
1182 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001183
Yan Zheng80ff3852008-10-30 14:20:02 -04001184 nocow = 0;
1185 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001186 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001187 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001188
Li Zefan33345d012011-04-20 10:31:50 +08001189 if (found_key.objectid > ino ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001190 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1191 found_key.offset > end)
1192 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001193
Yan Zheng80ff3852008-10-30 14:20:02 -04001194 if (found_key.offset > cur_offset) {
1195 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001196 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001197 goto out_check;
1198 }
Chris Masonc31f8832008-01-08 15:46:31 -05001199
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 fi = btrfs_item_ptr(leaf, path->slots[0],
1201 struct btrfs_file_extent_item);
1202 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001203
Yan Zhengd899e052008-10-30 14:25:28 -04001204 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1205 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001206 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001207 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 extent_end = found_key.offset +
1209 btrfs_file_extent_num_bytes(leaf, fi);
1210 if (extent_end <= start) {
1211 path->slots[0]++;
1212 goto next_slot;
1213 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001214 if (disk_bytenr == 0)
1215 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001216 if (btrfs_file_extent_compression(leaf, fi) ||
1217 btrfs_file_extent_encryption(leaf, fi) ||
1218 btrfs_file_extent_other_encoding(leaf, fi))
1219 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001220 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1221 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001222 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001223 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001224 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001225 found_key.offset -
1226 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001227 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001228 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001229 disk_bytenr += cur_offset - found_key.offset;
1230 num_bytes = min(end + 1, extent_end) - cur_offset;
1231 /*
1232 * force cow if csum exists in the range.
1233 * this ensure that csum for a given extent are
1234 * either valid or do not exist.
1235 */
1236 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1237 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 nocow = 1;
1239 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1240 extent_end = found_key.offset +
1241 btrfs_file_extent_inline_len(leaf, fi);
1242 extent_end = ALIGN(extent_end, root->sectorsize);
1243 } else {
1244 BUG_ON(1);
1245 }
1246out_check:
1247 if (extent_end <= start) {
1248 path->slots[0]++;
1249 goto next_slot;
1250 }
1251 if (!nocow) {
1252 if (cow_start == (u64)-1)
1253 cow_start = cur_offset;
1254 cur_offset = extent_end;
1255 if (cur_offset > end)
1256 break;
1257 path->slots[0]++;
1258 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001259 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001260
David Sterbab3b4aa72011-04-21 01:20:15 +02001261 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 if (cow_start != (u64)-1) {
1263 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001264 found_key.offset - 1, page_started,
1265 nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001266 if (ret) {
1267 btrfs_abort_transaction(trans, root, ret);
1268 goto error;
1269 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001271 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001272
Yan Zhengd899e052008-10-30 14:25:28 -04001273 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1274 struct extent_map *em;
1275 struct extent_map_tree *em_tree;
1276 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001277 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001278 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001279 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001280 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001281 em->len = num_bytes;
1282 em->block_len = num_bytes;
1283 em->block_start = disk_bytenr;
1284 em->bdev = root->fs_info->fs_devices->latest_bdev;
1285 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1286 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001287 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001288 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001289 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001290 if (ret != -EEXIST) {
1291 free_extent_map(em);
1292 break;
1293 }
1294 btrfs_drop_extent_cache(inode, em->start,
1295 em->start + em->len - 1, 0);
1296 }
1297 type = BTRFS_ORDERED_PREALLOC;
1298 } else {
1299 type = BTRFS_ORDERED_NOCOW;
1300 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001301
1302 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001303 num_bytes, num_bytes, type);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001304 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001305
Yan, Zhengefa56462010-05-16 10:49:59 -04001306 if (root->root_key.objectid ==
1307 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1308 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1309 num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001310 if (ret) {
1311 btrfs_abort_transaction(trans, root, ret);
1312 goto error;
1313 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001314 }
1315
Yan Zhengd899e052008-10-30 14:25:28 -04001316 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001317 cur_offset, cur_offset + num_bytes - 1,
1318 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1319 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1320 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 cur_offset = extent_end;
1322 if (cur_offset > end)
1323 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001324 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001325 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001326
1327 if (cur_offset <= end && cow_start == (u64)-1)
1328 cow_start = cur_offset;
1329 if (cow_start != (u64)-1) {
1330 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001331 page_started, nr_written, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001332 if (ret) {
1333 btrfs_abort_transaction(trans, root, ret);
1334 goto error;
1335 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 }
1337
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001338error:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001339 if (nolock) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001340 err = btrfs_end_transaction_nolock(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001341 } else {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001342 err = btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001343 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001344 if (!ret)
1345 ret = err;
1346
Yan Zheng7ea394f2008-08-05 13:05:02 -04001347 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001348 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001349}
1350
Chris Masond352ac62008-09-29 15:18:18 -04001351/*
1352 * extent_io.c call back to do delayed allocation processing
1353 */
Chris Masonc8b97812008-10-29 14:49:59 -04001354static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001355 u64 start, u64 end, int *page_started,
1356 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001357{
Chris Masonbe20aa92007-12-17 20:14:01 -05001358 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001359 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001360
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001361 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001362 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001363 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001364 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001365 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001366 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001367 else if (!btrfs_test_opt(root, COMPRESS) &&
Liu Bo75e7cb72011-03-22 10:12:20 +00001368 !(BTRFS_I(inode)->force_compress) &&
1369 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
Chris Mason7f366cf2009-03-12 20:12:45 -04001370 ret = cow_file_range(inode, locked_page, start, end,
1371 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001372 else
Chris Mason771ed682008-11-06 22:02:51 -05001373 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001374 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001375 return ret;
1376}
1377
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001378static void btrfs_split_extent_hook(struct inode *inode,
1379 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001380{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001381 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001382 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001383 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001384
Josef Bacik9e0baf62011-07-15 15:16:44 +00001385 spin_lock(&BTRFS_I(inode)->lock);
1386 BTRFS_I(inode)->outstanding_extents++;
1387 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001388}
1389
1390/*
1391 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1392 * extents so we can keep track of new extents that are just merged onto old
1393 * extents, such as when we are doing sequential writes, so we can properly
1394 * account for the metadata space we'll need.
1395 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001396static void btrfs_merge_extent_hook(struct inode *inode,
1397 struct extent_state *new,
1398 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001399{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001400 /* not delalloc, ignore it */
1401 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001402 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001403
Josef Bacik9e0baf62011-07-15 15:16:44 +00001404 spin_lock(&BTRFS_I(inode)->lock);
1405 BTRFS_I(inode)->outstanding_extents--;
1406 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001407}
1408
Chris Masond352ac62008-09-29 15:18:18 -04001409/*
1410 * extent_io.c set_bit_hook, used to track delayed allocation
1411 * bytes in this file, and to maintain the list of inodes that
1412 * have pending delalloc work to be done.
1413 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001414static void btrfs_set_bit_hook(struct inode *inode,
1415 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001416{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001417
Chris Mason75eff682008-12-15 15:54:40 -05001418 /*
1419 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001420 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001421 * bit, which is only set or cleared with irqs on
1422 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001423 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001424 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001425 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001426 bool do_list = !btrfs_is_free_space_inode(root, inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001427
Josef Bacik9e0baf62011-07-15 15:16:44 +00001428 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001429 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001430 } else {
1431 spin_lock(&BTRFS_I(inode)->lock);
1432 BTRFS_I(inode)->outstanding_extents++;
1433 spin_unlock(&BTRFS_I(inode)->lock);
1434 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001435
Chris Mason75eff682008-12-15 15:54:40 -05001436 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001437 BTRFS_I(inode)->delalloc_bytes += len;
1438 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001439 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001440 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1441 &root->fs_info->delalloc_inodes);
1442 }
Chris Mason75eff682008-12-15 15:54:40 -05001443 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001444 }
Chris Mason291d6732008-01-29 15:55:23 -05001445}
1446
Chris Masond352ac62008-09-29 15:18:18 -04001447/*
1448 * extent_io.c clear_bit_hook, see set_bit_hook for why
1449 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001450static void btrfs_clear_bit_hook(struct inode *inode,
1451 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001452{
Chris Mason75eff682008-12-15 15:54:40 -05001453 /*
1454 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001455 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001456 * bit, which is only set or cleared with irqs on
1457 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001458 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001459 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001460 u64 len = state->end + 1 - state->start;
Chris Mason2cf85722011-07-26 15:35:09 -04001461 bool do_list = !btrfs_is_free_space_inode(root, inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001462
Josef Bacik9e0baf62011-07-15 15:16:44 +00001463 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001464 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001465 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1466 spin_lock(&BTRFS_I(inode)->lock);
1467 BTRFS_I(inode)->outstanding_extents--;
1468 spin_unlock(&BTRFS_I(inode)->lock);
1469 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001470
1471 if (*bits & EXTENT_DO_ACCOUNTING)
1472 btrfs_delalloc_release_metadata(inode, len);
1473
Josef Bacik0cb59c92010-07-02 12:14:14 -04001474 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1475 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001476 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001477
Chris Mason75eff682008-12-15 15:54:40 -05001478 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001479 root->fs_info->delalloc_bytes -= len;
1480 BTRFS_I(inode)->delalloc_bytes -= len;
1481
Josef Bacik0cb59c92010-07-02 12:14:14 -04001482 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001483 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1484 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1485 }
Chris Mason75eff682008-12-15 15:54:40 -05001486 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001487 }
Chris Mason291d6732008-01-29 15:55:23 -05001488}
1489
Chris Masond352ac62008-09-29 15:18:18 -04001490/*
1491 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1492 * we don't create bios that span stripes or chunks
1493 */
Chris Mason239b14b2008-03-24 15:02:07 -04001494int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001495 size_t size, struct bio *bio,
1496 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001497{
1498 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1499 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001500 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001501 u64 length = 0;
1502 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001503 int ret;
1504
Chris Mason771ed682008-11-06 22:02:51 -05001505 if (bio_flags & EXTENT_BIO_COMPRESSED)
1506 return 0;
1507
Chris Masonf2d8d742008-04-21 10:03:05 -04001508 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001509 map_tree = &root->fs_info->mapping_tree;
1510 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001511 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001512 &map_length, NULL, 0);
Jeff Mahoney3444a972011-10-03 23:23:13 -04001513 /* Will always return 0 or 1 with map_multi == NULL */
1514 BUG_ON(ret < 0);
Chris Masond3977122009-01-05 21:25:51 -05001515 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001516 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001517 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001518}
1519
Chris Masond352ac62008-09-29 15:18:18 -04001520/*
1521 * in order to insert checksums into the metadata in large chunks,
1522 * we wait until bio submission time. All the pages in the bio are
1523 * checksummed and sums are attached onto the ordered extent record.
1524 *
1525 * At IO completion time the cums attached on the ordered extent record
1526 * are inserted into the btree
1527 */
Chris Masond3977122009-01-05 21:25:51 -05001528static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1529 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001530 unsigned long bio_flags,
1531 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001532{
Chris Mason065631f2008-02-20 12:07:25 -05001533 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001534 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001535
Chris Masond20f7042008-12-08 16:58:54 -05001536 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001537 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001538 return 0;
1539}
Chris Masone0156402008-04-16 11:15:20 -04001540
Chris Mason4a69a412008-11-06 22:03:00 -05001541/*
1542 * in order to insert checksums into the metadata in large chunks,
1543 * we wait until bio submission time. All the pages in the bio are
1544 * checksummed and sums are attached onto the ordered extent record.
1545 *
1546 * At IO completion time the cums attached on the ordered extent record
1547 * are inserted into the btree
1548 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001549static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001550 int mirror_num, unsigned long bio_flags,
1551 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001552{
1553 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001554 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001555}
1556
Chris Masond352ac62008-09-29 15:18:18 -04001557/*
Chris Masoncad321a2008-12-17 14:51:42 -05001558 * extent_io.c submission hook. This does the right thing for csum calculation
1559 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001560 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001561static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001562 int mirror_num, unsigned long bio_flags,
1563 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001564{
1565 struct btrfs_root *root = BTRFS_I(inode)->root;
1566 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001567 int skip_sum;
Jeff Mahoney04173412011-10-03 23:23:12 -04001568 int metadata = 0;
Chris Mason44b8bd72008-04-16 11:14:51 -04001569
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001570 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001571
Chris Mason2cf85722011-07-26 15:35:09 -04001572 if (btrfs_is_free_space_inode(root, inode))
Jeff Mahoney04173412011-10-03 23:23:12 -04001573 metadata = 2;
1574
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001575 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001576 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1577 if (ret)
1578 return ret;
1579
Chris Masond20f7042008-12-08 16:58:54 -05001580 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001581 return btrfs_submit_compressed_read(inode, bio,
1582 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001583 } else if (!skip_sum) {
1584 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1585 if (ret)
1586 return ret;
1587 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001588 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001589 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001590 /* csum items have already been cloned */
1591 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1592 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001593 /* we're doing a write, do the async checksumming */
1594 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001595 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001596 bio_flags, bio_offset,
1597 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001598 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001599 }
1600
Chris Mason0b86a832008-03-24 15:01:56 -04001601mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001602 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001603}
Chris Mason6885f302008-02-20 16:11:05 -05001604
Chris Masond352ac62008-09-29 15:18:18 -04001605/*
1606 * given a list of ordered sums record them in the inode. This happens
1607 * at IO completion time based on sums calculated at bio submission time.
1608 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001609static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001610 struct inode *inode, u64 file_offset,
1611 struct list_head *list)
1612{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001613 struct btrfs_ordered_sum *sum;
1614
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001615 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001616 btrfs_csum_file_blocks(trans,
1617 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001618 }
1619 return 0;
1620}
1621
Josef Bacik2ac55d42010-02-03 19:33:23 +00001622int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1623 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001624{
Chris Masond3977122009-01-05 21:25:51 -05001625 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001626 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001627 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001628 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001629}
1630
Chris Masond352ac62008-09-29 15:18:18 -04001631/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001632struct btrfs_writepage_fixup {
1633 struct page *page;
1634 struct btrfs_work work;
1635};
1636
Christoph Hellwigb2950862008-12-02 09:54:17 -05001637static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001638{
1639 struct btrfs_writepage_fixup *fixup;
1640 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001641 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001642 struct page *page;
1643 struct inode *inode;
1644 u64 page_start;
1645 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01001646 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04001647
1648 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1649 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001650again:
Chris Mason247e7432008-07-17 12:53:51 -04001651 lock_page(page);
1652 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1653 ClearPageChecked(page);
1654 goto out_page;
1655 }
1656
1657 inode = page->mapping->host;
1658 page_start = page_offset(page);
1659 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1660
Josef Bacik2ac55d42010-02-03 19:33:23 +00001661 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001662 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04001663
1664 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001665 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001666 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001667
1668 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1669 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001670 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1671 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001672 unlock_page(page);
1673 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001674 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04001675 goto again;
1676 }
Chris Mason247e7432008-07-17 12:53:51 -04001677
Jeff Mahoney87826df2012-02-15 16:23:57 +01001678 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1679 if (ret) {
1680 mapping_set_error(page->mapping, ret);
1681 end_extent_writepage(page, ret, page_start, page_end);
1682 ClearPageChecked(page);
1683 goto out;
1684 }
1685
Josef Bacik2ac55d42010-02-03 19:33:23 +00001686 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001687 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001688 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04001689out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001690 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1691 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001692out_page:
1693 unlock_page(page);
1694 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001695 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001696}
1697
1698/*
1699 * There are a few paths in the higher layers of the kernel that directly
1700 * set the page dirty bit without asking the filesystem if it is a
1701 * good idea. This causes problems because we want to make sure COW
1702 * properly happens and the data=ordered rules are followed.
1703 *
Chris Masonc8b97812008-10-29 14:49:59 -04001704 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001705 * hasn't been properly setup for IO. We kick off an async process
1706 * to fix it up. The async helper will wait for ordered extents, set
1707 * the delalloc bit and make it safe to write the page.
1708 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001709static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001710{
1711 struct inode *inode = page->mapping->host;
1712 struct btrfs_writepage_fixup *fixup;
1713 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001714
Chris Mason8b62b722009-09-02 16:53:46 -04001715 /* this page is properly in the ordered list */
1716 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001717 return 0;
1718
1719 if (PageChecked(page))
1720 return -EAGAIN;
1721
1722 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1723 if (!fixup)
1724 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001725
Chris Mason247e7432008-07-17 12:53:51 -04001726 SetPageChecked(page);
1727 page_cache_get(page);
1728 fixup->work.func = btrfs_writepage_fixup_worker;
1729 fixup->page = page;
1730 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01001731 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04001732}
1733
Yan Zhengd899e052008-10-30 14:25:28 -04001734static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1735 struct inode *inode, u64 file_pos,
1736 u64 disk_bytenr, u64 disk_num_bytes,
1737 u64 num_bytes, u64 ram_bytes,
1738 u8 compression, u8 encryption,
1739 u16 other_encoding, int extent_type)
1740{
1741 struct btrfs_root *root = BTRFS_I(inode)->root;
1742 struct btrfs_file_extent_item *fi;
1743 struct btrfs_path *path;
1744 struct extent_buffer *leaf;
1745 struct btrfs_key ins;
1746 u64 hint;
1747 int ret;
1748
1749 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07001750 if (!path)
1751 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04001752
Chris Masonb9473432009-03-13 11:00:37 -04001753 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001754
1755 /*
1756 * we may be replacing one extent in the tree with another.
1757 * The new extent is pinned in the extent map, and we don't want
1758 * to drop it from the cache until it is completely in the btree.
1759 *
1760 * So, tell btrfs_drop_extents to leave this extent in the cache.
1761 * the caller is expected to unpin it and allow it to be merged
1762 * with the others.
1763 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001764 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1765 &hint, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001766 if (ret)
1767 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001768
Li Zefan33345d012011-04-20 10:31:50 +08001769 ins.objectid = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04001770 ins.offset = file_pos;
1771 ins.type = BTRFS_EXTENT_DATA_KEY;
1772 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001773 if (ret)
1774 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04001775 leaf = path->nodes[0];
1776 fi = btrfs_item_ptr(leaf, path->slots[0],
1777 struct btrfs_file_extent_item);
1778 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1779 btrfs_set_file_extent_type(leaf, fi, extent_type);
1780 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1781 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1782 btrfs_set_file_extent_offset(leaf, fi, 0);
1783 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1784 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1785 btrfs_set_file_extent_compression(leaf, fi, compression);
1786 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1787 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001788
1789 btrfs_unlock_up_safe(path, 1);
1790 btrfs_set_lock_blocking(leaf);
1791
Yan Zhengd899e052008-10-30 14:25:28 -04001792 btrfs_mark_buffer_dirty(leaf);
1793
1794 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001795
1796 ins.objectid = disk_bytenr;
1797 ins.offset = disk_num_bytes;
1798 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001799 ret = btrfs_alloc_reserved_file_extent(trans, root,
1800 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08001801 btrfs_ino(inode), file_pos, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001802out:
Yan Zhengd899e052008-10-30 14:25:28 -04001803 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001804
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001805 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001806}
1807
Chris Mason5d13a982009-03-13 11:41:46 -04001808/*
1809 * helper function for btrfs_finish_ordered_io, this
1810 * just reads in some of the csum leaves to prime them into ram
1811 * before we start the transaction. It limits the amount of btree
1812 * reads required while inside the transaction.
1813 */
Chris Masond352ac62008-09-29 15:18:18 -04001814/* as ordered data IO finishes, this gets called so we can finish
1815 * an ordered extent if the range of bytes in the file it covers are
1816 * fully written.
1817 */
Josef Bacik5fd02042012-05-02 14:00:54 -04001818static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001819{
Josef Bacik5fd02042012-05-02 14:00:54 -04001820 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001821 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001822 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001823 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001824 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001825 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001826 int ret;
Li Zefan82d59022011-04-20 10:33:24 +08001827 bool nolock;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001828
Chris Mason2cf85722011-07-26 15:35:09 -04001829 nolock = btrfs_is_free_space_inode(root, inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001830
Josef Bacik5fd02042012-05-02 14:00:54 -04001831 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1832 ret = -EIO;
1833 goto out;
1834 }
1835
Yan, Zhengc2167752009-11-12 09:34:21 +00001836 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001837 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Yan, Zhengc2167752009-11-12 09:34:21 +00001838 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1839 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001840 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001841 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001842 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001843 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001844 if (IS_ERR(trans))
1845 return PTR_ERR(trans);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001846 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason21151332011-11-10 20:39:08 -05001847 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001848 if (ret) /* -ENOMEM or corruption */
1849 btrfs_abort_transaction(trans, root, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001850 }
1851 goto out;
1852 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001853
Josef Bacik2ac55d42010-02-03 19:33:23 +00001854 lock_extent_bits(io_tree, ordered_extent->file_offset,
1855 ordered_extent->file_offset + ordered_extent->len - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001856 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001857
Josef Bacik0cb59c92010-07-02 12:14:14 -04001858 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001859 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001860 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001861 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001862 if (IS_ERR(trans)) {
1863 ret = PTR_ERR(trans);
1864 trans = NULL;
1865 goto out_unlock;
1866 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001867 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001868
Chris Masonc8b97812008-10-29 14:49:59 -04001869 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001870 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001871 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001872 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001873 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001874 ordered_extent->file_offset,
1875 ordered_extent->file_offset +
1876 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001877 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001878 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001879 ret = insert_reserved_file_extent(trans, inode,
1880 ordered_extent->file_offset,
1881 ordered_extent->start,
1882 ordered_extent->disk_len,
1883 ordered_extent->len,
1884 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001885 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001886 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001887 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1888 ordered_extent->file_offset,
1889 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001890 }
Josef Bacik5fd02042012-05-02 14:00:54 -04001891
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001892 if (ret < 0) {
1893 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001894 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001895 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001896
Chris Masone6dcd2d2008-07-17 12:53:50 -04001897 add_pending_csums(trans, inode, ordered_extent->file_offset,
1898 &ordered_extent->list);
1899
Josef Bacik1ef30be2011-04-05 19:25:36 -04001900 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Miao Xiea39f7522011-09-11 10:52:25 -04001901 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Chris Mason21151332011-11-10 20:39:08 -05001902 ret = btrfs_update_inode_fallback(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001903 if (ret) { /* -ENOMEM or corruption */
1904 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04001905 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001906 }
Josef Bacik1ef30be2011-04-05 19:25:36 -04001907 }
1908 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04001909out_unlock:
1910 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1911 ordered_extent->file_offset +
1912 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00001913out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001914 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001915 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001916 if (trans) {
1917 if (nolock)
Josef Bacik0cb59c92010-07-02 12:14:14 -04001918 btrfs_end_transaction_nolock(trans, root);
Josef Bacik5b0e95b2011-10-06 08:58:24 -04001919 else
Josef Bacik0cb59c92010-07-02 12:14:14 -04001920 btrfs_end_transaction(trans, root);
1921 }
1922
Josef Bacik5fd02042012-05-02 14:00:54 -04001923 if (ret)
1924 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
1925 ordered_extent->file_offset +
1926 ordered_extent->len - 1, NULL, GFP_NOFS);
1927
1928 /*
1929 * This needs to be dont to make sure anybody waiting knows we are done
1930 * upating everything for this ordered extent.
1931 */
1932 btrfs_remove_ordered_extent(inode, ordered_extent);
1933
Chris Masone6dcd2d2008-07-17 12:53:50 -04001934 /* once for us */
1935 btrfs_put_ordered_extent(ordered_extent);
1936 /* once for the tree */
1937 btrfs_put_ordered_extent(ordered_extent);
1938
Josef Bacik5fd02042012-05-02 14:00:54 -04001939 return ret;
1940}
1941
1942static void finish_ordered_fn(struct btrfs_work *work)
1943{
1944 struct btrfs_ordered_extent *ordered_extent;
1945 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
1946 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001947}
1948
Christoph Hellwigb2950862008-12-02 09:54:17 -05001949static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001950 struct extent_state *state, int uptodate)
1951{
Josef Bacik5fd02042012-05-02 14:00:54 -04001952 struct inode *inode = page->mapping->host;
1953 struct btrfs_root *root = BTRFS_I(inode)->root;
1954 struct btrfs_ordered_extent *ordered_extent = NULL;
1955 struct btrfs_workers *workers;
1956
liubo1abe9b82011-03-24 11:18:59 +00001957 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1958
Chris Mason8b62b722009-09-02 16:53:46 -04001959 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04001960 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1961 end - start + 1, uptodate))
1962 return 0;
1963
1964 ordered_extent->work.func = finish_ordered_fn;
1965 ordered_extent->work.flags = 0;
1966
1967 if (btrfs_is_free_space_inode(root, inode))
1968 workers = &root->fs_info->endio_freespace_worker;
1969 else
1970 workers = &root->fs_info->endio_write_workers;
1971 btrfs_queue_worker(workers, &ordered_extent->work);
1972
1973 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04001974}
1975
Chris Masond352ac62008-09-29 15:18:18 -04001976/*
Chris Masond352ac62008-09-29 15:18:18 -04001977 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001978 * if there's a match, we allow the bio to finish. If not, the code in
1979 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04001980 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001981static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04001982 struct extent_state *state, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04001983{
Chris Mason35ebb932007-10-30 16:56:53 -04001984 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001985 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001986 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001987 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001988 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001989 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001990 struct btrfs_root *root = BTRFS_I(inode)->root;
1991 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001992
Chris Masond20f7042008-12-08 16:58:54 -05001993 if (PageChecked(page)) {
1994 ClearPageChecked(page);
1995 goto good;
1996 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001997
1998 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Jan Schmidt08d2f342011-05-04 16:18:50 +02001999 goto good;
Chris Masond20f7042008-12-08 16:58:54 -05002000
Yan Zheng17d217f2008-12-12 10:03:38 -05002001 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002002 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002003 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2004 GFP_NOFS);
2005 return 0;
2006 }
2007
Yanc2e639f2008-02-04 08:57:25 -05002008 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05002009 private = state->private;
2010 ret = 0;
2011 } else {
2012 ret = get_state_private(io_tree, start, &private);
2013 }
Cong Wang7ac687d2011-11-25 23:14:28 +08002014 kaddr = kmap_atomic(page);
Chris Masond3977122009-01-05 21:25:51 -05002015 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04002016 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002017
Chris Masonff79f812007-10-15 16:22:25 -04002018 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2019 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05002020 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002021 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002022
Cong Wang7ac687d2011-11-25 23:14:28 +08002023 kunmap_atomic(kaddr);
Chris Masond20f7042008-12-08 16:58:54 -05002024good:
Chris Mason07157aa2007-08-30 08:50:51 -04002025 return 0;
2026
2027zeroit:
Chris Mason945d8962011-05-22 12:33:42 -04002028 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
Li Zefan33345d012011-04-20 10:31:50 +08002029 "private %llu\n",
2030 (unsigned long long)btrfs_ino(page->mapping->host),
Chris Mason193f2842009-04-27 07:29:05 -04002031 (unsigned long long)start, csum,
2032 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04002033 memset(kaddr + offset, 1, end - start + 1);
2034 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002035 kunmap_atomic(kaddr);
Chris Mason3b951512008-04-17 11:29:12 -04002036 if (private == 0)
2037 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002038 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002039}
Chris Masonb888db22007-08-27 16:49:44 -04002040
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002041struct delayed_iput {
2042 struct list_head list;
2043 struct inode *inode;
2044};
2045
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002046/* JDM: If this is fs-wide, why can't we add a pointer to
2047 * btrfs_inode instead and avoid the allocation? */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002048void btrfs_add_delayed_iput(struct inode *inode)
2049{
2050 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2051 struct delayed_iput *delayed;
2052
2053 if (atomic_add_unless(&inode->i_count, -1, 1))
2054 return;
2055
2056 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2057 delayed->inode = inode;
2058
2059 spin_lock(&fs_info->delayed_iput_lock);
2060 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2061 spin_unlock(&fs_info->delayed_iput_lock);
2062}
2063
2064void btrfs_run_delayed_iputs(struct btrfs_root *root)
2065{
2066 LIST_HEAD(list);
2067 struct btrfs_fs_info *fs_info = root->fs_info;
2068 struct delayed_iput *delayed;
2069 int empty;
2070
2071 spin_lock(&fs_info->delayed_iput_lock);
2072 empty = list_empty(&fs_info->delayed_iputs);
2073 spin_unlock(&fs_info->delayed_iput_lock);
2074 if (empty)
2075 return;
2076
2077 down_read(&root->fs_info->cleanup_work_sem);
2078 spin_lock(&fs_info->delayed_iput_lock);
2079 list_splice_init(&fs_info->delayed_iputs, &list);
2080 spin_unlock(&fs_info->delayed_iput_lock);
2081
2082 while (!list_empty(&list)) {
2083 delayed = list_entry(list.next, struct delayed_iput, list);
2084 list_del(&delayed->list);
2085 iput(delayed->inode);
2086 kfree(delayed);
2087 }
2088 up_read(&root->fs_info->cleanup_work_sem);
2089}
2090
Yan, Zhengd68fc572010-05-16 10:49:58 -04002091enum btrfs_orphan_cleanup_state {
2092 ORPHAN_CLEANUP_STARTED = 1,
2093 ORPHAN_CLEANUP_DONE = 2,
2094};
2095
2096/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08002097 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04002098 * files in the subvolume, it removes orphan item and frees block_rsv
2099 * structure.
2100 */
2101void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2102 struct btrfs_root *root)
2103{
Josef Bacik90290e12011-12-02 15:44:12 -05002104 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002105 int ret;
2106
Josef Bacik8a35d952012-05-23 14:26:42 -04002107 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04002108 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2109 return;
2110
Josef Bacik90290e12011-12-02 15:44:12 -05002111 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002112 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05002113 spin_unlock(&root->orphan_lock);
2114 return;
2115 }
2116
2117 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2118 spin_unlock(&root->orphan_lock);
2119 return;
2120 }
2121
2122 block_rsv = root->orphan_block_rsv;
2123 root->orphan_block_rsv = NULL;
2124 spin_unlock(&root->orphan_lock);
2125
Yan, Zhengd68fc572010-05-16 10:49:58 -04002126 if (root->orphan_item_inserted &&
2127 btrfs_root_refs(&root->root_item) > 0) {
2128 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2129 root->root_key.objectid);
2130 BUG_ON(ret);
2131 root->orphan_item_inserted = 0;
2132 }
2133
Josef Bacik90290e12011-12-02 15:44:12 -05002134 if (block_rsv) {
2135 WARN_ON(block_rsv->size > 0);
2136 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002137 }
2138}
2139
2140/*
Josef Bacik7b128762008-07-24 12:17:14 -04002141 * This creates an orphan entry for the given inode in case something goes
2142 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002143 *
2144 * NOTE: caller of this function should reserve 5 units of metadata for
2145 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002146 */
2147int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2148{
2149 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002150 struct btrfs_block_rsv *block_rsv = NULL;
2151 int reserve = 0;
2152 int insert = 0;
2153 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002154
Yan, Zhengd68fc572010-05-16 10:49:58 -04002155 if (!root->orphan_block_rsv) {
2156 block_rsv = btrfs_alloc_block_rsv(root);
Tsutomu Itohb5324022011-07-19 07:27:20 +00002157 if (!block_rsv)
2158 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04002159 }
2160
Yan, Zhengd68fc572010-05-16 10:49:58 -04002161 spin_lock(&root->orphan_lock);
2162 if (!root->orphan_block_rsv) {
2163 root->orphan_block_rsv = block_rsv;
2164 } else if (block_rsv) {
2165 btrfs_free_block_rsv(root, block_rsv);
2166 block_rsv = NULL;
2167 }
Josef Bacik7b128762008-07-24 12:17:14 -04002168
Josef Bacik8a35d952012-05-23 14:26:42 -04002169 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2170 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002171#if 0
2172 /*
2173 * For proper ENOSPC handling, we should do orphan
2174 * cleanup when mounting. But this introduces backward
2175 * compatibility issue.
2176 */
2177 if (!xchg(&root->orphan_item_inserted, 1))
2178 insert = 2;
2179 else
2180 insert = 1;
2181#endif
2182 insert = 1;
Josef Bacik8a35d952012-05-23 14:26:42 -04002183 atomic_dec(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002184 }
Josef Bacik7b128762008-07-24 12:17:14 -04002185
Josef Bacik72ac3c02012-05-23 14:13:11 -04002186 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2187 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002188 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002189 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002190
Yan, Zhengd68fc572010-05-16 10:49:58 -04002191 /* grab metadata reservation from transaction handle */
2192 if (reserve) {
2193 ret = btrfs_orphan_reserve_metadata(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002194 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002195 }
2196
2197 /* insert an orphan item to track this unlinked/truncated file */
2198 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08002199 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002200 if (ret && ret != -EEXIST) {
Josef Bacik8a35d952012-05-23 14:26:42 -04002201 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2202 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002203 btrfs_abort_transaction(trans, root, ret);
2204 return ret;
2205 }
2206 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002207 }
2208
2209 /* insert an orphan item to track subvolume contains orphan files */
2210 if (insert >= 2) {
2211 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2212 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002213 if (ret && ret != -EEXIST) {
2214 btrfs_abort_transaction(trans, root, ret);
2215 return ret;
2216 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002217 }
2218 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002219}
2220
2221/*
2222 * We have done the truncate/delete so we can go ahead and remove the orphan
2223 * item for this particular inode.
2224 */
2225int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2226{
2227 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002228 int delete_item = 0;
2229 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002230 int ret = 0;
2231
Yan, Zhengd68fc572010-05-16 10:49:58 -04002232 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04002233 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2234 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002235 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002236
Josef Bacik72ac3c02012-05-23 14:13:11 -04002237 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2238 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04002239 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002240 spin_unlock(&root->orphan_lock);
2241
2242 if (trans && delete_item) {
Li Zefan33345d012011-04-20 10:31:50 +08002243 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002244 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacik7b128762008-07-24 12:17:14 -04002245 }
2246
Josef Bacik8a35d952012-05-23 14:26:42 -04002247 if (release_rsv) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04002248 btrfs_orphan_release_metadata(inode);
Josef Bacik8a35d952012-05-23 14:26:42 -04002249 atomic_dec(&root->orphan_inodes);
2250 }
Josef Bacik7b128762008-07-24 12:17:14 -04002251
Yan, Zhengd68fc572010-05-16 10:49:58 -04002252 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002253}
2254
2255/*
2256 * this cleans up any orphans that may be left on the list from the last use
2257 * of this root.
2258 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002259int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002260{
2261 struct btrfs_path *path;
2262 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002263 struct btrfs_key key, found_key;
2264 struct btrfs_trans_handle *trans;
2265 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002266 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002267 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2268
Yan, Zhengd68fc572010-05-16 10:49:58 -04002269 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002270 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002271
2272 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002273 if (!path) {
2274 ret = -ENOMEM;
2275 goto out;
2276 }
Josef Bacik7b128762008-07-24 12:17:14 -04002277 path->reada = -1;
2278
2279 key.objectid = BTRFS_ORPHAN_OBJECTID;
2280 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2281 key.offset = (u64)-1;
2282
Josef Bacik7b128762008-07-24 12:17:14 -04002283 while (1) {
2284 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002285 if (ret < 0)
2286 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002287
2288 /*
2289 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002290 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002291 * find the key and see if we have stuff that matches
2292 */
2293 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002294 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002295 if (path->slots[0] == 0)
2296 break;
2297 path->slots[0]--;
2298 }
2299
2300 /* pull out the item */
2301 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002302 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2303
2304 /* make sure the item matches what we want */
2305 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2306 break;
2307 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2308 break;
2309
2310 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002311 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002312
2313 /*
2314 * this is where we are basically btrfs_lookup, without the
2315 * crossing root thing. we store the inode number in the
2316 * offset of the orphan item.
2317 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002318
2319 if (found_key.offset == last_objectid) {
2320 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2321 "stopping orphan cleanup\n");
2322 ret = -EINVAL;
2323 goto out;
2324 }
2325
2326 last_objectid = found_key.offset;
2327
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002328 found_key.objectid = found_key.offset;
2329 found_key.type = BTRFS_INODE_ITEM_KEY;
2330 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002331 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacika8c9e572011-09-21 16:55:59 -04002332 ret = PTR_RET(inode);
2333 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002334 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002335
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002336 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2337 struct btrfs_root *dead_root;
2338 struct btrfs_fs_info *fs_info = root->fs_info;
2339 int is_dead_root = 0;
2340
2341 /*
2342 * this is an orphan in the tree root. Currently these
2343 * could come from 2 sources:
2344 * a) a snapshot deletion in progress
2345 * b) a free space cache inode
2346 * We need to distinguish those two, as the snapshot
2347 * orphan must not get deleted.
2348 * find_dead_roots already ran before us, so if this
2349 * is a snapshot deletion, we should find the root
2350 * in the dead_roots list
2351 */
2352 spin_lock(&fs_info->trans_lock);
2353 list_for_each_entry(dead_root, &fs_info->dead_roots,
2354 root_list) {
2355 if (dead_root->root_key.objectid ==
2356 found_key.objectid) {
2357 is_dead_root = 1;
2358 break;
2359 }
2360 }
2361 spin_unlock(&fs_info->trans_lock);
2362 if (is_dead_root) {
2363 /* prevent this orphan from being found again */
2364 key.offset = found_key.objectid - 1;
2365 continue;
2366 }
2367 }
Josef Bacika8c9e572011-09-21 16:55:59 -04002368 /*
2369 * Inode is already gone but the orphan item is still there,
2370 * kill the orphan item.
2371 */
2372 if (ret == -ESTALE) {
2373 trans = btrfs_start_transaction(root, 1);
2374 if (IS_ERR(trans)) {
2375 ret = PTR_ERR(trans);
2376 goto out;
2377 }
Josef Bacik8a35d952012-05-23 14:26:42 -04002378 printk(KERN_ERR "auto deleting %Lu\n",
2379 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04002380 ret = btrfs_del_orphan_item(trans, root,
2381 found_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002382 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
Josef Bacika8c9e572011-09-21 16:55:59 -04002383 btrfs_end_transaction(trans, root);
2384 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002385 }
Josef Bacik7b128762008-07-24 12:17:14 -04002386
Josef Bacik7b128762008-07-24 12:17:14 -04002387 /*
2388 * add this inode to the orphan list so btrfs_orphan_del does
2389 * the proper thing when we hit it
2390 */
Josef Bacik8a35d952012-05-23 14:26:42 -04002391 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2392 &BTRFS_I(inode)->runtime_flags);
Josef Bacik7b128762008-07-24 12:17:14 -04002393
Josef Bacik7b128762008-07-24 12:17:14 -04002394 /* if we have links, this was a truncate, lets do that */
2395 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002396 if (!S_ISREG(inode->i_mode)) {
2397 WARN_ON(1);
2398 iput(inode);
2399 continue;
2400 }
Josef Bacik7b128762008-07-24 12:17:14 -04002401 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002402 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002403 } else {
2404 nr_unlink++;
2405 }
2406
2407 /* this will do delete_inode and everything for us */
2408 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002409 if (ret)
2410 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002411 }
Miao Xie3254c872011-11-10 20:45:05 -05002412 /* release the path since we're done with it */
2413 btrfs_release_path(path);
2414
Yan, Zhengd68fc572010-05-16 10:49:58 -04002415 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2416
2417 if (root->orphan_block_rsv)
2418 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2419 (u64)-1);
2420
2421 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002422 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002423 if (!IS_ERR(trans))
2424 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002425 }
Josef Bacik7b128762008-07-24 12:17:14 -04002426
2427 if (nr_unlink)
2428 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2429 if (nr_truncate)
2430 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002431
2432out:
2433 if (ret)
2434 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2435 btrfs_free_path(path);
2436 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002437}
2438
Chris Masond352ac62008-09-29 15:18:18 -04002439/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002440 * very simple check to peek ahead in the leaf looking for xattrs. If we
2441 * don't find any xattrs, we know there can't be any acls.
2442 *
2443 * slot is the slot the inode is in, objectid is the objectid of the inode
2444 */
2445static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2446 int slot, u64 objectid)
2447{
2448 u32 nritems = btrfs_header_nritems(leaf);
2449 struct btrfs_key found_key;
2450 int scanned = 0;
2451
2452 slot++;
2453 while (slot < nritems) {
2454 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2455
2456 /* we found a different objectid, there must not be acls */
2457 if (found_key.objectid != objectid)
2458 return 0;
2459
2460 /* we found an xattr, assume we've got an acl */
2461 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2462 return 1;
2463
2464 /*
2465 * we found a key greater than an xattr key, there can't
2466 * be any acls later on
2467 */
2468 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2469 return 0;
2470
2471 slot++;
2472 scanned++;
2473
2474 /*
2475 * it goes inode, inode backrefs, xattrs, extents,
2476 * so if there are a ton of hard links to an inode there can
2477 * be a lot of backrefs. Don't waste time searching too hard,
2478 * this is just an optimization
2479 */
2480 if (scanned >= 8)
2481 break;
2482 }
2483 /* we hit the end of the leaf before we found an xattr or
2484 * something larger than an xattr. We have to assume the inode
2485 * has acls
2486 */
2487 return 1;
2488}
2489
2490/*
Chris Masond352ac62008-09-29 15:18:18 -04002491 * read an inode from the btree into the in-memory inode
2492 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002493static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002494{
2495 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002496 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002497 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002498 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002499 struct btrfs_root *root = BTRFS_I(inode)->root;
2500 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002501 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002502 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002503 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00002504 bool filled = false;
2505
2506 ret = btrfs_fill_inode(inode, &rdev);
2507 if (!ret)
2508 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04002509
2510 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07002511 if (!path)
2512 goto make_bad;
2513
Josef Bacikd90c7322011-05-17 09:50:54 -04002514 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002515 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002516
Chris Mason39279cc2007-06-12 06:35:45 -04002517 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002518 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002519 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002520
Chris Mason5f39d392007-10-15 16:14:19 -04002521 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00002522
2523 if (filled)
2524 goto cache_acl;
2525
Chris Mason5f39d392007-10-15 16:14:19 -04002526 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2527 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002528 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002529 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002530 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2531 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002532 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002533
2534 tspec = btrfs_inode_atime(inode_item);
2535 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2536 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2537
2538 tspec = btrfs_inode_mtime(inode_item);
2539 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2540 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2541
2542 tspec = btrfs_inode_ctime(inode_item);
2543 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2544 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2545
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002546 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002547 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002548 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002549 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002550 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002551 rdev = btrfs_inode_rdev(leaf, inode_item);
2552
Josef Bacikaec74772008-07-24 12:12:38 -04002553 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002554 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00002555cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04002556 /*
2557 * try to precache a NULL acl entry for files that don't have
2558 * any xattrs or acls
2559 */
Li Zefan33345d012011-04-20 10:31:50 +08002560 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2561 btrfs_ino(inode));
Al Viro72c04902009-06-24 16:58:48 -04002562 if (!maybe_acls)
2563 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002564
Chris Mason39279cc2007-06-12 06:35:45 -04002565 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002566
Chris Mason39279cc2007-06-12 06:35:45 -04002567 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002568 case S_IFREG:
2569 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002570 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002571 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002572 inode->i_fop = &btrfs_file_operations;
2573 inode->i_op = &btrfs_file_inode_operations;
2574 break;
2575 case S_IFDIR:
2576 inode->i_fop = &btrfs_dir_file_operations;
2577 if (root == root->fs_info->tree_root)
2578 inode->i_op = &btrfs_dir_ro_inode_operations;
2579 else
2580 inode->i_op = &btrfs_dir_inode_operations;
2581 break;
2582 case S_IFLNK:
2583 inode->i_op = &btrfs_symlink_inode_operations;
2584 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002585 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002586 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002587 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002588 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002589 init_special_inode(inode, inode->i_mode, rdev);
2590 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002591 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002592
2593 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002594 return;
2595
2596make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002597 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002598 make_bad_inode(inode);
2599}
2600
Chris Masond352ac62008-09-29 15:18:18 -04002601/*
2602 * given a leaf and an inode, copy the inode fields into the leaf
2603 */
Chris Masone02119d2008-09-05 16:13:11 -04002604static void fill_inode_item(struct btrfs_trans_handle *trans,
2605 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002606 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002607 struct inode *inode)
2608{
Chris Mason5f39d392007-10-15 16:14:19 -04002609 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2610 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002611 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002612 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2613 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2614
2615 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2616 inode->i_atime.tv_sec);
2617 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2618 inode->i_atime.tv_nsec);
2619
2620 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2621 inode->i_mtime.tv_sec);
2622 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2623 inode->i_mtime.tv_nsec);
2624
2625 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2626 inode->i_ctime.tv_sec);
2627 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2628 inode->i_ctime.tv_nsec);
2629
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002630 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002631 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002632 btrfs_set_inode_sequence(leaf, item, inode->i_version);
Chris Masone02119d2008-09-05 16:13:11 -04002633 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002634 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002635 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f12011-05-11 15:26:06 -04002636 btrfs_set_inode_block_group(leaf, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002637}
2638
Chris Masond352ac62008-09-29 15:18:18 -04002639/*
2640 * copy everything in the in-memory inode into the btree.
2641 */
Chris Mason21151332011-11-10 20:39:08 -05002642static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05002643 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002644{
2645 struct btrfs_inode_item *inode_item;
2646 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002647 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002648 int ret;
2649
2650 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08002651 if (!path)
2652 return -ENOMEM;
2653
Chris Masonb9473432009-03-13 11:00:37 -04002654 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08002655 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2656 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002657 if (ret) {
2658 if (ret > 0)
2659 ret = -ENOENT;
2660 goto failed;
2661 }
2662
Chris Masonb4ce94d2009-02-04 09:25:08 -05002663 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002664 leaf = path->nodes[0];
2665 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08002666 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04002667
Chris Masone02119d2008-09-05 16:13:11 -04002668 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002669 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002670 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002671 ret = 0;
2672failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002673 btrfs_free_path(path);
2674 return ret;
2675}
2676
Chris Masond352ac62008-09-29 15:18:18 -04002677/*
Chris Mason21151332011-11-10 20:39:08 -05002678 * copy everything in the in-memory inode into the btree.
2679 */
2680noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2681 struct btrfs_root *root, struct inode *inode)
2682{
2683 int ret;
2684
2685 /*
2686 * If the inode is a free space inode, we can deadlock during commit
2687 * if we put it into the delayed code.
2688 *
2689 * The data relocation inode should also be directly updated
2690 * without delay
2691 */
2692 if (!btrfs_is_free_space_inode(root, inode)
2693 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
2694 ret = btrfs_delayed_update_inode(trans, root, inode);
2695 if (!ret)
2696 btrfs_set_inode_last_trans(trans, inode);
2697 return ret;
2698 }
2699
2700 return btrfs_update_inode_item(trans, root, inode);
2701}
2702
2703static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2704 struct btrfs_root *root, struct inode *inode)
2705{
2706 int ret;
2707
2708 ret = btrfs_update_inode(trans, root, inode);
2709 if (ret == -ENOSPC)
2710 return btrfs_update_inode_item(trans, root, inode);
2711 return ret;
2712}
2713
2714/*
Chris Masond352ac62008-09-29 15:18:18 -04002715 * unlink helper that gets used here in inode.c and in the tree logging
2716 * recovery code. It remove a link in a directory with a given name, and
2717 * also drops the back refs in the inode to the directory
2718 */
Al Viro92986792011-03-04 17:14:37 +00002719static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2720 struct btrfs_root *root,
2721 struct inode *dir, struct inode *inode,
2722 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002723{
2724 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002725 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002726 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002727 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002728 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002729 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08002730 u64 ino = btrfs_ino(inode);
2731 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002732
2733 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002734 if (!path) {
2735 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002736 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002737 }
2738
Chris Masonb9473432009-03-13 11:00:37 -04002739 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08002740 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04002741 name, name_len, -1);
2742 if (IS_ERR(di)) {
2743 ret = PTR_ERR(di);
2744 goto err;
2745 }
2746 if (!di) {
2747 ret = -ENOENT;
2748 goto err;
2749 }
Chris Mason5f39d392007-10-15 16:14:19 -04002750 leaf = path->nodes[0];
2751 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002752 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002753 if (ret)
2754 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02002755 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002756
Li Zefan33345d012011-04-20 10:31:50 +08002757 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2758 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002759 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002760 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Li Zefan33345d012011-04-20 10:31:50 +08002761 "inode %llu parent %llu\n", name_len, name,
2762 (unsigned long long)ino, (unsigned long long)dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002763 btrfs_abort_transaction(trans, root, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002764 goto err;
2765 }
2766
Miao Xie16cdcec2011-04-22 18:12:22 +08002767 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002768 if (ret) {
2769 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002770 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002771 }
Chris Mason39279cc2007-06-12 06:35:45 -04002772
Chris Masone02119d2008-09-05 16:13:11 -04002773 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08002774 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002775 if (ret != 0 && ret != -ENOENT) {
2776 btrfs_abort_transaction(trans, root, ret);
2777 goto err;
2778 }
Chris Masone02119d2008-09-05 16:13:11 -04002779
2780 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2781 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002782 if (ret == -ENOENT)
2783 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002784err:
2785 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002786 if (ret)
2787 goto out;
2788
2789 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002790 inode_inc_iversion(inode);
2791 inode_inc_iversion(dir);
Chris Masone02119d2008-09-05 16:13:11 -04002792 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2793 btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002794out:
Chris Mason39279cc2007-06-12 06:35:45 -04002795 return ret;
2796}
2797
Al Viro92986792011-03-04 17:14:37 +00002798int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2799 struct btrfs_root *root,
2800 struct inode *dir, struct inode *inode,
2801 const char *name, int name_len)
2802{
2803 int ret;
2804 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2805 if (!ret) {
2806 btrfs_drop_nlink(inode);
2807 ret = btrfs_update_inode(trans, root, inode);
2808 }
2809 return ret;
2810}
2811
2812
Yan, Zhenga22285a2010-05-16 10:48:46 -04002813/* helper to check if there is any shared block in the path */
2814static int check_path_shared(struct btrfs_root *root,
2815 struct btrfs_path *path)
2816{
2817 struct extent_buffer *eb;
2818 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002819 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002820
2821 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002822 int ret;
2823
Yan, Zhenga22285a2010-05-16 10:48:46 -04002824 if (!path->nodes[level])
2825 break;
2826 eb = path->nodes[level];
2827 if (!btrfs_block_can_be_shared(root, eb))
2828 continue;
2829 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2830 &refs, NULL);
2831 if (refs > 1)
2832 return 1;
2833 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002834 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002835}
2836
2837/*
2838 * helper to start transaction for unlink and rmdir.
2839 *
2840 * unlink and rmdir are special in btrfs, they do not always free space.
2841 * so in enospc case, we should make sure they will free space before
2842 * allowing them to use the global metadata reservation.
2843 */
2844static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2845 struct dentry *dentry)
2846{
2847 struct btrfs_trans_handle *trans;
2848 struct btrfs_root *root = BTRFS_I(dir)->root;
2849 struct btrfs_path *path;
2850 struct btrfs_inode_ref *ref;
2851 struct btrfs_dir_item *di;
2852 struct inode *inode = dentry->d_inode;
2853 u64 index;
2854 int check_link = 1;
2855 int err = -ENOSPC;
2856 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08002857 u64 ino = btrfs_ino(inode);
2858 u64 dir_ino = btrfs_ino(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002859
Josef Bacike70bea52011-10-11 14:18:24 -04002860 /*
2861 * 1 for the possible orphan item
2862 * 1 for the dir item
2863 * 1 for the dir index
2864 * 1 for the inode ref
2865 * 1 for the inode ref in the tree log
2866 * 2 for the dir entries in the log
2867 * 1 for the inode
2868 */
2869 trans = btrfs_start_transaction(root, 8);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002870 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2871 return trans;
2872
Li Zefan33345d012011-04-20 10:31:50 +08002873 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhenga22285a2010-05-16 10:48:46 -04002874 return ERR_PTR(-ENOSPC);
2875
2876 /* check if there is someone else holds reference */
2877 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2878 return ERR_PTR(-ENOSPC);
2879
2880 if (atomic_read(&inode->i_count) > 2)
2881 return ERR_PTR(-ENOSPC);
2882
2883 if (xchg(&root->fs_info->enospc_unlink, 1))
2884 return ERR_PTR(-ENOSPC);
2885
2886 path = btrfs_alloc_path();
2887 if (!path) {
2888 root->fs_info->enospc_unlink = 0;
2889 return ERR_PTR(-ENOMEM);
2890 }
2891
Josef Bacik3880a1b2011-10-14 14:46:51 -04002892 /* 1 for the orphan item */
2893 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002894 if (IS_ERR(trans)) {
2895 btrfs_free_path(path);
2896 root->fs_info->enospc_unlink = 0;
2897 return trans;
2898 }
2899
2900 path->skip_locking = 1;
2901 path->search_commit_root = 1;
2902
2903 ret = btrfs_lookup_inode(trans, root, path,
2904 &BTRFS_I(dir)->location, 0);
2905 if (ret < 0) {
2906 err = ret;
2907 goto out;
2908 }
2909 if (ret == 0) {
2910 if (check_path_shared(root, path))
2911 goto out;
2912 } else {
2913 check_link = 0;
2914 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002915 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002916
2917 ret = btrfs_lookup_inode(trans, root, path,
2918 &BTRFS_I(inode)->location, 0);
2919 if (ret < 0) {
2920 err = ret;
2921 goto out;
2922 }
2923 if (ret == 0) {
2924 if (check_path_shared(root, path))
2925 goto out;
2926 } else {
2927 check_link = 0;
2928 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002929 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002930
2931 if (ret == 0 && S_ISREG(inode->i_mode)) {
2932 ret = btrfs_lookup_file_extent(trans, root, path,
Li Zefan33345d012011-04-20 10:31:50 +08002933 ino, (u64)-1, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002934 if (ret < 0) {
2935 err = ret;
2936 goto out;
2937 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002938 BUG_ON(ret == 0); /* Corruption */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002939 if (check_path_shared(root, path))
2940 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02002941 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002942 }
2943
2944 if (!check_link) {
2945 err = 0;
2946 goto out;
2947 }
2948
Li Zefan33345d012011-04-20 10:31:50 +08002949 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002950 dentry->d_name.name, dentry->d_name.len, 0);
2951 if (IS_ERR(di)) {
2952 err = PTR_ERR(di);
2953 goto out;
2954 }
2955 if (di) {
2956 if (check_path_shared(root, path))
2957 goto out;
2958 } else {
2959 err = 0;
2960 goto out;
2961 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002962 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002963
2964 ref = btrfs_lookup_inode_ref(trans, root, path,
2965 dentry->d_name.name, dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08002966 ino, dir_ino, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002967 if (IS_ERR(ref)) {
2968 err = PTR_ERR(ref);
2969 goto out;
2970 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002971 BUG_ON(!ref); /* Logic error */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002972 if (check_path_shared(root, path))
2973 goto out;
2974 index = btrfs_inode_ref_index(path->nodes[0], ref);
David Sterbab3b4aa72011-04-21 01:20:15 +02002975 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002976
Miao Xie16cdcec2011-04-22 18:12:22 +08002977 /*
2978 * This is a commit root search, if we can lookup inode item and other
2979 * relative items in the commit root, it means the transaction of
2980 * dir/file creation has been committed, and the dir index item that we
2981 * delay to insert has also been inserted into the commit root. So
2982 * we needn't worry about the delayed insertion of the dir index item
2983 * here.
2984 */
Li Zefan33345d012011-04-20 10:31:50 +08002985 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002986 dentry->d_name.name, dentry->d_name.len, 0);
2987 if (IS_ERR(di)) {
2988 err = PTR_ERR(di);
2989 goto out;
2990 }
2991 BUG_ON(ret == -ENOENT);
2992 if (check_path_shared(root, path))
2993 goto out;
2994
2995 err = 0;
2996out:
2997 btrfs_free_path(path);
Josef Bacik3880a1b2011-10-14 14:46:51 -04002998 /* Migrate the orphan reservation over */
2999 if (!err)
3000 err = btrfs_block_rsv_migrate(trans->block_rsv,
3001 &root->fs_info->global_block_rsv,
Josef Bacik5a77d762011-11-01 14:32:23 -04003002 trans->bytes_reserved);
Josef Bacik3880a1b2011-10-14 14:46:51 -04003003
Yan, Zhenga22285a2010-05-16 10:48:46 -04003004 if (err) {
3005 btrfs_end_transaction(trans, root);
3006 root->fs_info->enospc_unlink = 0;
3007 return ERR_PTR(err);
3008 }
3009
3010 trans->block_rsv = &root->fs_info->global_block_rsv;
3011 return trans;
3012}
3013
3014static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3015 struct btrfs_root *root)
3016{
3017 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
Josef Bacik5a77d762011-11-01 14:32:23 -04003018 btrfs_block_rsv_release(root, trans->block_rsv,
3019 trans->bytes_reserved);
3020 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003021 BUG_ON(!root->fs_info->enospc_unlink);
3022 root->fs_info->enospc_unlink = 0;
3023 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003024 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003025}
3026
Chris Mason39279cc2007-06-12 06:35:45 -04003027static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3028{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003029 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003030 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04003031 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003032 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05003033 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003034
Yan, Zhenga22285a2010-05-16 10:48:46 -04003035 trans = __unlink_start_trans(dir, dentry);
3036 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003037 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003038
Chris Mason12fcfd22009-03-24 10:24:20 -04003039 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3040
Chris Masone02119d2008-09-05 16:13:11 -04003041 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3042 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003043 if (ret)
3044 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003045
Yan, Zhenga22285a2010-05-16 10:48:46 -04003046 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04003047 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003048 if (ret)
3049 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003050 }
Josef Bacik7b128762008-07-24 12:17:14 -04003051
Tsutomu Itohb5324022011-07-19 07:27:20 +00003052out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003053 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003054 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003055 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003056 return ret;
3057}
3058
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003059int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3060 struct btrfs_root *root,
3061 struct inode *dir, u64 objectid,
3062 const char *name, int name_len)
3063{
3064 struct btrfs_path *path;
3065 struct extent_buffer *leaf;
3066 struct btrfs_dir_item *di;
3067 struct btrfs_key key;
3068 u64 index;
3069 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08003070 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003071
3072 path = btrfs_alloc_path();
3073 if (!path)
3074 return -ENOMEM;
3075
Li Zefan33345d012011-04-20 10:31:50 +08003076 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003077 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003078 if (IS_ERR_OR_NULL(di)) {
3079 if (!di)
3080 ret = -ENOENT;
3081 else
3082 ret = PTR_ERR(di);
3083 goto out;
3084 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003085
3086 leaf = path->nodes[0];
3087 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3088 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3089 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003090 if (ret) {
3091 btrfs_abort_transaction(trans, root, ret);
3092 goto out;
3093 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003094 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003095
3096 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3097 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003098 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003099 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003100 if (ret != -ENOENT) {
3101 btrfs_abort_transaction(trans, root, ret);
3102 goto out;
3103 }
Li Zefan33345d012011-04-20 10:31:50 +08003104 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003105 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003106 if (IS_ERR_OR_NULL(di)) {
3107 if (!di)
3108 ret = -ENOENT;
3109 else
3110 ret = PTR_ERR(di);
3111 btrfs_abort_transaction(trans, root, ret);
3112 goto out;
3113 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003114
3115 leaf = path->nodes[0];
3116 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02003117 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003118 index = key.offset;
3119 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003120 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003121
Miao Xie16cdcec2011-04-22 18:12:22 +08003122 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003123 if (ret) {
3124 btrfs_abort_transaction(trans, root, ret);
3125 goto out;
3126 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003127
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003128 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003129 inode_inc_iversion(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003130 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3131 ret = btrfs_update_inode(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003132 if (ret)
3133 btrfs_abort_transaction(trans, root, ret);
3134out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003135 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003136 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003137}
3138
Chris Mason39279cc2007-06-12 06:35:45 -04003139static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3140{
3141 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003142 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003143 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003144 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003145 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003146
Chris Mason3394e162008-11-17 20:42:26 -05003147 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Li Zefan33345d012011-04-20 10:31:50 +08003148 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003149 return -ENOTEMPTY;
3150
Yan, Zhenga22285a2010-05-16 10:48:46 -04003151 trans = __unlink_start_trans(dir, dentry);
3152 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003153 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003154
Li Zefan33345d012011-04-20 10:31:50 +08003155 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003156 err = btrfs_unlink_subvol(trans, root, dir,
3157 BTRFS_I(inode)->location.objectid,
3158 dentry->d_name.name,
3159 dentry->d_name.len);
3160 goto out;
3161 }
3162
Josef Bacik7b128762008-07-24 12:17:14 -04003163 err = btrfs_orphan_add(trans, inode);
3164 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003165 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003166
Chris Mason39279cc2007-06-12 06:35:45 -04003167 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003168 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3169 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003170 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003171 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003172out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003173 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003174 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003175 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003176
Chris Mason39279cc2007-06-12 06:35:45 -04003177 return err;
3178}
3179
Chris Mason323ac952008-10-01 19:05:46 -04003180/*
Chris Mason39279cc2007-06-12 06:35:45 -04003181 * this can truncate away extent items, csum items and directory items.
3182 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003183 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003184 *
3185 * csum items that cross the new i_size are truncated to the new size
3186 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003187 *
3188 * min_type is the minimum key type to truncate down to. If set to 0, this
3189 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003190 */
Yan, Zheng80825102009-11-12 09:35:36 +00003191int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3192 struct btrfs_root *root,
3193 struct inode *inode,
3194 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003195{
Chris Mason39279cc2007-06-12 06:35:45 -04003196 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003197 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003198 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003199 struct btrfs_key key;
3200 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003201 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003202 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003203 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003204 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003205 u64 mask = root->sectorsize - 1;
3206 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003207 int found_extent;
3208 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003209 int pending_del_nr = 0;
3210 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003211 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00003212 int ret;
3213 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08003214 u64 ino = btrfs_ino(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003215
3216 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003217
Mark Fasheh0eb0e192011-07-12 16:44:10 -07003218 path = btrfs_alloc_path();
3219 if (!path)
3220 return -ENOMEM;
3221 path->reada = -1;
3222
Josef Bacik0af3d002010-06-21 14:48:16 -04003223 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003224 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003225
Miao Xie16cdcec2011-04-22 18:12:22 +08003226 /*
3227 * This function is also used to drop the items in the log tree before
3228 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3229 * it is used to drop the loged items. So we shouldn't kill the delayed
3230 * items.
3231 */
3232 if (min_type == 0 && root == BTRFS_I(inode)->root)
3233 btrfs_kill_delayed_inode_items(inode);
3234
Li Zefan33345d012011-04-20 10:31:50 +08003235 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003236 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003237 key.type = (u8)-1;
3238
Chris Mason85e21ba2008-01-29 15:11:36 -05003239search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003240 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003241 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003242 if (ret < 0) {
3243 err = ret;
3244 goto out;
3245 }
Chris Masond3977122009-01-05 21:25:51 -05003246
Chris Mason85e21ba2008-01-29 15:11:36 -05003247 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003248 /* there are no items in the tree for us to truncate, we're
3249 * done
3250 */
Yan, Zheng80825102009-11-12 09:35:36 +00003251 if (path->slots[0] == 0)
3252 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003253 path->slots[0]--;
3254 }
3255
Chris Masond3977122009-01-05 21:25:51 -05003256 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003257 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003258 leaf = path->nodes[0];
3259 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3260 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04003261
Li Zefan33345d012011-04-20 10:31:50 +08003262 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003263 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003264
Chris Mason85e21ba2008-01-29 15:11:36 -05003265 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003266 break;
3267
Chris Mason5f39d392007-10-15 16:14:19 -04003268 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003269 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003270 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003271 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003272 extent_type = btrfs_file_extent_type(leaf, fi);
3273 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003274 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003275 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003276 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003277 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003278 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003279 }
Yan008630c2007-11-07 13:31:09 -05003280 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003281 }
Yan, Zheng80825102009-11-12 09:35:36 +00003282 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003283 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003284 } else {
3285 if (item_end < new_size)
3286 break;
3287 if (found_key.offset >= new_size)
3288 del_item = 1;
3289 else
3290 del_item = 0;
3291 }
Chris Mason39279cc2007-06-12 06:35:45 -04003292 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003293 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003294 if (found_type != BTRFS_EXTENT_DATA_KEY)
3295 goto delete;
3296
3297 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003298 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003299 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05003300 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04003301 u64 orig_num_bytes =
3302 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003303 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003304 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003305 extent_num_bytes = extent_num_bytes &
3306 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003307 btrfs_set_file_extent_num_bytes(leaf, fi,
3308 extent_num_bytes);
3309 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003310 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003311 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003312 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003313 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003314 } else {
Chris Masondb945352007-10-15 16:15:53 -04003315 extent_num_bytes =
3316 btrfs_file_extent_disk_num_bytes(leaf,
3317 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003318 extent_offset = found_key.offset -
3319 btrfs_file_extent_offset(leaf, fi);
3320
Chris Mason39279cc2007-06-12 06:35:45 -04003321 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003322 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003323 if (extent_start != 0) {
3324 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003325 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003326 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003327 }
3328 }
Chris Mason90692182008-02-08 13:49:28 -05003329 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003330 /*
3331 * we can't truncate inline items that have had
3332 * special encodings
3333 */
3334 if (!del_item &&
3335 btrfs_file_extent_compression(leaf, fi) == 0 &&
3336 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3337 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003338 u32 size = new_size - found_key.offset;
3339
3340 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003341 inode_sub_bytes(inode, item_end + 1 -
3342 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003343 }
3344 size =
3345 btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003346 btrfs_truncate_item(trans, root, path,
3347 size, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003348 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003349 inode_sub_bytes(inode, item_end + 1 -
3350 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003351 }
Chris Mason39279cc2007-06-12 06:35:45 -04003352 }
Chris Mason179e29e2007-11-01 11:28:41 -04003353delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003354 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003355 if (!pending_del_nr) {
3356 /* no pending yet, add ourselves */
3357 pending_del_slot = path->slots[0];
3358 pending_del_nr = 1;
3359 } else if (pending_del_nr &&
3360 path->slots[0] + 1 == pending_del_slot) {
3361 /* hop on the pending chunk */
3362 pending_del_nr++;
3363 pending_del_slot = path->slots[0];
3364 } else {
Chris Masond3977122009-01-05 21:25:51 -05003365 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003366 }
Chris Mason39279cc2007-06-12 06:35:45 -04003367 } else {
3368 break;
3369 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003370 if (found_extent && (root->ref_cows ||
3371 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003372 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003373 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003374 extent_num_bytes, 0,
3375 btrfs_header_owner(leaf),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003376 ino, extent_offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003377 BUG_ON(ret);
3378 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003379
Yan, Zheng80825102009-11-12 09:35:36 +00003380 if (found_type == BTRFS_INODE_ITEM_KEY)
3381 break;
3382
3383 if (path->slots[0] == 0 ||
3384 path->slots[0] != pending_del_slot) {
Li Zefan82d59022011-04-20 10:33:24 +08003385 if (root->ref_cows &&
3386 BTRFS_I(inode)->location.objectid !=
3387 BTRFS_FREE_INO_OBJECTID) {
Yan, Zheng80825102009-11-12 09:35:36 +00003388 err = -EAGAIN;
3389 goto out;
3390 }
3391 if (pending_del_nr) {
3392 ret = btrfs_del_items(trans, root, path,
3393 pending_del_slot,
3394 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003395 if (ret) {
3396 btrfs_abort_transaction(trans,
3397 root, ret);
3398 goto error;
3399 }
Yan, Zheng80825102009-11-12 09:35:36 +00003400 pending_del_nr = 0;
3401 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003402 btrfs_release_path(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003403 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003404 } else {
3405 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003406 }
Chris Mason39279cc2007-06-12 06:35:45 -04003407 }
Yan, Zheng80825102009-11-12 09:35:36 +00003408out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003409 if (pending_del_nr) {
3410 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3411 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003412 if (ret)
3413 btrfs_abort_transaction(trans, root, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003414 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003415error:
Chris Mason39279cc2007-06-12 06:35:45 -04003416 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003417 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003418}
3419
Chris Masona52d9a82007-08-27 16:49:44 -04003420/*
3421 * taken from block_truncate_page, but does cow as it zeros out
3422 * any bytes left in the last page in the file.
3423 */
3424static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3425{
3426 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003427 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003428 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3429 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003430 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003431 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003432 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003433 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3434 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3435 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003436 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04003437 int ret = 0;
3438 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003439 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003440
3441 if ((offset & (blocksize - 1)) == 0)
3442 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003443 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003444 if (ret)
3445 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003446
3447 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003448again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04003449 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003450 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003451 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003452 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003453 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003454
3455 page_start = page_offset(page);
3456 page_end = page_start + PAGE_CACHE_SIZE - 1;
3457
Chris Masona52d9a82007-08-27 16:49:44 -04003458 if (!PageUptodate(page)) {
3459 ret = btrfs_readpage(NULL, page);
3460 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003461 if (page->mapping != mapping) {
3462 unlock_page(page);
3463 page_cache_release(page);
3464 goto again;
3465 }
Chris Masona52d9a82007-08-27 16:49:44 -04003466 if (!PageUptodate(page)) {
3467 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003468 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003469 }
3470 }
Chris Mason211c17f2008-05-15 09:13:45 -04003471 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003472
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003473 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003474 set_page_extent_mapped(page);
3475
3476 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3477 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003478 unlock_extent_cached(io_tree, page_start, page_end,
3479 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003480 unlock_page(page);
3481 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003482 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003483 btrfs_put_ordered_extent(ordered);
3484 goto again;
3485 }
3486
Josef Bacik2ac55d42010-02-03 19:33:23 +00003487 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003488 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003489 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003490
Josef Bacik2ac55d42010-02-03 19:33:23 +00003491 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3492 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003493 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003494 unlock_extent_cached(io_tree, page_start, page_end,
3495 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003496 goto out_unlock;
3497 }
3498
Chris Masone6dcd2d2008-07-17 12:53:50 -04003499 ret = 0;
3500 if (offset != PAGE_CACHE_SIZE) {
3501 kaddr = kmap(page);
3502 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3503 flush_dcache_page(page);
3504 kunmap(page);
3505 }
Chris Mason247e7432008-07-17 12:53:51 -04003506 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003507 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003508 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3509 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003510
Chris Mason89642222008-07-24 09:41:53 -04003511out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003512 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003513 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003514 unlock_page(page);
3515 page_cache_release(page);
3516out:
3517 return ret;
3518}
3519
Josef Bacik695a0d02011-03-04 15:46:53 -05003520/*
3521 * This function puts in dummy file extents for the area we're creating a hole
3522 * for. So if we are truncating this file to a larger size we need to insert
3523 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3524 * the range between oldsize and size
3525 */
Josef Bacika41ad392011-01-31 15:30:16 -05003526int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003527{
3528 struct btrfs_trans_handle *trans;
3529 struct btrfs_root *root = BTRFS_I(inode)->root;
3530 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003531 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003532 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003533 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003534 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003535 u64 block_end = (size + mask) & ~mask;
3536 u64 last_byte;
3537 u64 cur_offset;
3538 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003539 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003540
3541 if (size <= hole_start)
3542 return 0;
3543
Yan Zheng9036c102008-10-30 14:19:41 -04003544 while (1) {
3545 struct btrfs_ordered_extent *ordered;
3546 btrfs_wait_ordered_range(inode, hole_start,
3547 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003548 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003549 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04003550 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3551 if (!ordered)
3552 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003553 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3554 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003555 btrfs_put_ordered_extent(ordered);
3556 }
3557
Yan Zheng9036c102008-10-30 14:19:41 -04003558 cur_offset = hole_start;
3559 while (1) {
3560 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3561 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003562 if (IS_ERR(em)) {
3563 err = PTR_ERR(em);
3564 break;
3565 }
Yan Zheng9036c102008-10-30 14:19:41 -04003566 last_byte = min(extent_map_end(em), block_end);
3567 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003568 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003569 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003570 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003571
Miao Xie36423202011-12-14 20:12:02 -05003572 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003573 if (IS_ERR(trans)) {
3574 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003575 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003576 }
Yan, Zheng80825102009-11-12 09:35:36 +00003577
3578 err = btrfs_drop_extents(trans, inode, cur_offset,
3579 cur_offset + hole_size,
3580 &hint_byte, 1);
Miao Xie5b397372011-09-11 10:52:24 -04003581 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003582 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003583 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003584 break;
Miao Xie5b397372011-09-11 10:52:24 -04003585 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04003586
Yan Zheng9036c102008-10-30 14:19:41 -04003587 err = btrfs_insert_file_extent(trans, root,
Li Zefan33345d012011-04-20 10:31:50 +08003588 btrfs_ino(inode), cur_offset, 0,
Yan Zheng9036c102008-10-30 14:19:41 -04003589 0, hole_size, 0, hole_size,
3590 0, 0, 0);
Miao Xie5b397372011-09-11 10:52:24 -04003591 if (err) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003592 btrfs_abort_transaction(trans, root, err);
Miao Xie5b397372011-09-11 10:52:24 -04003593 btrfs_end_transaction(trans, root);
Josef Bacik3893e332011-01-31 16:03:11 -05003594 break;
Miao Xie5b397372011-09-11 10:52:24 -04003595 }
Yan, Zheng80825102009-11-12 09:35:36 +00003596
Yan Zheng9036c102008-10-30 14:19:41 -04003597 btrfs_drop_extent_cache(inode, hole_start,
3598 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003599
Miao Xie36423202011-12-14 20:12:02 -05003600 btrfs_update_inode(trans, root, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003601 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003602 }
3603 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003604 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003605 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003606 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003607 break;
3608 }
3609
Yan, Zhenga22285a2010-05-16 10:48:46 -04003610 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003611 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3612 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003613 return err;
3614}
3615
Josef Bacika41ad392011-01-31 15:30:16 -05003616static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003617{
Miao Xief4a2f4c2011-12-14 20:12:01 -05003618 struct btrfs_root *root = BTRFS_I(inode)->root;
3619 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05003620 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003621 int ret;
3622
Josef Bacika41ad392011-01-31 15:30:16 -05003623 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003624 return 0;
3625
Josef Bacika41ad392011-01-31 15:30:16 -05003626 if (newsize > oldsize) {
Josef Bacika41ad392011-01-31 15:30:16 -05003627 truncate_pagecache(inode, oldsize, newsize);
3628 ret = btrfs_cont_expand(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05003629 if (ret)
Yan, Zheng80825102009-11-12 09:35:36 +00003630 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003631
Miao Xief4a2f4c2011-12-14 20:12:01 -05003632 trans = btrfs_start_transaction(root, 1);
3633 if (IS_ERR(trans))
3634 return PTR_ERR(trans);
3635
3636 i_size_write(inode, newsize);
3637 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3638 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05003639 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05003640 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003641
Josef Bacika41ad392011-01-31 15:30:16 -05003642 /*
3643 * We're truncating a file that used to have good data down to
3644 * zero. Make sure it gets into the ordered flush list so that
3645 * any new writes get down to disk quickly.
3646 */
3647 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04003648 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3649 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00003650
Josef Bacika41ad392011-01-31 15:30:16 -05003651 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3652 truncate_setsize(inode, newsize);
3653 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003654 }
3655
Josef Bacika41ad392011-01-31 15:30:16 -05003656 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003657}
3658
Chris Mason39279cc2007-06-12 06:35:45 -04003659static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3660{
3661 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003662 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003663 int err;
3664
Li Zefanb83cc962010-12-20 16:04:08 +08003665 if (btrfs_root_readonly(root))
3666 return -EROFS;
3667
Chris Mason39279cc2007-06-12 06:35:45 -04003668 err = inode_change_ok(inode, attr);
3669 if (err)
3670 return err;
3671
Chris Mason5a3f23d2009-03-31 13:27:11 -04003672 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003673 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003674 if (err)
3675 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003676 }
Yan Zheng9036c102008-10-30 14:19:41 -04003677
Christoph Hellwig10257742010-06-04 11:30:02 +02003678 if (attr->ia_valid) {
3679 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003680 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05003681 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003682
Josef Bacik22c44fe2011-11-30 10:45:38 -05003683 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig10257742010-06-04 11:30:02 +02003684 err = btrfs_acl_chmod(inode);
3685 }
3686
Chris Mason39279cc2007-06-12 06:35:45 -04003687 return err;
3688}
Chris Mason61295eb2008-01-14 16:24:38 -05003689
Al Virobd555972010-06-07 11:35:40 -04003690void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003691{
3692 struct btrfs_trans_handle *trans;
3693 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04003694 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik07127182011-08-19 10:29:59 -04003695 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003696 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003697 int ret;
3698
liubo1abe9b82011-03-24 11:18:59 +00003699 trace_btrfs_inode_evict(inode);
3700
Chris Mason39279cc2007-06-12 06:35:45 -04003701 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003702 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
Chris Mason2cf85722011-07-26 15:35:09 -04003703 btrfs_is_free_space_inode(root, inode)))
Al Virobd555972010-06-07 11:35:40 -04003704 goto no_delete;
3705
Chris Mason39279cc2007-06-12 06:35:45 -04003706 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003707 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003708 goto no_delete;
3709 }
Al Virobd555972010-06-07 11:35:40 -04003710 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003711 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003712
Yan, Zhengc71bf092009-11-12 09:34:40 +00003713 if (root->fs_info->log_root_recovering) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003714 BUG_ON(!test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3715 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00003716 goto no_delete;
3717 }
3718
Yan, Zheng76dda932009-09-21 16:00:26 -04003719 if (inode->i_nlink > 0) {
3720 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3721 goto no_delete;
3722 }
3723
Josef Bacik4289a662011-08-05 13:22:24 -04003724 rsv = btrfs_alloc_block_rsv(root);
3725 if (!rsv) {
3726 btrfs_orphan_del(NULL, inode);
3727 goto no_delete;
3728 }
Josef Bacik4a338542011-08-29 11:01:31 -04003729 rsv->size = min_size;
Josef Bacik726c35f2011-09-26 15:46:06 -04003730 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04003731
Chris Masondbe674a2008-07-17 12:54:05 -04003732 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003733
Josef Bacik4289a662011-08-05 13:22:24 -04003734 /*
3735 * This is a bit simpler than btrfs_truncate since
3736 *
3737 * 1) We've already reserved our space for our orphan item in the
3738 * unlink.
3739 * 2) We're going to delete the inode item, so we don't need to update
3740 * it at all.
3741 *
3742 * So we just need to reserve some slack space in case we add bytes when
3743 * doing the truncate.
3744 */
Yan, Zheng80825102009-11-12 09:35:36 +00003745 while (1) {
Miao Xieaa38a712011-11-18 17:43:00 +08003746 ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003747
Josef Bacik726c35f2011-09-26 15:46:06 -04003748 /*
3749 * Try and steal from the global reserve since we will
3750 * likely not use this space anyway, we want to try as
3751 * hard as possible to get this to work.
3752 */
3753 if (ret)
3754 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3755
Yan, Zhengd68fc572010-05-16 10:49:58 -04003756 if (ret) {
Josef Bacik4289a662011-08-05 13:22:24 -04003757 printk(KERN_WARNING "Could not get space for a "
Josef Bacik482e6dc2011-08-19 10:31:56 -04003758 "delete, will truncate on mount %d\n", ret);
Josef Bacik4289a662011-08-05 13:22:24 -04003759 btrfs_orphan_del(NULL, inode);
3760 btrfs_free_block_rsv(root, rsv);
3761 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003762 }
3763
Josef Bacik4289a662011-08-05 13:22:24 -04003764 trans = btrfs_start_transaction(root, 0);
3765 if (IS_ERR(trans)) {
3766 btrfs_orphan_del(NULL, inode);
3767 btrfs_free_block_rsv(root, rsv);
3768 goto no_delete;
3769 }
3770
3771 trans->block_rsv = rsv;
3772
Yan, Zhengd68fc572010-05-16 10:49:58 -04003773 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003774 if (ret != -EAGAIN)
3775 break;
3776
3777 nr = trans->blocks_used;
3778 btrfs_end_transaction(trans, root);
3779 trans = NULL;
3780 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003781 }
3782
Josef Bacik4289a662011-08-05 13:22:24 -04003783 btrfs_free_block_rsv(root, rsv);
3784
Yan, Zheng80825102009-11-12 09:35:36 +00003785 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04003786 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003787 ret = btrfs_orphan_del(trans, inode);
3788 BUG_ON(ret);
3789 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003790
Josef Bacik4289a662011-08-05 13:22:24 -04003791 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08003792 if (!(root == root->fs_info->tree_root ||
3793 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08003794 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08003795
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003796 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003797 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003798 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003799no_delete:
Al Virobd555972010-06-07 11:35:40 -04003800 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003801 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003802}
3803
3804/*
3805 * this returns the key found in the dir entry in the location pointer.
3806 * If no dir entries were found, location->objectid is 0.
3807 */
3808static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3809 struct btrfs_key *location)
3810{
3811 const char *name = dentry->d_name.name;
3812 int namelen = dentry->d_name.len;
3813 struct btrfs_dir_item *di;
3814 struct btrfs_path *path;
3815 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003816 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003817
3818 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07003819 if (!path)
3820 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05003821
Li Zefan33345d012011-04-20 10:31:50 +08003822 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04003823 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003824 if (IS_ERR(di))
3825 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003826
David Sterbac7040052011-04-19 18:00:01 +02003827 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05003828 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003829
Chris Mason5f39d392007-10-15 16:14:19 -04003830 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003831out:
Chris Mason39279cc2007-06-12 06:35:45 -04003832 btrfs_free_path(path);
3833 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003834out_err:
3835 location->objectid = 0;
3836 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003837}
3838
3839/*
3840 * when we hit a tree root in a directory, the btrfs part of the inode
3841 * needs to be changed to reflect the root directory of the tree root. This
3842 * is kind of like crossing a mount point.
3843 */
3844static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003845 struct inode *dir,
3846 struct dentry *dentry,
3847 struct btrfs_key *location,
3848 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003849{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003850 struct btrfs_path *path;
3851 struct btrfs_root *new_root;
3852 struct btrfs_root_ref *ref;
3853 struct extent_buffer *leaf;
3854 int ret;
3855 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003856
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003857 path = btrfs_alloc_path();
3858 if (!path) {
3859 err = -ENOMEM;
3860 goto out;
3861 }
Chris Mason39279cc2007-06-12 06:35:45 -04003862
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003863 err = -ENOENT;
3864 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3865 BTRFS_I(dir)->root->root_key.objectid,
3866 location->objectid);
3867 if (ret) {
3868 if (ret < 0)
3869 err = ret;
3870 goto out;
3871 }
Chris Mason39279cc2007-06-12 06:35:45 -04003872
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003873 leaf = path->nodes[0];
3874 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08003875 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003876 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3877 goto out;
3878
3879 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3880 (unsigned long)(ref + 1),
3881 dentry->d_name.len);
3882 if (ret)
3883 goto out;
3884
David Sterbab3b4aa72011-04-21 01:20:15 +02003885 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003886
3887 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3888 if (IS_ERR(new_root)) {
3889 err = PTR_ERR(new_root);
3890 goto out;
3891 }
3892
3893 if (btrfs_root_refs(&new_root->root_item) == 0) {
3894 err = -ENOENT;
3895 goto out;
3896 }
3897
3898 *sub_root = new_root;
3899 location->objectid = btrfs_root_dirid(&new_root->root_item);
3900 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003901 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003902 err = 0;
3903out:
3904 btrfs_free_path(path);
3905 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003906}
3907
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003908static void inode_tree_add(struct inode *inode)
3909{
3910 struct btrfs_root *root = BTRFS_I(inode)->root;
3911 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003912 struct rb_node **p;
3913 struct rb_node *parent;
Li Zefan33345d012011-04-20 10:31:50 +08003914 u64 ino = btrfs_ino(inode);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003915again:
3916 p = &root->inode_tree.rb_node;
3917 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003918
Al Viro1d3382cb2010-10-23 15:19:20 -04003919 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003920 return;
3921
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003922 spin_lock(&root->inode_lock);
3923 while (*p) {
3924 parent = *p;
3925 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3926
Li Zefan33345d012011-04-20 10:31:50 +08003927 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003928 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003929 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003930 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003931 else {
3932 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003933 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003934 rb_erase(parent, &root->inode_tree);
3935 RB_CLEAR_NODE(parent);
3936 spin_unlock(&root->inode_lock);
3937 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003938 }
3939 }
3940 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3941 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3942 spin_unlock(&root->inode_lock);
3943}
3944
3945static void inode_tree_del(struct inode *inode)
3946{
3947 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003948 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003949
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003950 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003951 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003952 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003953 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003954 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003955 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003956 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003957
Josef Bacik0af3d002010-06-21 14:48:16 -04003958 /*
3959 * Free space cache has inodes in the tree root, but the tree root has a
3960 * root_refs of 0, so this could end up dropping the tree root as a
3961 * snapshot, so we need the extra !root->fs_info->tree_root check to
3962 * make sure we don't drop it.
3963 */
3964 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3965 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003966 synchronize_srcu(&root->fs_info->subvol_srcu);
3967 spin_lock(&root->inode_lock);
3968 empty = RB_EMPTY_ROOT(&root->inode_tree);
3969 spin_unlock(&root->inode_lock);
3970 if (empty)
3971 btrfs_add_dead_root(root);
3972 }
3973}
3974
Jeff Mahoney143bede2012-03-01 14:56:26 +01003975void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04003976{
3977 struct rb_node *node;
3978 struct rb_node *prev;
3979 struct btrfs_inode *entry;
3980 struct inode *inode;
3981 u64 objectid = 0;
3982
3983 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3984
3985 spin_lock(&root->inode_lock);
3986again:
3987 node = root->inode_tree.rb_node;
3988 prev = NULL;
3989 while (node) {
3990 prev = node;
3991 entry = rb_entry(node, struct btrfs_inode, rb_node);
3992
Li Zefan33345d012011-04-20 10:31:50 +08003993 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003994 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08003995 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003996 node = node->rb_right;
3997 else
3998 break;
3999 }
4000 if (!node) {
4001 while (prev) {
4002 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004003 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04004004 node = prev;
4005 break;
4006 }
4007 prev = rb_next(prev);
4008 }
4009 }
4010 while (node) {
4011 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08004012 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04004013 inode = igrab(&entry->vfs_inode);
4014 if (inode) {
4015 spin_unlock(&root->inode_lock);
4016 if (atomic_read(&inode->i_count) > 1)
4017 d_prune_aliases(inode);
4018 /*
Al Viro45321ac2010-06-07 13:43:19 -04004019 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004020 * the inode cache when its usage count
4021 * hits zero.
4022 */
4023 iput(inode);
4024 cond_resched();
4025 spin_lock(&root->inode_lock);
4026 goto again;
4027 }
4028
4029 if (cond_resched_lock(&root->inode_lock))
4030 goto again;
4031
4032 node = rb_next(node);
4033 }
4034 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004035}
4036
Chris Masone02119d2008-09-05 16:13:11 -04004037static int btrfs_init_locked_inode(struct inode *inode, void *p)
4038{
4039 struct btrfs_iget_args *args = p;
4040 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004041 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004042 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004043 return 0;
4044}
4045
4046static int btrfs_find_actor(struct inode *inode, void *opaque)
4047{
4048 struct btrfs_iget_args *args = opaque;
Li Zefan33345d012011-04-20 10:31:50 +08004049 return args->ino == btrfs_ino(inode) &&
Chris Masond3977122009-01-05 21:25:51 -05004050 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004051}
4052
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004053static struct inode *btrfs_iget_locked(struct super_block *s,
4054 u64 objectid,
4055 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004056{
4057 struct inode *inode;
4058 struct btrfs_iget_args args;
4059 args.ino = objectid;
4060 args.root = root;
4061
4062 inode = iget5_locked(s, objectid, btrfs_find_actor,
4063 btrfs_init_locked_inode,
4064 (void *)&args);
4065 return inode;
4066}
4067
Balaji Rao1a54ef82008-07-21 02:01:04 +05304068/* Get an inode object given its location and corresponding root.
4069 * Returns in *is_new if the inode was read from disk
4070 */
4071struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004072 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304073{
4074 struct inode *inode;
4075
4076 inode = btrfs_iget_locked(s, location->objectid, root);
4077 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004078 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304079
4080 if (inode->i_state & I_NEW) {
4081 BTRFS_I(inode)->root = root;
4082 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4083 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07004084 if (!is_bad_inode(inode)) {
4085 inode_tree_add(inode);
4086 unlock_new_inode(inode);
4087 if (new)
4088 *new = 1;
4089 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04004090 unlock_new_inode(inode);
4091 iput(inode);
4092 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07004093 }
4094 }
4095
Balaji Rao1a54ef82008-07-21 02:01:04 +05304096 return inode;
4097}
4098
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004099static struct inode *new_simple_dir(struct super_block *s,
4100 struct btrfs_key *key,
4101 struct btrfs_root *root)
4102{
4103 struct inode *inode = new_inode(s);
4104
4105 if (!inode)
4106 return ERR_PTR(-ENOMEM);
4107
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004108 BTRFS_I(inode)->root = root;
4109 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04004110 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004111
4112 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08004113 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004114 inode->i_fop = &simple_dir_operations;
4115 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4116 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4117
4118 return inode;
4119}
4120
Chris Mason3de45862008-11-17 21:02:50 -05004121struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004122{
Chris Masond3977122009-01-05 21:25:51 -05004123 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004124 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004125 struct btrfs_root *sub_root = root;
4126 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004127 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004128 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004129
4130 if (dentry->d_name.len > BTRFS_NAME_LEN)
4131 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004132
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004133 if (unlikely(d_need_lookup(dentry))) {
4134 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4135 kfree(dentry->d_fsdata);
4136 dentry->d_fsdata = NULL;
Josef Bacika66e7cc2011-09-18 10:34:03 -04004137 /* This thing is hashed, drop it for now */
4138 d_drop(dentry);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004139 } else {
4140 ret = btrfs_inode_by_name(dir, dentry, &location);
4141 }
Chris Mason5f39d392007-10-15 16:14:19 -04004142
Chris Mason39279cc2007-06-12 06:35:45 -04004143 if (ret < 0)
4144 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004145
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004146 if (location.objectid == 0)
4147 return NULL;
4148
4149 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004150 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004151 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004152 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004153
4154 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4155
Yan, Zheng76dda932009-09-21 16:00:26 -04004156 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004157 ret = fixup_tree_root_location(root, dir, dentry,
4158 &location, &sub_root);
4159 if (ret < 0) {
4160 if (ret != -ENOENT)
4161 inode = ERR_PTR(ret);
4162 else
4163 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4164 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004165 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004166 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004167 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4168
Julia Lawall34d19ba2011-01-24 19:55:19 +00004169 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004170 down_read(&root->fs_info->cleanup_work_sem);
4171 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004172 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004173 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004174 if (ret)
4175 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004176 }
4177
Chris Mason3de45862008-11-17 21:02:50 -05004178 return inode;
4179}
4180
Nick Pigginfe15ce42011-01-07 17:49:23 +11004181static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004182{
4183 struct btrfs_root *root;
Li Zefan848cce02012-02-21 17:04:28 +08004184 struct inode *inode = dentry->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004185
Li Zefan848cce02012-02-21 17:04:28 +08004186 if (!inode && !IS_ROOT(dentry))
4187 inode = dentry->d_parent->d_inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04004188
Li Zefan848cce02012-02-21 17:04:28 +08004189 if (inode) {
4190 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04004191 if (btrfs_root_refs(&root->root_item) == 0)
4192 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08004193
4194 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4195 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04004196 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004197 return 0;
4198}
4199
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004200static void btrfs_dentry_release(struct dentry *dentry)
4201{
4202 if (dentry->d_fsdata)
4203 kfree(dentry->d_fsdata);
4204}
4205
Chris Mason3de45862008-11-17 21:02:50 -05004206static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4207 struct nameidata *nd)
4208{
Josef Bacika66e7cc2011-09-18 10:34:03 -04004209 struct dentry *ret;
4210
4211 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4212 if (unlikely(d_need_lookup(dentry))) {
4213 spin_lock(&dentry->d_lock);
4214 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4215 spin_unlock(&dentry->d_lock);
4216 }
4217 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004218}
4219
Miao Xie16cdcec2011-04-22 18:12:22 +08004220unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04004221 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4222};
4223
David Woodhousecbdf5a22008-08-06 19:42:33 +01004224static int btrfs_real_readdir(struct file *filp, void *dirent,
4225 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004226{
Chris Mason6da6aba2007-12-18 16:15:09 -05004227 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004228 struct btrfs_root *root = BTRFS_I(inode)->root;
4229 struct btrfs_item *item;
4230 struct btrfs_dir_item *di;
4231 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004232 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004233 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08004234 struct list_head ins_list;
4235 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04004236 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004237 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004238 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004239 unsigned char d_type;
4240 int over = 0;
4241 u32 di_cur;
4242 u32 di_total;
4243 u32 di_len;
4244 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004245 char tmp_name[32];
4246 char *name_ptr;
4247 int name_len;
Miao Xie16cdcec2011-04-22 18:12:22 +08004248 int is_curr = 0; /* filp->f_pos points to the current index? */
Chris Mason39279cc2007-06-12 06:35:45 -04004249
4250 /* FIXME, use a real flag for deciding about the key type */
4251 if (root->fs_info->tree_root == root)
4252 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004253
Chris Mason39544012007-12-12 14:38:19 -05004254 /* special case for "." */
4255 if (filp->f_pos == 0) {
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004256 over = filldir(dirent, ".", 1,
4257 filp->f_pos, btrfs_ino(inode), DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004258 if (over)
4259 return 0;
4260 filp->f_pos = 1;
4261 }
Chris Mason39544012007-12-12 14:38:19 -05004262 /* special case for .., just use the back ref */
4263 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004264 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004265 over = filldir(dirent, "..", 2,
Hidetoshi Seto3765fef2011-09-18 10:20:46 -04004266 filp->f_pos, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004267 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004268 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004269 filp->f_pos = 2;
4270 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004271 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004272 if (!path)
4273 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04004274
Josef Bacik026fd312011-05-13 10:32:11 -04004275 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004276
Miao Xie16cdcec2011-04-22 18:12:22 +08004277 if (key_type == BTRFS_DIR_INDEX_KEY) {
4278 INIT_LIST_HEAD(&ins_list);
4279 INIT_LIST_HEAD(&del_list);
4280 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4281 }
4282
Chris Mason39279cc2007-06-12 06:35:45 -04004283 btrfs_set_key_type(&key, key_type);
4284 key.offset = filp->f_pos;
Li Zefan33345d012011-04-20 10:31:50 +08004285 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004286
Chris Mason39279cc2007-06-12 06:35:45 -04004287 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4288 if (ret < 0)
4289 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004290
4291 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004292 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004293 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004294 if (slot >= btrfs_header_nritems(leaf)) {
4295 ret = btrfs_next_leaf(root, path);
4296 if (ret < 0)
4297 goto err;
4298 else if (ret > 0)
4299 break;
4300 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004301 }
Chris Mason3de45862008-11-17 21:02:50 -05004302
Chris Mason5f39d392007-10-15 16:14:19 -04004303 item = btrfs_item_nr(leaf, slot);
4304 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4305
4306 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004307 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004308 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004309 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004310 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004311 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08004312 if (key_type == BTRFS_DIR_INDEX_KEY &&
4313 btrfs_should_delete_dir_index(&del_list,
4314 found_key.offset))
4315 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004316
4317 filp->f_pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08004318 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004319
Chris Mason39279cc2007-06-12 06:35:45 -04004320 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4321 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004322 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004323
4324 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004325 struct btrfs_key location;
4326
Josef Bacik22a94d42011-03-16 16:47:17 -04004327 if (verify_dir_item(root, leaf, di))
4328 break;
4329
Chris Mason5f39d392007-10-15 16:14:19 -04004330 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004331 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004332 name_ptr = tmp_name;
4333 } else {
4334 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004335 if (!name_ptr) {
4336 ret = -ENOMEM;
4337 goto err;
4338 }
Chris Mason5f39d392007-10-15 16:14:19 -04004339 }
4340 read_extent_buffer(leaf, name_ptr,
4341 (unsigned long)(di + 1), name_len);
4342
4343 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4344 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004345
Josef Bacikb4aff1f2011-06-28 16:18:59 -04004346
Chris Mason3de45862008-11-17 21:02:50 -05004347 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01004348 * skip it.
4349 *
4350 * In contrast to old kernels, we insert the snapshot's
4351 * dir item and dir index after it has been created, so
4352 * we won't find a reference to our own snapshot. We
4353 * still keep the following code for backward
4354 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05004355 */
4356 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4357 location.objectid == root->root_key.objectid) {
4358 over = 0;
4359 goto skip;
4360 }
Chris Mason5f39d392007-10-15 16:14:19 -04004361 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004362 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004363 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004364
Chris Mason3de45862008-11-17 21:02:50 -05004365skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004366 if (name_ptr != tmp_name)
4367 kfree(name_ptr);
4368
Chris Mason39279cc2007-06-12 06:35:45 -04004369 if (over)
4370 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004371 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004372 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004373 di_cur += di_len;
4374 di = (struct btrfs_dir_item *)((char *)di + di_len);
4375 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004376next:
4377 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004378 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004379
Miao Xie16cdcec2011-04-22 18:12:22 +08004380 if (key_type == BTRFS_DIR_INDEX_KEY) {
4381 if (is_curr)
4382 filp->f_pos++;
4383 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4384 &ins_list);
4385 if (ret)
4386 goto nopos;
4387 }
4388
David Woodhouse49593bf2008-08-17 17:08:36 +01004389 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004390 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004391 /*
4392 * 32-bit glibc will use getdents64, but then strtol -
4393 * so the last number we can serve is this.
4394 */
4395 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004396 else
4397 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004398nopos:
4399 ret = 0;
4400err:
Miao Xie16cdcec2011-04-22 18:12:22 +08004401 if (key_type == BTRFS_DIR_INDEX_KEY)
4402 btrfs_put_delayed_items(&ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04004403 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004404 return ret;
4405}
4406
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004407int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004408{
4409 struct btrfs_root *root = BTRFS_I(inode)->root;
4410 struct btrfs_trans_handle *trans;
4411 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004412 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004413
Josef Bacik72ac3c02012-05-23 14:13:11 -04004414 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04004415 return 0;
4416
Chris Mason2cf85722011-07-26 15:35:09 -04004417 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(root, inode))
Li Zefan82d59022011-04-20 10:33:24 +08004418 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04004419
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004420 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004421 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004422 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004423 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004424 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004425 if (IS_ERR(trans))
4426 return PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04004427 if (nolock)
4428 ret = btrfs_end_transaction_nolock(trans, root);
4429 else
4430 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004431 }
4432 return ret;
4433}
4434
4435/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004436 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004437 * inode changes. But, it is most likely to find the inode in cache.
4438 * FIXME, needs more benchmarking...there are no reasons other than performance
4439 * to keep or drop this code.
4440 */
Josef Bacik22c44fe2011-11-30 10:45:38 -05004441int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004442{
4443 struct btrfs_root *root = BTRFS_I(inode)->root;
4444 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004445 int ret;
4446
Josef Bacik72ac3c02012-05-23 14:13:11 -04004447 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05004448 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004449
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004450 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004451 if (IS_ERR(trans))
4452 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004453
4454 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004455 if (ret && ret == -ENOSPC) {
4456 /* whoops, lets try again with the full transaction */
4457 btrfs_end_transaction(trans, root);
4458 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004459 if (IS_ERR(trans))
4460 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004461
Chris Mason94b60442010-05-26 11:02:00 -04004462 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004463 }
Chris Mason39279cc2007-06-12 06:35:45 -04004464 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08004465 if (BTRFS_I(inode)->delayed_node)
4466 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004467
4468 return ret;
4469}
4470
4471/*
4472 * This is a copy of file_update_time. We need this so we can return error on
4473 * ENOSPC for updating the inode in the case of file write and mmap writes.
4474 */
4475int btrfs_update_time(struct file *file)
4476{
4477 struct inode *inode = file->f_path.dentry->d_inode;
4478 struct timespec now;
4479 int ret;
4480 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
4481
4482 /* First try to exhaust all avenues to not sync */
4483 if (IS_NOCMTIME(inode))
4484 return 0;
4485
4486 now = current_fs_time(inode->i_sb);
4487 if (!timespec_equal(&inode->i_mtime, &now))
4488 sync_it = S_MTIME;
4489
4490 if (!timespec_equal(&inode->i_ctime, &now))
4491 sync_it |= S_CTIME;
4492
4493 if (IS_I_VERSION(inode))
4494 sync_it |= S_VERSION;
4495
4496 if (!sync_it)
4497 return 0;
4498
4499 /* Finally allowed to write? Takes lock. */
4500 if (mnt_want_write_file(file))
4501 return 0;
4502
4503 /* Only change inode inside the lock region */
4504 if (sync_it & S_VERSION)
4505 inode_inc_iversion(inode);
4506 if (sync_it & S_CTIME)
4507 inode->i_ctime = now;
4508 if (sync_it & S_MTIME)
4509 inode->i_mtime = now;
4510 ret = btrfs_dirty_inode(inode);
4511 if (!ret)
4512 mark_inode_dirty_sync(inode);
4513 mnt_drop_write(file->f_path.mnt);
4514 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004515}
4516
Chris Masond352ac62008-09-29 15:18:18 -04004517/*
4518 * find the highest existing sequence number in a directory
4519 * and then set the in-memory index_cnt variable to reflect
4520 * free sequence numbers
4521 */
Josef Bacikaec74772008-07-24 12:12:38 -04004522static int btrfs_set_inode_index_count(struct inode *inode)
4523{
4524 struct btrfs_root *root = BTRFS_I(inode)->root;
4525 struct btrfs_key key, found_key;
4526 struct btrfs_path *path;
4527 struct extent_buffer *leaf;
4528 int ret;
4529
Li Zefan33345d012011-04-20 10:31:50 +08004530 key.objectid = btrfs_ino(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004531 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4532 key.offset = (u64)-1;
4533
4534 path = btrfs_alloc_path();
4535 if (!path)
4536 return -ENOMEM;
4537
4538 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4539 if (ret < 0)
4540 goto out;
4541 /* FIXME: we should be able to handle this */
4542 if (ret == 0)
4543 goto out;
4544 ret = 0;
4545
4546 /*
4547 * MAGIC NUMBER EXPLANATION:
4548 * since we search a directory based on f_pos we have to start at 2
4549 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4550 * else has to start at 2
4551 */
4552 if (path->slots[0] == 0) {
4553 BTRFS_I(inode)->index_cnt = 2;
4554 goto out;
4555 }
4556
4557 path->slots[0]--;
4558
4559 leaf = path->nodes[0];
4560 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4561
Li Zefan33345d012011-04-20 10:31:50 +08004562 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacikaec74772008-07-24 12:12:38 -04004563 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4564 BTRFS_I(inode)->index_cnt = 2;
4565 goto out;
4566 }
4567
4568 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4569out:
4570 btrfs_free_path(path);
4571 return ret;
4572}
4573
Chris Masond352ac62008-09-29 15:18:18 -04004574/*
4575 * helper to find a free sequence number in a given directory. This current
4576 * code is very simple, later versions will do smarter things in the btree
4577 */
Chris Mason3de45862008-11-17 21:02:50 -05004578int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004579{
4580 int ret = 0;
4581
4582 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08004583 ret = btrfs_inode_delayed_dir_index_count(dir);
4584 if (ret) {
4585 ret = btrfs_set_inode_index_count(dir);
4586 if (ret)
4587 return ret;
4588 }
Josef Bacikaec74772008-07-24 12:12:38 -04004589 }
4590
Chris Mason00e4e6b2008-08-05 11:18:09 -04004591 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004592 BTRFS_I(dir)->index_cnt++;
4593
4594 return ret;
4595}
4596
Chris Mason39279cc2007-06-12 06:35:45 -04004597static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4598 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004599 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004600 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04004601 u64 ref_objectid, u64 objectid,
4602 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004603{
4604 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004605 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004606 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004607 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004608 struct btrfs_inode_ref *ref;
4609 struct btrfs_key key[2];
4610 u32 sizes[2];
4611 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004612 int ret;
4613 int owner;
4614
Chris Mason5f39d392007-10-15 16:14:19 -04004615 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07004616 if (!path)
4617 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04004618
Chris Mason39279cc2007-06-12 06:35:45 -04004619 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004620 if (!inode) {
4621 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004622 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004623 }
Chris Mason39279cc2007-06-12 06:35:45 -04004624
Li Zefan581bb052011-04-20 10:06:11 +08004625 /*
4626 * we have to initialize this early, so we can reclaim the inode
4627 * number if we fail afterwards in this function.
4628 */
4629 inode->i_ino = objectid;
4630
Josef Bacikaec74772008-07-24 12:12:38 -04004631 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004632 trace_btrfs_inode_request(dir);
4633
Chris Mason3de45862008-11-17 21:02:50 -05004634 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004635 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004636 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004637 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004638 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004639 }
Josef Bacikaec74772008-07-24 12:12:38 -04004640 }
4641 /*
4642 * index_cnt is ignored for everything but a dir,
4643 * btrfs_get_inode_index_count has an explanation for the magic
4644 * number
4645 */
4646 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004647 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004648 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004649 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004650 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004651
Al Viro569254b2011-07-24 17:08:40 -04004652 if (S_ISDIR(mode))
Chris Mason39279cc2007-06-12 06:35:45 -04004653 owner = 0;
4654 else
4655 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004656
4657 key[0].objectid = objectid;
4658 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4659 key[0].offset = 0;
4660
4661 key[1].objectid = objectid;
4662 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4663 key[1].offset = ref_objectid;
4664
4665 sizes[0] = sizeof(struct btrfs_inode_item);
4666 sizes[1] = name_len + sizeof(*ref);
4667
Chris Masonb9473432009-03-13 11:00:37 -04004668 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004669 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4670 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004671 goto fail;
4672
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004673 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004674 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004675 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004676 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4677 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004678 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004679
4680 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4681 struct btrfs_inode_ref);
4682 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004683 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004684 ptr = (unsigned long)(ref + 1);
4685 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4686
Chris Mason5f39d392007-10-15 16:14:19 -04004687 btrfs_mark_buffer_dirty(path->nodes[0]);
4688 btrfs_free_path(path);
4689
Chris Mason39279cc2007-06-12 06:35:45 -04004690 location = &BTRFS_I(inode)->location;
4691 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004692 location->offset = 0;
4693 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4694
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004695 btrfs_inherit_iflags(inode, dir);
4696
Al Viro569254b2011-07-24 17:08:40 -04004697 if (S_ISREG(mode)) {
Chris Mason94272162009-07-02 12:26:06 -04004698 if (btrfs_test_opt(root, NODATASUM))
4699 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004700 if (btrfs_test_opt(root, NODATACOW) ||
4701 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004702 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4703 }
4704
Chris Mason39279cc2007-06-12 06:35:45 -04004705 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004706 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004707
4708 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04004709 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00004710
Chris Mason39279cc2007-06-12 06:35:45 -04004711 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004712fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004713 if (dir)
4714 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004715 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004716 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004717 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004718}
4719
4720static inline u8 btrfs_inode_type(struct inode *inode)
4721{
4722 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4723}
4724
Chris Masond352ac62008-09-29 15:18:18 -04004725/*
4726 * utility function to add 'inode' into 'parent_inode' with
4727 * a give name and a given sequence number.
4728 * if 'add_backref' is true, also insert a backref from the
4729 * inode to the parent directory.
4730 */
Chris Masone02119d2008-09-05 16:13:11 -04004731int btrfs_add_link(struct btrfs_trans_handle *trans,
4732 struct inode *parent_inode, struct inode *inode,
4733 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004734{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004735 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004736 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004737 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08004738 u64 ino = btrfs_ino(inode);
4739 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004740
Li Zefan33345d012011-04-20 10:31:50 +08004741 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004742 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4743 } else {
Li Zefan33345d012011-04-20 10:31:50 +08004744 key.objectid = ino;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004745 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4746 key.offset = 0;
4747 }
Chris Mason39279cc2007-06-12 06:35:45 -04004748
Li Zefan33345d012011-04-20 10:31:50 +08004749 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004750 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4751 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004752 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004753 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08004754 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4755 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004756 }
4757
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004758 /* Nothing to clean up yet */
4759 if (ret)
4760 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004761
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004762 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4763 parent_inode, &key,
4764 btrfs_inode_type(inode), index);
4765 if (ret == -EEXIST)
4766 goto fail_dir_item;
4767 else if (ret) {
4768 btrfs_abort_transaction(trans, root, ret);
4769 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004770 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004771
4772 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4773 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004774 inode_inc_iversion(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004775 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4776 ret = btrfs_update_inode(trans, root, parent_inode);
4777 if (ret)
4778 btrfs_abort_transaction(trans, root, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004779 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05004780
4781fail_dir_item:
4782 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4783 u64 local_index;
4784 int err;
4785 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4786 key.objectid, root->root_key.objectid,
4787 parent_ino, &local_index, name, name_len);
4788
4789 } else if (add_backref) {
4790 u64 local_index;
4791 int err;
4792
4793 err = btrfs_del_inode_ref(trans, root, name, name_len,
4794 ino, parent_ino, &local_index);
4795 }
4796 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004797}
4798
4799static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004800 struct inode *dir, struct dentry *dentry,
4801 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004802{
Josef Bacika1b075d2010-11-19 20:36:11 +00004803 int err = btrfs_add_link(trans, dir, inode,
4804 dentry->d_name.name, dentry->d_name.len,
4805 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004806 if (err > 0)
4807 err = -EEXIST;
4808 return err;
4809}
4810
Josef Bacik618e21d2007-07-11 10:18:17 -04004811static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04004812 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04004813{
4814 struct btrfs_trans_handle *trans;
4815 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004816 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004817 int err;
4818 int drop_inode = 0;
4819 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004820 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004821 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004822
4823 if (!new_valid_dev(rdev))
4824 return -EINVAL;
4825
Josef Bacik9ed74f22009-09-11 16:12:44 -04004826 /*
4827 * 2 for inode item and ref
4828 * 2 for dir items
4829 * 1 for xattr if selinux is on
4830 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004831 trans = btrfs_start_transaction(root, 5);
4832 if (IS_ERR(trans))
4833 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004834
Li Zefan581bb052011-04-20 10:06:11 +08004835 err = btrfs_find_free_ino(root, &objectid);
4836 if (err)
4837 goto out_unlock;
4838
Josef Bacikaec74772008-07-24 12:12:38 -04004839 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004840 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004841 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004842 if (IS_ERR(inode)) {
4843 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004844 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004845 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004846
Eric Paris2a7dba32011-02-01 11:05:39 -05004847 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004848 if (err) {
4849 drop_inode = 1;
4850 goto out_unlock;
4851 }
4852
Casey Schauflerad19db72011-12-15 10:09:07 -05004853 /*
4854 * If the active LSM wants to access the inode during
4855 * d_instantiate it needs these. Smack checks to see
4856 * if the filesystem supports xattrs by looking at the
4857 * ops vector.
4858 */
4859
4860 inode->i_op = &btrfs_special_inode_operations;
Josef Bacika1b075d2010-11-19 20:36:11 +00004861 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004862 if (err)
4863 drop_inode = 1;
4864 else {
Josef Bacik618e21d2007-07-11 10:18:17 -04004865 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004866 btrfs_update_inode(trans, root, inode);
Al Viro08c422c2011-12-23 07:58:13 -05004867 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004868 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004869out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004870 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004871 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004872 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004873 if (drop_inode) {
4874 inode_dec_link_count(inode);
4875 iput(inode);
4876 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004877 return err;
4878}
4879
Chris Mason39279cc2007-06-12 06:35:45 -04004880static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viro4acdaf22011-07-26 01:42:34 -04004881 umode_t mode, struct nameidata *nd)
Chris Mason39279cc2007-06-12 06:35:45 -04004882{
4883 struct btrfs_trans_handle *trans;
4884 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004885 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004886 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004887 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004888 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004889 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004890 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004891
Josef Bacik9ed74f22009-09-11 16:12:44 -04004892 /*
4893 * 2 for inode item and ref
4894 * 2 for dir items
4895 * 1 for xattr if selinux is on
4896 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004897 trans = btrfs_start_transaction(root, 5);
4898 if (IS_ERR(trans))
4899 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004900
Li Zefan581bb052011-04-20 10:06:11 +08004901 err = btrfs_find_free_ino(root, &objectid);
4902 if (err)
4903 goto out_unlock;
4904
Josef Bacikaec74772008-07-24 12:12:38 -04004905 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08004906 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004907 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004908 if (IS_ERR(inode)) {
4909 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004910 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004911 }
Chris Mason39279cc2007-06-12 06:35:45 -04004912
Eric Paris2a7dba32011-02-01 11:05:39 -05004913 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004914 if (err) {
4915 drop_inode = 1;
4916 goto out_unlock;
4917 }
4918
Casey Schauflerad19db72011-12-15 10:09:07 -05004919 /*
4920 * If the active LSM wants to access the inode during
4921 * d_instantiate it needs these. Smack checks to see
4922 * if the filesystem supports xattrs by looking at the
4923 * ops vector.
4924 */
4925 inode->i_fop = &btrfs_file_operations;
4926 inode->i_op = &btrfs_file_inode_operations;
4927
Josef Bacika1b075d2010-11-19 20:36:11 +00004928 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004929 if (err)
4930 drop_inode = 1;
4931 else {
4932 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004933 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05004934 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro08c422c2011-12-23 07:58:13 -05004935 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004936 }
Chris Mason39279cc2007-06-12 06:35:45 -04004937out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004938 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004939 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004940 if (drop_inode) {
4941 inode_dec_link_count(inode);
4942 iput(inode);
4943 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004944 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004945 return err;
4946}
4947
4948static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4949 struct dentry *dentry)
4950{
4951 struct btrfs_trans_handle *trans;
4952 struct btrfs_root *root = BTRFS_I(dir)->root;
4953 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004954 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004955 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004956 int err;
4957 int drop_inode = 0;
4958
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004959 /* do not allow sys_link's with other subvols of the same device */
4960 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00004961 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004962
Al Viroc055e992011-03-04 17:15:18 +00004963 if (inode->i_nlink == ~0U)
4964 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004965
Chris Mason3de45862008-11-17 21:02:50 -05004966 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004967 if (err)
4968 goto fail;
4969
Yan, Zhenga22285a2010-05-16 10:48:46 -04004970 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00004971 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04004972 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00004973 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04004974 */
Miao Xie7e6b6462011-02-18 09:21:17 +00004975 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004976 if (IS_ERR(trans)) {
4977 err = PTR_ERR(trans);
4978 goto fail;
4979 }
Chris Mason5f39d392007-10-15 16:14:19 -04004980
Miao Xie31534952011-04-13 13:19:21 +08004981 btrfs_inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004982 inode_inc_iversion(inode);
Miao Xie31534952011-04-13 13:19:21 +08004983 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04004984 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004985
Josef Bacika1b075d2010-11-19 20:36:11 +00004986 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004987
Yan, Zhenga5719522009-09-24 09:17:31 -04004988 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004989 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004990 } else {
Al Viro10d9f302011-07-16 23:09:10 -04004991 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04004992 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004993 if (err)
4994 goto fail;
Al Viro08c422c2011-12-23 07:58:13 -05004995 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00004996 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004997 }
Chris Mason39279cc2007-06-12 06:35:45 -04004998
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004999 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005000 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005001fail:
Chris Mason39279cc2007-06-12 06:35:45 -04005002 if (drop_inode) {
5003 inode_dec_link_count(inode);
5004 iput(inode);
5005 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005006 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005007 return err;
5008}
5009
Al Viro18bb1db2011-07-26 01:41:39 -04005010static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04005011{
Chris Masonb9d86662008-05-02 16:13:49 -04005012 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005013 struct btrfs_trans_handle *trans;
5014 struct btrfs_root *root = BTRFS_I(dir)->root;
5015 int err = 0;
5016 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04005017 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005018 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005019 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005020
Josef Bacik9ed74f22009-09-11 16:12:44 -04005021 /*
5022 * 2 items for inode and ref
5023 * 2 items for dir items
5024 * 1 for xattr if selinux is on
5025 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005026 trans = btrfs_start_transaction(root, 5);
5027 if (IS_ERR(trans))
5028 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005029
Li Zefan581bb052011-04-20 10:06:11 +08005030 err = btrfs_find_free_ino(root, &objectid);
5031 if (err)
5032 goto out_fail;
5033
Josef Bacikaec74772008-07-24 12:12:38 -04005034 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08005035 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04005036 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005037 if (IS_ERR(inode)) {
5038 err = PTR_ERR(inode);
5039 goto out_fail;
5040 }
Chris Mason5f39d392007-10-15 16:14:19 -04005041
Chris Mason39279cc2007-06-12 06:35:45 -04005042 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04005043
Eric Paris2a7dba32011-02-01 11:05:39 -05005044 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04005045 if (err)
5046 goto out_fail;
5047
Chris Mason39279cc2007-06-12 06:35:45 -04005048 inode->i_op = &btrfs_dir_inode_operations;
5049 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04005050
Chris Masondbe674a2008-07-17 12:54:05 -04005051 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04005052 err = btrfs_update_inode(trans, root, inode);
5053 if (err)
5054 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005055
Josef Bacika1b075d2010-11-19 20:36:11 +00005056 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5057 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005058 if (err)
5059 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04005060
Chris Mason39279cc2007-06-12 06:35:45 -04005061 d_instantiate(dentry, inode);
5062 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005063
5064out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005065 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005066 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005067 if (drop_on_err)
5068 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005069 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005070 return err;
5071}
5072
Chris Masond352ac62008-09-29 15:18:18 -04005073/* helper for btfs_get_extent. Given an existing extent in the tree,
5074 * and an extent that you want to insert, deal with overlap and insert
5075 * the new extent into the tree.
5076 */
Chris Mason3b951512008-04-17 11:29:12 -04005077static int merge_extent_mapping(struct extent_map_tree *em_tree,
5078 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005079 struct extent_map *em,
5080 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04005081{
5082 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04005083
Chris Masone6dcd2d2008-07-17 12:53:50 -04005084 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5085 start_diff = map_start - em->start;
5086 em->start = map_start;
5087 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04005088 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5089 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005090 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04005091 em->block_len -= start_diff;
5092 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005093 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005094}
5095
Chris Masonc8b97812008-10-29 14:49:59 -04005096static noinline int uncompress_inline(struct btrfs_path *path,
5097 struct inode *inode, struct page *page,
5098 size_t pg_offset, u64 extent_offset,
5099 struct btrfs_file_extent_item *item)
5100{
5101 int ret;
5102 struct extent_buffer *leaf = path->nodes[0];
5103 char *tmp;
5104 size_t max_size;
5105 unsigned long inline_size;
5106 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08005107 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005108
5109 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08005110 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04005111 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5112 inline_size = btrfs_file_extent_inline_item_len(leaf,
5113 btrfs_item_nr(leaf, path->slots[0]));
5114 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04005115 if (!tmp)
5116 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04005117 ptr = btrfs_file_extent_inline_start(item);
5118
5119 read_extent_buffer(leaf, tmp, ptr, inline_size);
5120
Chris Mason5b050f02008-11-11 09:34:41 -05005121 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08005122 ret = btrfs_decompress(compress_type, tmp, page,
5123 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005124 if (ret) {
Cong Wang7ac687d2011-11-25 23:14:28 +08005125 char *kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04005126 unsigned long copy_size = min_t(u64,
5127 PAGE_CACHE_SIZE - pg_offset,
5128 max_size - extent_offset);
5129 memset(kaddr + pg_offset, 0, copy_size);
Cong Wang7ac687d2011-11-25 23:14:28 +08005130 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -04005131 }
5132 kfree(tmp);
5133 return 0;
5134}
5135
Chris Masond352ac62008-09-29 15:18:18 -04005136/*
5137 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005138 * the ugly parts come from merging extents from the disk with the in-ram
5139 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005140 * where the in-ram extents might be locked pending data=ordered completion.
5141 *
5142 * This also copies inline extents directly into the page.
5143 */
Chris Masond3977122009-01-05 21:25:51 -05005144
Chris Masona52d9a82007-08-27 16:49:44 -04005145struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005146 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005147 int create)
5148{
5149 int ret;
5150 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005151 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005152 u64 extent_start = 0;
5153 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08005154 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04005155 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005156 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005157 struct btrfs_root *root = BTRFS_I(inode)->root;
5158 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005159 struct extent_buffer *leaf;
5160 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005161 struct extent_map *em = NULL;
5162 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005163 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005164 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005165 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005166
Chris Masona52d9a82007-08-27 16:49:44 -04005167again:
Chris Mason890871b2009-09-02 16:24:52 -04005168 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005169 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005170 if (em)
5171 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005172 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005173
Chris Masona52d9a82007-08-27 16:49:44 -04005174 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005175 if (em->start > start || em->start + em->len <= start)
5176 free_extent_map(em);
5177 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005178 free_extent_map(em);
5179 else
5180 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005181 }
David Sterba172ddd62011-04-21 00:48:27 +02005182 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04005183 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005184 err = -ENOMEM;
5185 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005186 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005187 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005188 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005189 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005190 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005191 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005192
5193 if (!path) {
5194 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005195 if (!path) {
5196 err = -ENOMEM;
5197 goto out;
5198 }
5199 /*
5200 * Chances are we'll be called again, so go ahead and do
5201 * readahead
5202 */
5203 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005204 }
5205
Chris Mason179e29e2007-11-01 11:28:41 -04005206 ret = btrfs_lookup_file_extent(trans, root, path,
5207 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005208 if (ret < 0) {
5209 err = ret;
5210 goto out;
5211 }
5212
5213 if (ret != 0) {
5214 if (path->slots[0] == 0)
5215 goto not_found;
5216 path->slots[0]--;
5217 }
5218
Chris Mason5f39d392007-10-15 16:14:19 -04005219 leaf = path->nodes[0];
5220 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005221 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005222 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005223 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5224 found_type = btrfs_key_type(&found_key);
5225 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005226 found_type != BTRFS_EXTENT_DATA_KEY) {
5227 goto not_found;
5228 }
5229
Chris Mason5f39d392007-10-15 16:14:19 -04005230 found_type = btrfs_file_extent_type(leaf, item);
5231 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005232 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005233 if (found_type == BTRFS_FILE_EXTENT_REG ||
5234 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005235 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005236 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005237 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5238 size_t size;
5239 size = btrfs_file_extent_inline_len(leaf, item);
5240 extent_end = (extent_start + size + root->sectorsize - 1) &
5241 ~((u64)root->sectorsize - 1);
5242 }
5243
5244 if (start >= extent_end) {
5245 path->slots[0]++;
5246 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5247 ret = btrfs_next_leaf(root, path);
5248 if (ret < 0) {
5249 err = ret;
5250 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005251 }
Yan Zheng9036c102008-10-30 14:19:41 -04005252 if (ret > 0)
5253 goto not_found;
5254 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005255 }
Yan Zheng9036c102008-10-30 14:19:41 -04005256 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5257 if (found_key.objectid != objectid ||
5258 found_key.type != BTRFS_EXTENT_DATA_KEY)
5259 goto not_found;
5260 if (start + len <= found_key.offset)
5261 goto not_found;
5262 em->start = start;
5263 em->len = found_key.offset - start;
5264 goto not_found_em;
5265 }
5266
Yan Zhengd899e052008-10-30 14:25:28 -04005267 if (found_type == BTRFS_FILE_EXTENT_REG ||
5268 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005269 em->start = extent_start;
5270 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005271 em->orig_start = extent_start -
5272 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005273 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5274 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005275 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005276 goto insert;
5277 }
Li Zefan261507a02010-12-17 14:21:50 +08005278 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005279 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005280 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005281 em->block_start = bytenr;
5282 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5283 item);
5284 } else {
5285 bytenr += btrfs_file_extent_offset(leaf, item);
5286 em->block_start = bytenr;
5287 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005288 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5289 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005290 }
Chris Masona52d9a82007-08-27 16:49:44 -04005291 goto insert;
5292 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005293 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005294 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005295 size_t size;
5296 size_t extent_offset;
5297 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005298
Chris Masona52d9a82007-08-27 16:49:44 -04005299 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005300 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005301 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005302 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005303 goto out;
5304 }
5305
Yan Zheng9036c102008-10-30 14:19:41 -04005306 size = btrfs_file_extent_inline_len(leaf, item);
5307 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005308 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005309 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005310 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005311 em->len = (copy_size + root->sectorsize - 1) &
5312 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005313 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005314 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005315 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005316 em->compress_type = compress_type;
5317 }
Yan689f9342007-10-29 11:41:07 -04005318 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005319 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005320 if (btrfs_file_extent_compression(leaf, item) !=
5321 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005322 ret = uncompress_inline(path, inode, page,
5323 pg_offset,
5324 extent_offset, item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005325 BUG_ON(ret); /* -ENOMEM */
Chris Masonc8b97812008-10-29 14:49:59 -04005326 } else {
5327 map = kmap(page);
5328 read_extent_buffer(leaf, map + pg_offset, ptr,
5329 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005330 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5331 memset(map + pg_offset + copy_size, 0,
5332 PAGE_CACHE_SIZE - pg_offset -
5333 copy_size);
5334 }
Chris Masonc8b97812008-10-29 14:49:59 -04005335 kunmap(page);
5336 }
Chris Mason179e29e2007-11-01 11:28:41 -04005337 flush_dcache_page(page);
5338 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01005339 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04005340 if (!trans) {
5341 kunmap(page);
5342 free_extent_map(em);
5343 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04005344
David Sterbab3b4aa72011-04-21 01:20:15 +02005345 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005346 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04005347
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005348 if (IS_ERR(trans))
5349 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005350 goto again;
5351 }
Chris Masonc8b97812008-10-29 14:49:59 -04005352 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005353 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005354 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005355 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005356 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005357 }
Chris Masond1310b22008-01-24 16:13:08 -05005358 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005359 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005360 goto insert;
5361 } else {
Chris Masond3977122009-01-05 21:25:51 -05005362 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005363 WARN_ON(1);
5364 }
5365not_found:
5366 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005367 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005368not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005369 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005370 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005371insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02005372 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05005373 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005374 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5375 "[%llu %llu]\n", (unsigned long long)em->start,
5376 (unsigned long long)em->len,
5377 (unsigned long long)start,
5378 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005379 err = -EIO;
5380 goto out;
5381 }
Chris Masond1310b22008-01-24 16:13:08 -05005382
5383 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005384 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005385 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005386 /* it is possible that someone inserted the extent into the tree
5387 * while we had the lock dropped. It is also possible that
5388 * an overlapping map exists in the tree
5389 */
Chris Masona52d9a82007-08-27 16:49:44 -04005390 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005391 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005392
5393 ret = 0;
5394
Chris Mason3b951512008-04-17 11:29:12 -04005395 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005396 if (existing && (existing->start > start ||
5397 existing->start + existing->len <= start)) {
5398 free_extent_map(existing);
5399 existing = NULL;
5400 }
Chris Mason3b951512008-04-17 11:29:12 -04005401 if (!existing) {
5402 existing = lookup_extent_mapping(em_tree, em->start,
5403 em->len);
5404 if (existing) {
5405 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005406 em, start,
5407 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005408 free_extent_map(existing);
5409 if (err) {
5410 free_extent_map(em);
5411 em = NULL;
5412 }
5413 } else {
5414 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005415 free_extent_map(em);
5416 em = NULL;
5417 }
5418 } else {
5419 free_extent_map(em);
5420 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005421 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005422 }
Chris Masona52d9a82007-08-27 16:49:44 -04005423 }
Chris Mason890871b2009-09-02 16:24:52 -04005424 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005425out:
liubo1abe9b82011-03-24 11:18:59 +00005426
5427 trace_btrfs_get_extent(root, em);
5428
Chris Masonf4219502008-07-22 11:18:09 -04005429 if (path)
5430 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005431 if (trans) {
5432 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005433 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005434 err = ret;
5435 }
Chris Masona52d9a82007-08-27 16:49:44 -04005436 if (err) {
5437 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005438 return ERR_PTR(err);
5439 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005440 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04005441 return em;
5442}
5443
Chris Masonec29ed52011-02-23 16:23:20 -05005444struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5445 size_t pg_offset, u64 start, u64 len,
5446 int create)
5447{
5448 struct extent_map *em;
5449 struct extent_map *hole_em = NULL;
5450 u64 range_start = start;
5451 u64 end;
5452 u64 found;
5453 u64 found_end;
5454 int err = 0;
5455
5456 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5457 if (IS_ERR(em))
5458 return em;
5459 if (em) {
5460 /*
5461 * if our em maps to a hole, there might
5462 * actually be delalloc bytes behind it
5463 */
5464 if (em->block_start != EXTENT_MAP_HOLE)
5465 return em;
5466 else
5467 hole_em = em;
5468 }
5469
5470 /* check to see if we've wrapped (len == -1 or similar) */
5471 end = start + len;
5472 if (end < start)
5473 end = (u64)-1;
5474 else
5475 end -= 1;
5476
5477 em = NULL;
5478
5479 /* ok, we didn't find anything, lets look for delalloc */
5480 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5481 end, len, EXTENT_DELALLOC, 1);
5482 found_end = range_start + found;
5483 if (found_end < range_start)
5484 found_end = (u64)-1;
5485
5486 /*
5487 * we didn't find anything useful, return
5488 * the original results from get_extent()
5489 */
5490 if (range_start > end || found_end <= start) {
5491 em = hole_em;
5492 hole_em = NULL;
5493 goto out;
5494 }
5495
5496 /* adjust the range_start to make sure it doesn't
5497 * go backwards from the start they passed in
5498 */
5499 range_start = max(start,range_start);
5500 found = found_end - range_start;
5501
5502 if (found > 0) {
5503 u64 hole_start = start;
5504 u64 hole_len = len;
5505
David Sterba172ddd62011-04-21 00:48:27 +02005506 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05005507 if (!em) {
5508 err = -ENOMEM;
5509 goto out;
5510 }
5511 /*
5512 * when btrfs_get_extent can't find anything it
5513 * returns one huge hole
5514 *
5515 * make sure what it found really fits our range, and
5516 * adjust to make sure it is based on the start from
5517 * the caller
5518 */
5519 if (hole_em) {
5520 u64 calc_end = extent_map_end(hole_em);
5521
5522 if (calc_end <= start || (hole_em->start > end)) {
5523 free_extent_map(hole_em);
5524 hole_em = NULL;
5525 } else {
5526 hole_start = max(hole_em->start, start);
5527 hole_len = calc_end - hole_start;
5528 }
5529 }
5530 em->bdev = NULL;
5531 if (hole_em && range_start > hole_start) {
5532 /* our hole starts before our delalloc, so we
5533 * have to return just the parts of the hole
5534 * that go until the delalloc starts
5535 */
5536 em->len = min(hole_len,
5537 range_start - hole_start);
5538 em->start = hole_start;
5539 em->orig_start = hole_start;
5540 /*
5541 * don't adjust block start at all,
5542 * it is fixed at EXTENT_MAP_HOLE
5543 */
5544 em->block_start = hole_em->block_start;
5545 em->block_len = hole_len;
5546 } else {
5547 em->start = range_start;
5548 em->len = found;
5549 em->orig_start = range_start;
5550 em->block_start = EXTENT_MAP_DELALLOC;
5551 em->block_len = found;
5552 }
5553 } else if (hole_em) {
5554 return hole_em;
5555 }
5556out:
5557
5558 free_extent_map(hole_em);
5559 if (err) {
5560 free_extent_map(em);
5561 return ERR_PTR(err);
5562 }
5563 return em;
5564}
5565
Josef Bacik4b46fce2010-05-23 11:00:55 -04005566static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005567 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005568 u64 start, u64 len)
5569{
5570 struct btrfs_root *root = BTRFS_I(inode)->root;
5571 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005572 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5573 struct btrfs_key ins;
5574 u64 alloc_hint;
5575 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005576 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005577
Josef Bacik16d299a2011-04-06 14:53:07 -04005578 /*
5579 * Ok if the extent map we looked up is a hole and is for the exact
5580 * range we want, there is no reason to allocate a new one, however if
5581 * it is not right then we need to free this one and drop the cache for
5582 * our range.
5583 */
5584 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5585 em->len != len) {
5586 free_extent_map(em);
5587 em = NULL;
5588 insert = true;
5589 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5590 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005591
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005592 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005593 if (IS_ERR(trans))
5594 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005595
Chris Mason4cb53002011-05-24 15:35:30 -04005596 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5597 btrfs_add_inode_defrag(trans, inode);
5598
Josef Bacik4b46fce2010-05-23 11:00:55 -04005599 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5600
5601 alloc_hint = get_extent_allocation_hint(inode, start, len);
5602 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
Josef Bacik81c9ad22012-01-18 10:56:06 -05005603 alloc_hint, &ins, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005604 if (ret) {
5605 em = ERR_PTR(ret);
5606 goto out;
5607 }
5608
Josef Bacik4b46fce2010-05-23 11:00:55 -04005609 if (!em) {
David Sterba172ddd62011-04-21 00:48:27 +02005610 em = alloc_extent_map();
Josef Bacik16d299a2011-04-06 14:53:07 -04005611 if (!em) {
5612 em = ERR_PTR(-ENOMEM);
5613 goto out;
5614 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005615 }
5616
5617 em->start = start;
5618 em->orig_start = em->start;
5619 em->len = ins.offset;
5620
5621 em->block_start = ins.objectid;
5622 em->block_len = ins.offset;
5623 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005624
5625 /*
5626 * We need to do this because if we're using the original em we searched
5627 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5628 */
5629 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005630 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5631
Josef Bacik16d299a2011-04-06 14:53:07 -04005632 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005633 write_lock(&em_tree->lock);
5634 ret = add_extent_mapping(em_tree, em);
5635 write_unlock(&em_tree->lock);
5636 if (ret != -EEXIST)
5637 break;
5638 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5639 }
5640
5641 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5642 ins.offset, ins.offset, 0);
5643 if (ret) {
5644 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5645 em = ERR_PTR(ret);
5646 }
5647out:
5648 btrfs_end_transaction(trans, root);
5649 return em;
5650}
5651
Chris Mason46bfbb52010-05-26 11:04:10 -04005652/*
5653 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5654 * block must be cow'd
5655 */
5656static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5657 struct inode *inode, u64 offset, u64 len)
5658{
5659 struct btrfs_path *path;
5660 int ret;
5661 struct extent_buffer *leaf;
5662 struct btrfs_root *root = BTRFS_I(inode)->root;
5663 struct btrfs_file_extent_item *fi;
5664 struct btrfs_key key;
5665 u64 disk_bytenr;
5666 u64 backref_offset;
5667 u64 extent_end;
5668 u64 num_bytes;
5669 int slot;
5670 int found_type;
5671
5672 path = btrfs_alloc_path();
5673 if (!path)
5674 return -ENOMEM;
5675
Li Zefan33345d012011-04-20 10:31:50 +08005676 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005677 offset, 0);
5678 if (ret < 0)
5679 goto out;
5680
5681 slot = path->slots[0];
5682 if (ret == 1) {
5683 if (slot == 0) {
5684 /* can't find the item, must cow */
5685 ret = 0;
5686 goto out;
5687 }
5688 slot--;
5689 }
5690 ret = 0;
5691 leaf = path->nodes[0];
5692 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08005693 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04005694 key.type != BTRFS_EXTENT_DATA_KEY) {
5695 /* not our file or wrong item type, must cow */
5696 goto out;
5697 }
5698
5699 if (key.offset > offset) {
5700 /* Wrong offset, must cow */
5701 goto out;
5702 }
5703
5704 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5705 found_type = btrfs_file_extent_type(leaf, fi);
5706 if (found_type != BTRFS_FILE_EXTENT_REG &&
5707 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5708 /* not a regular extent, must cow */
5709 goto out;
5710 }
5711 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5712 backref_offset = btrfs_file_extent_offset(leaf, fi);
5713
5714 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5715 if (extent_end < offset + len) {
5716 /* extent doesn't include our full range, must cow */
5717 goto out;
5718 }
5719
5720 if (btrfs_extent_readonly(root, disk_bytenr))
5721 goto out;
5722
5723 /*
5724 * look for other files referencing this extent, if we
5725 * find any we must cow
5726 */
Li Zefan33345d012011-04-20 10:31:50 +08005727 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04005728 key.offset - backref_offset, disk_bytenr))
5729 goto out;
5730
5731 /*
5732 * adjust disk_bytenr and num_bytes to cover just the bytes
5733 * in this extent we are about to write. If there
5734 * are any csums in that range we have to cow in order
5735 * to keep the csums correct
5736 */
5737 disk_bytenr += backref_offset;
5738 disk_bytenr += offset - key.offset;
5739 num_bytes = min(offset + len, extent_end) - offset;
5740 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5741 goto out;
5742 /*
5743 * all of the above have passed, it is safe to overwrite this extent
5744 * without cow
5745 */
5746 ret = 1;
5747out:
5748 btrfs_free_path(path);
5749 return ret;
5750}
5751
Josef Bacik4b46fce2010-05-23 11:00:55 -04005752static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5753 struct buffer_head *bh_result, int create)
5754{
5755 struct extent_map *em;
5756 struct btrfs_root *root = BTRFS_I(inode)->root;
5757 u64 start = iblock << inode->i_blkbits;
5758 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005759 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005760
5761 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5762 if (IS_ERR(em))
5763 return PTR_ERR(em);
5764
5765 /*
5766 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5767 * io. INLINE is special, and we could probably kludge it in here, but
5768 * it's still buffered so for safety lets just fall back to the generic
5769 * buffered path.
5770 *
5771 * For COMPRESSED we _have_ to read the entire extent in so we can
5772 * decompress it, so there will be buffering required no matter what we
5773 * do, so go ahead and fallback to buffered.
5774 *
5775 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5776 * to buffered IO. Don't blame me, this is the price we pay for using
5777 * the generic code.
5778 */
5779 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5780 em->block_start == EXTENT_MAP_INLINE) {
5781 free_extent_map(em);
5782 return -ENOTBLK;
5783 }
5784
5785 /* Just a good old fashioned hole, return */
5786 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5787 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5788 free_extent_map(em);
5789 /* DIO will do one hole at a time, so just unlock a sector */
5790 unlock_extent(&BTRFS_I(inode)->io_tree, start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005791 start + root->sectorsize - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005792 return 0;
5793 }
5794
5795 /*
5796 * We don't allocate a new extent in the following cases
5797 *
5798 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5799 * existing extent.
5800 * 2) The extent is marked as PREALLOC. We're good to go here and can
5801 * just use the extent.
5802 *
5803 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005804 if (!create) {
5805 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005806 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005807 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005808
5809 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5810 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5811 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005812 int type;
5813 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005814 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005815
5816 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5817 type = BTRFS_ORDERED_PREALLOC;
5818 else
5819 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005820 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005821 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005822
5823 /*
5824 * we're not going to log anything, but we do need
5825 * to make sure the current transaction stays open
5826 * while we look for nocow cross refs
5827 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005828 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005829 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005830 goto must_cow;
5831
5832 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5833 ret = btrfs_add_ordered_extent_dio(inode, start,
5834 block_start, len, len, type);
5835 btrfs_end_transaction(trans, root);
5836 if (ret) {
5837 free_extent_map(em);
5838 return ret;
5839 }
5840 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005841 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005842 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005843 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005844must_cow:
5845 /*
5846 * this will cow the extent, reset the len in case we changed
5847 * it above
5848 */
5849 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04005850 em = btrfs_new_extent_direct(inode, em, start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04005851 if (IS_ERR(em))
5852 return PTR_ERR(em);
5853 len = min(len, em->len - (start - em->start));
5854unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005855 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5856 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5857 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005858map:
5859 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5860 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005861 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005862 bh_result->b_bdev = em->bdev;
5863 set_buffer_mapped(bh_result);
5864 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5865 set_buffer_new(bh_result);
5866
5867 free_extent_map(em);
5868
5869 return 0;
5870}
5871
5872struct btrfs_dio_private {
5873 struct inode *inode;
5874 u64 logical_offset;
5875 u64 disk_bytenr;
5876 u64 bytes;
5877 u32 *csums;
5878 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005879
5880 /* number of bios pending for this dio */
5881 atomic_t pending_bios;
5882
5883 /* IO errors */
5884 int errors;
5885
5886 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005887};
5888
5889static void btrfs_endio_direct_read(struct bio *bio, int err)
5890{
Miao Xiee65e1532010-11-22 03:04:43 +00005891 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005892 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5893 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005894 struct inode *inode = dip->inode;
5895 struct btrfs_root *root = BTRFS_I(inode)->root;
5896 u64 start;
5897 u32 *private = dip->csums;
5898
5899 start = dip->logical_offset;
5900 do {
5901 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5902 struct page *page = bvec->bv_page;
5903 char *kaddr;
5904 u32 csum = ~(u32)0;
5905 unsigned long flags;
5906
5907 local_irq_save(flags);
Cong Wang7ac687d2011-11-25 23:14:28 +08005908 kaddr = kmap_atomic(page);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005909 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5910 csum, bvec->bv_len);
5911 btrfs_csum_final(csum, (char *)&csum);
Cong Wang7ac687d2011-11-25 23:14:28 +08005912 kunmap_atomic(kaddr);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005913 local_irq_restore(flags);
5914
5915 flush_dcache_page(bvec->bv_page);
5916 if (csum != *private) {
Li Zefan33345d012011-04-20 10:31:50 +08005917 printk(KERN_ERR "btrfs csum failed ino %llu off"
Josef Bacik4b46fce2010-05-23 11:00:55 -04005918 " %llu csum %u private %u\n",
Li Zefan33345d012011-04-20 10:31:50 +08005919 (unsigned long long)btrfs_ino(inode),
5920 (unsigned long long)start,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005921 csum, *private);
5922 err = -EIO;
5923 }
5924 }
5925
5926 start += bvec->bv_len;
5927 private++;
5928 bvec++;
5929 } while (bvec <= bvec_end);
5930
5931 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01005932 dip->logical_offset + dip->bytes - 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005933 bio->bi_private = dip->private;
5934
5935 kfree(dip->csums);
5936 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005937
5938 /* If we had a csum failure make sure to clear the uptodate flag */
5939 if (err)
5940 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005941 dio_end_io(bio, err);
5942}
5943
5944static void btrfs_endio_direct_write(struct bio *bio, int err)
5945{
5946 struct btrfs_dio_private *dip = bio->bi_private;
5947 struct inode *inode = dip->inode;
5948 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005949 struct btrfs_ordered_extent *ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005950 u64 ordered_offset = dip->logical_offset;
5951 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005952 int ret;
5953
5954 if (err)
5955 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005956again:
5957 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5958 &ordered_offset,
Josef Bacik5fd02042012-05-02 14:00:54 -04005959 ordered_bytes, !err);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005960 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005961 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005962
Josef Bacik5fd02042012-05-02 14:00:54 -04005963 ordered->work.func = finish_ordered_fn;
5964 ordered->work.flags = 0;
5965 btrfs_queue_worker(&root->fs_info->endio_write_workers,
5966 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05005967out_test:
5968 /*
5969 * our bio might span multiple ordered extents. If we haven't
5970 * completed the accounting for the whole dio, go back and try again
5971 */
5972 if (ordered_offset < dip->logical_offset + dip->bytes) {
5973 ordered_bytes = dip->logical_offset + dip->bytes -
5974 ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04005975 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005976 goto again;
5977 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005978out_done:
5979 bio->bi_private = dip->private;
5980
Josef Bacik4b46fce2010-05-23 11:00:55 -04005981 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005982
5983 /* If we had an error make sure to clear the uptodate flag */
5984 if (err)
5985 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005986 dio_end_io(bio, err);
5987}
5988
Chris Masoneaf25d92010-05-25 09:48:28 -04005989static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5990 struct bio *bio, int mirror_num,
5991 unsigned long bio_flags, u64 offset)
5992{
5993 int ret;
5994 struct btrfs_root *root = BTRFS_I(inode)->root;
5995 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005996 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04005997 return 0;
5998}
5999
Miao Xiee65e1532010-11-22 03:04:43 +00006000static void btrfs_end_dio_bio(struct bio *bio, int err)
6001{
6002 struct btrfs_dio_private *dip = bio->bi_private;
6003
6004 if (err) {
Li Zefan33345d012011-04-20 10:31:50 +08006005 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00006006 "sector %#Lx len %u err no %d\n",
Li Zefan33345d012011-04-20 10:31:50 +08006007 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
Jan Beulich3dd14622010-12-07 14:54:09 +00006008 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00006009 dip->errors = 1;
6010
6011 /*
6012 * before atomic variable goto zero, we must make sure
6013 * dip->errors is perceived to be set.
6014 */
6015 smp_mb__before_atomic_dec();
6016 }
6017
6018 /* if there are more bios still pending for this dio, just exit */
6019 if (!atomic_dec_and_test(&dip->pending_bios))
6020 goto out;
6021
6022 if (dip->errors)
6023 bio_io_error(dip->orig_bio);
6024 else {
6025 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6026 bio_endio(dip->orig_bio, 0);
6027 }
6028out:
6029 bio_put(bio);
6030}
6031
6032static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6033 u64 first_sector, gfp_t gfp_flags)
6034{
6035 int nr_vecs = bio_get_nr_vecs(bdev);
6036 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6037}
6038
6039static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6040 int rw, u64 file_offset, int skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006041 u32 *csums, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00006042{
6043 int write = rw & REQ_WRITE;
6044 struct btrfs_root *root = BTRFS_I(inode)->root;
6045 int ret;
6046
6047 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04006048
6049 if (!write) {
6050 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6051 if (ret)
6052 goto err;
6053 }
Miao Xiee65e1532010-11-22 03:04:43 +00006054
Josef Bacik1ae39932011-04-06 14:41:34 -04006055 if (skip_sum)
6056 goto map;
6057
6058 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00006059 ret = btrfs_wq_submit_bio(root->fs_info,
6060 inode, rw, bio, 0, 0,
6061 file_offset,
6062 __btrfs_submit_bio_start_direct_io,
6063 __btrfs_submit_bio_done);
6064 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04006065 } else if (write) {
6066 /*
6067 * If we aren't doing async submit, calculate the csum of the
6068 * bio now.
6069 */
6070 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6071 if (ret)
6072 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006073 } else if (!skip_sum) {
6074 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
Miao Xiee65e1532010-11-22 03:04:43 +00006075 file_offset, csums);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00006076 if (ret)
6077 goto err;
6078 }
Miao Xiee65e1532010-11-22 03:04:43 +00006079
Josef Bacik1ae39932011-04-06 14:41:34 -04006080map:
6081 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006082err:
6083 bio_put(bio);
6084 return ret;
6085}
6086
6087static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6088 int skip_sum)
6089{
6090 struct inode *inode = dip->inode;
6091 struct btrfs_root *root = BTRFS_I(inode)->root;
6092 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6093 struct bio *bio;
6094 struct bio *orig_bio = dip->orig_bio;
6095 struct bio_vec *bvec = orig_bio->bi_io_vec;
6096 u64 start_sector = orig_bio->bi_sector;
6097 u64 file_offset = dip->logical_offset;
6098 u64 submit_len = 0;
6099 u64 map_length;
6100 int nr_pages = 0;
6101 u32 *csums = dip->csums;
6102 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006103 int async_submit = 0;
Josef Bacik98bc3142011-03-22 11:00:46 -04006104 int write = rw & REQ_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00006105
Miao Xiee65e1532010-11-22 03:04:43 +00006106 map_length = orig_bio->bi_size;
6107 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6108 &map_length, NULL, 0);
6109 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006110 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006111 return -EIO;
6112 }
6113
Josef Bacik02f57c72011-04-06 14:25:44 -04006114 if (map_length >= orig_bio->bi_size) {
6115 bio = orig_bio;
6116 goto submit;
6117 }
6118
Josef Bacik1ae39932011-04-06 14:41:34 -04006119 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006120 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6121 if (!bio)
6122 return -ENOMEM;
6123 bio->bi_private = dip;
6124 bio->bi_end_io = btrfs_end_dio_bio;
6125 atomic_inc(&dip->pending_bios);
6126
Miao Xiee65e1532010-11-22 03:04:43 +00006127 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6128 if (unlikely(map_length < submit_len + bvec->bv_len ||
6129 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6130 bvec->bv_offset) < bvec->bv_len)) {
6131 /*
6132 * inc the count before we submit the bio so
6133 * we know the end IO handler won't happen before
6134 * we inc the count. Otherwise, the dip might get freed
6135 * before we're done setting it up
6136 */
6137 atomic_inc(&dip->pending_bios);
6138 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6139 file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006140 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006141 if (ret) {
6142 bio_put(bio);
6143 atomic_dec(&dip->pending_bios);
6144 goto out_err;
6145 }
6146
Josef Bacik98bc3142011-03-22 11:00:46 -04006147 /* Write's use the ordered csums */
6148 if (!write && !skip_sum)
Miao Xiee65e1532010-11-22 03:04:43 +00006149 csums = csums + nr_pages;
6150 start_sector += submit_len >> 9;
6151 file_offset += submit_len;
6152
6153 submit_len = 0;
6154 nr_pages = 0;
6155
6156 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6157 start_sector, GFP_NOFS);
6158 if (!bio)
6159 goto out_err;
6160 bio->bi_private = dip;
6161 bio->bi_end_io = btrfs_end_dio_bio;
6162
6163 map_length = orig_bio->bi_size;
6164 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6165 &map_length, NULL, 0);
6166 if (ret) {
6167 bio_put(bio);
6168 goto out_err;
6169 }
6170 } else {
6171 submit_len += bvec->bv_len;
6172 nr_pages ++;
6173 bvec++;
6174 }
6175 }
6176
Josef Bacik02f57c72011-04-06 14:25:44 -04006177submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006178 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006179 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006180 if (!ret)
6181 return 0;
6182
6183 bio_put(bio);
6184out_err:
6185 dip->errors = 1;
6186 /*
6187 * before atomic variable goto zero, we must
6188 * make sure dip->errors is perceived to be set.
6189 */
6190 smp_mb__before_atomic_dec();
6191 if (atomic_dec_and_test(&dip->pending_bios))
6192 bio_io_error(dip->orig_bio);
6193
6194 /* bio_end_io() will handle error, so we needn't return it */
6195 return 0;
6196}
6197
Josef Bacik4b46fce2010-05-23 11:00:55 -04006198static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6199 loff_t file_offset)
6200{
6201 struct btrfs_root *root = BTRFS_I(inode)->root;
6202 struct btrfs_dio_private *dip;
6203 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006204 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006205 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006206 int ret = 0;
6207
6208 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6209
6210 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6211 if (!dip) {
6212 ret = -ENOMEM;
6213 goto free_ordered;
6214 }
6215 dip->csums = NULL;
6216
Josef Bacik98bc3142011-03-22 11:00:46 -04006217 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6218 if (!write && !skip_sum) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006219 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6220 if (!dip->csums) {
Daniel J Bluemanb4966b72011-03-09 16:46:42 +00006221 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006222 ret = -ENOMEM;
6223 goto free_ordered;
6224 }
6225 }
6226
6227 dip->private = bio->bi_private;
6228 dip->inode = inode;
6229 dip->logical_offset = file_offset;
6230
Josef Bacik4b46fce2010-05-23 11:00:55 -04006231 dip->bytes = 0;
6232 do {
6233 dip->bytes += bvec->bv_len;
6234 bvec++;
6235 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6236
Chris Mason46bfbb52010-05-26 11:04:10 -04006237 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006238 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006239 dip->errors = 0;
6240 dip->orig_bio = bio;
6241 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006242
6243 if (write)
6244 bio->bi_end_io = btrfs_endio_direct_write;
6245 else
6246 bio->bi_end_io = btrfs_endio_direct_read;
6247
Miao Xiee65e1532010-11-22 03:04:43 +00006248 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6249 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006250 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006251free_ordered:
6252 /*
6253 * If this is a write, we need to clean up the reserved space and kill
6254 * the ordered extent.
6255 */
6256 if (write) {
6257 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006258 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006259 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6260 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6261 btrfs_free_reserved_extent(root, ordered->start,
6262 ordered->disk_len);
6263 btrfs_put_ordered_extent(ordered);
6264 btrfs_put_ordered_extent(ordered);
6265 }
6266 bio_endio(bio, ret);
6267}
6268
Chris Mason5a5f79b2010-05-26 21:33:37 -04006269static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6270 const struct iovec *iov, loff_t offset,
6271 unsigned long nr_segs)
6272{
6273 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006274 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006275 size_t size;
6276 unsigned long addr;
6277 unsigned blocksize_mask = root->sectorsize - 1;
6278 ssize_t retval = -EINVAL;
6279 loff_t end = offset;
6280
6281 if (offset & blocksize_mask)
6282 goto out;
6283
6284 /* Check the memory alignment. Blocks cannot straddle pages */
6285 for (seg = 0; seg < nr_segs; seg++) {
6286 addr = (unsigned long)iov[seg].iov_base;
6287 size = iov[seg].iov_len;
6288 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006289 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006290 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006291
6292 /* If this is a write we don't need to check anymore */
6293 if (rw & WRITE)
6294 continue;
6295
6296 /*
6297 * Check to make sure we don't have duplicate iov_base's in this
6298 * iovec, if so return EINVAL, otherwise we'll get csum errors
6299 * when reading back.
6300 */
6301 for (i = seg + 1; i < nr_segs; i++) {
6302 if (iov[seg].iov_base == iov[i].iov_base)
6303 goto out;
6304 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006305 }
6306 retval = 0;
6307out:
6308 return retval;
6309}
Chris Mason16432982008-04-10 10:23:21 -04006310static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6311 const struct iovec *iov, loff_t offset,
6312 unsigned long nr_segs)
6313{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006314 struct file *file = iocb->ki_filp;
6315 struct inode *inode = file->f_mapping->host;
6316 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006317 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006318 u64 lockstart, lockend;
6319 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006320 int writing = rw & WRITE;
6321 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006322 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006323
Chris Mason5a5f79b2010-05-26 21:33:37 -04006324 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6325 offset, nr_segs)) {
6326 return 0;
6327 }
6328
Josef Bacik4b46fce2010-05-23 11:00:55 -04006329 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006330 lockend = offset + count - 1;
6331
6332 if (writing) {
6333 ret = btrfs_delalloc_reserve_space(inode, count);
6334 if (ret)
6335 goto out;
6336 }
Chris Mason4845e442010-05-25 20:56:50 -04006337
Josef Bacik4b46fce2010-05-23 11:00:55 -04006338 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006339 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006340 0, &cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006341 /*
6342 * We're concerned with the entire range that we're going to be
6343 * doing DIO to, so we need to make sure theres no ordered
6344 * extents in this range.
6345 */
6346 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6347 lockend - lockstart + 1);
6348 if (!ordered)
6349 break;
Chris Mason4845e442010-05-25 20:56:50 -04006350 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6351 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006352 btrfs_start_ordered_extent(inode, ordered, 1);
6353 btrfs_put_ordered_extent(ordered);
6354 cond_resched();
6355 }
6356
Chris Mason4845e442010-05-25 20:56:50 -04006357 /*
6358 * we don't use btrfs_set_extent_delalloc because we don't want
6359 * the dirty or uptodate bits
6360 */
6361 if (writing) {
6362 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6363 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01006364 EXTENT_DELALLOC, NULL, &cached_state,
Chris Mason4845e442010-05-25 20:56:50 -04006365 GFP_NOFS);
6366 if (ret) {
6367 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6368 lockend, EXTENT_LOCKED | write_bits,
6369 1, 0, &cached_state, GFP_NOFS);
6370 goto out;
6371 }
6372 }
6373
6374 free_extent_state(cached_state);
6375 cached_state = NULL;
6376
Chris Mason5a5f79b2010-05-26 21:33:37 -04006377 ret = __blockdev_direct_IO(rw, iocb, inode,
6378 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6379 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6380 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006381
6382 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006383 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6384 offset + iov_length(iov, nr_segs) - 1,
6385 EXTENT_LOCKED | write_bits, 1, 0,
6386 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006387 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6388 /*
6389 * We're falling back to buffered, unlock the section we didn't
6390 * do IO on.
6391 */
Chris Mason4845e442010-05-25 20:56:50 -04006392 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6393 offset + iov_length(iov, nr_segs) - 1,
6394 EXTENT_LOCKED | write_bits, 1, 0,
6395 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006396 }
Chris Mason4845e442010-05-25 20:56:50 -04006397out:
6398 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006399 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006400}
6401
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006402static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6403 __u64 start, __u64 len)
6404{
Chris Masonec29ed52011-02-23 16:23:20 -05006405 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006406}
6407
Chris Mason9ebefb182007-06-15 13:50:00 -04006408int btrfs_readpage(struct file *file, struct page *page)
6409{
Chris Masond1310b22008-01-24 16:13:08 -05006410 struct extent_io_tree *tree;
6411 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02006412 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006413}
Chris Mason1832a6d2007-12-21 16:27:21 -05006414
Chris Mason39279cc2007-06-12 06:35:45 -04006415static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6416{
Chris Masond1310b22008-01-24 16:13:08 -05006417 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006418
6419
6420 if (current->flags & PF_MEMALLOC) {
6421 redirty_page_for_writepage(wbc, page);
6422 unlock_page(page);
6423 return 0;
6424 }
Chris Masond1310b22008-01-24 16:13:08 -05006425 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006426 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6427}
Chris Mason39279cc2007-06-12 06:35:45 -04006428
Chris Masonf4219502008-07-22 11:18:09 -04006429int btrfs_writepages(struct address_space *mapping,
6430 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006431{
Chris Masond1310b22008-01-24 16:13:08 -05006432 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006433
Chris Masond1310b22008-01-24 16:13:08 -05006434 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006435 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6436}
6437
Chris Mason3ab2fb52007-11-08 10:59:22 -05006438static int
6439btrfs_readpages(struct file *file, struct address_space *mapping,
6440 struct list_head *pages, unsigned nr_pages)
6441{
Chris Masond1310b22008-01-24 16:13:08 -05006442 struct extent_io_tree *tree;
6443 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006444 return extent_readpages(tree, mapping, pages, nr_pages,
6445 btrfs_get_extent);
6446}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006447static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006448{
Chris Masond1310b22008-01-24 16:13:08 -05006449 struct extent_io_tree *tree;
6450 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006451 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006452
Chris Masond1310b22008-01-24 16:13:08 -05006453 tree = &BTRFS_I(page->mapping->host)->io_tree;
6454 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006455 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006456 if (ret == 1) {
6457 ClearPagePrivate(page);
6458 set_page_private(page, 0);
6459 page_cache_release(page);
6460 }
6461 return ret;
6462}
Chris Mason39279cc2007-06-12 06:35:45 -04006463
Chris Masone6dcd2d2008-07-17 12:53:50 -04006464static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6465{
Chris Mason98509cf2008-09-11 15:51:43 -04006466 if (PageWriteback(page) || PageDirty(page))
6467 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006468 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006469}
6470
Chris Masona52d9a82007-08-27 16:49:44 -04006471static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6472{
Josef Bacik5fd02042012-05-02 14:00:54 -04006473 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05006474 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006475 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006476 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006477 u64 page_start = page_offset(page);
6478 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006479
Chris Mason8b62b722009-09-02 16:53:46 -04006480 /*
6481 * we have the page locked, so new writeback can't start,
6482 * and the dirty bit won't be cleared while we are here.
6483 *
6484 * Wait for IO on this page so that we can safely clear
6485 * the PagePrivate2 bit and do ordered accounting
6486 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006487 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006488
Josef Bacik5fd02042012-05-02 14:00:54 -04006489 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006490 if (offset) {
6491 btrfs_releasepage(page, GFP_NOFS);
6492 return;
6493 }
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006494 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Josef Bacik5fd02042012-05-02 14:00:54 -04006495 ordered = btrfs_lookup_ordered_extent(inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006496 page_offset(page));
6497 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006498 /*
6499 * IO on this page will never be started, so we need
6500 * to account for any ordered extents now
6501 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006502 clear_extent_bit(tree, page_start, page_end,
6503 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006504 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006505 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006506 /*
6507 * whoever cleared the private bit is responsible
6508 * for the finish_ordered_io
6509 */
Josef Bacik5fd02042012-05-02 14:00:54 -04006510 if (TestClearPagePrivate2(page) &&
6511 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6512 PAGE_CACHE_SIZE, 1)) {
6513 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04006514 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006515 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006516 cached_state = NULL;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006517 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006518 }
6519 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006520 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006521 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006522 __btrfs_releasepage(page, GFP_NOFS);
6523
Chris Mason4a096752008-07-21 10:29:44 -04006524 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006525 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006526 ClearPagePrivate(page);
6527 set_page_private(page, 0);
6528 page_cache_release(page);
6529 }
Chris Mason39279cc2007-06-12 06:35:45 -04006530}
6531
Chris Mason9ebefb182007-06-15 13:50:00 -04006532/*
6533 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6534 * called from a page fault handler when a page is first dirtied. Hence we must
6535 * be careful to check for EOF conditions here. We set the page up correctly
6536 * for a written page which means we get ENOSPC checking when writing into
6537 * holes and correct delalloc and unwritten extent mapping on filesystems that
6538 * support these features.
6539 *
6540 * We are not allowed to take the i_mutex here so we have to play games to
6541 * protect against truncate races as the page could now be beyond EOF. Because
6542 * vmtruncate() writes the inode size before removing pages, once we have the
6543 * page lock we can determine safely if the page is beyond EOF. If it is not
6544 * beyond EOF, then the page is guaranteed safe against truncation until we
6545 * unlock the page.
6546 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006547int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006548{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006549 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006550 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006551 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006552 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6553 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006554 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006555 char *kaddr;
6556 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006557 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006558 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05006559 int reserved = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006560 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006561 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006562
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006563 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006564 if (!ret) {
Josef Bacik22c44fe2011-11-30 10:45:38 -05006565 ret = btrfs_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05006566 reserved = 1;
6567 }
Nick Piggin56a76f82009-03-31 15:23:23 -07006568 if (ret) {
6569 if (ret == -ENOMEM)
6570 ret = VM_FAULT_OOM;
6571 else /* -ENOSPC, -EIO, etc */
6572 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05006573 if (reserved)
6574 goto out;
6575 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07006576 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006577
Nick Piggin56a76f82009-03-31 15:23:23 -07006578 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006579again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006580 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006581 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006582 page_start = page_offset(page);
6583 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006584
Chris Mason9ebefb182007-06-15 13:50:00 -04006585 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006586 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006587 /* page got truncated out from underneath us */
6588 goto out_unlock;
6589 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006590 wait_on_page_writeback(page);
6591
Jeff Mahoneyd0082372012-03-01 14:57:19 +01006592 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006593 set_page_extent_mapped(page);
6594
Chris Masoneb84ae02008-07-17 13:53:27 -04006595 /*
6596 * we can't set the delalloc bits if there are pending ordered
6597 * extents. Drop our locks and wait for them to finish
6598 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006599 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6600 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006601 unlock_extent_cached(io_tree, page_start, page_end,
6602 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006603 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006604 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006605 btrfs_put_ordered_extent(ordered);
6606 goto again;
6607 }
6608
Josef Bacikfbf19082009-10-01 17:10:23 -04006609 /*
6610 * XXX - page_mkwrite gets called every time the page is dirtied, even
6611 * if it was already dirty, so for space accounting reasons we need to
6612 * clear any delalloc bits for the range we are fixing to save. There
6613 * is probably a better way to do this, but for now keep consistent with
6614 * prepare_pages in the normal write path.
6615 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006616 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006617 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006618 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006619
Josef Bacik2ac55d42010-02-03 19:33:23 +00006620 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6621 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006622 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006623 unlock_extent_cached(io_tree, page_start, page_end,
6624 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006625 ret = VM_FAULT_SIGBUS;
6626 goto out_unlock;
6627 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006628 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006629
6630 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006631 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006632 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006633 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006634 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006635
Chris Masone6dcd2d2008-07-17 12:53:50 -04006636 if (zero_start != PAGE_CACHE_SIZE) {
6637 kaddr = kmap(page);
6638 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6639 flush_dcache_page(page);
6640 kunmap(page);
6641 }
Chris Mason247e7432008-07-17 12:53:51 -04006642 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006643 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006644 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006645
Chris Mason257c62e2009-10-13 13:21:08 -04006646 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6647 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6648
Josef Bacik2ac55d42010-02-03 19:33:23 +00006649 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006650
6651out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006652 if (!ret)
6653 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006654 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05006655out:
Josef Bacikec39e182012-01-12 19:10:12 -05006656 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason9998eb72012-01-25 13:47:40 -05006657out_noreserve:
Chris Mason9ebefb182007-06-15 13:50:00 -04006658 return ret;
6659}
6660
Josef Bacika41ad392011-01-31 15:30:16 -05006661static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006662{
6663 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006664 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006665 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006666 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006667 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006668 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006669 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04006670 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04006671
Josef Bacik5d5e1032009-10-13 16:46:49 -04006672 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6673 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006674 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006675
Chris Mason4a096752008-07-21 10:29:44 -04006676 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006677 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006678
Josef Bacikfcb80c22011-05-03 10:40:22 -04006679 /*
6680 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6681 * 3 things going on here
6682 *
6683 * 1) We need to reserve space for our orphan item and the space to
6684 * delete our orphan item. Lord knows we don't want to have a dangling
6685 * orphan item because we didn't reserve space to remove it.
6686 *
6687 * 2) We need to reserve space to update our inode.
6688 *
6689 * 3) We need to have something to cache all the space that is going to
6690 * be free'd up by the truncate operation, but also have some slack
6691 * space reserved in case it uses space during the truncate (thank you
6692 * very much snapshotting).
6693 *
6694 * And we need these to all be seperate. The fact is we can use alot of
6695 * space doing the truncate, and we have no earthly idea how much space
6696 * we will use, so we need the truncate reservation to be seperate so it
6697 * doesn't end up using space reserved for updating the inode or
6698 * removing the orphan item. We also need to be able to stop the
6699 * transaction and start a new one, which means we need to be able to
6700 * update the inode several times, and we have no idea of knowing how
6701 * many times that will be, so we can't just reserve 1 item for the
6702 * entirety of the opration, so that has to be done seperately as well.
6703 * Then there is the orphan item, which does indeed need to be held on
6704 * to for the whole operation, and we need nobody to touch this reserved
6705 * space except the orphan code.
6706 *
6707 * So that leaves us with
6708 *
6709 * 1) root->orphan_block_rsv - for the orphan deletion.
6710 * 2) rsv - for the truncate reservation, which we will steal from the
6711 * transaction reservation.
6712 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6713 * updating the inode.
6714 */
6715 rsv = btrfs_alloc_block_rsv(root);
6716 if (!rsv)
6717 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04006718 rsv->size = min_size;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006719
Josef Bacik907cbce2011-08-08 13:46:15 -04006720 /*
Josef Bacik07127182011-08-19 10:29:59 -04006721 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04006722 * 1 for the orphan item we're going to add
6723 * 1 for the orphan item deletion
6724 * 1 for updating the inode.
6725 */
Josef Bacikfcb80c22011-05-03 10:40:22 -04006726 trans = btrfs_start_transaction(root, 4);
6727 if (IS_ERR(trans)) {
6728 err = PTR_ERR(trans);
6729 goto out;
6730 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006731
Josef Bacik907cbce2011-08-08 13:46:15 -04006732 /* Migrate the slack space for the truncate to our reserve */
6733 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6734 min_size);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006735 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05006736
6737 ret = btrfs_orphan_add(trans, inode);
6738 if (ret) {
6739 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006740 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006741 }
6742
Chris Mason5a3f23d2009-03-31 13:27:11 -04006743 /*
6744 * setattr is responsible for setting the ordered_data_close flag,
6745 * but that is only tested during the last file release. That
6746 * could happen well after the next commit, leaving a great big
6747 * window where new writes may get lost if someone chooses to write
6748 * to this file after truncating to zero
6749 *
6750 * The inode doesn't have any dirty data here, and so if we commit
6751 * this is a noop. If someone immediately starts writing to the inode
6752 * it is very likely we'll catch some of their writes in this
6753 * transaction, and the commit will find this file on the ordered
6754 * data list with good things to send down.
6755 *
6756 * This is a best effort solution, there is still a window where
6757 * using truncate to replace the contents of the file will
6758 * end up with a zero length file after a crash.
6759 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04006760 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
6761 &BTRFS_I(inode)->runtime_flags))
Chris Mason5a3f23d2009-03-31 13:27:11 -04006762 btrfs_add_ordered_operation(trans, root, inode);
6763
Yan, Zheng80825102009-11-12 09:35:36 +00006764 while (1) {
Josef Bacik36ba0222011-10-18 12:15:48 -04006765 ret = btrfs_block_rsv_refill(root, rsv, min_size);
Josef Bacik907cbce2011-08-08 13:46:15 -04006766 if (ret) {
6767 /*
6768 * This can only happen with the original transaction we
6769 * started above, every other time we shouldn't have a
6770 * transaction started yet.
6771 */
6772 if (ret == -EAGAIN)
6773 goto end_trans;
6774 err = ret;
6775 break;
6776 }
6777
Yan, Zhengd68fc572010-05-16 10:49:58 -04006778 if (!trans) {
Josef Bacik907cbce2011-08-08 13:46:15 -04006779 /* Just need the 1 for updating the inode */
6780 trans = btrfs_start_transaction(root, 1);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006781 if (IS_ERR(trans)) {
Josef Bacik7041ee92011-12-09 13:26:22 -05006782 ret = err = PTR_ERR(trans);
6783 trans = NULL;
6784 break;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006785 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006786 }
6787
Josef Bacik907cbce2011-08-08 13:46:15 -04006788 trans->block_rsv = rsv;
6789
Yan, Zheng80825102009-11-12 09:35:36 +00006790 ret = btrfs_truncate_inode_items(trans, root, inode,
6791 inode->i_size,
6792 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006793 if (ret != -EAGAIN) {
6794 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006795 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006796 }
Chris Mason39279cc2007-06-12 06:35:45 -04006797
Josef Bacikfcb80c22011-05-03 10:40:22 -04006798 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006799 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006800 if (ret) {
6801 err = ret;
6802 break;
6803 }
Josef Bacik907cbce2011-08-08 13:46:15 -04006804end_trans:
Yan, Zheng80825102009-11-12 09:35:36 +00006805 nr = trans->blocks_used;
6806 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006807 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006808 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006809 }
6810
6811 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04006812 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006813 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006814 if (ret)
6815 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006816 } else if (ret && inode->i_nlink > 0) {
6817 /*
6818 * Failed to do the truncate, remove us from the in memory
6819 * orphan list.
6820 */
6821 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006822 }
6823
Chris Mason917c16b2011-11-08 14:49:59 -05006824 if (trans) {
6825 trans->block_rsv = &root->fs_info->trans_block_rsv;
6826 ret = btrfs_update_inode(trans, root, inode);
6827 if (ret && !err)
6828 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006829
Chris Mason917c16b2011-11-08 14:49:59 -05006830 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006831 ret = btrfs_end_transaction(trans, root);
Chris Mason917c16b2011-11-08 14:49:59 -05006832 btrfs_btree_balance_dirty(root, nr);
6833 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04006834
6835out:
6836 btrfs_free_block_rsv(root, rsv);
6837
Josef Bacik3893e332011-01-31 16:03:11 -05006838 if (ret && !err)
6839 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05006840
Josef Bacik3893e332011-01-31 16:03:11 -05006841 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006842}
6843
Sven Wegener3b963622008-06-09 21:57:42 -04006844/*
Chris Masond352ac62008-09-29 15:18:18 -04006845 * create a new subvolume directory/inode (helper for the ioctl).
6846 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006847int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006848 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04006849{
Chris Mason39279cc2007-06-12 06:35:45 -04006850 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006851 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006852 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006853
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01006854 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
6855 new_dirid, new_dirid,
6856 S_IFDIR | (~current_umask() & S_IRWXUGO),
6857 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006858 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006859 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006860 inode->i_op = &btrfs_dir_inode_operations;
6861 inode->i_fop = &btrfs_dir_file_operations;
6862
Miklos Szeredibfe86842011-10-28 14:13:29 +02006863 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04006864 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006865
Yan, Zheng76dda932009-09-21 16:00:26 -04006866 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006867
Yan, Zheng76dda932009-09-21 16:00:26 -04006868 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07006869 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006870}
6871
Chris Mason39279cc2007-06-12 06:35:45 -04006872struct inode *btrfs_alloc_inode(struct super_block *sb)
6873{
6874 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006875 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006876
6877 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6878 if (!ei)
6879 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006880
6881 ei->root = NULL;
6882 ei->space_info = NULL;
6883 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006884 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006885 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006886 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006887 ei->delalloc_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006888 ei->disk_i_size = 0;
6889 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04006890 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006891 ei->index_cnt = (u64)-1;
6892 ei->last_unlink_trans = 0;
6893
Josef Bacik9e0baf62011-07-15 15:16:44 +00006894 spin_lock_init(&ei->lock);
6895 ei->outstanding_extents = 0;
6896 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006897
Josef Bacik72ac3c02012-05-23 14:13:11 -04006898 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006899 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006900
Miao Xie16cdcec2011-04-22 18:12:22 +08006901 ei->delayed_node = NULL;
6902
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006903 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02006904 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02006905 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6906 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04006907 ei->io_tree.track_uptodate = 1;
6908 ei->io_failure_tree.track_uptodate = 1;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006909 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05006910 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006911 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006912 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006913 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006914 RB_CLEAR_NODE(&ei->rb_node);
6915
6916 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006917}
6918
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006919static void btrfs_i_callback(struct rcu_head *head)
6920{
6921 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006922 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6923}
6924
Chris Mason39279cc2007-06-12 06:35:45 -04006925void btrfs_destroy_inode(struct inode *inode)
6926{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006927 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006928 struct btrfs_root *root = BTRFS_I(inode)->root;
6929
Chris Mason39279cc2007-06-12 06:35:45 -04006930 WARN_ON(!list_empty(&inode->i_dentry));
6931 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00006932 WARN_ON(BTRFS_I(inode)->outstanding_extents);
6933 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04006934 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
6935 WARN_ON(BTRFS_I(inode)->csum_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04006936
Chris Mason5a3f23d2009-03-31 13:27:11 -04006937 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006938 * This can happen where we create an inode, but somebody else also
6939 * created the same inode and we need to destroy the one we already
6940 * created.
6941 */
6942 if (!root)
6943 goto free;
6944
6945 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006946 * Make sure we're properly removed from the ordered operation
6947 * lists.
6948 */
6949 smp_mb();
6950 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6951 spin_lock(&root->fs_info->ordered_extent_lock);
6952 list_del_init(&BTRFS_I(inode)->ordered_operations);
6953 spin_unlock(&root->fs_info->ordered_extent_lock);
6954 }
6955
Josef Bacik8a35d952012-05-23 14:26:42 -04006956 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
6957 &BTRFS_I(inode)->runtime_flags)) {
Li Zefan33345d012011-04-20 10:31:50 +08006958 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6959 (unsigned long long)btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04006960 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04006961 }
Josef Bacik7b128762008-07-24 12:17:14 -04006962
Chris Masond3977122009-01-05 21:25:51 -05006963 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006964 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6965 if (!ordered)
6966 break;
6967 else {
Chris Masond3977122009-01-05 21:25:51 -05006968 printk(KERN_ERR "btrfs found ordered "
6969 "extent %llu %llu on inode cleanup\n",
6970 (unsigned long long)ordered->file_offset,
6971 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006972 btrfs_remove_ordered_extent(inode, ordered);
6973 btrfs_put_ordered_extent(ordered);
6974 btrfs_put_ordered_extent(ordered);
6975 }
6976 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006977 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006978 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006979free:
Miao Xie16cdcec2011-04-22 18:12:22 +08006980 btrfs_remove_delayed_node(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006981 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04006982}
6983
Al Viro45321ac2010-06-07 13:43:19 -04006984int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006985{
6986 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006987
Josef Bacik0af3d002010-06-21 14:48:16 -04006988 if (btrfs_root_refs(&root->root_item) == 0 &&
Chris Mason2cf85722011-07-26 15:35:09 -04006989 !btrfs_is_free_space_inode(root, inode))
Al Viro45321ac2010-06-07 13:43:19 -04006990 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006991 else
Al Viro45321ac2010-06-07 13:43:19 -04006992 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006993}
6994
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006995static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006996{
6997 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6998
6999 inode_init_once(&ei->vfs_inode);
7000}
7001
7002void btrfs_destroy_cachep(void)
7003{
7004 if (btrfs_inode_cachep)
7005 kmem_cache_destroy(btrfs_inode_cachep);
7006 if (btrfs_trans_handle_cachep)
7007 kmem_cache_destroy(btrfs_trans_handle_cachep);
7008 if (btrfs_transaction_cachep)
7009 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007010 if (btrfs_path_cachep)
7011 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05007012 if (btrfs_free_space_cachep)
7013 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04007014}
7015
7016int btrfs_init_cachep(void)
7017{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007018 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
7019 sizeof(struct btrfs_inode), 0,
7020 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04007021 if (!btrfs_inode_cachep)
7022 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007023
7024 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
7025 sizeof(struct btrfs_trans_handle), 0,
7026 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007027 if (!btrfs_trans_handle_cachep)
7028 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007029
7030 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
7031 sizeof(struct btrfs_transaction), 0,
7032 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007033 if (!btrfs_transaction_cachep)
7034 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007035
7036 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
7037 sizeof(struct btrfs_path), 0,
7038 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04007039 if (!btrfs_path_cachep)
7040 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02007041
Josef Bacikdc89e982011-01-28 17:05:48 -05007042 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
7043 sizeof(struct btrfs_free_space), 0,
7044 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7045 if (!btrfs_free_space_cachep)
7046 goto fail;
7047
Chris Mason39279cc2007-06-12 06:35:45 -04007048 return 0;
7049fail:
7050 btrfs_destroy_cachep();
7051 return -ENOMEM;
7052}
7053
7054static int btrfs_getattr(struct vfsmount *mnt,
7055 struct dentry *dentry, struct kstat *stat)
7056{
7057 struct inode *inode = dentry->d_inode;
David Sterbafadc0d82011-11-20 07:33:38 -05007058 u32 blocksize = inode->i_sb->s_blocksize;
7059
Chris Mason39279cc2007-06-12 06:35:45 -04007060 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04007061 stat->dev = BTRFS_I(inode)->root->anon_dev;
Chris Masond6667462008-01-03 14:51:00 -05007062 stat->blksize = PAGE_CACHE_SIZE;
David Sterbafadc0d82011-11-20 07:33:38 -05007063 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7064 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04007065 return 0;
7066}
7067
Liu Bo75e7cb72011-03-22 10:12:20 +00007068/*
7069 * If a file is moved, it will inherit the cow and compression flags of the new
7070 * directory.
7071 */
7072static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7073{
7074 struct btrfs_inode *b_dir = BTRFS_I(dir);
7075 struct btrfs_inode *b_inode = BTRFS_I(inode);
7076
7077 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7078 b_inode->flags |= BTRFS_INODE_NODATACOW;
7079 else
7080 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7081
7082 if (b_dir->flags & BTRFS_INODE_COMPRESS)
7083 b_inode->flags |= BTRFS_INODE_COMPRESS;
7084 else
7085 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7086}
7087
Chris Masond3977122009-01-05 21:25:51 -05007088static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7089 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007090{
7091 struct btrfs_trans_handle *trans;
7092 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007093 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007094 struct inode *new_inode = new_dentry->d_inode;
7095 struct inode *old_inode = old_dentry->d_inode;
7096 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007097 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007098 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007099 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08007100 u64 old_ino = btrfs_ino(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007101
Li Zefan33345d012011-04-20 10:31:50 +08007102 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04007103 return -EPERM;
7104
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007105 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08007106 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007107 return -EXDEV;
7108
Li Zefan33345d012011-04-20 10:31:50 +08007109 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7110 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007111 return -ENOTEMPTY;
7112
Chris Mason39279cc2007-06-12 06:35:45 -04007113 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007114 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007115 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007116 /*
7117 * we're using rename to replace one file with another.
7118 * and the replacement file is large. Start IO on it now so
7119 * we don't add too much work to the end of the transaction
7120 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007121 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007122 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7123 filemap_flush(old_inode->i_mapping);
7124
Yan, Zheng76dda932009-09-21 16:00:26 -04007125 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08007126 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007127 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007128 /*
7129 * We want to reserve the absolute worst case amount of items. So if
7130 * both inodes are subvols and we need to unlink them then that would
7131 * require 4 item modifications, but if they are both normal inodes it
7132 * would require 5 item modifications, so we'll assume their normal
7133 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7134 * should cover the worst case number of items we'll modify.
7135 */
7136 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007137 if (IS_ERR(trans)) {
7138 ret = PTR_ERR(trans);
7139 goto out_notrans;
7140 }
Chris Mason5f39d392007-10-15 16:14:19 -04007141
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007142 if (dest != root)
7143 btrfs_record_root_in_trans(trans, dest);
7144
Yan, Zhenga5719522009-09-24 09:17:31 -04007145 ret = btrfs_set_inode_index(new_dir, &index);
7146 if (ret)
7147 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007148
Li Zefan33345d012011-04-20 10:31:50 +08007149 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007150 /* force full log commit if subvolume involved. */
7151 root->fs_info->last_trans_log_full_commit = trans->transid;
7152 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007153 ret = btrfs_insert_inode_ref(trans, dest,
7154 new_dentry->d_name.name,
7155 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08007156 old_ino,
7157 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04007158 if (ret)
7159 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007160 /*
7161 * this is an ugly little race, but the rename is required
7162 * to make sure that if we crash, the inode is either at the
7163 * old name or the new one. pinning the log transaction lets
7164 * us make sure we don't allow a log commit to come in after
7165 * we unlink the name but before we add the new name back in.
7166 */
7167 btrfs_pin_log_trans(root);
7168 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007169 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007170 * make sure the inode gets flushed if it is replacing
7171 * something.
7172 */
Li Zefan33345d012011-04-20 10:31:50 +08007173 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
Chris Mason5a3f23d2009-03-31 13:27:11 -04007174 btrfs_add_ordered_operation(trans, root, old_inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04007175
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007176 inode_inc_iversion(old_dir);
7177 inode_inc_iversion(new_dir);
7178 inode_inc_iversion(old_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007179 old_dir->i_ctime = old_dir->i_mtime = ctime;
7180 new_dir->i_ctime = new_dir->i_mtime = ctime;
7181 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007182
Chris Mason12fcfd22009-03-24 10:24:20 -04007183 if (old_dentry->d_parent != new_dentry->d_parent)
7184 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7185
Li Zefan33345d012011-04-20 10:31:50 +08007186 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007187 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7188 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7189 old_dentry->d_name.name,
7190 old_dentry->d_name.len);
7191 } else {
Al Viro92986792011-03-04 17:14:37 +00007192 ret = __btrfs_unlink_inode(trans, root, old_dir,
7193 old_dentry->d_inode,
7194 old_dentry->d_name.name,
7195 old_dentry->d_name.len);
7196 if (!ret)
7197 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007198 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007199 if (ret) {
7200 btrfs_abort_transaction(trans, root, ret);
7201 goto out_fail;
7202 }
Chris Mason39279cc2007-06-12 06:35:45 -04007203
7204 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007205 inode_inc_iversion(new_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007206 new_inode->i_ctime = CURRENT_TIME;
Li Zefan33345d012011-04-20 10:31:50 +08007207 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007208 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7209 root_objectid = BTRFS_I(new_inode)->location.objectid;
7210 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7211 root_objectid,
7212 new_dentry->d_name.name,
7213 new_dentry->d_name.len);
7214 BUG_ON(new_inode->i_nlink == 0);
7215 } else {
7216 ret = btrfs_unlink_inode(trans, dest, new_dir,
7217 new_dentry->d_inode,
7218 new_dentry->d_name.name,
7219 new_dentry->d_name.len);
7220 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007221 if (!ret && new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007222 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007223 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007224 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007225 if (ret) {
7226 btrfs_abort_transaction(trans, root, ret);
7227 goto out_fail;
7228 }
Chris Mason39279cc2007-06-12 06:35:45 -04007229 }
Josef Bacikaec74772008-07-24 12:12:38 -04007230
Liu Bo75e7cb72011-03-22 10:12:20 +00007231 fixup_inode_flags(new_dir, old_inode);
7232
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007233 ret = btrfs_add_link(trans, new_dir, old_inode,
7234 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007235 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007236 if (ret) {
7237 btrfs_abort_transaction(trans, root, ret);
7238 goto out_fail;
7239 }
Chris Mason39279cc2007-06-12 06:35:45 -04007240
Li Zefan33345d012011-04-20 10:31:50 +08007241 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
Al Viro10d9f302011-07-16 23:09:10 -04007242 struct dentry *parent = new_dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +00007243 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007244 btrfs_end_log_trans(root);
7245 }
Chris Mason39279cc2007-06-12 06:35:45 -04007246out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007247 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007248out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08007249 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04007250 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007251
Chris Mason39279cc2007-06-12 06:35:45 -04007252 return ret;
7253}
7254
Chris Masond352ac62008-09-29 15:18:18 -04007255/*
7256 * some fairly slow code that needs optimization. This walks the list
7257 * of all the inodes with pending delalloc and forces them to disk.
7258 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007259int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007260{
7261 struct list_head *head = &root->fs_info->delalloc_inodes;
7262 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007263 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007264
Yan Zhengc146afa2008-11-12 14:34:12 -05007265 if (root->fs_info->sb->s_flags & MS_RDONLY)
7266 return -EROFS;
7267
Chris Mason75eff682008-12-15 15:54:40 -05007268 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007269 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007270 binode = list_entry(head->next, struct btrfs_inode,
7271 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007272 inode = igrab(&binode->vfs_inode);
7273 if (!inode)
7274 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007275 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007276 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007277 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007278 if (delay_iput)
7279 btrfs_add_delayed_iput(inode);
7280 else
7281 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007282 }
7283 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007284 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007285 }
Chris Mason75eff682008-12-15 15:54:40 -05007286 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007287
7288 /* the filemap_flush will queue IO into the worker threads, but
7289 * we have to make sure the IO is actually started and that
7290 * ordered extents get created before we return
7291 */
7292 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007293 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007294 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007295 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007296 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7297 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007298 }
7299 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007300 return 0;
7301}
7302
Chris Mason39279cc2007-06-12 06:35:45 -04007303static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7304 const char *symname)
7305{
7306 struct btrfs_trans_handle *trans;
7307 struct btrfs_root *root = BTRFS_I(dir)->root;
7308 struct btrfs_path *path;
7309 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007310 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007311 int err;
7312 int drop_inode = 0;
7313 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007314 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007315 int name_len;
7316 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007317 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007318 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007319 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007320 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007321
7322 name_len = strlen(symname) + 1;
7323 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7324 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007325
Josef Bacik9ed74f22009-09-11 16:12:44 -04007326 /*
7327 * 2 items for inode item and ref
7328 * 2 items for dir items
7329 * 1 item for xattr if selinux is on
7330 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007331 trans = btrfs_start_transaction(root, 5);
7332 if (IS_ERR(trans))
7333 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007334
Li Zefan581bb052011-04-20 10:06:11 +08007335 err = btrfs_find_free_ino(root, &objectid);
7336 if (err)
7337 goto out_unlock;
7338
Josef Bacikaec74772008-07-24 12:12:38 -04007339 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08007340 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007341 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007342 if (IS_ERR(inode)) {
7343 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007344 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007345 }
Chris Mason39279cc2007-06-12 06:35:45 -04007346
Eric Paris2a7dba32011-02-01 11:05:39 -05007347 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007348 if (err) {
7349 drop_inode = 1;
7350 goto out_unlock;
7351 }
7352
Casey Schauflerad19db72011-12-15 10:09:07 -05007353 /*
7354 * If the active LSM wants to access the inode during
7355 * d_instantiate it needs these. Smack checks to see
7356 * if the filesystem supports xattrs by looking at the
7357 * ops vector.
7358 */
7359 inode->i_fop = &btrfs_file_operations;
7360 inode->i_op = &btrfs_file_inode_operations;
7361
Josef Bacika1b075d2010-11-19 20:36:11 +00007362 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007363 if (err)
7364 drop_inode = 1;
7365 else {
7366 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007367 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05007368 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007369 }
Chris Mason39279cc2007-06-12 06:35:45 -04007370 if (drop_inode)
7371 goto out_unlock;
7372
7373 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07007374 if (!path) {
7375 err = -ENOMEM;
7376 drop_inode = 1;
7377 goto out_unlock;
7378 }
Li Zefan33345d012011-04-20 10:31:50 +08007379 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007380 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007381 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7382 datasize = btrfs_file_extent_calc_inline_size(name_len);
7383 err = btrfs_insert_empty_item(trans, root, path, &key,
7384 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007385 if (err) {
7386 drop_inode = 1;
Julia Lawallb0839162011-05-14 07:10:51 +00007387 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04007388 goto out_unlock;
7389 }
Chris Mason5f39d392007-10-15 16:14:19 -04007390 leaf = path->nodes[0];
7391 ei = btrfs_item_ptr(leaf, path->slots[0],
7392 struct btrfs_file_extent_item);
7393 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7394 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007395 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007396 btrfs_set_file_extent_encryption(leaf, ei, 0);
7397 btrfs_set_file_extent_compression(leaf, ei, 0);
7398 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7399 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7400
Chris Mason39279cc2007-06-12 06:35:45 -04007401 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007402 write_extent_buffer(leaf, symname, ptr, name_len);
7403 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007404 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007405
Chris Mason39279cc2007-06-12 06:35:45 -04007406 inode->i_op = &btrfs_symlink_inode_operations;
7407 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007408 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007409 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007410 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007411 err = btrfs_update_inode(trans, root, inode);
7412 if (err)
7413 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007414
7415out_unlock:
Al Viro08c422c2011-12-23 07:58:13 -05007416 if (!err)
7417 d_instantiate(dentry, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007418 nr = trans->blocks_used;
Josef Bacik7ad85bb2012-01-12 19:10:12 -05007419 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007420 if (drop_inode) {
7421 inode_dec_link_count(inode);
7422 iput(inode);
7423 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007424 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007425 return err;
7426}
Chris Mason16432982008-04-10 10:23:21 -04007427
Josef Bacik0af3d002010-06-21 14:48:16 -04007428static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7429 u64 start, u64 num_bytes, u64 min_size,
7430 loff_t actual_len, u64 *alloc_hint,
7431 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007432{
Yan Zhengd899e052008-10-30 14:25:28 -04007433 struct btrfs_root *root = BTRFS_I(inode)->root;
7434 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007435 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007436 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007437 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007438 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007439
Josef Bacik0af3d002010-06-21 14:48:16 -04007440 if (trans)
7441 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007442 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007443 if (own_trans) {
7444 trans = btrfs_start_transaction(root, 3);
7445 if (IS_ERR(trans)) {
7446 ret = PTR_ERR(trans);
7447 break;
7448 }
Yan Zhengd899e052008-10-30 14:25:28 -04007449 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007450
Yan, Zhengefa56462010-05-16 10:49:59 -04007451 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007452 0, *alloc_hint, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007453 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007454 if (own_trans)
7455 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007456 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007457 }
7458
Yan Zhengd899e052008-10-30 14:25:28 -04007459 ret = insert_reserved_file_extent(trans, inode,
7460 cur_offset, ins.objectid,
7461 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007462 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007463 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007464 if (ret) {
7465 btrfs_abort_transaction(trans, root, ret);
7466 if (own_trans)
7467 btrfs_end_transaction(trans, root);
7468 break;
7469 }
Chris Masona1ed8352009-09-11 12:27:37 -04007470 btrfs_drop_extent_cache(inode, cur_offset,
7471 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007472
Yan Zhengd899e052008-10-30 14:25:28 -04007473 num_bytes -= ins.offset;
7474 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007475 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007476
Josef Bacik0c4d2d92012-04-05 15:03:02 -04007477 inode_inc_iversion(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04007478 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007479 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007480 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007481 (actual_len > inode->i_size) &&
7482 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007483 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007484 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007485 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007486 i_size = cur_offset;
7487 i_size_write(inode, i_size);
7488 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007489 }
7490
Yan Zhengd899e052008-10-30 14:25:28 -04007491 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007492
7493 if (ret) {
7494 btrfs_abort_transaction(trans, root, ret);
7495 if (own_trans)
7496 btrfs_end_transaction(trans, root);
7497 break;
7498 }
Yan Zhengd899e052008-10-30 14:25:28 -04007499
Josef Bacik0af3d002010-06-21 14:48:16 -04007500 if (own_trans)
7501 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007502 }
Yan Zhengd899e052008-10-30 14:25:28 -04007503 return ret;
7504}
7505
Josef Bacik0af3d002010-06-21 14:48:16 -04007506int btrfs_prealloc_file_range(struct inode *inode, int mode,
7507 u64 start, u64 num_bytes, u64 min_size,
7508 loff_t actual_len, u64 *alloc_hint)
7509{
7510 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7511 min_size, actual_len, alloc_hint,
7512 NULL);
7513}
7514
7515int btrfs_prealloc_file_range_trans(struct inode *inode,
7516 struct btrfs_trans_handle *trans, int mode,
7517 u64 start, u64 num_bytes, u64 min_size,
7518 loff_t actual_len, u64 *alloc_hint)
7519{
7520 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7521 min_size, actual_len, alloc_hint, trans);
7522}
7523
Chris Masone6dcd2d2008-07-17 12:53:50 -04007524static int btrfs_set_page_dirty(struct page *page)
7525{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007526 return __set_page_dirty_nobuffers(page);
7527}
7528
Al Viro10556cb2011-06-20 19:28:19 -04007529static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007530{
Li Zefanb83cc962010-12-20 16:04:08 +08007531 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007532 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08007533
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00007534 if (mask & MAY_WRITE &&
7535 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7536 if (btrfs_root_readonly(root))
7537 return -EROFS;
7538 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7539 return -EACCES;
7540 }
Al Viro2830ba72011-06-20 19:16:29 -04007541 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05007542}
Chris Mason39279cc2007-06-12 06:35:45 -04007543
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007544static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007545 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007546 .lookup = btrfs_lookup,
7547 .create = btrfs_create,
7548 .unlink = btrfs_unlink,
7549 .link = btrfs_link,
7550 .mkdir = btrfs_mkdir,
7551 .rmdir = btrfs_rmdir,
7552 .rename = btrfs_rename,
7553 .symlink = btrfs_symlink,
7554 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007555 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007556 .setxattr = btrfs_setxattr,
7557 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007558 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007559 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007560 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007561 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007562};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007563static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007564 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007565 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007566 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007567};
Yan, Zheng76dda932009-09-21 16:00:26 -04007568
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007569static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007570 .llseek = generic_file_llseek,
7571 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007572 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007573 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007574#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007575 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007576#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007577 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007578 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007579};
7580
Chris Masond1310b22008-01-24 16:13:08 -05007581static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007582 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007583 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007584 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007585 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007586 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007587 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007588 .set_bit_hook = btrfs_set_bit_hook,
7589 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007590 .merge_extent_hook = btrfs_merge_extent_hook,
7591 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007592};
7593
Chris Mason35054392009-01-21 13:11:13 -05007594/*
7595 * btrfs doesn't support the bmap operation because swapfiles
7596 * use bmap to make a mapping of extents in the file. They assume
7597 * these extents won't change over the life of the file and they
7598 * use the bmap result to do IO directly to the drive.
7599 *
7600 * the btrfs bmap call would return logical addresses that aren't
7601 * suitable for IO and they also will change frequently as COW
7602 * operations happen. So, swapfile + btrfs == corruption.
7603 *
7604 * For now we're avoiding this by dropping bmap.
7605 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007606static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007607 .readpage = btrfs_readpage,
7608 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007609 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007610 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007611 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007612 .invalidatepage = btrfs_invalidatepage,
7613 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007614 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007615 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007616};
7617
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007618static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007619 .readpage = btrfs_readpage,
7620 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007621 .invalidatepage = btrfs_invalidatepage,
7622 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007623};
7624
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007625static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007626 .getattr = btrfs_getattr,
7627 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007628 .setxattr = btrfs_setxattr,
7629 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007630 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007631 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007632 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007633 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007634 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007635};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007636static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007637 .getattr = btrfs_getattr,
7638 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007639 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007640 .setxattr = btrfs_setxattr,
7641 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007642 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007643 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007644 .get_acl = btrfs_get_acl,
Josef Bacik618e21d2007-07-11 10:18:17 -04007645};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007646static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007647 .readlink = generic_readlink,
7648 .follow_link = page_follow_link_light,
7649 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007650 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -05007651 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007652 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007653 .setxattr = btrfs_setxattr,
7654 .getxattr = btrfs_getxattr,
7655 .listxattr = btrfs_listxattr,
7656 .removexattr = btrfs_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02007657 .get_acl = btrfs_get_acl,
Chris Mason39279cc2007-06-12 06:35:45 -04007658};
Yan, Zheng76dda932009-09-21 16:00:26 -04007659
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007660const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007661 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -04007662 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -04007663};