blob: dc8fb2b3a145d559a6f1915ee70723f6ea28a3c9 [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050040#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040041#include "ctree.h"
42#include "disk-io.h"
43#include "transaction.h"
44#include "btrfs_inode.h"
45#include "ioctl.h"
46#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040047#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040048#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040049#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040050#include "tree-log.h"
Chris Masonc8b97812008-10-29 14:49:59 -040051#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050052#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050053#include "free-space-cache.h"
Chris Mason39279cc2007-06-12 06:35:45 -040054
55struct btrfs_iget_args {
56 u64 ino;
57 struct btrfs_root *root;
58};
59
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070060static const struct inode_operations btrfs_dir_inode_operations;
61static const struct inode_operations btrfs_symlink_inode_operations;
62static const struct inode_operations btrfs_dir_ro_inode_operations;
63static const struct inode_operations btrfs_special_inode_operations;
64static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070065static const struct address_space_operations btrfs_aops;
66static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070067static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050068static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040069
70static struct kmem_cache *btrfs_inode_cachep;
71struct kmem_cache *btrfs_trans_handle_cachep;
72struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040073struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050074struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075
76#define S_SHIFT 12
77static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
78 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
79 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
80 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
81 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
82 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
83 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
84 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
85};
86
Josef Bacika41ad392011-01-31 15:30:16 -050087static int btrfs_setsize(struct inode *inode, loff_t newsize);
88static int btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040089static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050090static noinline int cow_file_range(struct inode *inode,
91 struct page *locked_page,
92 u64 start, u64 end, int *page_started,
93 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040094
Yan, Zhengf34f57a2009-11-12 09:35:27 +000095static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -050096 struct inode *inode, struct inode *dir,
97 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -050098{
99 int err;
100
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000101 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500102 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500103 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500104 return err;
105}
106
Chris Masond352ac62008-09-29 15:18:18 -0400107/*
Chris Masonc8b97812008-10-29 14:49:59 -0400108 * this does all the hard work for inserting an inline extent into
109 * the btree. The caller should have done a btrfs_drop_extents so that
110 * no overlapping inline items exist in the btree
111 */
Chris Masond3977122009-01-05 21:25:51 -0500112static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400113 struct btrfs_root *root, struct inode *inode,
114 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000115 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400116 struct page **compressed_pages)
117{
118 struct btrfs_key key;
119 struct btrfs_path *path;
120 struct extent_buffer *leaf;
121 struct page *page = NULL;
122 char *kaddr;
123 unsigned long ptr;
124 struct btrfs_file_extent_item *ei;
125 int err = 0;
126 int ret;
127 size_t cur_size = size;
128 size_t datasize;
129 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400130
Li Zefanfe3f5662011-03-28 08:30:38 +0000131 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400132 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400133
Chris Masond3977122009-01-05 21:25:51 -0500134 path = btrfs_alloc_path();
135 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400136 return -ENOMEM;
137
Chris Masonb9473432009-03-13 11:00:37 -0400138 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400139
140 key.objectid = inode->i_ino;
141 key.offset = start;
142 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400143 datasize = btrfs_file_extent_calc_inline_size(cur_size);
144
145 inode_add_bytes(inode, size);
146 ret = btrfs_insert_empty_item(trans, root, path, &key,
147 datasize);
148 BUG_ON(ret);
149 if (ret) {
150 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400151 goto fail;
152 }
153 leaf = path->nodes[0];
154 ei = btrfs_item_ptr(leaf, path->slots[0],
155 struct btrfs_file_extent_item);
156 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
157 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
158 btrfs_set_file_extent_encryption(leaf, ei, 0);
159 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
160 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
161 ptr = btrfs_file_extent_inline_start(ei);
162
Li Zefan261507a02010-12-17 14:21:50 +0800163 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400164 struct page *cpage;
165 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500166 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400167 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500168 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400169 PAGE_CACHE_SIZE);
170
Chris Masonb9473432009-03-13 11:00:37 -0400171 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400172 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400173 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400174
175 i++;
176 ptr += cur_size;
177 compressed_size -= cur_size;
178 }
179 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800180 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400181 } else {
182 page = find_get_page(inode->i_mapping,
183 start >> PAGE_CACHE_SHIFT);
184 btrfs_set_file_extent_compression(leaf, ei, 0);
185 kaddr = kmap_atomic(page, KM_USER0);
186 offset = start & (PAGE_CACHE_SIZE - 1);
187 write_extent_buffer(leaf, kaddr + offset, ptr, size);
188 kunmap_atomic(kaddr, KM_USER0);
189 page_cache_release(page);
190 }
191 btrfs_mark_buffer_dirty(leaf);
192 btrfs_free_path(path);
193
Yan, Zhengc2167752009-11-12 09:34:21 +0000194 /*
195 * we're an inline extent, so nobody can
196 * extend the file past i_size without locking
197 * a page we already have locked.
198 *
199 * We must do any isize and inode updates
200 * before we unlock the pages. Otherwise we
201 * could end up racing with unlink.
202 */
Chris Masonc8b97812008-10-29 14:49:59 -0400203 BTRFS_I(inode)->disk_i_size = inode->i_size;
204 btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000205
Chris Masonc8b97812008-10-29 14:49:59 -0400206 return 0;
207fail:
208 btrfs_free_path(path);
209 return err;
210}
211
212
213/*
214 * conditionally insert an inline extent into the file. This
215 * does the checks required to make sure the data is small enough
216 * to fit as an inline extent.
217 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400218static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400219 struct btrfs_root *root,
220 struct inode *inode, u64 start, u64 end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000221 size_t compressed_size, int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400222 struct page **compressed_pages)
223{
224 u64 isize = i_size_read(inode);
225 u64 actual_end = min(end + 1, isize);
226 u64 inline_len = actual_end - start;
227 u64 aligned_end = (end + root->sectorsize - 1) &
228 ~((u64)root->sectorsize - 1);
229 u64 hint_byte;
230 u64 data_len = inline_len;
231 int ret;
232
233 if (compressed_size)
234 data_len = compressed_size;
235
236 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400237 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400238 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
239 (!compressed_size &&
240 (actual_end & (root->sectorsize - 1)) == 0) ||
241 end + 1 < isize ||
242 data_len > root->fs_info->max_inline) {
243 return 1;
244 }
245
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000246 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400247 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400248 BUG_ON(ret);
249
250 if (isize > actual_end)
251 inline_len = min_t(u64, isize, actual_end);
252 ret = insert_inline_extent(trans, root, inode, start,
253 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000254 compress_type, compressed_pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400255 BUG_ON(ret);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400256 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400257 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400258 return 0;
259}
260
Chris Mason771ed682008-11-06 22:02:51 -0500261struct async_extent {
262 u64 start;
263 u64 ram_size;
264 u64 compressed_size;
265 struct page **pages;
266 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800267 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500268 struct list_head list;
269};
270
271struct async_cow {
272 struct inode *inode;
273 struct btrfs_root *root;
274 struct page *locked_page;
275 u64 start;
276 u64 end;
277 struct list_head extents;
278 struct btrfs_work work;
279};
280
281static noinline int add_async_extent(struct async_cow *cow,
282 u64 start, u64 ram_size,
283 u64 compressed_size,
284 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800285 unsigned long nr_pages,
286 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500287{
288 struct async_extent *async_extent;
289
290 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Yoshinori Sanodac97e52011-02-15 12:01:42 +0000291 BUG_ON(!async_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500292 async_extent->start = start;
293 async_extent->ram_size = ram_size;
294 async_extent->compressed_size = compressed_size;
295 async_extent->pages = pages;
296 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800297 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500298 list_add_tail(&async_extent->list, &cow->extents);
299 return 0;
300}
301
Chris Masonc8b97812008-10-29 14:49:59 -0400302/*
Chris Mason771ed682008-11-06 22:02:51 -0500303 * we create compressed extents in two phases. The first
304 * phase compresses a range of pages that have already been
305 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400306 *
Chris Mason771ed682008-11-06 22:02:51 -0500307 * This is done inside an ordered work queue, and the compression
308 * is spread across many cpus. The actual IO submission is step
309 * two, and the ordered work queue takes care of making sure that
310 * happens in the same order things were put onto the queue by
311 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400312 *
Chris Mason771ed682008-11-06 22:02:51 -0500313 * If this code finds it can't get good compression, it puts an
314 * entry onto the work queue to write the uncompressed bytes. This
315 * makes sure that both compressed inodes and uncompressed inodes
316 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400317 */
Chris Mason771ed682008-11-06 22:02:51 -0500318static noinline int compress_file_range(struct inode *inode,
319 struct page *locked_page,
320 u64 start, u64 end,
321 struct async_cow *async_cow,
322 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400323{
324 struct btrfs_root *root = BTRFS_I(inode)->root;
325 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400326 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400327 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400328 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500329 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400330 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400331 struct page **pages = NULL;
332 unsigned long nr_pages;
333 unsigned long nr_pages_ret = 0;
334 unsigned long total_compressed = 0;
335 unsigned long total_in = 0;
336 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500337 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400338 int i;
339 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800340 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400341
Chris Mason42dc7ba2008-12-15 11:44:56 -0500342 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400343again:
344 will_compress = 0;
345 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
346 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
347
Chris Masonf03d9301f2009-02-04 09:31:06 -0500348 /*
349 * we don't want to send crud past the end of i_size through
350 * compression, that's just a waste of CPU time. So, if the
351 * end of the file is before the start of our current
352 * requested range of bytes, we bail out to the uncompressed
353 * cleanup code that can deal with all of this.
354 *
355 * It isn't really the fastest way to fix things, but this is a
356 * very uncommon corner.
357 */
358 if (actual_end <= start)
359 goto cleanup_and_bail_uncompressed;
360
Chris Masonc8b97812008-10-29 14:49:59 -0400361 total_compressed = actual_end - start;
362
363 /* we want to make sure that amount of ram required to uncompress
364 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500365 * of a compressed extent to 128k. This is a crucial number
366 * because it also controls how easily we can spread reads across
367 * cpus for decompression.
368 *
369 * We also want to make sure the amount of IO required to do
370 * a random read is reasonably small, so we limit the size of
371 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400372 */
373 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400374 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500375 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400376 total_in = 0;
377 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400378
Chris Mason771ed682008-11-06 22:02:51 -0500379 /*
380 * we do compression for mount -o compress and when the
381 * inode has not been flagged as nocompress. This flag can
382 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400383 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200384 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500385 (btrfs_test_opt(root, COMPRESS) ||
Liu Bo75e7cb72011-03-22 10:12:20 +0000386 (BTRFS_I(inode)->force_compress) ||
387 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400388 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400389 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Yoshinori Sanodac97e52011-02-15 12:01:42 +0000390 BUG_ON(!pages);
Chris Mason179e29e2007-11-01 11:28:41 -0400391
Li Zefan261507a02010-12-17 14:21:50 +0800392 if (BTRFS_I(inode)->force_compress)
393 compress_type = BTRFS_I(inode)->force_compress;
394
395 ret = btrfs_compress_pages(compress_type,
396 inode->i_mapping, start,
397 total_compressed, pages,
398 nr_pages, &nr_pages_ret,
399 &total_in,
400 &total_compressed,
401 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400402
403 if (!ret) {
404 unsigned long offset = total_compressed &
405 (PAGE_CACHE_SIZE - 1);
406 struct page *page = pages[nr_pages_ret - 1];
407 char *kaddr;
408
409 /* zero the tail end of the last page, we might be
410 * sending it down to disk
411 */
412 if (offset) {
413 kaddr = kmap_atomic(page, KM_USER0);
414 memset(kaddr + offset, 0,
415 PAGE_CACHE_SIZE - offset);
416 kunmap_atomic(kaddr, KM_USER0);
417 }
418 will_compress = 1;
419 }
420 }
421 if (start == 0) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400422 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000423 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400424 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500425
Chris Masonc8b97812008-10-29 14:49:59 -0400426 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500427 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400428 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500429 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400430 */
431 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000432 start, end, 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400433 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500434 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400435 ret = cow_file_range_inline(trans, root, inode,
436 start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000437 total_compressed,
438 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400439 }
440 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500441 /*
442 * inline extent creation worked, we don't need
443 * to create any more async work items. Unlock
444 * and free up our temp pages.
445 */
Chris Masonc8b97812008-10-29 14:49:59 -0400446 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400447 &BTRFS_I(inode)->io_tree,
448 start, end, NULL,
449 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400450 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400451 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000452
453 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400454 goto free_pages_out;
455 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000456 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400457 }
458
459 if (will_compress) {
460 /*
461 * we aren't doing an inline extent round the compressed size
462 * up to a block size boundary so the allocator does sane
463 * things
464 */
465 total_compressed = (total_compressed + blocksize - 1) &
466 ~(blocksize - 1);
467
468 /*
469 * one last check to make sure the compression is really a
470 * win, compare the page count read with the blocks on disk
471 */
472 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
473 ~(PAGE_CACHE_SIZE - 1);
474 if (total_compressed >= total_in) {
475 will_compress = 0;
476 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400477 num_bytes = total_in;
478 }
479 }
480 if (!will_compress && pages) {
481 /*
482 * the compression code ran but failed to make things smaller,
483 * free any pages it allocated and our page pointer array
484 */
485 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400486 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400487 page_cache_release(pages[i]);
488 }
489 kfree(pages);
490 pages = NULL;
491 total_compressed = 0;
492 nr_pages_ret = 0;
493
494 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500495 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
496 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500497 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500498 }
Chris Masonc8b97812008-10-29 14:49:59 -0400499 }
Chris Mason771ed682008-11-06 22:02:51 -0500500 if (will_compress) {
501 *num_added += 1;
502
503 /* the async work queues will take care of doing actual
504 * allocation on disk for these compressed pages,
505 * and will submit them to the elevator.
506 */
507 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800508 total_compressed, pages, nr_pages_ret,
509 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500510
Yan, Zheng24ae6362010-12-06 07:02:36 +0000511 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500512 start += num_bytes;
513 pages = NULL;
514 cond_resched();
515 goto again;
516 }
517 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500518cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500519 /*
520 * No compression, but we still need to write the pages in
521 * the file we've been given so far. redirty the locked
522 * page if it corresponds to our extent and set things up
523 * for the async work queue to run cow_file_range to do
524 * the normal delalloc dance
525 */
526 if (page_offset(locked_page) >= start &&
527 page_offset(locked_page) <= end) {
528 __set_page_dirty_nobuffers(locked_page);
529 /* unlocked later on in the async handlers */
530 }
Li Zefan261507a02010-12-17 14:21:50 +0800531 add_async_extent(async_cow, start, end - start + 1,
532 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500533 *num_added += 1;
534 }
535
536out:
537 return 0;
538
539free_pages_out:
540 for (i = 0; i < nr_pages_ret; i++) {
541 WARN_ON(pages[i]->mapping);
542 page_cache_release(pages[i]);
543 }
Chris Masond3977122009-01-05 21:25:51 -0500544 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500545
546 goto out;
547}
548
549/*
550 * phase two of compressed writeback. This is the ordered portion
551 * of the code, which only gets called in the order the work was
552 * queued. We walk all the async extents created by compress_file_range
553 * and send them down to the disk.
554 */
555static noinline int submit_compressed_extents(struct inode *inode,
556 struct async_cow *async_cow)
557{
558 struct async_extent *async_extent;
559 u64 alloc_hint = 0;
560 struct btrfs_trans_handle *trans;
561 struct btrfs_key ins;
562 struct extent_map *em;
563 struct btrfs_root *root = BTRFS_I(inode)->root;
564 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
565 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500566 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500567
568 if (list_empty(&async_cow->extents))
569 return 0;
570
Chris Mason771ed682008-11-06 22:02:51 -0500571
Chris Masond3977122009-01-05 21:25:51 -0500572 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500573 async_extent = list_entry(async_cow->extents.next,
574 struct async_extent, list);
575 list_del(&async_extent->list);
576
577 io_tree = &BTRFS_I(inode)->io_tree;
578
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500579retry:
Chris Mason771ed682008-11-06 22:02:51 -0500580 /* did the compression code fall back to uncompressed IO? */
581 if (!async_extent->pages) {
582 int page_started = 0;
583 unsigned long nr_written = 0;
584
585 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000586 async_extent->start +
587 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500588
589 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500590 ret = cow_file_range(inode, async_cow->locked_page,
591 async_extent->start,
592 async_extent->start +
593 async_extent->ram_size - 1,
594 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500595
596 /*
597 * if page_started, cow_file_range inserted an
598 * inline extent and took care of all the unlocking
599 * and IO for us. Otherwise, we need to submit
600 * all those pages down to the drive.
601 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500602 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500603 extent_write_locked_range(io_tree,
604 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500605 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500606 async_extent->ram_size - 1,
607 btrfs_get_extent,
608 WB_SYNC_ALL);
609 kfree(async_extent);
610 cond_resched();
611 continue;
612 }
613
614 lock_extent(io_tree, async_extent->start,
615 async_extent->start + async_extent->ram_size - 1,
616 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500617
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400618 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000619 BUG_ON(IS_ERR(trans));
Josef Bacik74b21072011-04-13 12:02:53 -0400620 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500621 ret = btrfs_reserve_extent(trans, root,
622 async_extent->compressed_size,
623 async_extent->compressed_size,
624 0, alloc_hint,
625 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000626 btrfs_end_transaction(trans, root);
627
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500628 if (ret) {
629 int i;
630 for (i = 0; i < async_extent->nr_pages; i++) {
631 WARN_ON(async_extent->pages[i]->mapping);
632 page_cache_release(async_extent->pages[i]);
633 }
634 kfree(async_extent->pages);
635 async_extent->nr_pages = 0;
636 async_extent->pages = NULL;
637 unlock_extent(io_tree, async_extent->start,
638 async_extent->start +
639 async_extent->ram_size - 1, GFP_NOFS);
640 goto retry;
641 }
642
Yan, Zhengc2167752009-11-12 09:34:21 +0000643 /*
644 * here we're doing allocation and writeback of the
645 * compressed pages
646 */
647 btrfs_drop_extent_cache(inode, async_extent->start,
648 async_extent->start +
649 async_extent->ram_size - 1, 0);
650
Chris Mason771ed682008-11-06 22:02:51 -0500651 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000652 BUG_ON(!em);
Chris Mason771ed682008-11-06 22:02:51 -0500653 em->start = async_extent->start;
654 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500655 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500656
657 em->block_start = ins.objectid;
658 em->block_len = ins.offset;
659 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800660 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500661 set_bit(EXTENT_FLAG_PINNED, &em->flags);
662 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
663
Chris Masond3977122009-01-05 21:25:51 -0500664 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400665 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500666 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400667 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500668 if (ret != -EEXIST) {
669 free_extent_map(em);
670 break;
671 }
672 btrfs_drop_extent_cache(inode, async_extent->start,
673 async_extent->start +
674 async_extent->ram_size - 1, 0);
675 }
676
Li Zefan261507a02010-12-17 14:21:50 +0800677 ret = btrfs_add_ordered_extent_compress(inode,
678 async_extent->start,
679 ins.objectid,
680 async_extent->ram_size,
681 ins.offset,
682 BTRFS_ORDERED_COMPRESSED,
683 async_extent->compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500684 BUG_ON(ret);
685
Chris Mason771ed682008-11-06 22:02:51 -0500686 /*
687 * clear dirty, set writeback and unlock the pages.
688 */
689 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400690 &BTRFS_I(inode)->io_tree,
691 async_extent->start,
692 async_extent->start +
693 async_extent->ram_size - 1,
694 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
695 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400696 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400697 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500698
699 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500700 async_extent->start,
701 async_extent->ram_size,
702 ins.objectid,
703 ins.offset, async_extent->pages,
704 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500705
706 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500707 alloc_hint = ins.objectid + ins.offset;
708 kfree(async_extent);
709 cond_resched();
710 }
711
Chris Mason771ed682008-11-06 22:02:51 -0500712 return 0;
713}
714
Josef Bacik4b46fce2010-05-23 11:00:55 -0400715static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
716 u64 num_bytes)
717{
718 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
719 struct extent_map *em;
720 u64 alloc_hint = 0;
721
722 read_lock(&em_tree->lock);
723 em = search_extent_mapping(em_tree, start, num_bytes);
724 if (em) {
725 /*
726 * if block start isn't an actual block number then find the
727 * first block in this inode and use that as a hint. If that
728 * block is also bogus then just don't worry about it.
729 */
730 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
731 free_extent_map(em);
732 em = search_extent_mapping(em_tree, 0, 0);
733 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
734 alloc_hint = em->block_start;
735 if (em)
736 free_extent_map(em);
737 } else {
738 alloc_hint = em->block_start;
739 free_extent_map(em);
740 }
741 }
742 read_unlock(&em_tree->lock);
743
744 return alloc_hint;
745}
746
Chris Mason771ed682008-11-06 22:02:51 -0500747/*
748 * when extent_io.c finds a delayed allocation range in the file,
749 * the call backs end up in this code. The basic idea is to
750 * allocate extents on disk for the range, and create ordered data structs
751 * in ram to track those extents.
752 *
753 * locked_page is the page that writepage had locked already. We use
754 * it to make sure we don't do extra locks or unlocks.
755 *
756 * *page_started is set to one if we unlock locked_page and do everything
757 * required to start IO on it. It may be clean and already done with
758 * IO when we return.
759 */
760static noinline int cow_file_range(struct inode *inode,
761 struct page *locked_page,
762 u64 start, u64 end, int *page_started,
763 unsigned long *nr_written,
764 int unlock)
765{
766 struct btrfs_root *root = BTRFS_I(inode)->root;
767 struct btrfs_trans_handle *trans;
768 u64 alloc_hint = 0;
769 u64 num_bytes;
770 unsigned long ram_size;
771 u64 disk_num_bytes;
772 u64 cur_alloc_size;
773 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500774 struct btrfs_key ins;
775 struct extent_map *em;
776 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
777 int ret = 0;
778
Josef Bacik0cb59c92010-07-02 12:14:14 -0400779 BUG_ON(root == root->fs_info->tree_root);
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400780 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000781 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400782 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500783
Chris Mason771ed682008-11-06 22:02:51 -0500784 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
785 num_bytes = max(blocksize, num_bytes);
786 disk_num_bytes = num_bytes;
787 ret = 0;
788
789 if (start == 0) {
790 /* lets try to make an inline extent */
791 ret = cow_file_range_inline(trans, root, inode,
Li Zefanfe3f5662011-03-28 08:30:38 +0000792 start, end, 0, 0, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500793 if (ret == 0) {
794 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400795 &BTRFS_I(inode)->io_tree,
796 start, end, NULL,
797 EXTENT_CLEAR_UNLOCK_PAGE |
798 EXTENT_CLEAR_UNLOCK |
799 EXTENT_CLEAR_DELALLOC |
800 EXTENT_CLEAR_DIRTY |
801 EXTENT_SET_WRITEBACK |
802 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000803
Chris Mason771ed682008-11-06 22:02:51 -0500804 *nr_written = *nr_written +
805 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
806 *page_started = 1;
807 ret = 0;
808 goto out;
809 }
810 }
Chris Masonc8b97812008-10-29 14:49:59 -0400811
812 BUG_ON(disk_num_bytes >
813 btrfs_super_total_bytes(&root->fs_info->super_copy));
814
Josef Bacik4b46fce2010-05-23 11:00:55 -0400815 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400816 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400817
Chris Masond3977122009-01-05 21:25:51 -0500818 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400819 unsigned long op;
820
Josef Bacik287a0ab2010-03-19 18:07:23 +0000821 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400822 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500823 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400824 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500825 BUG_ON(ret);
826
Chris Masone6dcd2d2008-07-17 12:53:50 -0400827 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000828 BUG_ON(!em);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400829 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500830 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500831 ram_size = ins.offset;
832 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400833
Chris Masone6dcd2d2008-07-17 12:53:50 -0400834 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400835 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400836 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400837 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400838
Chris Masond3977122009-01-05 21:25:51 -0500839 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400840 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400841 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400842 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400843 if (ret != -EEXIST) {
844 free_extent_map(em);
845 break;
846 }
847 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400848 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400849 }
850
Chris Mason98d20f62008-04-14 09:46:10 -0400851 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400852 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500853 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400854 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400855
Yan Zheng17d217f2008-12-12 10:03:38 -0500856 if (root->root_key.objectid ==
857 BTRFS_DATA_RELOC_TREE_OBJECTID) {
858 ret = btrfs_reloc_clone_csums(inode, start,
859 cur_alloc_size);
860 BUG_ON(ret);
861 }
862
Chris Masond3977122009-01-05 21:25:51 -0500863 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400864 break;
Chris Masond3977122009-01-05 21:25:51 -0500865
Chris Masonc8b97812008-10-29 14:49:59 -0400866 /* we're not doing compressed IO, don't unlock the first
867 * page (which the caller expects to stay locked), don't
868 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400869 *
870 * Do set the Private2 bit so we know this page was properly
871 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400872 */
Chris Masona791e352009-10-08 11:27:10 -0400873 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
874 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
875 EXTENT_SET_PRIVATE2;
876
Chris Masonc8b97812008-10-29 14:49:59 -0400877 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
878 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400879 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400880 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500881 num_bytes -= cur_alloc_size;
882 alloc_hint = ins.objectid + ins.offset;
883 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400884 }
Chris Masonb888db22007-08-27 16:49:44 -0400885out:
Chris Mason771ed682008-11-06 22:02:51 -0500886 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400887 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400888
Chris Masonbe20aa92007-12-17 20:14:01 -0500889 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500890}
Chris Masonc8b97812008-10-29 14:49:59 -0400891
Chris Mason771ed682008-11-06 22:02:51 -0500892/*
893 * work queue call back to started compression on a file and pages
894 */
895static noinline void async_cow_start(struct btrfs_work *work)
896{
897 struct async_cow *async_cow;
898 int num_added = 0;
899 async_cow = container_of(work, struct async_cow, work);
900
901 compress_file_range(async_cow->inode, async_cow->locked_page,
902 async_cow->start, async_cow->end, async_cow,
903 &num_added);
904 if (num_added == 0)
905 async_cow->inode = NULL;
906}
907
908/*
909 * work queue call back to submit previously compressed pages
910 */
911static noinline void async_cow_submit(struct btrfs_work *work)
912{
913 struct async_cow *async_cow;
914 struct btrfs_root *root;
915 unsigned long nr_pages;
916
917 async_cow = container_of(work, struct async_cow, work);
918
919 root = async_cow->root;
920 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
921 PAGE_CACHE_SHIFT;
922
923 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
924
925 if (atomic_read(&root->fs_info->async_delalloc_pages) <
926 5 * 1042 * 1024 &&
927 waitqueue_active(&root->fs_info->async_submit_wait))
928 wake_up(&root->fs_info->async_submit_wait);
929
Chris Masond3977122009-01-05 21:25:51 -0500930 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500931 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500932}
Chris Masonc8b97812008-10-29 14:49:59 -0400933
Chris Mason771ed682008-11-06 22:02:51 -0500934static noinline void async_cow_free(struct btrfs_work *work)
935{
936 struct async_cow *async_cow;
937 async_cow = container_of(work, struct async_cow, work);
938 kfree(async_cow);
939}
940
941static int cow_file_range_async(struct inode *inode, struct page *locked_page,
942 u64 start, u64 end, int *page_started,
943 unsigned long *nr_written)
944{
945 struct async_cow *async_cow;
946 struct btrfs_root *root = BTRFS_I(inode)->root;
947 unsigned long nr_pages;
948 u64 cur_end;
949 int limit = 10 * 1024 * 1042;
950
Chris Masona3429ab2009-10-08 12:30:20 -0400951 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
952 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500953 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500954 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -0400955 BUG_ON(!async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500956 async_cow->inode = inode;
957 async_cow->root = root;
958 async_cow->locked_page = locked_page;
959 async_cow->start = start;
960
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200961 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500962 cur_end = end;
963 else
964 cur_end = min(end, start + 512 * 1024 - 1);
965
966 async_cow->end = cur_end;
967 INIT_LIST_HEAD(&async_cow->extents);
968
969 async_cow->work.func = async_cow_start;
970 async_cow->work.ordered_func = async_cow_submit;
971 async_cow->work.ordered_free = async_cow_free;
972 async_cow->work.flags = 0;
973
Chris Mason771ed682008-11-06 22:02:51 -0500974 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
975 PAGE_CACHE_SHIFT;
976 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
977
978 btrfs_queue_worker(&root->fs_info->delalloc_workers,
979 &async_cow->work);
980
981 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
982 wait_event(root->fs_info->async_submit_wait,
983 (atomic_read(&root->fs_info->async_delalloc_pages) <
984 limit));
985 }
986
Chris Masond3977122009-01-05 21:25:51 -0500987 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500988 atomic_read(&root->fs_info->async_delalloc_pages)) {
989 wait_event(root->fs_info->async_submit_wait,
990 (atomic_read(&root->fs_info->async_delalloc_pages) ==
991 0));
992 }
993
994 *nr_written += nr_pages;
995 start = cur_end + 1;
996 }
997 *page_started = 1;
998 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500999}
1000
Chris Masond3977122009-01-05 21:25:51 -05001001static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001002 u64 bytenr, u64 num_bytes)
1003{
1004 int ret;
1005 struct btrfs_ordered_sum *sums;
1006 LIST_HEAD(list);
1007
Yan Zheng07d400a2009-01-06 11:42:00 -05001008 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1009 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -05001010 if (ret == 0 && list_empty(&list))
1011 return 0;
1012
1013 while (!list_empty(&list)) {
1014 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1015 list_del(&sums->list);
1016 kfree(sums);
1017 }
1018 return 1;
1019}
1020
Chris Masond352ac62008-09-29 15:18:18 -04001021/*
1022 * when nowcow writeback call back. This checks for snapshots or COW copies
1023 * of the extents that exist in the file, and COWs the file as required.
1024 *
1025 * If no cow copies or snapshots exist, we write directly to the existing
1026 * blocks on disk
1027 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001028static noinline int run_delalloc_nocow(struct inode *inode,
1029 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001030 u64 start, u64 end, int *page_started, int force,
1031 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001032{
Chris Masonbe20aa92007-12-17 20:14:01 -05001033 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001034 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001035 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001036 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001037 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001038 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001039 u64 cow_start;
1040 u64 cur_offset;
1041 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001042 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001043 u64 disk_bytenr;
1044 u64 num_bytes;
1045 int extent_type;
1046 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001047 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001048 int nocow;
1049 int check_prev = 1;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001050 bool nolock = false;
Chris Masonbe20aa92007-12-17 20:14:01 -05001051
1052 path = btrfs_alloc_path();
1053 BUG_ON(!path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001054 if (root == root->fs_info->tree_root) {
1055 nolock = true;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001056 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001057 } else {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001058 trans = btrfs_join_transaction(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001059 }
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001060 BUG_ON(IS_ERR(trans));
Josef Bacik74b21072011-04-13 12:02:53 -04001061 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001062
Yan Zheng80ff3852008-10-30 14:20:02 -04001063 cow_start = (u64)-1;
1064 cur_offset = start;
1065 while (1) {
1066 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1067 cur_offset, 0);
1068 BUG_ON(ret < 0);
1069 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1070 leaf = path->nodes[0];
1071 btrfs_item_key_to_cpu(leaf, &found_key,
1072 path->slots[0] - 1);
1073 if (found_key.objectid == inode->i_ino &&
1074 found_key.type == BTRFS_EXTENT_DATA_KEY)
1075 path->slots[0]--;
1076 }
1077 check_prev = 0;
1078next_slot:
1079 leaf = path->nodes[0];
1080 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1081 ret = btrfs_next_leaf(root, path);
1082 if (ret < 0)
1083 BUG_ON(1);
1084 if (ret > 0)
1085 break;
1086 leaf = path->nodes[0];
1087 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001088
Yan Zheng80ff3852008-10-30 14:20:02 -04001089 nocow = 0;
1090 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001091 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001092 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001093
Yan Zheng80ff3852008-10-30 14:20:02 -04001094 if (found_key.objectid > inode->i_ino ||
1095 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1096 found_key.offset > end)
1097 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001098
Yan Zheng80ff3852008-10-30 14:20:02 -04001099 if (found_key.offset > cur_offset) {
1100 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001101 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001102 goto out_check;
1103 }
Chris Masonc31f8832008-01-08 15:46:31 -05001104
Yan Zheng80ff3852008-10-30 14:20:02 -04001105 fi = btrfs_item_ptr(leaf, path->slots[0],
1106 struct btrfs_file_extent_item);
1107 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001108
Yan Zhengd899e052008-10-30 14:25:28 -04001109 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1110 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001111 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001112 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001113 extent_end = found_key.offset +
1114 btrfs_file_extent_num_bytes(leaf, fi);
1115 if (extent_end <= start) {
1116 path->slots[0]++;
1117 goto next_slot;
1118 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001119 if (disk_bytenr == 0)
1120 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001121 if (btrfs_file_extent_compression(leaf, fi) ||
1122 btrfs_file_extent_encryption(leaf, fi) ||
1123 btrfs_file_extent_other_encoding(leaf, fi))
1124 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001125 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1126 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001127 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001128 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001129 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001130 found_key.offset -
1131 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001132 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001133 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001134 disk_bytenr += cur_offset - found_key.offset;
1135 num_bytes = min(end + 1, extent_end) - cur_offset;
1136 /*
1137 * force cow if csum exists in the range.
1138 * this ensure that csum for a given extent are
1139 * either valid or do not exist.
1140 */
1141 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1142 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001143 nocow = 1;
1144 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1145 extent_end = found_key.offset +
1146 btrfs_file_extent_inline_len(leaf, fi);
1147 extent_end = ALIGN(extent_end, root->sectorsize);
1148 } else {
1149 BUG_ON(1);
1150 }
1151out_check:
1152 if (extent_end <= start) {
1153 path->slots[0]++;
1154 goto next_slot;
1155 }
1156 if (!nocow) {
1157 if (cow_start == (u64)-1)
1158 cow_start = cur_offset;
1159 cur_offset = extent_end;
1160 if (cur_offset > end)
1161 break;
1162 path->slots[0]++;
1163 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001164 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001165
Yan Zheng7ea394f2008-08-05 13:05:02 -04001166 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001167 if (cow_start != (u64)-1) {
1168 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001169 found_key.offset - 1, page_started,
1170 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001171 BUG_ON(ret);
1172 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001173 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001174
Yan Zhengd899e052008-10-30 14:25:28 -04001175 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1176 struct extent_map *em;
1177 struct extent_map_tree *em_tree;
1178 em_tree = &BTRFS_I(inode)->extent_tree;
1179 em = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +00001180 BUG_ON(!em);
Yan Zhengd899e052008-10-30 14:25:28 -04001181 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001182 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001183 em->len = num_bytes;
1184 em->block_len = num_bytes;
1185 em->block_start = disk_bytenr;
1186 em->bdev = root->fs_info->fs_devices->latest_bdev;
1187 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1188 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001189 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001190 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001191 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001192 if (ret != -EEXIST) {
1193 free_extent_map(em);
1194 break;
1195 }
1196 btrfs_drop_extent_cache(inode, em->start,
1197 em->start + em->len - 1, 0);
1198 }
1199 type = BTRFS_ORDERED_PREALLOC;
1200 } else {
1201 type = BTRFS_ORDERED_NOCOW;
1202 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001203
1204 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001205 num_bytes, num_bytes, type);
1206 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001207
Yan, Zhengefa56462010-05-16 10:49:59 -04001208 if (root->root_key.objectid ==
1209 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1210 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1211 num_bytes);
1212 BUG_ON(ret);
1213 }
1214
Yan Zhengd899e052008-10-30 14:25:28 -04001215 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001216 cur_offset, cur_offset + num_bytes - 1,
1217 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1218 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1219 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001220 cur_offset = extent_end;
1221 if (cur_offset > end)
1222 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001223 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001224 btrfs_release_path(root, path);
1225
1226 if (cur_offset <= end && cow_start == (u64)-1)
1227 cow_start = cur_offset;
1228 if (cow_start != (u64)-1) {
1229 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001230 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001231 BUG_ON(ret);
1232 }
1233
Josef Bacik0cb59c92010-07-02 12:14:14 -04001234 if (nolock) {
1235 ret = btrfs_end_transaction_nolock(trans, root);
1236 BUG_ON(ret);
1237 } else {
1238 ret = btrfs_end_transaction(trans, root);
1239 BUG_ON(ret);
1240 }
Yan Zheng7ea394f2008-08-05 13:05:02 -04001241 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001242 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001243}
1244
Chris Masond352ac62008-09-29 15:18:18 -04001245/*
1246 * extent_io.c call back to do delayed allocation processing
1247 */
Chris Masonc8b97812008-10-29 14:49:59 -04001248static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001249 u64 start, u64 end, int *page_started,
1250 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001251{
Chris Masonbe20aa92007-12-17 20:14:01 -05001252 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001253 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001254
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001255 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001256 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001257 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001258 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001259 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001260 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001261 else if (!btrfs_test_opt(root, COMPRESS) &&
Liu Bo75e7cb72011-03-22 10:12:20 +00001262 !(BTRFS_I(inode)->force_compress) &&
1263 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
Chris Mason7f366cf2009-03-12 20:12:45 -04001264 ret = cow_file_range(inode, locked_page, start, end,
1265 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001266 else
Chris Mason771ed682008-11-06 22:02:51 -05001267 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001268 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001269 return ret;
1270}
1271
Josef Bacik9ed74f22009-09-11 16:12:44 -04001272static int btrfs_split_extent_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001273 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001274{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001275 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001276 if (!(orig->state & EXTENT_DELALLOC))
1277 return 0;
1278
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001279 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001280 return 0;
1281}
1282
1283/*
1284 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1285 * extents so we can keep track of new extents that are just merged onto old
1286 * extents, such as when we are doing sequential writes, so we can properly
1287 * account for the metadata space we'll need.
1288 */
1289static int btrfs_merge_extent_hook(struct inode *inode,
1290 struct extent_state *new,
1291 struct extent_state *other)
1292{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001293 /* not delalloc, ignore it */
1294 if (!(other->state & EXTENT_DELALLOC))
1295 return 0;
1296
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001297 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001298 return 0;
1299}
1300
Chris Masond352ac62008-09-29 15:18:18 -04001301/*
1302 * extent_io.c set_bit_hook, used to track delayed allocation
1303 * bytes in this file, and to maintain the list of inodes that
1304 * have pending delalloc work to be done.
1305 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001306static int btrfs_set_bit_hook(struct inode *inode,
1307 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001308{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001309
Chris Mason75eff682008-12-15 15:54:40 -05001310 /*
1311 * set_bit and clear bit hooks normally require _irqsave/restore
1312 * but in this case, we are only testeing for the DELALLOC
1313 * bit, which is only set or cleared with irqs on
1314 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001315 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001316 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001317 u64 len = state->end + 1 - state->start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001318 int do_list = (root->root_key.objectid !=
1319 BTRFS_ROOT_TREE_OBJECTID);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001320
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001321 if (*bits & EXTENT_FIRST_DELALLOC)
1322 *bits &= ~EXTENT_FIRST_DELALLOC;
1323 else
1324 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001325
Chris Mason75eff682008-12-15 15:54:40 -05001326 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001327 BTRFS_I(inode)->delalloc_bytes += len;
1328 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001329 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001330 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1331 &root->fs_info->delalloc_inodes);
1332 }
Chris Mason75eff682008-12-15 15:54:40 -05001333 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001334 }
1335 return 0;
1336}
1337
Chris Masond352ac62008-09-29 15:18:18 -04001338/*
1339 * extent_io.c clear_bit_hook, see set_bit_hook for why
1340 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001341static int btrfs_clear_bit_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001342 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001343{
Chris Mason75eff682008-12-15 15:54:40 -05001344 /*
1345 * set_bit and clear bit hooks normally require _irqsave/restore
1346 * but in this case, we are only testeing for the DELALLOC
1347 * bit, which is only set or cleared with irqs on
1348 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001349 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001350 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001351 u64 len = state->end + 1 - state->start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001352 int do_list = (root->root_key.objectid !=
1353 BTRFS_ROOT_TREE_OBJECTID);
Chris Masonbcbfce82008-04-22 13:26:47 -04001354
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001355 if (*bits & EXTENT_FIRST_DELALLOC)
1356 *bits &= ~EXTENT_FIRST_DELALLOC;
1357 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1358 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1359
1360 if (*bits & EXTENT_DO_ACCOUNTING)
1361 btrfs_delalloc_release_metadata(inode, len);
1362
Josef Bacik0cb59c92010-07-02 12:14:14 -04001363 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1364 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001365 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001366
Chris Mason75eff682008-12-15 15:54:40 -05001367 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001368 root->fs_info->delalloc_bytes -= len;
1369 BTRFS_I(inode)->delalloc_bytes -= len;
1370
Josef Bacik0cb59c92010-07-02 12:14:14 -04001371 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001372 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1373 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1374 }
Chris Mason75eff682008-12-15 15:54:40 -05001375 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001376 }
1377 return 0;
1378}
1379
Chris Masond352ac62008-09-29 15:18:18 -04001380/*
1381 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1382 * we don't create bios that span stripes or chunks
1383 */
Chris Mason239b14b2008-03-24 15:02:07 -04001384int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001385 size_t size, struct bio *bio,
1386 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001387{
1388 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1389 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001390 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001391 u64 length = 0;
1392 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001393 int ret;
1394
Chris Mason771ed682008-11-06 22:02:51 -05001395 if (bio_flags & EXTENT_BIO_COMPRESSED)
1396 return 0;
1397
Chris Masonf2d8d742008-04-21 10:03:05 -04001398 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001399 map_tree = &root->fs_info->mapping_tree;
1400 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001401 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001402 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001403
Chris Masond3977122009-01-05 21:25:51 -05001404 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001405 return 1;
Andi Kleen411fc6b2010-10-29 15:14:31 -04001406 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04001407}
1408
Chris Masond352ac62008-09-29 15:18:18 -04001409/*
1410 * in order to insert checksums into the metadata in large chunks,
1411 * we wait until bio submission time. All the pages in the bio are
1412 * checksummed and sums are attached onto the ordered extent record.
1413 *
1414 * At IO completion time the cums attached on the ordered extent record
1415 * are inserted into the btree
1416 */
Chris Masond3977122009-01-05 21:25:51 -05001417static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1418 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001419 unsigned long bio_flags,
1420 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001421{
Chris Mason065631f2008-02-20 12:07:25 -05001422 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001423 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001424
Chris Masond20f7042008-12-08 16:58:54 -05001425 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001426 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001427 return 0;
1428}
Chris Masone0156402008-04-16 11:15:20 -04001429
Chris Mason4a69a412008-11-06 22:03:00 -05001430/*
1431 * in order to insert checksums into the metadata in large chunks,
1432 * we wait until bio submission time. All the pages in the bio are
1433 * checksummed and sums are attached onto the ordered extent record.
1434 *
1435 * At IO completion time the cums attached on the ordered extent record
1436 * are inserted into the btree
1437 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001438static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001439 int mirror_num, unsigned long bio_flags,
1440 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001441{
1442 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001443 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001444}
1445
Chris Masond352ac62008-09-29 15:18:18 -04001446/*
Chris Masoncad321a2008-12-17 14:51:42 -05001447 * extent_io.c submission hook. This does the right thing for csum calculation
1448 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001449 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001450static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001451 int mirror_num, unsigned long bio_flags,
1452 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001453{
1454 struct btrfs_root *root = BTRFS_I(inode)->root;
1455 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001456 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001457
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001458 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001459
Josef Bacik0cb59c92010-07-02 12:14:14 -04001460 if (root == root->fs_info->tree_root)
1461 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1462 else
1463 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001464 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001465
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001466 if (!(rw & REQ_WRITE)) {
Chris Masond20f7042008-12-08 16:58:54 -05001467 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001468 return btrfs_submit_compressed_read(inode, bio,
1469 mirror_num, bio_flags);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001470 } else if (!skip_sum) {
1471 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1472 if (ret)
1473 return ret;
1474 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001475 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001476 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001477 /* csum items have already been cloned */
1478 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1479 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001480 /* we're doing a write, do the async checksumming */
1481 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001482 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001483 bio_flags, bio_offset,
1484 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001485 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001486 }
1487
Chris Mason0b86a832008-03-24 15:01:56 -04001488mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001489 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001490}
Chris Mason6885f302008-02-20 16:11:05 -05001491
Chris Masond352ac62008-09-29 15:18:18 -04001492/*
1493 * given a list of ordered sums record them in the inode. This happens
1494 * at IO completion time based on sums calculated at bio submission time.
1495 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001496static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001497 struct inode *inode, u64 file_offset,
1498 struct list_head *list)
1499{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001500 struct btrfs_ordered_sum *sum;
1501
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001502 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001503 btrfs_csum_file_blocks(trans,
1504 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001505 }
1506 return 0;
1507}
1508
Josef Bacik2ac55d42010-02-03 19:33:23 +00001509int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1510 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001511{
Chris Masond3977122009-01-05 21:25:51 -05001512 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001513 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001514 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001515 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001516}
1517
Chris Masond352ac62008-09-29 15:18:18 -04001518/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001519struct btrfs_writepage_fixup {
1520 struct page *page;
1521 struct btrfs_work work;
1522};
1523
Christoph Hellwigb2950862008-12-02 09:54:17 -05001524static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001525{
1526 struct btrfs_writepage_fixup *fixup;
1527 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001528 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001529 struct page *page;
1530 struct inode *inode;
1531 u64 page_start;
1532 u64 page_end;
1533
1534 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1535 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001536again:
Chris Mason247e7432008-07-17 12:53:51 -04001537 lock_page(page);
1538 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1539 ClearPageChecked(page);
1540 goto out_page;
1541 }
1542
1543 inode = page->mapping->host;
1544 page_start = page_offset(page);
1545 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1546
Josef Bacik2ac55d42010-02-03 19:33:23 +00001547 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1548 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001549
1550 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001551 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001552 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001553
1554 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1555 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001556 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1557 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001558 unlock_page(page);
1559 btrfs_start_ordered_extent(inode, ordered, 1);
1560 goto again;
1561 }
Chris Mason247e7432008-07-17 12:53:51 -04001562
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001563 BUG();
Josef Bacik2ac55d42010-02-03 19:33:23 +00001564 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001565 ClearPageChecked(page);
1566out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001567 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1568 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001569out_page:
1570 unlock_page(page);
1571 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001572 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001573}
1574
1575/*
1576 * There are a few paths in the higher layers of the kernel that directly
1577 * set the page dirty bit without asking the filesystem if it is a
1578 * good idea. This causes problems because we want to make sure COW
1579 * properly happens and the data=ordered rules are followed.
1580 *
Chris Masonc8b97812008-10-29 14:49:59 -04001581 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001582 * hasn't been properly setup for IO. We kick off an async process
1583 * to fix it up. The async helper will wait for ordered extents, set
1584 * the delalloc bit and make it safe to write the page.
1585 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001586static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001587{
1588 struct inode *inode = page->mapping->host;
1589 struct btrfs_writepage_fixup *fixup;
1590 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001591
Chris Mason8b62b722009-09-02 16:53:46 -04001592 /* this page is properly in the ordered list */
1593 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001594 return 0;
1595
1596 if (PageChecked(page))
1597 return -EAGAIN;
1598
1599 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1600 if (!fixup)
1601 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001602
Chris Mason247e7432008-07-17 12:53:51 -04001603 SetPageChecked(page);
1604 page_cache_get(page);
1605 fixup->work.func = btrfs_writepage_fixup_worker;
1606 fixup->page = page;
1607 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1608 return -EAGAIN;
1609}
1610
Yan Zhengd899e052008-10-30 14:25:28 -04001611static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1612 struct inode *inode, u64 file_pos,
1613 u64 disk_bytenr, u64 disk_num_bytes,
1614 u64 num_bytes, u64 ram_bytes,
1615 u8 compression, u8 encryption,
1616 u16 other_encoding, int extent_type)
1617{
1618 struct btrfs_root *root = BTRFS_I(inode)->root;
1619 struct btrfs_file_extent_item *fi;
1620 struct btrfs_path *path;
1621 struct extent_buffer *leaf;
1622 struct btrfs_key ins;
1623 u64 hint;
1624 int ret;
1625
1626 path = btrfs_alloc_path();
1627 BUG_ON(!path);
1628
Chris Masonb9473432009-03-13 11:00:37 -04001629 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001630
1631 /*
1632 * we may be replacing one extent in the tree with another.
1633 * The new extent is pinned in the extent map, and we don't want
1634 * to drop it from the cache until it is completely in the btree.
1635 *
1636 * So, tell btrfs_drop_extents to leave this extent in the cache.
1637 * the caller is expected to unpin it and allow it to be merged
1638 * with the others.
1639 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001640 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1641 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001642 BUG_ON(ret);
1643
1644 ins.objectid = inode->i_ino;
1645 ins.offset = file_pos;
1646 ins.type = BTRFS_EXTENT_DATA_KEY;
1647 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1648 BUG_ON(ret);
1649 leaf = path->nodes[0];
1650 fi = btrfs_item_ptr(leaf, path->slots[0],
1651 struct btrfs_file_extent_item);
1652 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1653 btrfs_set_file_extent_type(leaf, fi, extent_type);
1654 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1655 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1656 btrfs_set_file_extent_offset(leaf, fi, 0);
1657 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1658 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1659 btrfs_set_file_extent_compression(leaf, fi, compression);
1660 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1661 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001662
1663 btrfs_unlock_up_safe(path, 1);
1664 btrfs_set_lock_blocking(leaf);
1665
Yan Zhengd899e052008-10-30 14:25:28 -04001666 btrfs_mark_buffer_dirty(leaf);
1667
1668 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001669
1670 ins.objectid = disk_bytenr;
1671 ins.offset = disk_num_bytes;
1672 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001673 ret = btrfs_alloc_reserved_file_extent(trans, root,
1674 root->root_key.objectid,
1675 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001676 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001677 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001678
Yan Zhengd899e052008-10-30 14:25:28 -04001679 return 0;
1680}
1681
Chris Mason5d13a982009-03-13 11:41:46 -04001682/*
1683 * helper function for btrfs_finish_ordered_io, this
1684 * just reads in some of the csum leaves to prime them into ram
1685 * before we start the transaction. It limits the amount of btree
1686 * reads required while inside the transaction.
1687 */
Chris Masond352ac62008-09-29 15:18:18 -04001688/* as ordered data IO finishes, this gets called so we can finish
1689 * an ordered extent if the range of bytes in the file it covers are
1690 * fully written.
1691 */
Chris Mason211f90e2008-07-18 11:56:15 -04001692static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001693{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001694 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001695 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001696 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001697 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001698 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001699 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001700 int ret;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001701 bool nolock = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001702
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001703 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1704 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001705 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001706 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001707 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001708
Josef Bacik0cb59c92010-07-02 12:14:14 -04001709 nolock = (root == root->fs_info->tree_root);
1710
Yan, Zhengc2167752009-11-12 09:34:21 +00001711 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1712 BUG_ON(!list_empty(&ordered_extent->list));
1713 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1714 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001715 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001716 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001717 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001718 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001719 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001720 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001721 ret = btrfs_update_inode(trans, root, inode);
1722 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001723 }
1724 goto out;
1725 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001726
Josef Bacik2ac55d42010-02-03 19:33:23 +00001727 lock_extent_bits(io_tree, ordered_extent->file_offset,
1728 ordered_extent->file_offset + ordered_extent->len - 1,
1729 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001730
Josef Bacik0cb59c92010-07-02 12:14:14 -04001731 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001732 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001733 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001734 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001735 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001736 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001737
Chris Masonc8b97812008-10-29 14:49:59 -04001738 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001739 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001740 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001741 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001742 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001743 ordered_extent->file_offset,
1744 ordered_extent->file_offset +
1745 ordered_extent->len);
1746 BUG_ON(ret);
1747 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001748 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001749 ret = insert_reserved_file_extent(trans, inode,
1750 ordered_extent->file_offset,
1751 ordered_extent->start,
1752 ordered_extent->disk_len,
1753 ordered_extent->len,
1754 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001755 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001756 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001757 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1758 ordered_extent->file_offset,
1759 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001760 BUG_ON(ret);
1761 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001762 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1763 ordered_extent->file_offset +
1764 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1765
Chris Masone6dcd2d2008-07-17 12:53:50 -04001766 add_pending_csums(trans, inode, ordered_extent->file_offset,
1767 &ordered_extent->list);
1768
Josef Bacik1ef30be2011-04-05 19:25:36 -04001769 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1770 if (!ret) {
1771 ret = btrfs_update_inode(trans, root, inode);
1772 BUG_ON(ret);
1773 }
1774 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00001775out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001776 if (nolock) {
1777 if (trans)
1778 btrfs_end_transaction_nolock(trans, root);
1779 } else {
1780 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1781 if (trans)
1782 btrfs_end_transaction(trans, root);
1783 }
1784
Chris Masone6dcd2d2008-07-17 12:53:50 -04001785 /* once for us */
1786 btrfs_put_ordered_extent(ordered_extent);
1787 /* once for the tree */
1788 btrfs_put_ordered_extent(ordered_extent);
1789
Chris Masone6dcd2d2008-07-17 12:53:50 -04001790 return 0;
1791}
1792
Christoph Hellwigb2950862008-12-02 09:54:17 -05001793static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001794 struct extent_state *state, int uptodate)
1795{
liubo1abe9b82011-03-24 11:18:59 +00001796 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1797
Chris Mason8b62b722009-09-02 16:53:46 -04001798 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001799 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1800}
1801
Chris Masond352ac62008-09-29 15:18:18 -04001802/*
1803 * When IO fails, either with EIO or csum verification fails, we
1804 * try other mirrors that might have a good copy of the data. This
1805 * io_failure_record is used to record state as we go through all the
1806 * mirrors. If another mirror has good data, the page is set up to date
1807 * and things continue. If a good mirror can't be found, the original
1808 * bio end_io callback is called to indicate things have failed.
1809 */
Chris Mason7e383262008-04-09 16:28:12 -04001810struct io_failure_record {
1811 struct page *page;
1812 u64 start;
1813 u64 len;
1814 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001815 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001816 int last_mirror;
1817};
1818
Christoph Hellwigb2950862008-12-02 09:54:17 -05001819static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001820 struct page *page, u64 start, u64 end,
1821 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001822{
1823 struct io_failure_record *failrec = NULL;
1824 u64 private;
1825 struct extent_map *em;
1826 struct inode *inode = page->mapping->host;
1827 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001828 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001829 struct bio *bio;
1830 int num_copies;
1831 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001832 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001833 u64 logical;
1834
1835 ret = get_state_private(failure_tree, start, &private);
1836 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001837 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1838 if (!failrec)
1839 return -ENOMEM;
1840 failrec->start = start;
1841 failrec->len = end - start + 1;
1842 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001843 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001844
Chris Mason890871b2009-09-02 16:24:52 -04001845 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001846 em = lookup_extent_mapping(em_tree, start, failrec->len);
1847 if (em->start > start || em->start + em->len < start) {
1848 free_extent_map(em);
1849 em = NULL;
1850 }
Chris Mason890871b2009-09-02 16:24:52 -04001851 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001852
1853 if (!em || IS_ERR(em)) {
1854 kfree(failrec);
1855 return -EIO;
1856 }
1857 logical = start - em->start;
1858 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001859 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1860 logical = em->block_start;
1861 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08001862 extent_set_compress_type(&failrec->bio_flags,
1863 em->compress_type);
Chris Masond20f7042008-12-08 16:58:54 -05001864 }
Chris Mason7e383262008-04-09 16:28:12 -04001865 failrec->logical = logical;
1866 free_extent_map(em);
1867 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1868 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001869 set_state_private(failure_tree, start,
1870 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001871 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001872 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001873 }
1874 num_copies = btrfs_num_copies(
1875 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1876 failrec->logical, failrec->len);
1877 failrec->last_mirror++;
1878 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001879 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001880 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1881 failrec->start,
1882 EXTENT_LOCKED);
1883 if (state && state->start != failrec->start)
1884 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001885 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001886 }
1887 if (!state || failrec->last_mirror > num_copies) {
1888 set_state_private(failure_tree, failrec->start, 0);
1889 clear_extent_bits(failure_tree, failrec->start,
1890 failrec->start + failrec->len - 1,
1891 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1892 kfree(failrec);
1893 return -EIO;
1894 }
1895 bio = bio_alloc(GFP_NOFS, 1);
1896 bio->bi_private = state;
1897 bio->bi_end_io = failed_bio->bi_end_io;
1898 bio->bi_sector = failrec->logical >> 9;
1899 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001900 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001901
Chris Mason7e383262008-04-09 16:28:12 -04001902 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001903 if (failed_bio->bi_rw & REQ_WRITE)
Chris Mason1259ab72008-05-12 13:39:03 -04001904 rw = WRITE;
1905 else
1906 rw = READ;
1907
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001908 ret = BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001909 failrec->last_mirror,
Chris Masoneaf25d92010-05-25 09:48:28 -04001910 failrec->bio_flags, 0);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001911 return ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001912}
1913
Chris Masond352ac62008-09-29 15:18:18 -04001914/*
1915 * each time an IO finishes, we do a fast check in the IO failure tree
1916 * to see if we need to process or clean up an io_failure_record
1917 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001918static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001919{
1920 u64 private;
1921 u64 private_failure;
1922 struct io_failure_record *failure;
1923 int ret;
1924
1925 private = 0;
1926 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
Chris Masonec29ed52011-02-23 16:23:20 -05001927 (u64)-1, 1, EXTENT_DIRTY, 0)) {
Chris Mason1259ab72008-05-12 13:39:03 -04001928 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1929 start, &private_failure);
1930 if (ret == 0) {
1931 failure = (struct io_failure_record *)(unsigned long)
1932 private_failure;
1933 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1934 failure->start, 0);
1935 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1936 failure->start,
1937 failure->start + failure->len - 1,
1938 EXTENT_DIRTY | EXTENT_LOCKED,
1939 GFP_NOFS);
1940 kfree(failure);
1941 }
1942 }
Chris Mason7e383262008-04-09 16:28:12 -04001943 return 0;
1944}
1945
Chris Masond352ac62008-09-29 15:18:18 -04001946/*
1947 * when reads are done, we need to check csums to verify the data is correct
1948 * if there's a match, we allow the bio to finish. If not, we go through
1949 * the io_failure_record routines to find good copies
1950 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001951static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001952 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001953{
Chris Mason35ebb932007-10-30 16:56:53 -04001954 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001955 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001956 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001957 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001958 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001959 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001960 struct btrfs_root *root = BTRFS_I(inode)->root;
1961 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001962
Chris Masond20f7042008-12-08 16:58:54 -05001963 if (PageChecked(page)) {
1964 ClearPageChecked(page);
1965 goto good;
1966 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001967
1968 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001969 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001970
Yan Zheng17d217f2008-12-12 10:03:38 -05001971 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001972 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001973 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1974 GFP_NOFS);
1975 return 0;
1976 }
1977
Yanc2e639f2008-02-04 08:57:25 -05001978 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001979 private = state->private;
1980 ret = 0;
1981 } else {
1982 ret = get_state_private(io_tree, start, &private);
1983 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001984 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001985 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001986 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001987
Chris Masonff79f812007-10-15 16:22:25 -04001988 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1989 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001990 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001991 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001992
Chris Mason9ab86c82009-01-07 09:48:51 -05001993 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001994good:
Chris Mason7e383262008-04-09 16:28:12 -04001995 /* if the io failure tree for this inode is non-empty,
1996 * check to see if we've recovered from a failed IO
1997 */
Chris Mason1259ab72008-05-12 13:39:03 -04001998 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001999 return 0;
2000
2001zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04002002 if (printk_ratelimit()) {
2003 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
2004 "private %llu\n", page->mapping->host->i_ino,
2005 (unsigned long long)start, csum,
2006 (unsigned long long)private);
2007 }
Chris Masondb945352007-10-15 16:15:53 -04002008 memset(kaddr + offset, 1, end - start + 1);
2009 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05002010 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04002011 if (private == 0)
2012 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002013 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002014}
Chris Masonb888db22007-08-27 16:49:44 -04002015
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002016struct delayed_iput {
2017 struct list_head list;
2018 struct inode *inode;
2019};
2020
2021void btrfs_add_delayed_iput(struct inode *inode)
2022{
2023 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2024 struct delayed_iput *delayed;
2025
2026 if (atomic_add_unless(&inode->i_count, -1, 1))
2027 return;
2028
2029 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2030 delayed->inode = inode;
2031
2032 spin_lock(&fs_info->delayed_iput_lock);
2033 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2034 spin_unlock(&fs_info->delayed_iput_lock);
2035}
2036
2037void btrfs_run_delayed_iputs(struct btrfs_root *root)
2038{
2039 LIST_HEAD(list);
2040 struct btrfs_fs_info *fs_info = root->fs_info;
2041 struct delayed_iput *delayed;
2042 int empty;
2043
2044 spin_lock(&fs_info->delayed_iput_lock);
2045 empty = list_empty(&fs_info->delayed_iputs);
2046 spin_unlock(&fs_info->delayed_iput_lock);
2047 if (empty)
2048 return;
2049
2050 down_read(&root->fs_info->cleanup_work_sem);
2051 spin_lock(&fs_info->delayed_iput_lock);
2052 list_splice_init(&fs_info->delayed_iputs, &list);
2053 spin_unlock(&fs_info->delayed_iput_lock);
2054
2055 while (!list_empty(&list)) {
2056 delayed = list_entry(list.next, struct delayed_iput, list);
2057 list_del(&delayed->list);
2058 iput(delayed->inode);
2059 kfree(delayed);
2060 }
2061 up_read(&root->fs_info->cleanup_work_sem);
2062}
2063
Josef Bacik7b128762008-07-24 12:17:14 -04002064/*
Yan, Zhengd68fc572010-05-16 10:49:58 -04002065 * calculate extra metadata reservation when snapshotting a subvolume
2066 * contains orphan files.
2067 */
2068void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2069 struct btrfs_pending_snapshot *pending,
2070 u64 *bytes_to_reserve)
2071{
2072 struct btrfs_root *root;
2073 struct btrfs_block_rsv *block_rsv;
2074 u64 num_bytes;
2075 int index;
2076
2077 root = pending->root;
2078 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2079 return;
2080
2081 block_rsv = root->orphan_block_rsv;
2082
2083 /* orphan block reservation for the snapshot */
2084 num_bytes = block_rsv->size;
2085
2086 /*
2087 * after the snapshot is created, COWing tree blocks may use more
2088 * space than it frees. So we should make sure there is enough
2089 * reserved space.
2090 */
2091 index = trans->transid & 0x1;
2092 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2093 num_bytes += block_rsv->size -
2094 (block_rsv->reserved + block_rsv->freed[index]);
2095 }
2096
2097 *bytes_to_reserve += num_bytes;
2098}
2099
2100void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2101 struct btrfs_pending_snapshot *pending)
2102{
2103 struct btrfs_root *root = pending->root;
2104 struct btrfs_root *snap = pending->snap;
2105 struct btrfs_block_rsv *block_rsv;
2106 u64 num_bytes;
2107 int index;
2108 int ret;
2109
2110 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2111 return;
2112
2113 /* refill source subvolume's orphan block reservation */
2114 block_rsv = root->orphan_block_rsv;
2115 index = trans->transid & 0x1;
2116 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2117 num_bytes = block_rsv->size -
2118 (block_rsv->reserved + block_rsv->freed[index]);
2119 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2120 root->orphan_block_rsv,
2121 num_bytes);
2122 BUG_ON(ret);
2123 }
2124
2125 /* setup orphan block reservation for the snapshot */
2126 block_rsv = btrfs_alloc_block_rsv(snap);
2127 BUG_ON(!block_rsv);
2128
2129 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2130 snap->orphan_block_rsv = block_rsv;
2131
2132 num_bytes = root->orphan_block_rsv->size;
2133 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2134 block_rsv, num_bytes);
2135 BUG_ON(ret);
2136
2137#if 0
2138 /* insert orphan item for the snapshot */
2139 WARN_ON(!root->orphan_item_inserted);
2140 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2141 snap->root_key.objectid);
2142 BUG_ON(ret);
2143 snap->orphan_item_inserted = 1;
2144#endif
2145}
2146
2147enum btrfs_orphan_cleanup_state {
2148 ORPHAN_CLEANUP_STARTED = 1,
2149 ORPHAN_CLEANUP_DONE = 2,
2150};
2151
2152/*
2153 * This is called in transaction commmit time. If there are no orphan
2154 * files in the subvolume, it removes orphan item and frees block_rsv
2155 * structure.
2156 */
2157void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2158 struct btrfs_root *root)
2159{
2160 int ret;
2161
2162 if (!list_empty(&root->orphan_list) ||
2163 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2164 return;
2165
2166 if (root->orphan_item_inserted &&
2167 btrfs_root_refs(&root->root_item) > 0) {
2168 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2169 root->root_key.objectid);
2170 BUG_ON(ret);
2171 root->orphan_item_inserted = 0;
2172 }
2173
2174 if (root->orphan_block_rsv) {
2175 WARN_ON(root->orphan_block_rsv->size > 0);
2176 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2177 root->orphan_block_rsv = NULL;
2178 }
2179}
2180
2181/*
Josef Bacik7b128762008-07-24 12:17:14 -04002182 * This creates an orphan entry for the given inode in case something goes
2183 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002184 *
2185 * NOTE: caller of this function should reserve 5 units of metadata for
2186 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002187 */
2188int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2189{
2190 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002191 struct btrfs_block_rsv *block_rsv = NULL;
2192 int reserve = 0;
2193 int insert = 0;
2194 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002195
Yan, Zhengd68fc572010-05-16 10:49:58 -04002196 if (!root->orphan_block_rsv) {
2197 block_rsv = btrfs_alloc_block_rsv(root);
2198 BUG_ON(!block_rsv);
Josef Bacik7b128762008-07-24 12:17:14 -04002199 }
2200
Yan, Zhengd68fc572010-05-16 10:49:58 -04002201 spin_lock(&root->orphan_lock);
2202 if (!root->orphan_block_rsv) {
2203 root->orphan_block_rsv = block_rsv;
2204 } else if (block_rsv) {
2205 btrfs_free_block_rsv(root, block_rsv);
2206 block_rsv = NULL;
2207 }
Josef Bacik7b128762008-07-24 12:17:14 -04002208
Yan, Zhengd68fc572010-05-16 10:49:58 -04002209 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2210 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2211#if 0
2212 /*
2213 * For proper ENOSPC handling, we should do orphan
2214 * cleanup when mounting. But this introduces backward
2215 * compatibility issue.
2216 */
2217 if (!xchg(&root->orphan_item_inserted, 1))
2218 insert = 2;
2219 else
2220 insert = 1;
2221#endif
2222 insert = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002223 }
Josef Bacik7b128762008-07-24 12:17:14 -04002224
Yan, Zhengd68fc572010-05-16 10:49:58 -04002225 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2226 BTRFS_I(inode)->orphan_meta_reserved = 1;
2227 reserve = 1;
2228 }
2229 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002230
Yan, Zhengd68fc572010-05-16 10:49:58 -04002231 if (block_rsv)
2232 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2233
2234 /* grab metadata reservation from transaction handle */
2235 if (reserve) {
2236 ret = btrfs_orphan_reserve_metadata(trans, inode);
2237 BUG_ON(ret);
2238 }
2239
2240 /* insert an orphan item to track this unlinked/truncated file */
2241 if (insert >= 1) {
2242 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2243 BUG_ON(ret);
2244 }
2245
2246 /* insert an orphan item to track subvolume contains orphan files */
2247 if (insert >= 2) {
2248 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2249 root->root_key.objectid);
2250 BUG_ON(ret);
2251 }
2252 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002253}
2254
2255/*
2256 * We have done the truncate/delete so we can go ahead and remove the orphan
2257 * item for this particular inode.
2258 */
2259int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2260{
2261 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002262 int delete_item = 0;
2263 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002264 int ret = 0;
2265
Yan, Zhengd68fc572010-05-16 10:49:58 -04002266 spin_lock(&root->orphan_lock);
2267 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2268 list_del_init(&BTRFS_I(inode)->i_orphan);
2269 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002270 }
2271
Yan, Zhengd68fc572010-05-16 10:49:58 -04002272 if (BTRFS_I(inode)->orphan_meta_reserved) {
2273 BTRFS_I(inode)->orphan_meta_reserved = 0;
2274 release_rsv = 1;
2275 }
2276 spin_unlock(&root->orphan_lock);
2277
2278 if (trans && delete_item) {
2279 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2280 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002281 }
2282
Yan, Zhengd68fc572010-05-16 10:49:58 -04002283 if (release_rsv)
2284 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002285
Yan, Zhengd68fc572010-05-16 10:49:58 -04002286 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002287}
2288
2289/*
2290 * this cleans up any orphans that may be left on the list from the last use
2291 * of this root.
2292 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002293int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002294{
2295 struct btrfs_path *path;
2296 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002297 struct btrfs_key key, found_key;
2298 struct btrfs_trans_handle *trans;
2299 struct inode *inode;
2300 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2301
Yan, Zhengd68fc572010-05-16 10:49:58 -04002302 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002303 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002304
2305 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002306 if (!path) {
2307 ret = -ENOMEM;
2308 goto out;
2309 }
Josef Bacik7b128762008-07-24 12:17:14 -04002310 path->reada = -1;
2311
2312 key.objectid = BTRFS_ORPHAN_OBJECTID;
2313 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2314 key.offset = (u64)-1;
2315
Josef Bacik7b128762008-07-24 12:17:14 -04002316 while (1) {
2317 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002318 if (ret < 0)
2319 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002320
2321 /*
2322 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002323 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002324 * find the key and see if we have stuff that matches
2325 */
2326 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002327 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002328 if (path->slots[0] == 0)
2329 break;
2330 path->slots[0]--;
2331 }
2332
2333 /* pull out the item */
2334 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002335 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2336
2337 /* make sure the item matches what we want */
2338 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2339 break;
2340 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2341 break;
2342
2343 /* release the path since we're done with it */
2344 btrfs_release_path(root, path);
2345
2346 /*
2347 * this is where we are basically btrfs_lookup, without the
2348 * crossing root thing. we store the inode number in the
2349 * offset of the orphan item.
2350 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002351 found_key.objectid = found_key.offset;
2352 found_key.type = BTRFS_INODE_ITEM_KEY;
2353 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002354 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002355 if (IS_ERR(inode)) {
2356 ret = PTR_ERR(inode);
2357 goto out;
2358 }
Josef Bacik7b128762008-07-24 12:17:14 -04002359
Josef Bacik7b128762008-07-24 12:17:14 -04002360 /*
2361 * add this inode to the orphan list so btrfs_orphan_del does
2362 * the proper thing when we hit it
2363 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002364 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002365 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002366 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002367
2368 /*
2369 * if this is a bad inode, means we actually succeeded in
2370 * removing the inode, but not the orphan record, which means
2371 * we need to manually delete the orphan since iput will just
2372 * do a destroy_inode
2373 */
2374 if (is_bad_inode(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002375 trans = btrfs_start_transaction(root, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002376 if (IS_ERR(trans)) {
2377 ret = PTR_ERR(trans);
2378 goto out;
2379 }
Josef Bacik7b128762008-07-24 12:17:14 -04002380 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002381 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002382 iput(inode);
2383 continue;
2384 }
2385
2386 /* if we have links, this was a truncate, lets do that */
2387 if (inode->i_nlink) {
Josef Bacika41ad392011-01-31 15:30:16 -05002388 if (!S_ISREG(inode->i_mode)) {
2389 WARN_ON(1);
2390 iput(inode);
2391 continue;
2392 }
Josef Bacik7b128762008-07-24 12:17:14 -04002393 nr_truncate++;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002394 ret = btrfs_truncate(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002395 } else {
2396 nr_unlink++;
2397 }
2398
2399 /* this will do delete_inode and everything for us */
2400 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002401 if (ret)
2402 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002403 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002404 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2405
2406 if (root->orphan_block_rsv)
2407 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2408 (u64)-1);
2409
2410 if (root->orphan_block_rsv || root->orphan_item_inserted) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002411 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002412 if (!IS_ERR(trans))
2413 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002414 }
Josef Bacik7b128762008-07-24 12:17:14 -04002415
2416 if (nr_unlink)
2417 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2418 if (nr_truncate)
2419 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002420
2421out:
2422 if (ret)
2423 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2424 btrfs_free_path(path);
2425 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002426}
2427
Chris Masond352ac62008-09-29 15:18:18 -04002428/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002429 * very simple check to peek ahead in the leaf looking for xattrs. If we
2430 * don't find any xattrs, we know there can't be any acls.
2431 *
2432 * slot is the slot the inode is in, objectid is the objectid of the inode
2433 */
2434static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2435 int slot, u64 objectid)
2436{
2437 u32 nritems = btrfs_header_nritems(leaf);
2438 struct btrfs_key found_key;
2439 int scanned = 0;
2440
2441 slot++;
2442 while (slot < nritems) {
2443 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2444
2445 /* we found a different objectid, there must not be acls */
2446 if (found_key.objectid != objectid)
2447 return 0;
2448
2449 /* we found an xattr, assume we've got an acl */
2450 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2451 return 1;
2452
2453 /*
2454 * we found a key greater than an xattr key, there can't
2455 * be any acls later on
2456 */
2457 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2458 return 0;
2459
2460 slot++;
2461 scanned++;
2462
2463 /*
2464 * it goes inode, inode backrefs, xattrs, extents,
2465 * so if there are a ton of hard links to an inode there can
2466 * be a lot of backrefs. Don't waste time searching too hard,
2467 * this is just an optimization
2468 */
2469 if (scanned >= 8)
2470 break;
2471 }
2472 /* we hit the end of the leaf before we found an xattr or
2473 * something larger than an xattr. We have to assume the inode
2474 * has acls
2475 */
2476 return 1;
2477}
2478
2479/*
Chris Masond352ac62008-09-29 15:18:18 -04002480 * read an inode from the btree into the in-memory inode
2481 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002482static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002483{
2484 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002485 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002486 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002487 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002488 struct btrfs_root *root = BTRFS_I(inode)->root;
2489 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002490 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04002491 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002492 int ret;
2493
2494 path = btrfs_alloc_path();
2495 BUG_ON(!path);
Josef Bacikd90c7322011-05-17 09:50:54 -04002496 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002497 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002498
Chris Mason39279cc2007-06-12 06:35:45 -04002499 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002500 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002501 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002502
Chris Mason5f39d392007-10-15 16:14:19 -04002503 leaf = path->nodes[0];
2504 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2505 struct btrfs_inode_item);
Josef Bacikd90c7322011-05-17 09:50:54 -04002506 if (!leaf->map_token)
2507 map_private_extent_buffer(leaf, (unsigned long)inode_item,
2508 sizeof(struct btrfs_inode_item),
2509 &leaf->map_token, &leaf->kaddr,
2510 &leaf->map_start, &leaf->map_len,
2511 KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -04002512
2513 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2514 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2515 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2516 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002517 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002518
2519 tspec = btrfs_inode_atime(inode_item);
2520 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2521 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2522
2523 tspec = btrfs_inode_mtime(inode_item);
2524 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2525 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2526
2527 tspec = btrfs_inode_ctime(inode_item);
2528 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2529 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2530
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002531 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002532 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002533 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002534 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002535 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002536 rdev = btrfs_inode_rdev(leaf, inode_item);
2537
Josef Bacikaec74772008-07-24 12:12:38 -04002538 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002539 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002540
Chris Mason46a53cc2009-04-27 11:47:50 -04002541 /*
2542 * try to precache a NULL acl entry for files that don't have
2543 * any xattrs or acls
2544 */
2545 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002546 if (!maybe_acls)
2547 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002548
Josef Bacikd90c7322011-05-17 09:50:54 -04002549 if (leaf->map_token) {
2550 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2551 leaf->map_token = NULL;
2552 }
2553
Chris Mason39279cc2007-06-12 06:35:45 -04002554 btrfs_free_path(path);
2555 inode_item = NULL;
2556
Chris Mason39279cc2007-06-12 06:35:45 -04002557 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002558 case S_IFREG:
2559 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002560 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002561 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002562 inode->i_fop = &btrfs_file_operations;
2563 inode->i_op = &btrfs_file_inode_operations;
2564 break;
2565 case S_IFDIR:
2566 inode->i_fop = &btrfs_dir_file_operations;
2567 if (root == root->fs_info->tree_root)
2568 inode->i_op = &btrfs_dir_ro_inode_operations;
2569 else
2570 inode->i_op = &btrfs_dir_inode_operations;
2571 break;
2572 case S_IFLNK:
2573 inode->i_op = &btrfs_symlink_inode_operations;
2574 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002575 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002576 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002577 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002578 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002579 init_special_inode(inode, inode->i_mode, rdev);
2580 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002581 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002582
2583 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002584 return;
2585
2586make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002587 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002588 make_bad_inode(inode);
2589}
2590
Chris Masond352ac62008-09-29 15:18:18 -04002591/*
2592 * given a leaf and an inode, copy the inode fields into the leaf
2593 */
Chris Masone02119d2008-09-05 16:13:11 -04002594static void fill_inode_item(struct btrfs_trans_handle *trans,
2595 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002596 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002597 struct inode *inode)
2598{
Josef Bacik12ddb962011-04-05 13:02:27 -04002599 if (!leaf->map_token)
2600 map_private_extent_buffer(leaf, (unsigned long)item,
2601 sizeof(struct btrfs_inode_item),
2602 &leaf->map_token, &leaf->kaddr,
2603 &leaf->map_start, &leaf->map_len,
2604 KM_USER1);
2605
Chris Mason5f39d392007-10-15 16:14:19 -04002606 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2607 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002608 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002609 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2610 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2611
2612 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2613 inode->i_atime.tv_sec);
2614 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2615 inode->i_atime.tv_nsec);
2616
2617 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2618 inode->i_mtime.tv_sec);
2619 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2620 inode->i_mtime.tv_nsec);
2621
2622 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2623 inode->i_ctime.tv_sec);
2624 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2625 inode->i_ctime.tv_nsec);
2626
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002627 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002628 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002629 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002630 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002631 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002632 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Josef Bacikd82a6f12011-05-11 15:26:06 -04002633 btrfs_set_inode_block_group(leaf, item, 0);
Josef Bacik12ddb962011-04-05 13:02:27 -04002634
2635 if (leaf->map_token) {
2636 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2637 leaf->map_token = NULL;
2638 }
Chris Mason39279cc2007-06-12 06:35:45 -04002639}
2640
Chris Masond352ac62008-09-29 15:18:18 -04002641/*
2642 * copy everything in the in-memory inode into the btree.
2643 */
Chris Masond3977122009-01-05 21:25:51 -05002644noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2645 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002646{
2647 struct btrfs_inode_item *inode_item;
2648 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002649 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002650 int ret;
2651
2652 path = btrfs_alloc_path();
2653 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002654 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002655 ret = btrfs_lookup_inode(trans, root, path,
2656 &BTRFS_I(inode)->location, 1);
2657 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],
Chris Mason39279cc2007-06-12 06:35:45 -04002666 struct btrfs_inode_item);
2667
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
2677
Chris Masond352ac62008-09-29 15:18:18 -04002678/*
2679 * unlink helper that gets used here in inode.c and in the tree logging
2680 * recovery code. It remove a link in a directory with a given name, and
2681 * also drops the back refs in the inode to the directory
2682 */
Al Viro92986792011-03-04 17:14:37 +00002683static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2684 struct btrfs_root *root,
2685 struct inode *dir, struct inode *inode,
2686 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002687{
2688 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002689 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002690 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002691 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002692 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002693 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002694
2695 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002696 if (!path) {
2697 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00002698 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002699 }
2700
Chris Masonb9473432009-03-13 11:00:37 -04002701 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002702 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2703 name, name_len, -1);
2704 if (IS_ERR(di)) {
2705 ret = PTR_ERR(di);
2706 goto err;
2707 }
2708 if (!di) {
2709 ret = -ENOENT;
2710 goto err;
2711 }
Chris Mason5f39d392007-10-15 16:14:19 -04002712 leaf = path->nodes[0];
2713 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002714 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002715 if (ret)
2716 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002717 btrfs_release_path(root, path);
2718
Josef Bacikaec74772008-07-24 12:12:38 -04002719 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002720 inode->i_ino,
2721 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002722 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002723 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002724 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002725 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002726 goto err;
2727 }
2728
Chris Mason39279cc2007-06-12 06:35:45 -04002729 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002730 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002731 if (IS_ERR(di)) {
2732 ret = PTR_ERR(di);
2733 goto err;
2734 }
2735 if (!di) {
2736 ret = -ENOENT;
2737 goto err;
2738 }
2739 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002740 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002741
Chris Masone02119d2008-09-05 16:13:11 -04002742 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2743 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002744 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002745
2746 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2747 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002748 if (ret == -ENOENT)
2749 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002750err:
2751 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002752 if (ret)
2753 goto out;
2754
2755 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2756 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2757 btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002758out:
Chris Mason39279cc2007-06-12 06:35:45 -04002759 return ret;
2760}
2761
Al Viro92986792011-03-04 17:14:37 +00002762int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2763 struct btrfs_root *root,
2764 struct inode *dir, struct inode *inode,
2765 const char *name, int name_len)
2766{
2767 int ret;
2768 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2769 if (!ret) {
2770 btrfs_drop_nlink(inode);
2771 ret = btrfs_update_inode(trans, root, inode);
2772 }
2773 return ret;
2774}
2775
2776
Yan, Zhenga22285a2010-05-16 10:48:46 -04002777/* helper to check if there is any shared block in the path */
2778static int check_path_shared(struct btrfs_root *root,
2779 struct btrfs_path *path)
2780{
2781 struct extent_buffer *eb;
2782 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002783 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002784
2785 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002786 int ret;
2787
Yan, Zhenga22285a2010-05-16 10:48:46 -04002788 if (!path->nodes[level])
2789 break;
2790 eb = path->nodes[level];
2791 if (!btrfs_block_can_be_shared(root, eb))
2792 continue;
2793 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2794 &refs, NULL);
2795 if (refs > 1)
2796 return 1;
2797 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002798 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002799}
2800
2801/*
2802 * helper to start transaction for unlink and rmdir.
2803 *
2804 * unlink and rmdir are special in btrfs, they do not always free space.
2805 * so in enospc case, we should make sure they will free space before
2806 * allowing them to use the global metadata reservation.
2807 */
2808static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2809 struct dentry *dentry)
2810{
2811 struct btrfs_trans_handle *trans;
2812 struct btrfs_root *root = BTRFS_I(dir)->root;
2813 struct btrfs_path *path;
2814 struct btrfs_inode_ref *ref;
2815 struct btrfs_dir_item *di;
2816 struct inode *inode = dentry->d_inode;
2817 u64 index;
2818 int check_link = 1;
2819 int err = -ENOSPC;
2820 int ret;
2821
2822 trans = btrfs_start_transaction(root, 10);
2823 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2824 return trans;
2825
2826 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2827 return ERR_PTR(-ENOSPC);
2828
2829 /* check if there is someone else holds reference */
2830 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2831 return ERR_PTR(-ENOSPC);
2832
2833 if (atomic_read(&inode->i_count) > 2)
2834 return ERR_PTR(-ENOSPC);
2835
2836 if (xchg(&root->fs_info->enospc_unlink, 1))
2837 return ERR_PTR(-ENOSPC);
2838
2839 path = btrfs_alloc_path();
2840 if (!path) {
2841 root->fs_info->enospc_unlink = 0;
2842 return ERR_PTR(-ENOMEM);
2843 }
2844
2845 trans = btrfs_start_transaction(root, 0);
2846 if (IS_ERR(trans)) {
2847 btrfs_free_path(path);
2848 root->fs_info->enospc_unlink = 0;
2849 return trans;
2850 }
2851
2852 path->skip_locking = 1;
2853 path->search_commit_root = 1;
2854
2855 ret = btrfs_lookup_inode(trans, root, path,
2856 &BTRFS_I(dir)->location, 0);
2857 if (ret < 0) {
2858 err = ret;
2859 goto out;
2860 }
2861 if (ret == 0) {
2862 if (check_path_shared(root, path))
2863 goto out;
2864 } else {
2865 check_link = 0;
2866 }
2867 btrfs_release_path(root, path);
2868
2869 ret = btrfs_lookup_inode(trans, root, path,
2870 &BTRFS_I(inode)->location, 0);
2871 if (ret < 0) {
2872 err = ret;
2873 goto out;
2874 }
2875 if (ret == 0) {
2876 if (check_path_shared(root, path))
2877 goto out;
2878 } else {
2879 check_link = 0;
2880 }
2881 btrfs_release_path(root, path);
2882
2883 if (ret == 0 && S_ISREG(inode->i_mode)) {
2884 ret = btrfs_lookup_file_extent(trans, root, path,
2885 inode->i_ino, (u64)-1, 0);
2886 if (ret < 0) {
2887 err = ret;
2888 goto out;
2889 }
2890 BUG_ON(ret == 0);
2891 if (check_path_shared(root, path))
2892 goto out;
2893 btrfs_release_path(root, path);
2894 }
2895
2896 if (!check_link) {
2897 err = 0;
2898 goto out;
2899 }
2900
2901 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2902 dentry->d_name.name, dentry->d_name.len, 0);
2903 if (IS_ERR(di)) {
2904 err = PTR_ERR(di);
2905 goto out;
2906 }
2907 if (di) {
2908 if (check_path_shared(root, path))
2909 goto out;
2910 } else {
2911 err = 0;
2912 goto out;
2913 }
2914 btrfs_release_path(root, path);
2915
2916 ref = btrfs_lookup_inode_ref(trans, root, path,
2917 dentry->d_name.name, dentry->d_name.len,
2918 inode->i_ino, dir->i_ino, 0);
2919 if (IS_ERR(ref)) {
2920 err = PTR_ERR(ref);
2921 goto out;
2922 }
2923 BUG_ON(!ref);
2924 if (check_path_shared(root, path))
2925 goto out;
2926 index = btrfs_inode_ref_index(path->nodes[0], ref);
2927 btrfs_release_path(root, path);
2928
2929 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2930 dentry->d_name.name, dentry->d_name.len, 0);
2931 if (IS_ERR(di)) {
2932 err = PTR_ERR(di);
2933 goto out;
2934 }
2935 BUG_ON(ret == -ENOENT);
2936 if (check_path_shared(root, path))
2937 goto out;
2938
2939 err = 0;
2940out:
2941 btrfs_free_path(path);
2942 if (err) {
2943 btrfs_end_transaction(trans, root);
2944 root->fs_info->enospc_unlink = 0;
2945 return ERR_PTR(err);
2946 }
2947
2948 trans->block_rsv = &root->fs_info->global_block_rsv;
2949 return trans;
2950}
2951
2952static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2953 struct btrfs_root *root)
2954{
2955 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2956 BUG_ON(!root->fs_info->enospc_unlink);
2957 root->fs_info->enospc_unlink = 0;
2958 }
2959 btrfs_end_transaction_throttle(trans, root);
2960}
2961
Chris Mason39279cc2007-06-12 06:35:45 -04002962static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2963{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002964 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002965 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002966 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002967 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002968 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002969
Yan, Zhenga22285a2010-05-16 10:48:46 -04002970 trans = __unlink_start_trans(dir, dentry);
2971 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002972 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002973
Chris Mason12fcfd22009-03-24 10:24:20 -04002974 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2975
Chris Masone02119d2008-09-05 16:13:11 -04002976 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2977 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002978 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002979
Yan, Zhenga22285a2010-05-16 10:48:46 -04002980 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002981 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002982 BUG_ON(ret);
2983 }
Josef Bacik7b128762008-07-24 12:17:14 -04002984
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002985 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002986 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002987 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002988 return ret;
2989}
2990
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002991int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2992 struct btrfs_root *root,
2993 struct inode *dir, u64 objectid,
2994 const char *name, int name_len)
2995{
2996 struct btrfs_path *path;
2997 struct extent_buffer *leaf;
2998 struct btrfs_dir_item *di;
2999 struct btrfs_key key;
3000 u64 index;
3001 int ret;
3002
3003 path = btrfs_alloc_path();
3004 if (!path)
3005 return -ENOMEM;
3006
3007 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
3008 name, name_len, -1);
3009 BUG_ON(!di || IS_ERR(di));
3010
3011 leaf = path->nodes[0];
3012 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3013 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3014 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3015 BUG_ON(ret);
3016 btrfs_release_path(root, path);
3017
3018 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3019 objectid, root->root_key.objectid,
3020 dir->i_ino, &index, name, name_len);
3021 if (ret < 0) {
3022 BUG_ON(ret != -ENOENT);
3023 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
3024 name, name_len);
3025 BUG_ON(!di || IS_ERR(di));
3026
3027 leaf = path->nodes[0];
3028 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3029 btrfs_release_path(root, path);
3030 index = key.offset;
3031 }
3032
3033 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
3034 index, name, name_len, -1);
3035 BUG_ON(!di || IS_ERR(di));
3036
3037 leaf = path->nodes[0];
3038 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3039 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3040 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3041 BUG_ON(ret);
3042 btrfs_release_path(root, path);
3043
3044 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3045 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3046 ret = btrfs_update_inode(trans, root, dir);
3047 BUG_ON(ret);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003048
3049 btrfs_free_path(path);
3050 return 0;
3051}
3052
Chris Mason39279cc2007-06-12 06:35:45 -04003053static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3054{
3055 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003056 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003057 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003058 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05003059 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003060
Chris Mason3394e162008-11-17 20:42:26 -05003061 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003062 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003063 return -ENOTEMPTY;
3064
Yan, Zhenga22285a2010-05-16 10:48:46 -04003065 trans = __unlink_start_trans(dir, dentry);
3066 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003067 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003068
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003069 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3070 err = btrfs_unlink_subvol(trans, root, dir,
3071 BTRFS_I(inode)->location.objectid,
3072 dentry->d_name.name,
3073 dentry->d_name.len);
3074 goto out;
3075 }
3076
Josef Bacik7b128762008-07-24 12:17:14 -04003077 err = btrfs_orphan_add(trans, inode);
3078 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003079 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003080
Chris Mason39279cc2007-06-12 06:35:45 -04003081 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003082 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3083 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003084 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003085 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003086out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003087 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003088 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003089 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003090
Chris Mason39279cc2007-06-12 06:35:45 -04003091 return err;
3092}
3093
Chris Masond20f7042008-12-08 16:58:54 -05003094#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04003095/*
Chris Mason323ac952008-10-01 19:05:46 -04003096 * when truncating bytes in a file, it is possible to avoid reading
3097 * the leaves that contain only checksum items. This can be the
3098 * majority of the IO required to delete a large file, but it must
3099 * be done carefully.
3100 *
3101 * The keys in the level just above the leaves are checked to make sure
3102 * the lowest key in a given leaf is a csum key, and starts at an offset
3103 * after the new size.
3104 *
3105 * Then the key for the next leaf is checked to make sure it also has
3106 * a checksum item for the same file. If it does, we know our target leaf
3107 * contains only checksum items, and it can be safely freed without reading
3108 * it.
3109 *
3110 * This is just an optimization targeted at large files. It may do
3111 * nothing. It will return 0 unless things went badly.
3112 */
3113static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3114 struct btrfs_root *root,
3115 struct btrfs_path *path,
3116 struct inode *inode, u64 new_size)
3117{
3118 struct btrfs_key key;
3119 int ret;
3120 int nritems;
3121 struct btrfs_key found_key;
3122 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003123 struct btrfs_leaf_ref *ref;
3124 u64 leaf_gen;
3125 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04003126
3127 path->lowest_level = 1;
3128 key.objectid = inode->i_ino;
3129 key.type = BTRFS_CSUM_ITEM_KEY;
3130 key.offset = new_size;
3131again:
3132 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3133 if (ret < 0)
3134 goto out;
3135
3136 if (path->nodes[1] == NULL) {
3137 ret = 0;
3138 goto out;
3139 }
3140 ret = 0;
3141 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3142 nritems = btrfs_header_nritems(path->nodes[1]);
3143
3144 if (!nritems)
3145 goto out;
3146
3147 if (path->slots[1] >= nritems)
3148 goto next_node;
3149
3150 /* did we find a key greater than anything we want to delete? */
3151 if (found_key.objectid > inode->i_ino ||
3152 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3153 goto out;
3154
3155 /* we check the next key in the node to make sure the leave contains
3156 * only checksum items. This comparison doesn't work if our
3157 * leaf is the last one in the node
3158 */
3159 if (path->slots[1] + 1 >= nritems) {
3160next_node:
3161 /* search forward from the last key in the node, this
3162 * will bring us into the next node in the tree
3163 */
3164 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3165
3166 /* unlikely, but we inc below, so check to be safe */
3167 if (found_key.offset == (u64)-1)
3168 goto out;
3169
3170 /* search_forward needs a path with locks held, do the
3171 * search again for the original key. It is possible
3172 * this will race with a balance and return a path that
3173 * we could modify, but this drop is just an optimization
3174 * and is allowed to miss some leaves.
3175 */
3176 btrfs_release_path(root, path);
3177 found_key.offset++;
3178
3179 /* setup a max key for search_forward */
3180 other_key.offset = (u64)-1;
3181 other_key.type = key.type;
3182 other_key.objectid = key.objectid;
3183
3184 path->keep_locks = 1;
3185 ret = btrfs_search_forward(root, &found_key, &other_key,
3186 path, 0, 0);
3187 path->keep_locks = 0;
3188 if (ret || found_key.objectid != key.objectid ||
3189 found_key.type != key.type) {
3190 ret = 0;
3191 goto out;
3192 }
3193
3194 key.offset = found_key.offset;
3195 btrfs_release_path(root, path);
3196 cond_resched();
3197 goto again;
3198 }
3199
3200 /* we know there's one more slot after us in the tree,
3201 * read that key so we can verify it is also a checksum item
3202 */
3203 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3204
3205 if (found_key.objectid < inode->i_ino)
3206 goto next_key;
3207
3208 if (found_key.type != key.type || found_key.offset < new_size)
3209 goto next_key;
3210
3211 /*
3212 * if the key for the next leaf isn't a csum key from this objectid,
3213 * we can't be sure there aren't good items inside this leaf.
3214 * Bail out
3215 */
3216 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3217 goto out;
3218
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003219 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3220 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003221 /*
3222 * it is safe to delete this leaf, it contains only
3223 * csum items from this inode at an offset >= new_size
3224 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003225 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04003226 BUG_ON(ret);
3227
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003228 if (root->ref_cows && leaf_gen < trans->transid) {
3229 ref = btrfs_alloc_leaf_ref(root, 0);
3230 if (ref) {
3231 ref->root_gen = root->root_key.offset;
3232 ref->bytenr = leaf_start;
3233 ref->owner = 0;
3234 ref->generation = leaf_gen;
3235 ref->nritems = 0;
3236
Chris Masonbd56b302009-02-04 09:27:02 -05003237 btrfs_sort_leaf_ref(ref);
3238
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003239 ret = btrfs_add_leaf_ref(root, ref, 0);
3240 WARN_ON(ret);
3241 btrfs_free_leaf_ref(root, ref);
3242 } else {
3243 WARN_ON(1);
3244 }
3245 }
Chris Mason323ac952008-10-01 19:05:46 -04003246next_key:
3247 btrfs_release_path(root, path);
3248
3249 if (other_key.objectid == inode->i_ino &&
3250 other_key.type == key.type && other_key.offset > key.offset) {
3251 key.offset = other_key.offset;
3252 cond_resched();
3253 goto again;
3254 }
3255 ret = 0;
3256out:
3257 /* fixup any changes we've made to the path */
3258 path->lowest_level = 0;
3259 path->keep_locks = 0;
3260 btrfs_release_path(root, path);
3261 return ret;
3262}
3263
Chris Masond20f7042008-12-08 16:58:54 -05003264#endif
3265
Chris Mason323ac952008-10-01 19:05:46 -04003266/*
Chris Mason39279cc2007-06-12 06:35:45 -04003267 * this can truncate away extent items, csum items and directory items.
3268 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003269 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003270 *
3271 * csum items that cross the new i_size are truncated to the new size
3272 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003273 *
3274 * min_type is the minimum key type to truncate down to. If set to 0, this
3275 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003276 */
Yan, Zheng80825102009-11-12 09:35:36 +00003277int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3278 struct btrfs_root *root,
3279 struct inode *inode,
3280 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003281{
Chris Mason39279cc2007-06-12 06:35:45 -04003282 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003283 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003284 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003285 struct btrfs_key key;
3286 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003287 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003288 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003289 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003290 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003291 u64 mask = root->sectorsize - 1;
3292 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003293 int found_extent;
3294 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003295 int pending_del_nr = 0;
3296 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003297 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003298 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003299 int ret;
3300 int err = 0;
3301
3302 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003303
Josef Bacik0af3d002010-06-21 14:48:16 -04003304 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003305 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003306
Chris Mason39279cc2007-06-12 06:35:45 -04003307 path = btrfs_alloc_path();
3308 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003309 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003310
Chris Mason39279cc2007-06-12 06:35:45 -04003311 key.objectid = inode->i_ino;
3312 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003313 key.type = (u8)-1;
3314
Chris Mason85e21ba2008-01-29 15:11:36 -05003315search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003316 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003317 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003318 if (ret < 0) {
3319 err = ret;
3320 goto out;
3321 }
Chris Masond3977122009-01-05 21:25:51 -05003322
Chris Mason85e21ba2008-01-29 15:11:36 -05003323 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003324 /* there are no items in the tree for us to truncate, we're
3325 * done
3326 */
Yan, Zheng80825102009-11-12 09:35:36 +00003327 if (path->slots[0] == 0)
3328 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003329 path->slots[0]--;
3330 }
3331
Chris Masond3977122009-01-05 21:25:51 -05003332 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003333 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003334 leaf = path->nodes[0];
3335 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3336 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003337 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003338
Chris Mason5f39d392007-10-15 16:14:19 -04003339 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003340 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003341
Chris Mason85e21ba2008-01-29 15:11:36 -05003342 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003343 break;
3344
Chris Mason5f39d392007-10-15 16:14:19 -04003345 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003346 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003347 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003348 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003349 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003350 encoding = btrfs_file_extent_compression(leaf, fi);
3351 encoding |= btrfs_file_extent_encryption(leaf, fi);
3352 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3353
Chris Mason179e29e2007-11-01 11:28:41 -04003354 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003355 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003356 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003357 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003358 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003359 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003360 }
Yan008630c2007-11-07 13:31:09 -05003361 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003362 }
Yan, Zheng80825102009-11-12 09:35:36 +00003363 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003364 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003365 } else {
3366 if (item_end < new_size)
3367 break;
3368 if (found_key.offset >= new_size)
3369 del_item = 1;
3370 else
3371 del_item = 0;
3372 }
Chris Mason39279cc2007-06-12 06:35:45 -04003373 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003374 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003375 if (found_type != BTRFS_EXTENT_DATA_KEY)
3376 goto delete;
3377
3378 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003379 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003380 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003381 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003382 u64 orig_num_bytes =
3383 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003384 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003385 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003386 extent_num_bytes = extent_num_bytes &
3387 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003388 btrfs_set_file_extent_num_bytes(leaf, fi,
3389 extent_num_bytes);
3390 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003391 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003392 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003393 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003394 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003395 } else {
Chris Masondb945352007-10-15 16:15:53 -04003396 extent_num_bytes =
3397 btrfs_file_extent_disk_num_bytes(leaf,
3398 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003399 extent_offset = found_key.offset -
3400 btrfs_file_extent_offset(leaf, fi);
3401
Chris Mason39279cc2007-06-12 06:35:45 -04003402 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003403 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003404 if (extent_start != 0) {
3405 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003406 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003407 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003408 }
3409 }
Chris Mason90692182008-02-08 13:49:28 -05003410 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003411 /*
3412 * we can't truncate inline items that have had
3413 * special encodings
3414 */
3415 if (!del_item &&
3416 btrfs_file_extent_compression(leaf, fi) == 0 &&
3417 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3418 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003419 u32 size = new_size - found_key.offset;
3420
3421 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003422 inode_sub_bytes(inode, item_end + 1 -
3423 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003424 }
3425 size =
3426 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003427 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003428 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003429 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003430 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003431 inode_sub_bytes(inode, item_end + 1 -
3432 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003433 }
Chris Mason39279cc2007-06-12 06:35:45 -04003434 }
Chris Mason179e29e2007-11-01 11:28:41 -04003435delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003436 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003437 if (!pending_del_nr) {
3438 /* no pending yet, add ourselves */
3439 pending_del_slot = path->slots[0];
3440 pending_del_nr = 1;
3441 } else if (pending_del_nr &&
3442 path->slots[0] + 1 == pending_del_slot) {
3443 /* hop on the pending chunk */
3444 pending_del_nr++;
3445 pending_del_slot = path->slots[0];
3446 } else {
Chris Masond3977122009-01-05 21:25:51 -05003447 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003448 }
Chris Mason39279cc2007-06-12 06:35:45 -04003449 } else {
3450 break;
3451 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003452 if (found_extent && (root->ref_cows ||
3453 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003454 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003455 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003456 extent_num_bytes, 0,
3457 btrfs_header_owner(leaf),
3458 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003459 BUG_ON(ret);
3460 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003461
Yan, Zheng80825102009-11-12 09:35:36 +00003462 if (found_type == BTRFS_INODE_ITEM_KEY)
3463 break;
3464
3465 if (path->slots[0] == 0 ||
3466 path->slots[0] != pending_del_slot) {
3467 if (root->ref_cows) {
3468 err = -EAGAIN;
3469 goto out;
3470 }
3471 if (pending_del_nr) {
3472 ret = btrfs_del_items(trans, root, path,
3473 pending_del_slot,
3474 pending_del_nr);
3475 BUG_ON(ret);
3476 pending_del_nr = 0;
3477 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003478 btrfs_release_path(root, path);
3479 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003480 } else {
3481 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003482 }
Chris Mason39279cc2007-06-12 06:35:45 -04003483 }
Yan, Zheng80825102009-11-12 09:35:36 +00003484out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003485 if (pending_del_nr) {
3486 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3487 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003488 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003489 }
Chris Mason39279cc2007-06-12 06:35:45 -04003490 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003491 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003492}
3493
Chris Masona52d9a82007-08-27 16:49:44 -04003494/*
3495 * taken from block_truncate_page, but does cow as it zeros out
3496 * any bytes left in the last page in the file.
3497 */
3498static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3499{
3500 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003501 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003502 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3503 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003504 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003505 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003506 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003507 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3508 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3509 struct page *page;
3510 int ret = 0;
3511 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003512 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003513
3514 if ((offset & (blocksize - 1)) == 0)
3515 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003516 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003517 if (ret)
3518 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003519
3520 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003521again:
Chris Masona52d9a82007-08-27 16:49:44 -04003522 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003523 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003524 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003525 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003526 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003527
3528 page_start = page_offset(page);
3529 page_end = page_start + PAGE_CACHE_SIZE - 1;
3530
Chris Masona52d9a82007-08-27 16:49:44 -04003531 if (!PageUptodate(page)) {
3532 ret = btrfs_readpage(NULL, page);
3533 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003534 if (page->mapping != mapping) {
3535 unlock_page(page);
3536 page_cache_release(page);
3537 goto again;
3538 }
Chris Masona52d9a82007-08-27 16:49:44 -04003539 if (!PageUptodate(page)) {
3540 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003541 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003542 }
3543 }
Chris Mason211c17f2008-05-15 09:13:45 -04003544 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003545
Josef Bacik2ac55d42010-02-03 19:33:23 +00003546 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3547 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003548 set_page_extent_mapped(page);
3549
3550 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3551 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003552 unlock_extent_cached(io_tree, page_start, page_end,
3553 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003554 unlock_page(page);
3555 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003556 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003557 btrfs_put_ordered_extent(ordered);
3558 goto again;
3559 }
3560
Josef Bacik2ac55d42010-02-03 19:33:23 +00003561 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003562 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003563 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003564
Josef Bacik2ac55d42010-02-03 19:33:23 +00003565 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3566 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003567 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003568 unlock_extent_cached(io_tree, page_start, page_end,
3569 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003570 goto out_unlock;
3571 }
3572
Chris Masone6dcd2d2008-07-17 12:53:50 -04003573 ret = 0;
3574 if (offset != PAGE_CACHE_SIZE) {
3575 kaddr = kmap(page);
3576 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3577 flush_dcache_page(page);
3578 kunmap(page);
3579 }
Chris Mason247e7432008-07-17 12:53:51 -04003580 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003581 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003582 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3583 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003584
Chris Mason89642222008-07-24 09:41:53 -04003585out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003586 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003587 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003588 unlock_page(page);
3589 page_cache_release(page);
3590out:
3591 return ret;
3592}
3593
Josef Bacik695a0d02011-03-04 15:46:53 -05003594/*
3595 * This function puts in dummy file extents for the area we're creating a hole
3596 * for. So if we are truncating this file to a larger size we need to insert
3597 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3598 * the range between oldsize and size
3599 */
Josef Bacika41ad392011-01-31 15:30:16 -05003600int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04003601{
3602 struct btrfs_trans_handle *trans;
3603 struct btrfs_root *root = BTRFS_I(inode)->root;
3604 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003605 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003606 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003607 u64 mask = root->sectorsize - 1;
Josef Bacika41ad392011-01-31 15:30:16 -05003608 u64 hole_start = (oldsize + mask) & ~mask;
Yan Zheng9036c102008-10-30 14:19:41 -04003609 u64 block_end = (size + mask) & ~mask;
3610 u64 last_byte;
3611 u64 cur_offset;
3612 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003613 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003614
3615 if (size <= hole_start)
3616 return 0;
3617
Yan Zheng9036c102008-10-30 14:19:41 -04003618 while (1) {
3619 struct btrfs_ordered_extent *ordered;
3620 btrfs_wait_ordered_range(inode, hole_start,
3621 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003622 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3623 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003624 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3625 if (!ordered)
3626 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003627 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3628 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003629 btrfs_put_ordered_extent(ordered);
3630 }
3631
Yan Zheng9036c102008-10-30 14:19:41 -04003632 cur_offset = hole_start;
3633 while (1) {
3634 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3635 block_end - cur_offset, 0);
3636 BUG_ON(IS_ERR(em) || !em);
3637 last_byte = min(extent_map_end(em), block_end);
3638 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003639 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003640 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003641 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003642
Yan, Zhenga22285a2010-05-16 10:48:46 -04003643 trans = btrfs_start_transaction(root, 2);
3644 if (IS_ERR(trans)) {
3645 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003646 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003647 }
Yan, Zheng80825102009-11-12 09:35:36 +00003648
3649 err = btrfs_drop_extents(trans, inode, cur_offset,
3650 cur_offset + hole_size,
3651 &hint_byte, 1);
Josef Bacik3893e332011-01-31 16:03:11 -05003652 if (err)
3653 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003654
Yan Zheng9036c102008-10-30 14:19:41 -04003655 err = btrfs_insert_file_extent(trans, root,
3656 inode->i_ino, cur_offset, 0,
3657 0, hole_size, 0, hole_size,
3658 0, 0, 0);
Josef Bacik3893e332011-01-31 16:03:11 -05003659 if (err)
3660 break;
Yan, Zheng80825102009-11-12 09:35:36 +00003661
Yan Zheng9036c102008-10-30 14:19:41 -04003662 btrfs_drop_extent_cache(inode, hole_start,
3663 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003664
3665 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003666 }
3667 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003669 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003670 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003671 break;
3672 }
3673
Yan, Zhenga22285a2010-05-16 10:48:46 -04003674 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003675 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3676 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003677 return err;
3678}
3679
Josef Bacika41ad392011-01-31 15:30:16 -05003680static int btrfs_setsize(struct inode *inode, loff_t newsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003681{
Josef Bacika41ad392011-01-31 15:30:16 -05003682 loff_t oldsize = i_size_read(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003683 int ret;
3684
Josef Bacika41ad392011-01-31 15:30:16 -05003685 if (newsize == oldsize)
Yan, Zheng80825102009-11-12 09:35:36 +00003686 return 0;
3687
Josef Bacika41ad392011-01-31 15:30:16 -05003688 if (newsize > oldsize) {
3689 i_size_write(inode, newsize);
3690 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3691 truncate_pagecache(inode, oldsize, newsize);
3692 ret = btrfs_cont_expand(inode, oldsize, newsize);
Yan, Zheng80825102009-11-12 09:35:36 +00003693 if (ret) {
Josef Bacika41ad392011-01-31 15:30:16 -05003694 btrfs_setsize(inode, oldsize);
Yan, Zheng80825102009-11-12 09:35:36 +00003695 return ret;
3696 }
3697
Josef Bacik930f0282011-03-04 14:41:41 -05003698 mark_inode_dirty(inode);
Josef Bacika41ad392011-01-31 15:30:16 -05003699 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00003700
Josef Bacika41ad392011-01-31 15:30:16 -05003701 /*
3702 * We're truncating a file that used to have good data down to
3703 * zero. Make sure it gets into the ordered flush list so that
3704 * any new writes get down to disk quickly.
3705 */
3706 if (newsize == 0)
3707 BTRFS_I(inode)->ordered_data_close = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003708
Josef Bacika41ad392011-01-31 15:30:16 -05003709 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3710 truncate_setsize(inode, newsize);
3711 ret = btrfs_truncate(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003712 }
3713
Josef Bacika41ad392011-01-31 15:30:16 -05003714 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00003715}
3716
Chris Mason39279cc2007-06-12 06:35:45 -04003717static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3718{
3719 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003720 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003721 int err;
3722
Li Zefanb83cc962010-12-20 16:04:08 +08003723 if (btrfs_root_readonly(root))
3724 return -EROFS;
3725
Chris Mason39279cc2007-06-12 06:35:45 -04003726 err = inode_change_ok(inode, attr);
3727 if (err)
3728 return err;
3729
Chris Mason5a3f23d2009-03-31 13:27:11 -04003730 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Josef Bacika41ad392011-01-31 15:30:16 -05003731 err = btrfs_setsize(inode, attr->ia_size);
Yan, Zheng80825102009-11-12 09:35:36 +00003732 if (err)
3733 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003734 }
Yan Zheng9036c102008-10-30 14:19:41 -04003735
Christoph Hellwig10257742010-06-04 11:30:02 +02003736 if (attr->ia_valid) {
3737 setattr_copy(inode, attr);
3738 mark_inode_dirty(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003739
Christoph Hellwig10257742010-06-04 11:30:02 +02003740 if (attr->ia_valid & ATTR_MODE)
3741 err = btrfs_acl_chmod(inode);
3742 }
3743
Chris Mason39279cc2007-06-12 06:35:45 -04003744 return err;
3745}
Chris Mason61295eb2008-01-14 16:24:38 -05003746
Al Virobd555972010-06-07 11:35:40 -04003747void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003748{
3749 struct btrfs_trans_handle *trans;
3750 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003751 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003752 int ret;
3753
liubo1abe9b82011-03-24 11:18:59 +00003754 trace_btrfs_inode_evict(inode);
3755
Chris Mason39279cc2007-06-12 06:35:45 -04003756 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003757 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3758 root == root->fs_info->tree_root))
Al Virobd555972010-06-07 11:35:40 -04003759 goto no_delete;
3760
Chris Mason39279cc2007-06-12 06:35:45 -04003761 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003762 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003763 goto no_delete;
3764 }
Al Virobd555972010-06-07 11:35:40 -04003765 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003766 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003767
Yan, Zhengc71bf092009-11-12 09:34:40 +00003768 if (root->fs_info->log_root_recovering) {
3769 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3770 goto no_delete;
3771 }
3772
Yan, Zheng76dda932009-09-21 16:00:26 -04003773 if (inode->i_nlink > 0) {
3774 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3775 goto no_delete;
3776 }
3777
Chris Masondbe674a2008-07-17 12:54:05 -04003778 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003779
Yan, Zheng80825102009-11-12 09:35:36 +00003780 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003781 trans = btrfs_start_transaction(root, 0);
3782 BUG_ON(IS_ERR(trans));
Yan, Zhengd68fc572010-05-16 10:49:58 -04003783 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003784
Yan, Zhengd68fc572010-05-16 10:49:58 -04003785 ret = btrfs_block_rsv_check(trans, root,
3786 root->orphan_block_rsv, 0, 5);
3787 if (ret) {
3788 BUG_ON(ret != -EAGAIN);
3789 ret = btrfs_commit_transaction(trans, root);
3790 BUG_ON(ret);
3791 continue;
3792 }
3793
3794 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003795 if (ret != -EAGAIN)
3796 break;
3797
3798 nr = trans->blocks_used;
3799 btrfs_end_transaction(trans, root);
3800 trans = NULL;
3801 btrfs_btree_balance_dirty(root, nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003802
Josef Bacik7b128762008-07-24 12:17:14 -04003803 }
3804
Yan, Zheng80825102009-11-12 09:35:36 +00003805 if (ret == 0) {
3806 ret = btrfs_orphan_del(trans, inode);
3807 BUG_ON(ret);
3808 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003809
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003810 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003811 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003812 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003813no_delete:
Al Virobd555972010-06-07 11:35:40 -04003814 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003815 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003816}
3817
3818/*
3819 * this returns the key found in the dir entry in the location pointer.
3820 * If no dir entries were found, location->objectid is 0.
3821 */
3822static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3823 struct btrfs_key *location)
3824{
3825 const char *name = dentry->d_name.name;
3826 int namelen = dentry->d_name.len;
3827 struct btrfs_dir_item *di;
3828 struct btrfs_path *path;
3829 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003830 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003831
3832 path = btrfs_alloc_path();
3833 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003834
Chris Mason39279cc2007-06-12 06:35:45 -04003835 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3836 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003837 if (IS_ERR(di))
3838 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003839
3840 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003841 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003842
Chris Mason5f39d392007-10-15 16:14:19 -04003843 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003844out:
Chris Mason39279cc2007-06-12 06:35:45 -04003845 btrfs_free_path(path);
3846 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003847out_err:
3848 location->objectid = 0;
3849 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003850}
3851
3852/*
3853 * when we hit a tree root in a directory, the btrfs part of the inode
3854 * needs to be changed to reflect the root directory of the tree root. This
3855 * is kind of like crossing a mount point.
3856 */
3857static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003858 struct inode *dir,
3859 struct dentry *dentry,
3860 struct btrfs_key *location,
3861 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003862{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003863 struct btrfs_path *path;
3864 struct btrfs_root *new_root;
3865 struct btrfs_root_ref *ref;
3866 struct extent_buffer *leaf;
3867 int ret;
3868 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003869
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003870 path = btrfs_alloc_path();
3871 if (!path) {
3872 err = -ENOMEM;
3873 goto out;
3874 }
Chris Mason39279cc2007-06-12 06:35:45 -04003875
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003876 err = -ENOENT;
3877 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3878 BTRFS_I(dir)->root->root_key.objectid,
3879 location->objectid);
3880 if (ret) {
3881 if (ret < 0)
3882 err = ret;
3883 goto out;
3884 }
Chris Mason39279cc2007-06-12 06:35:45 -04003885
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003886 leaf = path->nodes[0];
3887 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3888 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3889 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3890 goto out;
3891
3892 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3893 (unsigned long)(ref + 1),
3894 dentry->d_name.len);
3895 if (ret)
3896 goto out;
3897
3898 btrfs_release_path(root->fs_info->tree_root, path);
3899
3900 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3901 if (IS_ERR(new_root)) {
3902 err = PTR_ERR(new_root);
3903 goto out;
3904 }
3905
3906 if (btrfs_root_refs(&new_root->root_item) == 0) {
3907 err = -ENOENT;
3908 goto out;
3909 }
3910
3911 *sub_root = new_root;
3912 location->objectid = btrfs_root_dirid(&new_root->root_item);
3913 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003914 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003915 err = 0;
3916out:
3917 btrfs_free_path(path);
3918 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003919}
3920
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003921static void inode_tree_add(struct inode *inode)
3922{
3923 struct btrfs_root *root = BTRFS_I(inode)->root;
3924 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003925 struct rb_node **p;
3926 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003927again:
3928 p = &root->inode_tree.rb_node;
3929 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003930
Al Viro1d3382cb2010-10-23 15:19:20 -04003931 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003932 return;
3933
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003934 spin_lock(&root->inode_lock);
3935 while (*p) {
3936 parent = *p;
3937 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3938
3939 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003940 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003941 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003942 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003943 else {
3944 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003945 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003946 rb_erase(parent, &root->inode_tree);
3947 RB_CLEAR_NODE(parent);
3948 spin_unlock(&root->inode_lock);
3949 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003950 }
3951 }
3952 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3953 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3954 spin_unlock(&root->inode_lock);
3955}
3956
3957static void inode_tree_del(struct inode *inode)
3958{
3959 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003960 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003961
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003962 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003963 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003964 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003965 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003966 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003967 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003968 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003969
Josef Bacik0af3d002010-06-21 14:48:16 -04003970 /*
3971 * Free space cache has inodes in the tree root, but the tree root has a
3972 * root_refs of 0, so this could end up dropping the tree root as a
3973 * snapshot, so we need the extra !root->fs_info->tree_root check to
3974 * make sure we don't drop it.
3975 */
3976 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3977 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003978 synchronize_srcu(&root->fs_info->subvol_srcu);
3979 spin_lock(&root->inode_lock);
3980 empty = RB_EMPTY_ROOT(&root->inode_tree);
3981 spin_unlock(&root->inode_lock);
3982 if (empty)
3983 btrfs_add_dead_root(root);
3984 }
3985}
3986
3987int btrfs_invalidate_inodes(struct btrfs_root *root)
3988{
3989 struct rb_node *node;
3990 struct rb_node *prev;
3991 struct btrfs_inode *entry;
3992 struct inode *inode;
3993 u64 objectid = 0;
3994
3995 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3996
3997 spin_lock(&root->inode_lock);
3998again:
3999 node = root->inode_tree.rb_node;
4000 prev = NULL;
4001 while (node) {
4002 prev = node;
4003 entry = rb_entry(node, struct btrfs_inode, rb_node);
4004
4005 if (objectid < entry->vfs_inode.i_ino)
4006 node = node->rb_left;
4007 else if (objectid > entry->vfs_inode.i_ino)
4008 node = node->rb_right;
4009 else
4010 break;
4011 }
4012 if (!node) {
4013 while (prev) {
4014 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4015 if (objectid <= entry->vfs_inode.i_ino) {
4016 node = prev;
4017 break;
4018 }
4019 prev = rb_next(prev);
4020 }
4021 }
4022 while (node) {
4023 entry = rb_entry(node, struct btrfs_inode, rb_node);
4024 objectid = entry->vfs_inode.i_ino + 1;
4025 inode = igrab(&entry->vfs_inode);
4026 if (inode) {
4027 spin_unlock(&root->inode_lock);
4028 if (atomic_read(&inode->i_count) > 1)
4029 d_prune_aliases(inode);
4030 /*
Al Viro45321ac2010-06-07 13:43:19 -04004031 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004032 * the inode cache when its usage count
4033 * hits zero.
4034 */
4035 iput(inode);
4036 cond_resched();
4037 spin_lock(&root->inode_lock);
4038 goto again;
4039 }
4040
4041 if (cond_resched_lock(&root->inode_lock))
4042 goto again;
4043
4044 node = rb_next(node);
4045 }
4046 spin_unlock(&root->inode_lock);
4047 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004048}
4049
Chris Masone02119d2008-09-05 16:13:11 -04004050static int btrfs_init_locked_inode(struct inode *inode, void *p)
4051{
4052 struct btrfs_iget_args *args = p;
4053 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004054 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004055 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004056 return 0;
4057}
4058
4059static int btrfs_find_actor(struct inode *inode, void *opaque)
4060{
4061 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05004062 return args->ino == inode->i_ino &&
4063 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004064}
4065
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004066static struct inode *btrfs_iget_locked(struct super_block *s,
4067 u64 objectid,
4068 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004069{
4070 struct inode *inode;
4071 struct btrfs_iget_args args;
4072 args.ino = objectid;
4073 args.root = root;
4074
4075 inode = iget5_locked(s, objectid, btrfs_find_actor,
4076 btrfs_init_locked_inode,
4077 (void *)&args);
4078 return inode;
4079}
4080
Balaji Rao1a54ef82008-07-21 02:01:04 +05304081/* Get an inode object given its location and corresponding root.
4082 * Returns in *is_new if the inode was read from disk
4083 */
4084struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004085 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304086{
4087 struct inode *inode;
4088
4089 inode = btrfs_iget_locked(s, location->objectid, root);
4090 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004091 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304092
4093 if (inode->i_state & I_NEW) {
4094 BTRFS_I(inode)->root = root;
4095 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4096 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004097 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304098 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00004099 if (new)
4100 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05304101 }
4102
4103 return inode;
4104}
4105
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004106static struct inode *new_simple_dir(struct super_block *s,
4107 struct btrfs_key *key,
4108 struct btrfs_root *root)
4109{
4110 struct inode *inode = new_inode(s);
4111
4112 if (!inode)
4113 return ERR_PTR(-ENOMEM);
4114
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115 BTRFS_I(inode)->root = root;
4116 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4117 BTRFS_I(inode)->dummy_inode = 1;
4118
4119 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4120 inode->i_op = &simple_dir_inode_operations;
4121 inode->i_fop = &simple_dir_operations;
4122 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4123 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4124
4125 return inode;
4126}
4127
Chris Mason3de45862008-11-17 21:02:50 -05004128struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004129{
Chris Masond3977122009-01-05 21:25:51 -05004130 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004131 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004132 struct btrfs_root *sub_root = root;
4133 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004134 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004135 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004136
4137 if (dentry->d_name.len > BTRFS_NAME_LEN)
4138 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004139
Chris Mason39279cc2007-06-12 06:35:45 -04004140 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04004141
Chris Mason39279cc2007-06-12 06:35:45 -04004142 if (ret < 0)
4143 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004144
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004145 if (location.objectid == 0)
4146 return NULL;
4147
4148 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004149 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004150 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004151 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004152
4153 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4154
Yan, Zheng76dda932009-09-21 16:00:26 -04004155 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004156 ret = fixup_tree_root_location(root, dir, dentry,
4157 &location, &sub_root);
4158 if (ret < 0) {
4159 if (ret != -ENOENT)
4160 inode = ERR_PTR(ret);
4161 else
4162 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4163 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004164 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004165 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004166 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4167
Julia Lawall34d19ba2011-01-24 19:55:19 +00004168 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004169 down_read(&root->fs_info->cleanup_work_sem);
4170 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004171 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004172 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05004173 if (ret)
4174 inode = ERR_PTR(ret);
Yan, Zhengc71bf092009-11-12 09:34:40 +00004175 }
4176
Chris Mason3de45862008-11-17 21:02:50 -05004177 return inode;
4178}
4179
Nick Pigginfe15ce42011-01-07 17:49:23 +11004180static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004181{
4182 struct btrfs_root *root;
4183
Yan, Zhengefefb142009-10-09 09:25:16 -04004184 if (!dentry->d_inode && !IS_ROOT(dentry))
4185 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004186
Yan, Zhengefefb142009-10-09 09:25:16 -04004187 if (dentry->d_inode) {
4188 root = BTRFS_I(dentry->d_inode)->root;
4189 if (btrfs_root_refs(&root->root_item) == 0)
4190 return 1;
4191 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004192 return 0;
4193}
4194
Chris Mason3de45862008-11-17 21:02:50 -05004195static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4196 struct nameidata *nd)
4197{
4198 struct inode *inode;
4199
Chris Mason3de45862008-11-17 21:02:50 -05004200 inode = btrfs_lookup_dentry(dir, dentry);
4201 if (IS_ERR(inode))
4202 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004203
Chris Mason39279cc2007-06-12 06:35:45 -04004204 return d_splice_alias(inode, dentry);
4205}
4206
Chris Mason39279cc2007-06-12 06:35:45 -04004207static unsigned char btrfs_filetype_table[] = {
4208 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4209};
4210
David Woodhousecbdf5a22008-08-06 19:42:33 +01004211static int btrfs_real_readdir(struct file *filp, void *dirent,
4212 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004213{
Chris Mason6da6aba2007-12-18 16:15:09 -05004214 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004215 struct btrfs_root *root = BTRFS_I(inode)->root;
4216 struct btrfs_item *item;
4217 struct btrfs_dir_item *di;
4218 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004219 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004220 struct btrfs_path *path;
4221 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004222 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004223 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04004224 unsigned char d_type;
4225 int over = 0;
4226 u32 di_cur;
4227 u32 di_total;
4228 u32 di_len;
4229 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004230 char tmp_name[32];
4231 char *name_ptr;
4232 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04004233
4234 /* FIXME, use a real flag for deciding about the key type */
4235 if (root->fs_info->tree_root == root)
4236 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004237
Chris Mason39544012007-12-12 14:38:19 -05004238 /* special case for "." */
4239 if (filp->f_pos == 0) {
4240 over = filldir(dirent, ".", 1,
4241 1, inode->i_ino,
4242 DT_DIR);
4243 if (over)
4244 return 0;
4245 filp->f_pos = 1;
4246 }
Chris Mason39544012007-12-12 14:38:19 -05004247 /* special case for .., just use the back ref */
4248 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004249 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004250 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004251 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004252 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004253 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004254 filp->f_pos = 2;
4255 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004256 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04004257 if (!path)
4258 return -ENOMEM;
4259 path->reada = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01004260
Chris Mason39279cc2007-06-12 06:35:45 -04004261 btrfs_set_key_type(&key, key_type);
4262 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01004263 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04004264
Chris Mason39279cc2007-06-12 06:35:45 -04004265 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4266 if (ret < 0)
4267 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01004268
4269 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004270 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04004271 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08004272 if (slot >= btrfs_header_nritems(leaf)) {
4273 ret = btrfs_next_leaf(root, path);
4274 if (ret < 0)
4275 goto err;
4276 else if (ret > 0)
4277 break;
4278 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04004279 }
Chris Mason3de45862008-11-17 21:02:50 -05004280
Chris Mason5f39d392007-10-15 16:14:19 -04004281 item = btrfs_item_nr(leaf, slot);
4282 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4283
4284 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004285 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004286 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004287 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004288 if (found_key.offset < filp->f_pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08004289 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04004290
4291 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01004292
Chris Mason39279cc2007-06-12 06:35:45 -04004293 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4294 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004295 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004296
4297 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004298 struct btrfs_key location;
4299
Josef Bacik22a94d42011-03-16 16:47:17 -04004300 if (verify_dir_item(root, leaf, di))
4301 break;
4302
Chris Mason5f39d392007-10-15 16:14:19 -04004303 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004304 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004305 name_ptr = tmp_name;
4306 } else {
4307 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004308 if (!name_ptr) {
4309 ret = -ENOMEM;
4310 goto err;
4311 }
Chris Mason5f39d392007-10-15 16:14:19 -04004312 }
4313 read_extent_buffer(leaf, name_ptr,
4314 (unsigned long)(di + 1), name_len);
4315
4316 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4317 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004318
4319 /* is this a reference to our own snapshot? If so
4320 * skip it
4321 */
4322 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4323 location.objectid == root->root_key.objectid) {
4324 over = 0;
4325 goto skip;
4326 }
Chris Mason5f39d392007-10-15 16:14:19 -04004327 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004328 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004329 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004330
Chris Mason3de45862008-11-17 21:02:50 -05004331skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004332 if (name_ptr != tmp_name)
4333 kfree(name_ptr);
4334
Chris Mason39279cc2007-06-12 06:35:45 -04004335 if (over)
4336 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004337 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004338 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004339 di_cur += di_len;
4340 di = (struct btrfs_dir_item *)((char *)di + di_len);
4341 }
Li Zefanb9e03af2011-03-23 10:43:58 +08004342next:
4343 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04004344 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004345
4346 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004347 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004348 /*
4349 * 32-bit glibc will use getdents64, but then strtol -
4350 * so the last number we can serve is this.
4351 */
4352 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004353 else
4354 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004355nopos:
4356 ret = 0;
4357err:
Chris Mason39279cc2007-06-12 06:35:45 -04004358 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004359 return ret;
4360}
4361
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004362int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004363{
4364 struct btrfs_root *root = BTRFS_I(inode)->root;
4365 struct btrfs_trans_handle *trans;
4366 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004367 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004368
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004369 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004370 return 0;
4371
Josef Bacik0af3d002010-06-21 14:48:16 -04004372 smp_mb();
4373 nolock = (root->fs_info->closing && root == root->fs_info->tree_root);
4374
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004375 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004376 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004377 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04004378 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004379 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004380 if (IS_ERR(trans))
4381 return PTR_ERR(trans);
Josef Bacik0af3d002010-06-21 14:48:16 -04004382 if (nolock)
4383 ret = btrfs_end_transaction_nolock(trans, root);
4384 else
4385 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004386 }
4387 return ret;
4388}
4389
4390/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004391 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004392 * inode changes. But, it is most likely to find the inode in cache.
4393 * FIXME, needs more benchmarking...there are no reasons other than performance
4394 * to keep or drop this code.
4395 */
4396void btrfs_dirty_inode(struct inode *inode)
4397{
4398 struct btrfs_root *root = BTRFS_I(inode)->root;
4399 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004400 int ret;
4401
4402 if (BTRFS_I(inode)->dummy_inode)
4403 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004404
Josef Bacik7a7eaa42011-04-13 12:54:33 -04004405 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004406 BUG_ON(IS_ERR(trans));
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004407
4408 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004409 if (ret && ret == -ENOSPC) {
4410 /* whoops, lets try again with the full transaction */
4411 btrfs_end_transaction(trans, root);
4412 trans = btrfs_start_transaction(root, 1);
Chris Mason9aeead72010-05-27 10:23:00 -04004413 if (IS_ERR(trans)) {
4414 if (printk_ratelimit()) {
4415 printk(KERN_ERR "btrfs: fail to "
4416 "dirty inode %lu error %ld\n",
4417 inode->i_ino, PTR_ERR(trans));
4418 }
4419 return;
4420 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004421
Chris Mason94b60442010-05-26 11:02:00 -04004422 ret = btrfs_update_inode(trans, root, inode);
4423 if (ret) {
Chris Mason9aeead72010-05-27 10:23:00 -04004424 if (printk_ratelimit()) {
4425 printk(KERN_ERR "btrfs: fail to "
4426 "dirty inode %lu error %d\n",
4427 inode->i_ino, ret);
4428 }
Chris Mason94b60442010-05-26 11:02:00 -04004429 }
4430 }
Chris Mason39279cc2007-06-12 06:35:45 -04004431 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004432}
4433
Chris Masond352ac62008-09-29 15:18:18 -04004434/*
4435 * find the highest existing sequence number in a directory
4436 * and then set the in-memory index_cnt variable to reflect
4437 * free sequence numbers
4438 */
Josef Bacikaec74772008-07-24 12:12:38 -04004439static int btrfs_set_inode_index_count(struct inode *inode)
4440{
4441 struct btrfs_root *root = BTRFS_I(inode)->root;
4442 struct btrfs_key key, found_key;
4443 struct btrfs_path *path;
4444 struct extent_buffer *leaf;
4445 int ret;
4446
4447 key.objectid = inode->i_ino;
4448 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4449 key.offset = (u64)-1;
4450
4451 path = btrfs_alloc_path();
4452 if (!path)
4453 return -ENOMEM;
4454
4455 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4456 if (ret < 0)
4457 goto out;
4458 /* FIXME: we should be able to handle this */
4459 if (ret == 0)
4460 goto out;
4461 ret = 0;
4462
4463 /*
4464 * MAGIC NUMBER EXPLANATION:
4465 * since we search a directory based on f_pos we have to start at 2
4466 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4467 * else has to start at 2
4468 */
4469 if (path->slots[0] == 0) {
4470 BTRFS_I(inode)->index_cnt = 2;
4471 goto out;
4472 }
4473
4474 path->slots[0]--;
4475
4476 leaf = path->nodes[0];
4477 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4478
4479 if (found_key.objectid != inode->i_ino ||
4480 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4481 BTRFS_I(inode)->index_cnt = 2;
4482 goto out;
4483 }
4484
4485 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4486out:
4487 btrfs_free_path(path);
4488 return ret;
4489}
4490
Chris Masond352ac62008-09-29 15:18:18 -04004491/*
4492 * helper to find a free sequence number in a given directory. This current
4493 * code is very simple, later versions will do smarter things in the btree
4494 */
Chris Mason3de45862008-11-17 21:02:50 -05004495int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004496{
4497 int ret = 0;
4498
4499 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4500 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004501 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004502 return ret;
4503 }
4504
Chris Mason00e4e6b2008-08-05 11:18:09 -04004505 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004506 BTRFS_I(dir)->index_cnt++;
4507
4508 return ret;
4509}
4510
Chris Mason39279cc2007-06-12 06:35:45 -04004511static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4512 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004513 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004514 const char *name, int name_len,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004515 u64 ref_objectid, u64 objectid, int mode,
4516 u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004517{
4518 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004519 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004520 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004521 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004522 struct btrfs_inode_ref *ref;
4523 struct btrfs_key key[2];
4524 u32 sizes[2];
4525 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004526 int ret;
4527 int owner;
4528
Chris Mason5f39d392007-10-15 16:14:19 -04004529 path = btrfs_alloc_path();
4530 BUG_ON(!path);
4531
Chris Mason39279cc2007-06-12 06:35:45 -04004532 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004533 if (!inode) {
4534 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004535 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004536 }
Chris Mason39279cc2007-06-12 06:35:45 -04004537
Josef Bacikaec74772008-07-24 12:12:38 -04004538 if (dir) {
liubo1abe9b82011-03-24 11:18:59 +00004539 trace_btrfs_inode_request(dir);
4540
Chris Mason3de45862008-11-17 21:02:50 -05004541 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004542 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00004543 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004544 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004545 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004546 }
Josef Bacikaec74772008-07-24 12:12:38 -04004547 }
4548 /*
4549 * index_cnt is ignored for everything but a dir,
4550 * btrfs_get_inode_index_count has an explanation for the magic
4551 * number
4552 */
4553 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004554 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004555 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004556 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004557 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004558
Chris Mason39279cc2007-06-12 06:35:45 -04004559 if (mode & S_IFDIR)
4560 owner = 0;
4561 else
4562 owner = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004563
4564 key[0].objectid = objectid;
4565 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4566 key[0].offset = 0;
4567
4568 key[1].objectid = objectid;
4569 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4570 key[1].offset = ref_objectid;
4571
4572 sizes[0] = sizeof(struct btrfs_inode_item);
4573 sizes[1] = name_len + sizeof(*ref);
4574
Chris Masonb9473432009-03-13 11:00:37 -04004575 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004576 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4577 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004578 goto fail;
4579
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004580 inode_init_owner(inode, dir, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04004581 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004582 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004583 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004584 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4585 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004586 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004587
4588 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4589 struct btrfs_inode_ref);
4590 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004591 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004592 ptr = (unsigned long)(ref + 1);
4593 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4594
Chris Mason5f39d392007-10-15 16:14:19 -04004595 btrfs_mark_buffer_dirty(path->nodes[0]);
4596 btrfs_free_path(path);
4597
Chris Mason39279cc2007-06-12 06:35:45 -04004598 location = &BTRFS_I(inode)->location;
4599 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004600 location->offset = 0;
4601 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4602
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004603 btrfs_inherit_iflags(inode, dir);
4604
Chris Mason94272162009-07-02 12:26:06 -04004605 if ((mode & S_IFREG)) {
4606 if (btrfs_test_opt(root, NODATASUM))
4607 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Liu Bo75e7cb72011-03-22 10:12:20 +00004608 if (btrfs_test_opt(root, NODATACOW) ||
4609 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
Chris Mason94272162009-07-02 12:26:06 -04004610 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4611 }
4612
Chris Mason39279cc2007-06-12 06:35:45 -04004613 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004614 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00004615
4616 trace_btrfs_inode_new(inode);
4617
Chris Mason39279cc2007-06-12 06:35:45 -04004618 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004619fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004620 if (dir)
4621 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004622 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004623 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004624 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004625}
4626
4627static inline u8 btrfs_inode_type(struct inode *inode)
4628{
4629 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4630}
4631
Chris Masond352ac62008-09-29 15:18:18 -04004632/*
4633 * utility function to add 'inode' into 'parent_inode' with
4634 * a give name and a given sequence number.
4635 * if 'add_backref' is true, also insert a backref from the
4636 * inode to the parent directory.
4637 */
Chris Masone02119d2008-09-05 16:13:11 -04004638int btrfs_add_link(struct btrfs_trans_handle *trans,
4639 struct inode *parent_inode, struct inode *inode,
4640 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004641{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004642 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004643 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004644 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004645
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004646 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4647 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4648 } else {
4649 key.objectid = inode->i_ino;
4650 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4651 key.offset = 0;
4652 }
Chris Mason39279cc2007-06-12 06:35:45 -04004653
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004654 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4655 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4656 key.objectid, root->root_key.objectid,
4657 parent_inode->i_ino,
4658 index, name, name_len);
4659 } else if (add_backref) {
4660 ret = btrfs_insert_inode_ref(trans, root,
4661 name, name_len, inode->i_ino,
4662 parent_inode->i_ino, index);
4663 }
4664
Chris Mason39279cc2007-06-12 06:35:45 -04004665 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004666 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4667 parent_inode->i_ino, &key,
4668 btrfs_inode_type(inode), index);
4669 BUG_ON(ret);
4670
Chris Masondbe674a2008-07-17 12:54:05 -04004671 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004672 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004673 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004674 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004675 }
4676 return ret;
4677}
4678
4679static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004680 struct inode *dir, struct dentry *dentry,
4681 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004682{
Josef Bacika1b075d2010-11-19 20:36:11 +00004683 int err = btrfs_add_link(trans, dir, inode,
4684 dentry->d_name.name, dentry->d_name.len,
4685 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004686 if (!err) {
4687 d_instantiate(dentry, inode);
4688 return 0;
4689 }
4690 if (err > 0)
4691 err = -EEXIST;
4692 return err;
4693}
4694
Josef Bacik618e21d2007-07-11 10:18:17 -04004695static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4696 int mode, dev_t rdev)
4697{
4698 struct btrfs_trans_handle *trans;
4699 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004700 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004701 int err;
4702 int drop_inode = 0;
4703 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004704 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004705 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004706
4707 if (!new_valid_dev(rdev))
4708 return -EINVAL;
4709
Yan, Zhenga22285a2010-05-16 10:48:46 -04004710 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4711 if (err)
4712 return err;
4713
Josef Bacik9ed74f22009-09-11 16:12:44 -04004714 /*
4715 * 2 for inode item and ref
4716 * 2 for dir items
4717 * 1 for xattr if selinux is on
4718 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004719 trans = btrfs_start_transaction(root, 5);
4720 if (IS_ERR(trans))
4721 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004722
Josef Bacikaec74772008-07-24 12:12:38 -04004723 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004724 dentry->d_name.len, dir->i_ino, objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004725 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004726 if (IS_ERR(inode)) {
4727 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004728 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004729 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004730
Eric Paris2a7dba32011-02-01 11:05:39 -05004731 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004732 if (err) {
4733 drop_inode = 1;
4734 goto out_unlock;
4735 }
4736
Josef Bacika1b075d2010-11-19 20:36:11 +00004737 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004738 if (err)
4739 drop_inode = 1;
4740 else {
4741 inode->i_op = &btrfs_special_inode_operations;
4742 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004743 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004744 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004745out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004746 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004747 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004748 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004749 if (drop_inode) {
4750 inode_dec_link_count(inode);
4751 iput(inode);
4752 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004753 return err;
4754}
4755
Chris Mason39279cc2007-06-12 06:35:45 -04004756static int btrfs_create(struct inode *dir, struct dentry *dentry,
4757 int mode, struct nameidata *nd)
4758{
4759 struct btrfs_trans_handle *trans;
4760 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004761 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004762 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004763 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004764 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004765 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004766 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004767
Yan, Zhenga22285a2010-05-16 10:48:46 -04004768 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4769 if (err)
4770 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004771 /*
4772 * 2 for inode item and ref
4773 * 2 for dir items
4774 * 1 for xattr if selinux is on
4775 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004776 trans = btrfs_start_transaction(root, 5);
4777 if (IS_ERR(trans))
4778 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004779
Josef Bacikaec74772008-07-24 12:12:38 -04004780 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004781 dentry->d_name.len, dir->i_ino, objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004782 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004783 if (IS_ERR(inode)) {
4784 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004785 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04004786 }
Chris Mason39279cc2007-06-12 06:35:45 -04004787
Eric Paris2a7dba32011-02-01 11:05:39 -05004788 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004789 if (err) {
4790 drop_inode = 1;
4791 goto out_unlock;
4792 }
4793
Josef Bacika1b075d2010-11-19 20:36:11 +00004794 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004795 if (err)
4796 drop_inode = 1;
4797 else {
4798 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004799 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004800 inode->i_fop = &btrfs_file_operations;
4801 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004802 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004803 }
Chris Mason39279cc2007-06-12 06:35:45 -04004804out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004805 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004806 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004807 if (drop_inode) {
4808 inode_dec_link_count(inode);
4809 iput(inode);
4810 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004811 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004812 return err;
4813}
4814
4815static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4816 struct dentry *dentry)
4817{
4818 struct btrfs_trans_handle *trans;
4819 struct btrfs_root *root = BTRFS_I(dir)->root;
4820 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004821 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004822 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004823 int err;
4824 int drop_inode = 0;
4825
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004826 /* do not allow sys_link's with other subvols of the same device */
4827 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00004828 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004829
Al Viroc055e992011-03-04 17:15:18 +00004830 if (inode->i_nlink == ~0U)
4831 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004832
Chris Mason3de45862008-11-17 21:02:50 -05004833 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004834 if (err)
4835 goto fail;
4836
Yan, Zhenga22285a2010-05-16 10:48:46 -04004837 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00004838 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04004839 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00004840 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04004841 */
Miao Xie7e6b6462011-02-18 09:21:17 +00004842 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004843 if (IS_ERR(trans)) {
4844 err = PTR_ERR(trans);
4845 goto fail;
4846 }
Chris Mason5f39d392007-10-15 16:14:19 -04004847
Miao Xie31534952011-04-13 13:19:21 +08004848 btrfs_inc_nlink(inode);
4849 inode->i_ctime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -04004850 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004851
Josef Bacika1b075d2010-11-19 20:36:11 +00004852 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004853
Yan, Zhenga5719522009-09-24 09:17:31 -04004854 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004855 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004856 } else {
Josef Bacik6a912212010-11-20 09:48:00 +00004857 struct dentry *parent = dget_parent(dentry);
Yan, Zhenga5719522009-09-24 09:17:31 -04004858 err = btrfs_update_inode(trans, root, inode);
4859 BUG_ON(err);
Josef Bacik6a912212010-11-20 09:48:00 +00004860 btrfs_log_new_name(trans, inode, NULL, parent);
4861 dput(parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004862 }
Chris Mason39279cc2007-06-12 06:35:45 -04004863
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004864 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004865 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004866fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004867 if (drop_inode) {
4868 inode_dec_link_count(inode);
4869 iput(inode);
4870 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004871 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004872 return err;
4873}
4874
Chris Mason39279cc2007-06-12 06:35:45 -04004875static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4876{
Chris Masonb9d86662008-05-02 16:13:49 -04004877 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004878 struct btrfs_trans_handle *trans;
4879 struct btrfs_root *root = BTRFS_I(dir)->root;
4880 int err = 0;
4881 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004882 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004883 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004884 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004885
Yan, Zhenga22285a2010-05-16 10:48:46 -04004886 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4887 if (err)
4888 return err;
4889
Josef Bacik9ed74f22009-09-11 16:12:44 -04004890 /*
4891 * 2 items for inode and ref
4892 * 2 items for dir items
4893 * 1 for xattr if selinux is on
4894 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004895 trans = btrfs_start_transaction(root, 5);
4896 if (IS_ERR(trans))
4897 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04004898
Josef Bacikaec74772008-07-24 12:12:38 -04004899 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004900 dentry->d_name.len, dir->i_ino, objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04004901 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004902 if (IS_ERR(inode)) {
4903 err = PTR_ERR(inode);
4904 goto out_fail;
4905 }
Chris Mason5f39d392007-10-15 16:14:19 -04004906
Chris Mason39279cc2007-06-12 06:35:45 -04004907 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004908
Eric Paris2a7dba32011-02-01 11:05:39 -05004909 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04004910 if (err)
4911 goto out_fail;
4912
Chris Mason39279cc2007-06-12 06:35:45 -04004913 inode->i_op = &btrfs_dir_inode_operations;
4914 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04004915
Chris Masondbe674a2008-07-17 12:54:05 -04004916 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004917 err = btrfs_update_inode(trans, root, inode);
4918 if (err)
4919 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004920
Josef Bacika1b075d2010-11-19 20:36:11 +00004921 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4922 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004923 if (err)
4924 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004925
Chris Mason39279cc2007-06-12 06:35:45 -04004926 d_instantiate(dentry, inode);
4927 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004928
4929out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004930 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004931 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004932 if (drop_on_err)
4933 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004934 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004935 return err;
4936}
4937
Chris Masond352ac62008-09-29 15:18:18 -04004938/* helper for btfs_get_extent. Given an existing extent in the tree,
4939 * and an extent that you want to insert, deal with overlap and insert
4940 * the new extent into the tree.
4941 */
Chris Mason3b951512008-04-17 11:29:12 -04004942static int merge_extent_mapping(struct extent_map_tree *em_tree,
4943 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004944 struct extent_map *em,
4945 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004946{
4947 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004948
Chris Masone6dcd2d2008-07-17 12:53:50 -04004949 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4950 start_diff = map_start - em->start;
4951 em->start = map_start;
4952 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004953 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4954 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004955 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004956 em->block_len -= start_diff;
4957 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004958 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004959}
4960
Chris Masonc8b97812008-10-29 14:49:59 -04004961static noinline int uncompress_inline(struct btrfs_path *path,
4962 struct inode *inode, struct page *page,
4963 size_t pg_offset, u64 extent_offset,
4964 struct btrfs_file_extent_item *item)
4965{
4966 int ret;
4967 struct extent_buffer *leaf = path->nodes[0];
4968 char *tmp;
4969 size_t max_size;
4970 unsigned long inline_size;
4971 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08004972 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04004973
4974 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08004975 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04004976 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4977 inline_size = btrfs_file_extent_inline_item_len(leaf,
4978 btrfs_item_nr(leaf, path->slots[0]));
4979 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04004980 if (!tmp)
4981 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04004982 ptr = btrfs_file_extent_inline_start(item);
4983
4984 read_extent_buffer(leaf, tmp, ptr, inline_size);
4985
Chris Mason5b050f02008-11-11 09:34:41 -05004986 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08004987 ret = btrfs_decompress(compress_type, tmp, page,
4988 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004989 if (ret) {
4990 char *kaddr = kmap_atomic(page, KM_USER0);
4991 unsigned long copy_size = min_t(u64,
4992 PAGE_CACHE_SIZE - pg_offset,
4993 max_size - extent_offset);
4994 memset(kaddr + pg_offset, 0, copy_size);
4995 kunmap_atomic(kaddr, KM_USER0);
4996 }
4997 kfree(tmp);
4998 return 0;
4999}
5000
Chris Masond352ac62008-09-29 15:18:18 -04005001/*
5002 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05005003 * the ugly parts come from merging extents from the disk with the in-ram
5004 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04005005 * where the in-ram extents might be locked pending data=ordered completion.
5006 *
5007 * This also copies inline extents directly into the page.
5008 */
Chris Masond3977122009-01-05 21:25:51 -05005009
Chris Masona52d9a82007-08-27 16:49:44 -04005010struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05005011 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04005012 int create)
5013{
5014 int ret;
5015 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005016 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005017 u64 extent_start = 0;
5018 u64 extent_end = 0;
5019 u64 objectid = inode->i_ino;
5020 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005021 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005022 struct btrfs_root *root = BTRFS_I(inode)->root;
5023 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005024 struct extent_buffer *leaf;
5025 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005026 struct extent_map *em = NULL;
5027 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005028 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005029 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005030 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005031
Chris Masona52d9a82007-08-27 16:49:44 -04005032again:
Chris Mason890871b2009-09-02 16:24:52 -04005033 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005034 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005035 if (em)
5036 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005037 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005038
Chris Masona52d9a82007-08-27 16:49:44 -04005039 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005040 if (em->start > start || em->start + em->len <= start)
5041 free_extent_map(em);
5042 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005043 free_extent_map(em);
5044 else
5045 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005046 }
Chris Masond1310b22008-01-24 16:13:08 -05005047 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005048 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005049 err = -ENOMEM;
5050 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005051 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005052 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005053 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005054 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005055 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005056 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005057
5058 if (!path) {
5059 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04005060 if (!path) {
5061 err = -ENOMEM;
5062 goto out;
5063 }
5064 /*
5065 * Chances are we'll be called again, so go ahead and do
5066 * readahead
5067 */
5068 path->reada = 1;
Chris Masonf4219502008-07-22 11:18:09 -04005069 }
5070
Chris Mason179e29e2007-11-01 11:28:41 -04005071 ret = btrfs_lookup_file_extent(trans, root, path,
5072 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005073 if (ret < 0) {
5074 err = ret;
5075 goto out;
5076 }
5077
5078 if (ret != 0) {
5079 if (path->slots[0] == 0)
5080 goto not_found;
5081 path->slots[0]--;
5082 }
5083
Chris Mason5f39d392007-10-15 16:14:19 -04005084 leaf = path->nodes[0];
5085 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005086 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005087 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005088 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5089 found_type = btrfs_key_type(&found_key);
5090 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005091 found_type != BTRFS_EXTENT_DATA_KEY) {
5092 goto not_found;
5093 }
5094
Chris Mason5f39d392007-10-15 16:14:19 -04005095 found_type = btrfs_file_extent_type(leaf, item);
5096 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005097 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005098 if (found_type == BTRFS_FILE_EXTENT_REG ||
5099 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005100 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005101 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005102 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5103 size_t size;
5104 size = btrfs_file_extent_inline_len(leaf, item);
5105 extent_end = (extent_start + size + root->sectorsize - 1) &
5106 ~((u64)root->sectorsize - 1);
5107 }
5108
5109 if (start >= extent_end) {
5110 path->slots[0]++;
5111 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5112 ret = btrfs_next_leaf(root, path);
5113 if (ret < 0) {
5114 err = ret;
5115 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005116 }
Yan Zheng9036c102008-10-30 14:19:41 -04005117 if (ret > 0)
5118 goto not_found;
5119 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005120 }
Yan Zheng9036c102008-10-30 14:19:41 -04005121 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5122 if (found_key.objectid != objectid ||
5123 found_key.type != BTRFS_EXTENT_DATA_KEY)
5124 goto not_found;
5125 if (start + len <= found_key.offset)
5126 goto not_found;
5127 em->start = start;
5128 em->len = found_key.offset - start;
5129 goto not_found_em;
5130 }
5131
Yan Zhengd899e052008-10-30 14:25:28 -04005132 if (found_type == BTRFS_FILE_EXTENT_REG ||
5133 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005134 em->start = extent_start;
5135 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005136 em->orig_start = extent_start -
5137 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005138 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5139 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005140 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005141 goto insert;
5142 }
Li Zefan261507a02010-12-17 14:21:50 +08005143 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005144 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005145 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005146 em->block_start = bytenr;
5147 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5148 item);
5149 } else {
5150 bytenr += btrfs_file_extent_offset(leaf, item);
5151 em->block_start = bytenr;
5152 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005153 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5154 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005155 }
Chris Masona52d9a82007-08-27 16:49:44 -04005156 goto insert;
5157 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005158 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005159 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005160 size_t size;
5161 size_t extent_offset;
5162 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005163
Chris Masona52d9a82007-08-27 16:49:44 -04005164 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005165 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005166 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005167 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005168 goto out;
5169 }
5170
Yan Zheng9036c102008-10-30 14:19:41 -04005171 size = btrfs_file_extent_inline_len(leaf, item);
5172 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005173 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005174 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005175 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005176 em->len = (copy_size + root->sectorsize - 1) &
5177 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005178 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005179 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005180 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005181 em->compress_type = compress_type;
5182 }
Yan689f9342007-10-29 11:41:07 -04005183 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005184 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005185 if (btrfs_file_extent_compression(leaf, item) !=
5186 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005187 ret = uncompress_inline(path, inode, page,
5188 pg_offset,
5189 extent_offset, item);
5190 BUG_ON(ret);
5191 } else {
5192 map = kmap(page);
5193 read_extent_buffer(leaf, map + pg_offset, ptr,
5194 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005195 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5196 memset(map + pg_offset + copy_size, 0,
5197 PAGE_CACHE_SIZE - pg_offset -
5198 copy_size);
5199 }
Chris Masonc8b97812008-10-29 14:49:59 -04005200 kunmap(page);
5201 }
Chris Mason179e29e2007-11-01 11:28:41 -04005202 flush_dcache_page(page);
5203 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005204 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005205 if (!trans) {
5206 kunmap(page);
5207 free_extent_map(em);
5208 em = NULL;
5209 btrfs_release_path(root, path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005210 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005211 if (IS_ERR(trans))
5212 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005213 goto again;
5214 }
Chris Masonc8b97812008-10-29 14:49:59 -04005215 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005216 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005217 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005218 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005219 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005220 }
Chris Masond1310b22008-01-24 16:13:08 -05005221 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00005222 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005223 goto insert;
5224 } else {
Chris Masond3977122009-01-05 21:25:51 -05005225 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005226 WARN_ON(1);
5227 }
5228not_found:
5229 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005230 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005231not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005232 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005233 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005234insert:
5235 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05005236 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005237 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5238 "[%llu %llu]\n", (unsigned long long)em->start,
5239 (unsigned long long)em->len,
5240 (unsigned long long)start,
5241 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005242 err = -EIO;
5243 goto out;
5244 }
Chris Masond1310b22008-01-24 16:13:08 -05005245
5246 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005247 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005248 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005249 /* it is possible that someone inserted the extent into the tree
5250 * while we had the lock dropped. It is also possible that
5251 * an overlapping map exists in the tree
5252 */
Chris Masona52d9a82007-08-27 16:49:44 -04005253 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005254 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005255
5256 ret = 0;
5257
Chris Mason3b951512008-04-17 11:29:12 -04005258 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005259 if (existing && (existing->start > start ||
5260 existing->start + existing->len <= start)) {
5261 free_extent_map(existing);
5262 existing = NULL;
5263 }
Chris Mason3b951512008-04-17 11:29:12 -04005264 if (!existing) {
5265 existing = lookup_extent_mapping(em_tree, em->start,
5266 em->len);
5267 if (existing) {
5268 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005269 em, start,
5270 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005271 free_extent_map(existing);
5272 if (err) {
5273 free_extent_map(em);
5274 em = NULL;
5275 }
5276 } else {
5277 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005278 free_extent_map(em);
5279 em = NULL;
5280 }
5281 } else {
5282 free_extent_map(em);
5283 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005284 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005285 }
Chris Masona52d9a82007-08-27 16:49:44 -04005286 }
Chris Mason890871b2009-09-02 16:24:52 -04005287 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005288out:
liubo1abe9b82011-03-24 11:18:59 +00005289
5290 trace_btrfs_get_extent(root, em);
5291
Chris Masonf4219502008-07-22 11:18:09 -04005292 if (path)
5293 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005294 if (trans) {
5295 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005296 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005297 err = ret;
5298 }
Chris Masona52d9a82007-08-27 16:49:44 -04005299 if (err) {
5300 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005301 return ERR_PTR(err);
5302 }
5303 return em;
5304}
5305
Chris Masonec29ed52011-02-23 16:23:20 -05005306struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5307 size_t pg_offset, u64 start, u64 len,
5308 int create)
5309{
5310 struct extent_map *em;
5311 struct extent_map *hole_em = NULL;
5312 u64 range_start = start;
5313 u64 end;
5314 u64 found;
5315 u64 found_end;
5316 int err = 0;
5317
5318 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5319 if (IS_ERR(em))
5320 return em;
5321 if (em) {
5322 /*
5323 * if our em maps to a hole, there might
5324 * actually be delalloc bytes behind it
5325 */
5326 if (em->block_start != EXTENT_MAP_HOLE)
5327 return em;
5328 else
5329 hole_em = em;
5330 }
5331
5332 /* check to see if we've wrapped (len == -1 or similar) */
5333 end = start + len;
5334 if (end < start)
5335 end = (u64)-1;
5336 else
5337 end -= 1;
5338
5339 em = NULL;
5340
5341 /* ok, we didn't find anything, lets look for delalloc */
5342 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5343 end, len, EXTENT_DELALLOC, 1);
5344 found_end = range_start + found;
5345 if (found_end < range_start)
5346 found_end = (u64)-1;
5347
5348 /*
5349 * we didn't find anything useful, return
5350 * the original results from get_extent()
5351 */
5352 if (range_start > end || found_end <= start) {
5353 em = hole_em;
5354 hole_em = NULL;
5355 goto out;
5356 }
5357
5358 /* adjust the range_start to make sure it doesn't
5359 * go backwards from the start they passed in
5360 */
5361 range_start = max(start,range_start);
5362 found = found_end - range_start;
5363
5364 if (found > 0) {
5365 u64 hole_start = start;
5366 u64 hole_len = len;
5367
5368 em = alloc_extent_map(GFP_NOFS);
5369 if (!em) {
5370 err = -ENOMEM;
5371 goto out;
5372 }
5373 /*
5374 * when btrfs_get_extent can't find anything it
5375 * returns one huge hole
5376 *
5377 * make sure what it found really fits our range, and
5378 * adjust to make sure it is based on the start from
5379 * the caller
5380 */
5381 if (hole_em) {
5382 u64 calc_end = extent_map_end(hole_em);
5383
5384 if (calc_end <= start || (hole_em->start > end)) {
5385 free_extent_map(hole_em);
5386 hole_em = NULL;
5387 } else {
5388 hole_start = max(hole_em->start, start);
5389 hole_len = calc_end - hole_start;
5390 }
5391 }
5392 em->bdev = NULL;
5393 if (hole_em && range_start > hole_start) {
5394 /* our hole starts before our delalloc, so we
5395 * have to return just the parts of the hole
5396 * that go until the delalloc starts
5397 */
5398 em->len = min(hole_len,
5399 range_start - hole_start);
5400 em->start = hole_start;
5401 em->orig_start = hole_start;
5402 /*
5403 * don't adjust block start at all,
5404 * it is fixed at EXTENT_MAP_HOLE
5405 */
5406 em->block_start = hole_em->block_start;
5407 em->block_len = hole_len;
5408 } else {
5409 em->start = range_start;
5410 em->len = found;
5411 em->orig_start = range_start;
5412 em->block_start = EXTENT_MAP_DELALLOC;
5413 em->block_len = found;
5414 }
5415 } else if (hole_em) {
5416 return hole_em;
5417 }
5418out:
5419
5420 free_extent_map(hole_em);
5421 if (err) {
5422 free_extent_map(em);
5423 return ERR_PTR(err);
5424 }
5425 return em;
5426}
5427
Josef Bacik4b46fce2010-05-23 11:00:55 -04005428static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
Josef Bacik16d299a2011-04-06 14:53:07 -04005429 struct extent_map *em,
Josef Bacik4b46fce2010-05-23 11:00:55 -04005430 u64 start, u64 len)
5431{
5432 struct btrfs_root *root = BTRFS_I(inode)->root;
5433 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005434 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5435 struct btrfs_key ins;
5436 u64 alloc_hint;
5437 int ret;
Josef Bacik16d299a2011-04-06 14:53:07 -04005438 bool insert = false;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005439
Josef Bacik16d299a2011-04-06 14:53:07 -04005440 /*
5441 * Ok if the extent map we looked up is a hole and is for the exact
5442 * range we want, there is no reason to allocate a new one, however if
5443 * it is not right then we need to free this one and drop the cache for
5444 * our range.
5445 */
5446 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5447 em->len != len) {
5448 free_extent_map(em);
5449 em = NULL;
5450 insert = true;
5451 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5452 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005453
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005454 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005455 if (IS_ERR(trans))
5456 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005457
5458 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5459
5460 alloc_hint = get_extent_allocation_hint(inode, start, len);
5461 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5462 alloc_hint, (u64)-1, &ins, 1);
5463 if (ret) {
5464 em = ERR_PTR(ret);
5465 goto out;
5466 }
5467
Josef Bacik4b46fce2010-05-23 11:00:55 -04005468 if (!em) {
Josef Bacik16d299a2011-04-06 14:53:07 -04005469 em = alloc_extent_map(GFP_NOFS);
5470 if (!em) {
5471 em = ERR_PTR(-ENOMEM);
5472 goto out;
5473 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005474 }
5475
5476 em->start = start;
5477 em->orig_start = em->start;
5478 em->len = ins.offset;
5479
5480 em->block_start = ins.objectid;
5481 em->block_len = ins.offset;
5482 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik16d299a2011-04-06 14:53:07 -04005483
5484 /*
5485 * We need to do this because if we're using the original em we searched
5486 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5487 */
5488 em->flags = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005489 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5490
Josef Bacik16d299a2011-04-06 14:53:07 -04005491 while (insert) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005492 write_lock(&em_tree->lock);
5493 ret = add_extent_mapping(em_tree, em);
5494 write_unlock(&em_tree->lock);
5495 if (ret != -EEXIST)
5496 break;
5497 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5498 }
5499
5500 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5501 ins.offset, ins.offset, 0);
5502 if (ret) {
5503 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5504 em = ERR_PTR(ret);
5505 }
5506out:
5507 btrfs_end_transaction(trans, root);
5508 return em;
5509}
5510
Chris Mason46bfbb52010-05-26 11:04:10 -04005511/*
5512 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5513 * block must be cow'd
5514 */
5515static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5516 struct inode *inode, u64 offset, u64 len)
5517{
5518 struct btrfs_path *path;
5519 int ret;
5520 struct extent_buffer *leaf;
5521 struct btrfs_root *root = BTRFS_I(inode)->root;
5522 struct btrfs_file_extent_item *fi;
5523 struct btrfs_key key;
5524 u64 disk_bytenr;
5525 u64 backref_offset;
5526 u64 extent_end;
5527 u64 num_bytes;
5528 int slot;
5529 int found_type;
5530
5531 path = btrfs_alloc_path();
5532 if (!path)
5533 return -ENOMEM;
5534
5535 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
5536 offset, 0);
5537 if (ret < 0)
5538 goto out;
5539
5540 slot = path->slots[0];
5541 if (ret == 1) {
5542 if (slot == 0) {
5543 /* can't find the item, must cow */
5544 ret = 0;
5545 goto out;
5546 }
5547 slot--;
5548 }
5549 ret = 0;
5550 leaf = path->nodes[0];
5551 btrfs_item_key_to_cpu(leaf, &key, slot);
5552 if (key.objectid != inode->i_ino ||
5553 key.type != BTRFS_EXTENT_DATA_KEY) {
5554 /* not our file or wrong item type, must cow */
5555 goto out;
5556 }
5557
5558 if (key.offset > offset) {
5559 /* Wrong offset, must cow */
5560 goto out;
5561 }
5562
5563 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5564 found_type = btrfs_file_extent_type(leaf, fi);
5565 if (found_type != BTRFS_FILE_EXTENT_REG &&
5566 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5567 /* not a regular extent, must cow */
5568 goto out;
5569 }
5570 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5571 backref_offset = btrfs_file_extent_offset(leaf, fi);
5572
5573 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5574 if (extent_end < offset + len) {
5575 /* extent doesn't include our full range, must cow */
5576 goto out;
5577 }
5578
5579 if (btrfs_extent_readonly(root, disk_bytenr))
5580 goto out;
5581
5582 /*
5583 * look for other files referencing this extent, if we
5584 * find any we must cow
5585 */
5586 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
5587 key.offset - backref_offset, disk_bytenr))
5588 goto out;
5589
5590 /*
5591 * adjust disk_bytenr and num_bytes to cover just the bytes
5592 * in this extent we are about to write. If there
5593 * are any csums in that range we have to cow in order
5594 * to keep the csums correct
5595 */
5596 disk_bytenr += backref_offset;
5597 disk_bytenr += offset - key.offset;
5598 num_bytes = min(offset + len, extent_end) - offset;
5599 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5600 goto out;
5601 /*
5602 * all of the above have passed, it is safe to overwrite this extent
5603 * without cow
5604 */
5605 ret = 1;
5606out:
5607 btrfs_free_path(path);
5608 return ret;
5609}
5610
Josef Bacik4b46fce2010-05-23 11:00:55 -04005611static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5612 struct buffer_head *bh_result, int create)
5613{
5614 struct extent_map *em;
5615 struct btrfs_root *root = BTRFS_I(inode)->root;
5616 u64 start = iblock << inode->i_blkbits;
5617 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005618 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005619
5620 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5621 if (IS_ERR(em))
5622 return PTR_ERR(em);
5623
5624 /*
5625 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5626 * io. INLINE is special, and we could probably kludge it in here, but
5627 * it's still buffered so for safety lets just fall back to the generic
5628 * buffered path.
5629 *
5630 * For COMPRESSED we _have_ to read the entire extent in so we can
5631 * decompress it, so there will be buffering required no matter what we
5632 * do, so go ahead and fallback to buffered.
5633 *
5634 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5635 * to buffered IO. Don't blame me, this is the price we pay for using
5636 * the generic code.
5637 */
5638 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5639 em->block_start == EXTENT_MAP_INLINE) {
5640 free_extent_map(em);
5641 return -ENOTBLK;
5642 }
5643
5644 /* Just a good old fashioned hole, return */
5645 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5646 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5647 free_extent_map(em);
5648 /* DIO will do one hole at a time, so just unlock a sector */
5649 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5650 start + root->sectorsize - 1, GFP_NOFS);
5651 return 0;
5652 }
5653
5654 /*
5655 * We don't allocate a new extent in the following cases
5656 *
5657 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5658 * existing extent.
5659 * 2) The extent is marked as PREALLOC. We're good to go here and can
5660 * just use the extent.
5661 *
5662 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005663 if (!create) {
5664 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005665 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005666 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005667
5668 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5669 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5670 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005671 int type;
5672 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005673 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005674
5675 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5676 type = BTRFS_ORDERED_PREALLOC;
5677 else
5678 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005679 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005680 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005681
5682 /*
5683 * we're not going to log anything, but we do need
5684 * to make sure the current transaction stays open
5685 * while we look for nocow cross refs
5686 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005687 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005688 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005689 goto must_cow;
5690
5691 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5692 ret = btrfs_add_ordered_extent_dio(inode, start,
5693 block_start, len, len, type);
5694 btrfs_end_transaction(trans, root);
5695 if (ret) {
5696 free_extent_map(em);
5697 return ret;
5698 }
5699 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005700 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005701 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005702 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005703must_cow:
5704 /*
5705 * this will cow the extent, reset the len in case we changed
5706 * it above
5707 */
5708 len = bh_result->b_size;
Josef Bacik16d299a2011-04-06 14:53:07 -04005709 em = btrfs_new_extent_direct(inode, em, start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04005710 if (IS_ERR(em))
5711 return PTR_ERR(em);
5712 len = min(len, em->len - (start - em->start));
5713unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005714 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5715 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5716 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005717map:
5718 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5719 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005720 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005721 bh_result->b_bdev = em->bdev;
5722 set_buffer_mapped(bh_result);
5723 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5724 set_buffer_new(bh_result);
5725
5726 free_extent_map(em);
5727
5728 return 0;
5729}
5730
5731struct btrfs_dio_private {
5732 struct inode *inode;
5733 u64 logical_offset;
5734 u64 disk_bytenr;
5735 u64 bytes;
5736 u32 *csums;
5737 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005738
5739 /* number of bios pending for this dio */
5740 atomic_t pending_bios;
5741
5742 /* IO errors */
5743 int errors;
5744
5745 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005746};
5747
5748static void btrfs_endio_direct_read(struct bio *bio, int err)
5749{
Miao Xiee65e1532010-11-22 03:04:43 +00005750 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005751 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5752 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005753 struct inode *inode = dip->inode;
5754 struct btrfs_root *root = BTRFS_I(inode)->root;
5755 u64 start;
5756 u32 *private = dip->csums;
5757
5758 start = dip->logical_offset;
5759 do {
5760 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5761 struct page *page = bvec->bv_page;
5762 char *kaddr;
5763 u32 csum = ~(u32)0;
5764 unsigned long flags;
5765
5766 local_irq_save(flags);
5767 kaddr = kmap_atomic(page, KM_IRQ0);
5768 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5769 csum, bvec->bv_len);
5770 btrfs_csum_final(csum, (char *)&csum);
5771 kunmap_atomic(kaddr, KM_IRQ0);
5772 local_irq_restore(flags);
5773
5774 flush_dcache_page(bvec->bv_page);
5775 if (csum != *private) {
5776 printk(KERN_ERR "btrfs csum failed ino %lu off"
5777 " %llu csum %u private %u\n",
5778 inode->i_ino, (unsigned long long)start,
5779 csum, *private);
5780 err = -EIO;
5781 }
5782 }
5783
5784 start += bvec->bv_len;
5785 private++;
5786 bvec++;
5787 } while (bvec <= bvec_end);
5788
5789 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5790 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5791 bio->bi_private = dip->private;
5792
5793 kfree(dip->csums);
5794 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005795
5796 /* If we had a csum failure make sure to clear the uptodate flag */
5797 if (err)
5798 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005799 dio_end_io(bio, err);
5800}
5801
5802static void btrfs_endio_direct_write(struct bio *bio, int err)
5803{
5804 struct btrfs_dio_private *dip = bio->bi_private;
5805 struct inode *inode = dip->inode;
5806 struct btrfs_root *root = BTRFS_I(inode)->root;
5807 struct btrfs_trans_handle *trans;
5808 struct btrfs_ordered_extent *ordered = NULL;
5809 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005810 u64 ordered_offset = dip->logical_offset;
5811 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005812 int ret;
5813
5814 if (err)
5815 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005816again:
5817 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5818 &ordered_offset,
5819 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005820 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005821 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005822
5823 BUG_ON(!ordered);
5824
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005825 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005826 if (IS_ERR(trans)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005827 err = -ENOMEM;
5828 goto out;
5829 }
5830 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5831
5832 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5833 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5834 if (!ret)
5835 ret = btrfs_update_inode(trans, root, inode);
5836 err = ret;
5837 goto out;
5838 }
5839
5840 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5841 ordered->file_offset + ordered->len - 1, 0,
5842 &cached_state, GFP_NOFS);
5843
5844 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5845 ret = btrfs_mark_extent_written(trans, inode,
5846 ordered->file_offset,
5847 ordered->file_offset +
5848 ordered->len);
5849 if (ret) {
5850 err = ret;
5851 goto out_unlock;
5852 }
5853 } else {
5854 ret = insert_reserved_file_extent(trans, inode,
5855 ordered->file_offset,
5856 ordered->start,
5857 ordered->disk_len,
5858 ordered->len,
5859 ordered->len,
5860 0, 0, 0,
5861 BTRFS_FILE_EXTENT_REG);
5862 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5863 ordered->file_offset, ordered->len);
5864 if (ret) {
5865 err = ret;
5866 WARN_ON(1);
5867 goto out_unlock;
5868 }
5869 }
5870
5871 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
Josef Bacik1ef30be2011-04-05 19:25:36 -04005872 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5873 if (!ret)
5874 btrfs_update_inode(trans, root, inode);
5875 ret = 0;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005876out_unlock:
5877 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5878 ordered->file_offset + ordered->len - 1,
5879 &cached_state, GFP_NOFS);
5880out:
5881 btrfs_delalloc_release_metadata(inode, ordered->len);
5882 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005883 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005884 btrfs_put_ordered_extent(ordered);
5885 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05005886
5887out_test:
5888 /*
5889 * our bio might span multiple ordered extents. If we haven't
5890 * completed the accounting for the whole dio, go back and try again
5891 */
5892 if (ordered_offset < dip->logical_offset + dip->bytes) {
5893 ordered_bytes = dip->logical_offset + dip->bytes -
5894 ordered_offset;
5895 goto again;
5896 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005897out_done:
5898 bio->bi_private = dip->private;
5899
5900 kfree(dip->csums);
5901 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04005902
5903 /* If we had an error make sure to clear the uptodate flag */
5904 if (err)
5905 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005906 dio_end_io(bio, err);
5907}
5908
Chris Masoneaf25d92010-05-25 09:48:28 -04005909static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5910 struct bio *bio, int mirror_num,
5911 unsigned long bio_flags, u64 offset)
5912{
5913 int ret;
5914 struct btrfs_root *root = BTRFS_I(inode)->root;
5915 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5916 BUG_ON(ret);
5917 return 0;
5918}
5919
Miao Xiee65e1532010-11-22 03:04:43 +00005920static void btrfs_end_dio_bio(struct bio *bio, int err)
5921{
5922 struct btrfs_dio_private *dip = bio->bi_private;
5923
5924 if (err) {
5925 printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00005926 "sector %#Lx len %u err no %d\n",
5927 dip->inode->i_ino, bio->bi_rw,
5928 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00005929 dip->errors = 1;
5930
5931 /*
5932 * before atomic variable goto zero, we must make sure
5933 * dip->errors is perceived to be set.
5934 */
5935 smp_mb__before_atomic_dec();
5936 }
5937
5938 /* if there are more bios still pending for this dio, just exit */
5939 if (!atomic_dec_and_test(&dip->pending_bios))
5940 goto out;
5941
5942 if (dip->errors)
5943 bio_io_error(dip->orig_bio);
5944 else {
5945 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
5946 bio_endio(dip->orig_bio, 0);
5947 }
5948out:
5949 bio_put(bio);
5950}
5951
5952static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
5953 u64 first_sector, gfp_t gfp_flags)
5954{
5955 int nr_vecs = bio_get_nr_vecs(bdev);
5956 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
5957}
5958
5959static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
5960 int rw, u64 file_offset, int skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04005961 u32 *csums, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00005962{
5963 int write = rw & REQ_WRITE;
5964 struct btrfs_root *root = BTRFS_I(inode)->root;
5965 int ret;
5966
5967 bio_get(bio);
5968 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
5969 if (ret)
5970 goto err;
5971
Josef Bacik1ae39932011-04-06 14:41:34 -04005972 if (skip_sum)
5973 goto map;
5974
5975 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00005976 ret = btrfs_wq_submit_bio(root->fs_info,
5977 inode, rw, bio, 0, 0,
5978 file_offset,
5979 __btrfs_submit_bio_start_direct_io,
5980 __btrfs_submit_bio_done);
5981 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04005982 } else if (write) {
5983 /*
5984 * If we aren't doing async submit, calculate the csum of the
5985 * bio now.
5986 */
5987 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
5988 if (ret)
5989 goto err;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00005990 } else if (!skip_sum) {
5991 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
Miao Xiee65e1532010-11-22 03:04:43 +00005992 file_offset, csums);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00005993 if (ret)
5994 goto err;
5995 }
Miao Xiee65e1532010-11-22 03:04:43 +00005996
Josef Bacik1ae39932011-04-06 14:41:34 -04005997map:
5998 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00005999err:
6000 bio_put(bio);
6001 return ret;
6002}
6003
6004static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6005 int skip_sum)
6006{
6007 struct inode *inode = dip->inode;
6008 struct btrfs_root *root = BTRFS_I(inode)->root;
6009 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6010 struct bio *bio;
6011 struct bio *orig_bio = dip->orig_bio;
6012 struct bio_vec *bvec = orig_bio->bi_io_vec;
6013 u64 start_sector = orig_bio->bi_sector;
6014 u64 file_offset = dip->logical_offset;
6015 u64 submit_len = 0;
6016 u64 map_length;
6017 int nr_pages = 0;
6018 u32 *csums = dip->csums;
6019 int ret = 0;
Josef Bacik1ae39932011-04-06 14:41:34 -04006020 int async_submit = 0;
Josef Bacik98bc3142011-03-22 11:00:46 -04006021 int write = rw & REQ_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00006022
Miao Xiee65e1532010-11-22 03:04:43 +00006023 map_length = orig_bio->bi_size;
6024 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6025 &map_length, NULL, 0);
6026 if (ret) {
Josef Bacik64728bb2011-04-25 19:43:52 -04006027 bio_put(orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00006028 return -EIO;
6029 }
6030
Josef Bacik02f57c72011-04-06 14:25:44 -04006031 if (map_length >= orig_bio->bi_size) {
6032 bio = orig_bio;
6033 goto submit;
6034 }
6035
Josef Bacik1ae39932011-04-06 14:41:34 -04006036 async_submit = 1;
Josef Bacik02f57c72011-04-06 14:25:44 -04006037 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6038 if (!bio)
6039 return -ENOMEM;
6040 bio->bi_private = dip;
6041 bio->bi_end_io = btrfs_end_dio_bio;
6042 atomic_inc(&dip->pending_bios);
6043
Miao Xiee65e1532010-11-22 03:04:43 +00006044 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6045 if (unlikely(map_length < submit_len + bvec->bv_len ||
6046 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6047 bvec->bv_offset) < bvec->bv_len)) {
6048 /*
6049 * inc the count before we submit the bio so
6050 * we know the end IO handler won't happen before
6051 * we inc the count. Otherwise, the dip might get freed
6052 * before we're done setting it up
6053 */
6054 atomic_inc(&dip->pending_bios);
6055 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6056 file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006057 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006058 if (ret) {
6059 bio_put(bio);
6060 atomic_dec(&dip->pending_bios);
6061 goto out_err;
6062 }
6063
Josef Bacik98bc3142011-03-22 11:00:46 -04006064 /* Write's use the ordered csums */
6065 if (!write && !skip_sum)
Miao Xiee65e1532010-11-22 03:04:43 +00006066 csums = csums + nr_pages;
6067 start_sector += submit_len >> 9;
6068 file_offset += submit_len;
6069
6070 submit_len = 0;
6071 nr_pages = 0;
6072
6073 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6074 start_sector, GFP_NOFS);
6075 if (!bio)
6076 goto out_err;
6077 bio->bi_private = dip;
6078 bio->bi_end_io = btrfs_end_dio_bio;
6079
6080 map_length = orig_bio->bi_size;
6081 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6082 &map_length, NULL, 0);
6083 if (ret) {
6084 bio_put(bio);
6085 goto out_err;
6086 }
6087 } else {
6088 submit_len += bvec->bv_len;
6089 nr_pages ++;
6090 bvec++;
6091 }
6092 }
6093
Josef Bacik02f57c72011-04-06 14:25:44 -04006094submit:
Miao Xiee65e1532010-11-22 03:04:43 +00006095 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacik1ae39932011-04-06 14:41:34 -04006096 csums, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00006097 if (!ret)
6098 return 0;
6099
6100 bio_put(bio);
6101out_err:
6102 dip->errors = 1;
6103 /*
6104 * before atomic variable goto zero, we must
6105 * make sure dip->errors is perceived to be set.
6106 */
6107 smp_mb__before_atomic_dec();
6108 if (atomic_dec_and_test(&dip->pending_bios))
6109 bio_io_error(dip->orig_bio);
6110
6111 /* bio_end_io() will handle error, so we needn't return it */
6112 return 0;
6113}
6114
Josef Bacik4b46fce2010-05-23 11:00:55 -04006115static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6116 loff_t file_offset)
6117{
6118 struct btrfs_root *root = BTRFS_I(inode)->root;
6119 struct btrfs_dio_private *dip;
6120 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006121 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006122 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006123 int ret = 0;
6124
6125 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6126
6127 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6128 if (!dip) {
6129 ret = -ENOMEM;
6130 goto free_ordered;
6131 }
6132 dip->csums = NULL;
6133
Josef Bacik98bc3142011-03-22 11:00:46 -04006134 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6135 if (!write && !skip_sum) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04006136 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6137 if (!dip->csums) {
Daniel J Bluemanb4966b72011-03-09 16:46:42 +00006138 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006139 ret = -ENOMEM;
6140 goto free_ordered;
6141 }
6142 }
6143
6144 dip->private = bio->bi_private;
6145 dip->inode = inode;
6146 dip->logical_offset = file_offset;
6147
Josef Bacik4b46fce2010-05-23 11:00:55 -04006148 dip->bytes = 0;
6149 do {
6150 dip->bytes += bvec->bv_len;
6151 bvec++;
6152 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6153
Chris Mason46bfbb52010-05-26 11:04:10 -04006154 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006155 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00006156 dip->errors = 0;
6157 dip->orig_bio = bio;
6158 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006159
6160 if (write)
6161 bio->bi_end_io = btrfs_endio_direct_write;
6162 else
6163 bio->bi_end_io = btrfs_endio_direct_read;
6164
Miao Xiee65e1532010-11-22 03:04:43 +00006165 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6166 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04006167 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006168free_ordered:
6169 /*
6170 * If this is a write, we need to clean up the reserved space and kill
6171 * the ordered extent.
6172 */
6173 if (write) {
6174 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05006175 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006176 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6177 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6178 btrfs_free_reserved_extent(root, ordered->start,
6179 ordered->disk_len);
6180 btrfs_put_ordered_extent(ordered);
6181 btrfs_put_ordered_extent(ordered);
6182 }
6183 bio_endio(bio, ret);
6184}
6185
Chris Mason5a5f79b2010-05-26 21:33:37 -04006186static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6187 const struct iovec *iov, loff_t offset,
6188 unsigned long nr_segs)
6189{
6190 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00006191 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04006192 size_t size;
6193 unsigned long addr;
6194 unsigned blocksize_mask = root->sectorsize - 1;
6195 ssize_t retval = -EINVAL;
6196 loff_t end = offset;
6197
6198 if (offset & blocksize_mask)
6199 goto out;
6200
6201 /* Check the memory alignment. Blocks cannot straddle pages */
6202 for (seg = 0; seg < nr_segs; seg++) {
6203 addr = (unsigned long)iov[seg].iov_base;
6204 size = iov[seg].iov_len;
6205 end += size;
Josef Bacika1b75f72011-04-08 15:51:18 +00006206 if ((addr & blocksize_mask) || (size & blocksize_mask))
Chris Mason5a5f79b2010-05-26 21:33:37 -04006207 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00006208
6209 /* If this is a write we don't need to check anymore */
6210 if (rw & WRITE)
6211 continue;
6212
6213 /*
6214 * Check to make sure we don't have duplicate iov_base's in this
6215 * iovec, if so return EINVAL, otherwise we'll get csum errors
6216 * when reading back.
6217 */
6218 for (i = seg + 1; i < nr_segs; i++) {
6219 if (iov[seg].iov_base == iov[i].iov_base)
6220 goto out;
6221 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04006222 }
6223 retval = 0;
6224out:
6225 return retval;
6226}
Chris Mason16432982008-04-10 10:23:21 -04006227static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6228 const struct iovec *iov, loff_t offset,
6229 unsigned long nr_segs)
6230{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006231 struct file *file = iocb->ki_filp;
6232 struct inode *inode = file->f_mapping->host;
6233 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006234 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006235 u64 lockstart, lockend;
6236 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006237 int writing = rw & WRITE;
6238 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006239 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006240
Chris Mason5a5f79b2010-05-26 21:33:37 -04006241 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6242 offset, nr_segs)) {
6243 return 0;
6244 }
6245
Josef Bacik4b46fce2010-05-23 11:00:55 -04006246 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006247 lockend = offset + count - 1;
6248
6249 if (writing) {
6250 ret = btrfs_delalloc_reserve_space(inode, count);
6251 if (ret)
6252 goto out;
6253 }
Chris Mason4845e442010-05-25 20:56:50 -04006254
Josef Bacik4b46fce2010-05-23 11:00:55 -04006255 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006256 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6257 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006258 /*
6259 * We're concerned with the entire range that we're going to be
6260 * doing DIO to, so we need to make sure theres no ordered
6261 * extents in this range.
6262 */
6263 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6264 lockend - lockstart + 1);
6265 if (!ordered)
6266 break;
Chris Mason4845e442010-05-25 20:56:50 -04006267 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6268 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006269 btrfs_start_ordered_extent(inode, ordered, 1);
6270 btrfs_put_ordered_extent(ordered);
6271 cond_resched();
6272 }
6273
Chris Mason4845e442010-05-25 20:56:50 -04006274 /*
6275 * we don't use btrfs_set_extent_delalloc because we don't want
6276 * the dirty or uptodate bits
6277 */
6278 if (writing) {
6279 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6280 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6281 EXTENT_DELALLOC, 0, NULL, &cached_state,
6282 GFP_NOFS);
6283 if (ret) {
6284 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6285 lockend, EXTENT_LOCKED | write_bits,
6286 1, 0, &cached_state, GFP_NOFS);
6287 goto out;
6288 }
6289 }
6290
6291 free_extent_state(cached_state);
6292 cached_state = NULL;
6293
Chris Mason5a5f79b2010-05-26 21:33:37 -04006294 ret = __blockdev_direct_IO(rw, iocb, inode,
6295 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6296 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6297 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006298
6299 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006300 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6301 offset + iov_length(iov, nr_segs) - 1,
6302 EXTENT_LOCKED | write_bits, 1, 0,
6303 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006304 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6305 /*
6306 * We're falling back to buffered, unlock the section we didn't
6307 * do IO on.
6308 */
Chris Mason4845e442010-05-25 20:56:50 -04006309 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6310 offset + iov_length(iov, nr_segs) - 1,
6311 EXTENT_LOCKED | write_bits, 1, 0,
6312 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006313 }
Chris Mason4845e442010-05-25 20:56:50 -04006314out:
6315 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006316 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006317}
6318
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006319static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6320 __u64 start, __u64 len)
6321{
Chris Masonec29ed52011-02-23 16:23:20 -05006322 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006323}
6324
Chris Mason9ebefb182007-06-15 13:50:00 -04006325int btrfs_readpage(struct file *file, struct page *page)
6326{
Chris Masond1310b22008-01-24 16:13:08 -05006327 struct extent_io_tree *tree;
6328 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006329 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04006330}
Chris Mason1832a6d2007-12-21 16:27:21 -05006331
Chris Mason39279cc2007-06-12 06:35:45 -04006332static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6333{
Chris Masond1310b22008-01-24 16:13:08 -05006334 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006335
6336
6337 if (current->flags & PF_MEMALLOC) {
6338 redirty_page_for_writepage(wbc, page);
6339 unlock_page(page);
6340 return 0;
6341 }
Chris Masond1310b22008-01-24 16:13:08 -05006342 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006343 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6344}
Chris Mason39279cc2007-06-12 06:35:45 -04006345
Chris Masonf4219502008-07-22 11:18:09 -04006346int btrfs_writepages(struct address_space *mapping,
6347 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006348{
Chris Masond1310b22008-01-24 16:13:08 -05006349 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006350
Chris Masond1310b22008-01-24 16:13:08 -05006351 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006352 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6353}
6354
Chris Mason3ab2fb52007-11-08 10:59:22 -05006355static int
6356btrfs_readpages(struct file *file, struct address_space *mapping,
6357 struct list_head *pages, unsigned nr_pages)
6358{
Chris Masond1310b22008-01-24 16:13:08 -05006359 struct extent_io_tree *tree;
6360 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006361 return extent_readpages(tree, mapping, pages, nr_pages,
6362 btrfs_get_extent);
6363}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006364static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006365{
Chris Masond1310b22008-01-24 16:13:08 -05006366 struct extent_io_tree *tree;
6367 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006368 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006369
Chris Masond1310b22008-01-24 16:13:08 -05006370 tree = &BTRFS_I(page->mapping->host)->io_tree;
6371 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006372 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006373 if (ret == 1) {
6374 ClearPagePrivate(page);
6375 set_page_private(page, 0);
6376 page_cache_release(page);
6377 }
6378 return ret;
6379}
Chris Mason39279cc2007-06-12 06:35:45 -04006380
Chris Masone6dcd2d2008-07-17 12:53:50 -04006381static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6382{
Chris Mason98509cf2008-09-11 15:51:43 -04006383 if (PageWriteback(page) || PageDirty(page))
6384 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006385 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006386}
6387
Chris Masona52d9a82007-08-27 16:49:44 -04006388static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6389{
Chris Masond1310b22008-01-24 16:13:08 -05006390 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006391 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006392 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006393 u64 page_start = page_offset(page);
6394 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006395
Chris Mason8b62b722009-09-02 16:53:46 -04006396
6397 /*
6398 * we have the page locked, so new writeback can't start,
6399 * and the dirty bit won't be cleared while we are here.
6400 *
6401 * Wait for IO on this page so that we can safely clear
6402 * the PagePrivate2 bit and do ordered accounting
6403 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006404 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006405
Chris Masond1310b22008-01-24 16:13:08 -05006406 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006407 if (offset) {
6408 btrfs_releasepage(page, GFP_NOFS);
6409 return;
6410 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006411 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6412 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006413 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6414 page_offset(page));
6415 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006416 /*
6417 * IO on this page will never be started, so we need
6418 * to account for any ordered extents now
6419 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006420 clear_extent_bit(tree, page_start, page_end,
6421 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006422 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006423 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006424 /*
6425 * whoever cleared the private bit is responsible
6426 * for the finish_ordered_io
6427 */
6428 if (TestClearPagePrivate2(page)) {
6429 btrfs_finish_ordered_io(page->mapping->host,
6430 page_start, page_end);
6431 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006432 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006433 cached_state = NULL;
6434 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6435 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006436 }
6437 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006438 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006439 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006440 __btrfs_releasepage(page, GFP_NOFS);
6441
Chris Mason4a096752008-07-21 10:29:44 -04006442 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006443 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006444 ClearPagePrivate(page);
6445 set_page_private(page, 0);
6446 page_cache_release(page);
6447 }
Chris Mason39279cc2007-06-12 06:35:45 -04006448}
6449
Chris Mason9ebefb182007-06-15 13:50:00 -04006450/*
6451 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6452 * called from a page fault handler when a page is first dirtied. Hence we must
6453 * be careful to check for EOF conditions here. We set the page up correctly
6454 * for a written page which means we get ENOSPC checking when writing into
6455 * holes and correct delalloc and unwritten extent mapping on filesystems that
6456 * support these features.
6457 *
6458 * We are not allowed to take the i_mutex here so we have to play games to
6459 * protect against truncate races as the page could now be beyond EOF. Because
6460 * vmtruncate() writes the inode size before removing pages, once we have the
6461 * page lock we can determine safely if the page is beyond EOF. If it is not
6462 * beyond EOF, then the page is guaranteed safe against truncation until we
6463 * unlock the page.
6464 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006465int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006466{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006467 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006468 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006469 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006470 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6471 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006472 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006473 char *kaddr;
6474 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006475 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006476 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04006477 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006478 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006479
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006480 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07006481 if (ret) {
6482 if (ret == -ENOMEM)
6483 ret = VM_FAULT_OOM;
6484 else /* -ENOSPC, -EIO, etc */
6485 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05006486 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07006487 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006488
Nick Piggin56a76f82009-03-31 15:23:23 -07006489 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006490again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006491 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006492 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006493 page_start = page_offset(page);
6494 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006495
Chris Mason9ebefb182007-06-15 13:50:00 -04006496 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006497 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006498 /* page got truncated out from underneath us */
6499 goto out_unlock;
6500 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006501 wait_on_page_writeback(page);
6502
Josef Bacik2ac55d42010-02-03 19:33:23 +00006503 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6504 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006505 set_page_extent_mapped(page);
6506
Chris Masoneb84ae02008-07-17 13:53:27 -04006507 /*
6508 * we can't set the delalloc bits if there are pending ordered
6509 * extents. Drop our locks and wait for them to finish
6510 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006511 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6512 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006513 unlock_extent_cached(io_tree, page_start, page_end,
6514 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006515 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006516 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006517 btrfs_put_ordered_extent(ordered);
6518 goto again;
6519 }
6520
Josef Bacikfbf19082009-10-01 17:10:23 -04006521 /*
6522 * XXX - page_mkwrite gets called every time the page is dirtied, even
6523 * if it was already dirty, so for space accounting reasons we need to
6524 * clear any delalloc bits for the range we are fixing to save. There
6525 * is probably a better way to do this, but for now keep consistent with
6526 * prepare_pages in the normal write path.
6527 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006528 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006529 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006530 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006531
Josef Bacik2ac55d42010-02-03 19:33:23 +00006532 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6533 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006534 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006535 unlock_extent_cached(io_tree, page_start, page_end,
6536 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006537 ret = VM_FAULT_SIGBUS;
6538 goto out_unlock;
6539 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006540 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006541
6542 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006543 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006544 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006545 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006546 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006547
Chris Masone6dcd2d2008-07-17 12:53:50 -04006548 if (zero_start != PAGE_CACHE_SIZE) {
6549 kaddr = kmap(page);
6550 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6551 flush_dcache_page(page);
6552 kunmap(page);
6553 }
Chris Mason247e7432008-07-17 12:53:51 -04006554 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006555 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006556 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006557
Chris Mason257c62e2009-10-13 13:21:08 -04006558 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6559 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6560
Josef Bacik2ac55d42010-02-03 19:33:23 +00006561 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006562
6563out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006564 if (!ret)
6565 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006566 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006567 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05006568out:
Chris Mason9ebefb182007-06-15 13:50:00 -04006569 return ret;
6570}
6571
Josef Bacika41ad392011-01-31 15:30:16 -05006572static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006573{
6574 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04006575 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04006576 int ret;
Josef Bacik3893e332011-01-31 16:03:11 -05006577 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006578 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006579 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006580 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006581
Josef Bacik5d5e1032009-10-13 16:46:49 -04006582 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6583 if (ret)
Josef Bacika41ad392011-01-31 15:30:16 -05006584 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006585
Chris Mason4a096752008-07-21 10:29:44 -04006586 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006587 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006588
Josef Bacikfcb80c22011-05-03 10:40:22 -04006589 /*
6590 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6591 * 3 things going on here
6592 *
6593 * 1) We need to reserve space for our orphan item and the space to
6594 * delete our orphan item. Lord knows we don't want to have a dangling
6595 * orphan item because we didn't reserve space to remove it.
6596 *
6597 * 2) We need to reserve space to update our inode.
6598 *
6599 * 3) We need to have something to cache all the space that is going to
6600 * be free'd up by the truncate operation, but also have some slack
6601 * space reserved in case it uses space during the truncate (thank you
6602 * very much snapshotting).
6603 *
6604 * And we need these to all be seperate. The fact is we can use alot of
6605 * space doing the truncate, and we have no earthly idea how much space
6606 * we will use, so we need the truncate reservation to be seperate so it
6607 * doesn't end up using space reserved for updating the inode or
6608 * removing the orphan item. We also need to be able to stop the
6609 * transaction and start a new one, which means we need to be able to
6610 * update the inode several times, and we have no idea of knowing how
6611 * many times that will be, so we can't just reserve 1 item for the
6612 * entirety of the opration, so that has to be done seperately as well.
6613 * Then there is the orphan item, which does indeed need to be held on
6614 * to for the whole operation, and we need nobody to touch this reserved
6615 * space except the orphan code.
6616 *
6617 * So that leaves us with
6618 *
6619 * 1) root->orphan_block_rsv - for the orphan deletion.
6620 * 2) rsv - for the truncate reservation, which we will steal from the
6621 * transaction reservation.
6622 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6623 * updating the inode.
6624 */
6625 rsv = btrfs_alloc_block_rsv(root);
6626 if (!rsv)
6627 return -ENOMEM;
6628 btrfs_add_durable_block_rsv(root->fs_info, rsv);
6629
6630 trans = btrfs_start_transaction(root, 4);
6631 if (IS_ERR(trans)) {
6632 err = PTR_ERR(trans);
6633 goto out;
6634 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05006635
Josef Bacikfcb80c22011-05-03 10:40:22 -04006636 /*
6637 * Reserve space for the truncate process. Truncate should be adding
6638 * space, but if there are snapshots it may end up using space.
6639 */
6640 ret = btrfs_truncate_reserve_metadata(trans, root, rsv);
6641 BUG_ON(ret);
6642
Josef Bacikf0cd8462011-03-04 14:37:08 -05006643 ret = btrfs_orphan_add(trans, inode);
6644 if (ret) {
6645 btrfs_end_transaction(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006646 goto out;
Josef Bacikf0cd8462011-03-04 14:37:08 -05006647 }
6648
6649 nr = trans->blocks_used;
6650 btrfs_end_transaction(trans, root);
6651 btrfs_btree_balance_dirty(root, nr);
6652
Josef Bacikfcb80c22011-05-03 10:40:22 -04006653 /*
6654 * Ok so we've already migrated our bytes over for the truncate, so here
6655 * just reserve the one slot we need for updating the inode.
6656 */
6657 trans = btrfs_start_transaction(root, 1);
6658 if (IS_ERR(trans)) {
6659 err = PTR_ERR(trans);
6660 goto out;
6661 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04006662 trans->block_rsv = rsv;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006663
6664 /*
6665 * setattr is responsible for setting the ordered_data_close flag,
6666 * but that is only tested during the last file release. That
6667 * could happen well after the next commit, leaving a great big
6668 * window where new writes may get lost if someone chooses to write
6669 * to this file after truncating to zero
6670 *
6671 * The inode doesn't have any dirty data here, and so if we commit
6672 * this is a noop. If someone immediately starts writing to the inode
6673 * it is very likely we'll catch some of their writes in this
6674 * transaction, and the commit will find this file on the ordered
6675 * data list with good things to send down.
6676 *
6677 * This is a best effort solution, there is still a window where
6678 * using truncate to replace the contents of the file will
6679 * end up with a zero length file after a crash.
6680 */
6681 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6682 btrfs_add_ordered_operation(trans, root, inode);
6683
Yan, Zheng80825102009-11-12 09:35:36 +00006684 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006685 if (!trans) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04006686 trans = btrfs_start_transaction(root, 3);
6687 if (IS_ERR(trans)) {
6688 err = PTR_ERR(trans);
6689 goto out;
6690 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006691
Josef Bacikfcb80c22011-05-03 10:40:22 -04006692 ret = btrfs_truncate_reserve_metadata(trans, root,
6693 rsv);
6694 BUG_ON(ret);
6695
Josef Bacikfcb80c22011-05-03 10:40:22 -04006696 trans->block_rsv = rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006697 }
6698
Yan, Zheng80825102009-11-12 09:35:36 +00006699 ret = btrfs_truncate_inode_items(trans, root, inode,
6700 inode->i_size,
6701 BTRFS_EXTENT_DATA_KEY);
Josef Bacik3893e332011-01-31 16:03:11 -05006702 if (ret != -EAGAIN) {
6703 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00006704 break;
Josef Bacik3893e332011-01-31 16:03:11 -05006705 }
Chris Mason39279cc2007-06-12 06:35:45 -04006706
Josef Bacikfcb80c22011-05-03 10:40:22 -04006707 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006708 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006709 if (ret) {
6710 err = ret;
6711 break;
6712 }
Chris Mason5f39d392007-10-15 16:14:19 -04006713
Yan, Zheng80825102009-11-12 09:35:36 +00006714 nr = trans->blocks_used;
6715 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006716 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006717 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006718 }
6719
6720 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04006721 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006722 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006723 if (ret)
6724 err = ret;
Josef Bacikded5db92011-03-04 14:09:46 -05006725 } else if (ret && inode->i_nlink > 0) {
6726 /*
6727 * Failed to do the truncate, remove us from the in memory
6728 * orphan list.
6729 */
6730 ret = btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00006731 }
6732
Josef Bacikfcb80c22011-05-03 10:40:22 -04006733 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00006734 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05006735 if (ret && !err)
6736 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04006737
Josef Bacik7b128762008-07-24 12:17:14 -04006738 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04006739 ret = btrfs_end_transaction_throttle(trans, root);
Josef Bacikfcb80c22011-05-03 10:40:22 -04006740 btrfs_btree_balance_dirty(root, nr);
6741
6742out:
6743 btrfs_free_block_rsv(root, rsv);
6744
Josef Bacik3893e332011-01-31 16:03:11 -05006745 if (ret && !err)
6746 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05006747
Josef Bacik3893e332011-01-31 16:03:11 -05006748 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04006749}
6750
Sven Wegener3b963622008-06-09 21:57:42 -04006751/*
Chris Masond352ac62008-09-29 15:18:18 -04006752 * create a new subvolume directory/inode (helper for the ioctl).
6753 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006754int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006755 struct btrfs_root *new_root, u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04006756{
Chris Mason39279cc2007-06-12 06:35:45 -04006757 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006758 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006759 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006760
Josef Bacikaec74772008-07-24 12:12:38 -04006761 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006762 new_dirid, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006763 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006764 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006765 inode->i_op = &btrfs_dir_inode_operations;
6766 inode->i_fop = &btrfs_dir_file_operations;
6767
Chris Mason39279cc2007-06-12 06:35:45 -04006768 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006769 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006770
Yan, Zheng76dda932009-09-21 16:00:26 -04006771 err = btrfs_update_inode(trans, new_root, inode);
6772 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006773
Yan, Zheng76dda932009-09-21 16:00:26 -04006774 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006775 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006776}
6777
Chris Masond352ac62008-09-29 15:18:18 -04006778/* helper function for file defrag and space balancing. This
6779 * forces readahead on a given range of bytes in an inode
6780 */
Chris Masonedbd8d42007-12-21 16:27:24 -05006781unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04006782 struct file_ra_state *ra, struct file *file,
6783 pgoff_t offset, pgoff_t last_index)
6784{
Chris Mason8e7bf942008-04-28 09:02:36 -04006785 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04006786
Chris Mason86479a02007-09-10 19:58:16 -04006787 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6788 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04006789}
6790
Chris Mason39279cc2007-06-12 06:35:45 -04006791struct inode *btrfs_alloc_inode(struct super_block *sb)
6792{
6793 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006794 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006795
6796 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6797 if (!ei)
6798 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006799
6800 ei->root = NULL;
6801 ei->space_info = NULL;
6802 ei->generation = 0;
6803 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006804 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006805 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006806 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006807 ei->delalloc_bytes = 0;
6808 ei->reserved_bytes = 0;
6809 ei->disk_i_size = 0;
6810 ei->flags = 0;
6811 ei->index_cnt = (u64)-1;
6812 ei->last_unlink_trans = 0;
6813
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006814 atomic_set(&ei->outstanding_extents, 0);
Josef Bacik57a45ced2011-01-25 16:30:38 -05006815 atomic_set(&ei->reserved_extents, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006816
6817 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006818 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006819 ei->dummy_inode = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006820 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006821
6822 inode = &ei->vfs_inode;
6823 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6824 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6825 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6826 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006827 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006828 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006829 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006830 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006831 RB_CLEAR_NODE(&ei->rb_node);
6832
6833 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006834}
6835
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006836static void btrfs_i_callback(struct rcu_head *head)
6837{
6838 struct inode *inode = container_of(head, struct inode, i_rcu);
6839 INIT_LIST_HEAD(&inode->i_dentry);
6840 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6841}
6842
Chris Mason39279cc2007-06-12 06:35:45 -04006843void btrfs_destroy_inode(struct inode *inode)
6844{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006845 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006846 struct btrfs_root *root = BTRFS_I(inode)->root;
6847
Chris Mason39279cc2007-06-12 06:35:45 -04006848 WARN_ON(!list_empty(&inode->i_dentry));
6849 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006850 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
Josef Bacik57a45ced2011-01-25 16:30:38 -05006851 WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents));
Chris Mason39279cc2007-06-12 06:35:45 -04006852
Chris Mason5a3f23d2009-03-31 13:27:11 -04006853 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006854 * This can happen where we create an inode, but somebody else also
6855 * created the same inode and we need to destroy the one we already
6856 * created.
6857 */
6858 if (!root)
6859 goto free;
6860
6861 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006862 * Make sure we're properly removed from the ordered operation
6863 * lists.
6864 */
6865 smp_mb();
6866 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6867 spin_lock(&root->fs_info->ordered_extent_lock);
6868 list_del_init(&BTRFS_I(inode)->ordered_operations);
6869 spin_unlock(&root->fs_info->ordered_extent_lock);
6870 }
6871
Yan, Zhengd68fc572010-05-16 10:49:58 -04006872 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006873 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00006874 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
6875 inode->i_ino);
6876 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006877 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006878 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006879
Chris Masond3977122009-01-05 21:25:51 -05006880 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006881 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6882 if (!ordered)
6883 break;
6884 else {
Chris Masond3977122009-01-05 21:25:51 -05006885 printk(KERN_ERR "btrfs found ordered "
6886 "extent %llu %llu on inode cleanup\n",
6887 (unsigned long long)ordered->file_offset,
6888 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006889 btrfs_remove_ordered_extent(inode, ordered);
6890 btrfs_put_ordered_extent(ordered);
6891 btrfs_put_ordered_extent(ordered);
6892 }
6893 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006894 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006895 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006896free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006897 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04006898}
6899
Al Viro45321ac2010-06-07 13:43:19 -04006900int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006901{
6902 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006903
Josef Bacik0af3d002010-06-21 14:48:16 -04006904 if (btrfs_root_refs(&root->root_item) == 0 &&
6905 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04006906 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006907 else
Al Viro45321ac2010-06-07 13:43:19 -04006908 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006909}
6910
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006911static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006912{
6913 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6914
6915 inode_init_once(&ei->vfs_inode);
6916}
6917
6918void btrfs_destroy_cachep(void)
6919{
6920 if (btrfs_inode_cachep)
6921 kmem_cache_destroy(btrfs_inode_cachep);
6922 if (btrfs_trans_handle_cachep)
6923 kmem_cache_destroy(btrfs_trans_handle_cachep);
6924 if (btrfs_transaction_cachep)
6925 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006926 if (btrfs_path_cachep)
6927 kmem_cache_destroy(btrfs_path_cachep);
Josef Bacikdc89e982011-01-28 17:05:48 -05006928 if (btrfs_free_space_cachep)
6929 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006930}
6931
6932int btrfs_init_cachep(void)
6933{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006934 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6935 sizeof(struct btrfs_inode), 0,
6936 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006937 if (!btrfs_inode_cachep)
6938 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006939
6940 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6941 sizeof(struct btrfs_trans_handle), 0,
6942 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006943 if (!btrfs_trans_handle_cachep)
6944 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006945
6946 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6947 sizeof(struct btrfs_transaction), 0,
6948 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006949 if (!btrfs_transaction_cachep)
6950 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006951
6952 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6953 sizeof(struct btrfs_path), 0,
6954 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006955 if (!btrfs_path_cachep)
6956 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006957
Josef Bacikdc89e982011-01-28 17:05:48 -05006958 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
6959 sizeof(struct btrfs_free_space), 0,
6960 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
6961 if (!btrfs_free_space_cachep)
6962 goto fail;
6963
Chris Mason39279cc2007-06-12 06:35:45 -04006964 return 0;
6965fail:
6966 btrfs_destroy_cachep();
6967 return -ENOMEM;
6968}
6969
6970static int btrfs_getattr(struct vfsmount *mnt,
6971 struct dentry *dentry, struct kstat *stat)
6972{
6973 struct inode *inode = dentry->d_inode;
6974 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05006975 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05006976 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006977 stat->blocks = (inode_get_bytes(inode) +
6978 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04006979 return 0;
6980}
6981
Liu Bo75e7cb72011-03-22 10:12:20 +00006982/*
6983 * If a file is moved, it will inherit the cow and compression flags of the new
6984 * directory.
6985 */
6986static void fixup_inode_flags(struct inode *dir, struct inode *inode)
6987{
6988 struct btrfs_inode *b_dir = BTRFS_I(dir);
6989 struct btrfs_inode *b_inode = BTRFS_I(inode);
6990
6991 if (b_dir->flags & BTRFS_INODE_NODATACOW)
6992 b_inode->flags |= BTRFS_INODE_NODATACOW;
6993 else
6994 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
6995
6996 if (b_dir->flags & BTRFS_INODE_COMPRESS)
6997 b_inode->flags |= BTRFS_INODE_COMPRESS;
6998 else
6999 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7000}
7001
Chris Masond3977122009-01-05 21:25:51 -05007002static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7003 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04007004{
7005 struct btrfs_trans_handle *trans;
7006 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007007 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04007008 struct inode *new_inode = new_dentry->d_inode;
7009 struct inode *old_inode = old_dentry->d_inode;
7010 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007011 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007012 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04007013 int ret;
7014
Yan, Zhengf679a842009-09-24 09:17:31 -04007015 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
7016 return -EPERM;
7017
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007018 /* we only allow rename subvolume link between subvolumes */
7019 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05007020 return -EXDEV;
7021
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007022 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7023 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
7024 return -ENOTEMPTY;
7025
Chris Mason39279cc2007-06-12 06:35:45 -04007026 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007027 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04007028 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007029 /*
7030 * we're using rename to replace one file with another.
7031 * and the replacement file is large. Start IO on it now so
7032 * we don't add too much work to the end of the transaction
7033 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04007034 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04007035 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7036 filemap_flush(old_inode->i_mapping);
7037
Yan, Zheng76dda932009-09-21 16:00:26 -04007038 /* close the racy window with snapshot create/destroy ioctl */
7039 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
7040 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007041 /*
7042 * We want to reserve the absolute worst case amount of items. So if
7043 * both inodes are subvols and we need to unlink them then that would
7044 * require 4 item modifications, but if they are both normal inodes it
7045 * would require 5 item modifications, so we'll assume their normal
7046 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7047 * should cover the worst case number of items we'll modify.
7048 */
7049 trans = btrfs_start_transaction(root, 20);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007050 if (IS_ERR(trans)) {
7051 ret = PTR_ERR(trans);
7052 goto out_notrans;
7053 }
Chris Mason5f39d392007-10-15 16:14:19 -04007054
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007055 if (dest != root)
7056 btrfs_record_root_in_trans(trans, dest);
7057
Yan, Zhenga5719522009-09-24 09:17:31 -04007058 ret = btrfs_set_inode_index(new_dir, &index);
7059 if (ret)
7060 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04007061
Yan, Zhenga5719522009-09-24 09:17:31 -04007062 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007063 /* force full log commit if subvolume involved. */
7064 root->fs_info->last_trans_log_full_commit = trans->transid;
7065 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04007066 ret = btrfs_insert_inode_ref(trans, dest,
7067 new_dentry->d_name.name,
7068 new_dentry->d_name.len,
7069 old_inode->i_ino,
7070 new_dir->i_ino, index);
7071 if (ret)
7072 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007073 /*
7074 * this is an ugly little race, but the rename is required
7075 * to make sure that if we crash, the inode is either at the
7076 * old name or the new one. pinning the log transaction lets
7077 * us make sure we don't allow a log commit to come in after
7078 * we unlink the name but before we add the new name back in.
7079 */
7080 btrfs_pin_log_trans(root);
7081 }
Chris Mason12fcfd22009-03-24 10:24:20 -04007082 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04007083 * make sure the inode gets flushed if it is replacing
7084 * something.
7085 */
7086 if (new_inode && new_inode->i_size &&
7087 old_inode && S_ISREG(old_inode->i_mode)) {
7088 btrfs_add_ordered_operation(trans, root, old_inode);
7089 }
7090
Chris Mason39279cc2007-06-12 06:35:45 -04007091 old_dir->i_ctime = old_dir->i_mtime = ctime;
7092 new_dir->i_ctime = new_dir->i_mtime = ctime;
7093 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04007094
Chris Mason12fcfd22009-03-24 10:24:20 -04007095 if (old_dentry->d_parent != new_dentry->d_parent)
7096 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7097
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007098 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7099 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7100 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7101 old_dentry->d_name.name,
7102 old_dentry->d_name.len);
7103 } else {
Al Viro92986792011-03-04 17:14:37 +00007104 ret = __btrfs_unlink_inode(trans, root, old_dir,
7105 old_dentry->d_inode,
7106 old_dentry->d_name.name,
7107 old_dentry->d_name.len);
7108 if (!ret)
7109 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007110 }
7111 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04007112
7113 if (new_inode) {
7114 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007115 if (unlikely(new_inode->i_ino ==
7116 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7117 root_objectid = BTRFS_I(new_inode)->location.objectid;
7118 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7119 root_objectid,
7120 new_dentry->d_name.name,
7121 new_dentry->d_name.len);
7122 BUG_ON(new_inode->i_nlink == 0);
7123 } else {
7124 ret = btrfs_unlink_inode(trans, dest, new_dir,
7125 new_dentry->d_inode,
7126 new_dentry->d_name.name,
7127 new_dentry->d_name.len);
7128 }
7129 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007130 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04007131 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007132 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04007133 }
Chris Mason39279cc2007-06-12 06:35:45 -04007134 }
Josef Bacikaec74772008-07-24 12:12:38 -04007135
Liu Bo75e7cb72011-03-22 10:12:20 +00007136 fixup_inode_flags(new_dir, old_inode);
7137
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007138 ret = btrfs_add_link(trans, new_dir, old_inode,
7139 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04007140 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007141 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04007142
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007143 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik6a912212010-11-20 09:48:00 +00007144 struct dentry *parent = dget_parent(new_dentry);
7145 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7146 dput(parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04007147 btrfs_end_log_trans(root);
7148 }
Chris Mason39279cc2007-06-12 06:35:45 -04007149out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04007150 btrfs_end_transaction_throttle(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00007151out_notrans:
Yan, Zheng76dda932009-09-21 16:00:26 -04007152 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
7153 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04007154
Chris Mason39279cc2007-06-12 06:35:45 -04007155 return ret;
7156}
7157
Chris Masond352ac62008-09-29 15:18:18 -04007158/*
7159 * some fairly slow code that needs optimization. This walks the list
7160 * of all the inodes with pending delalloc and forces them to disk.
7161 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007162int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04007163{
7164 struct list_head *head = &root->fs_info->delalloc_inodes;
7165 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007166 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04007167
Yan Zhengc146afa2008-11-12 14:34:12 -05007168 if (root->fs_info->sb->s_flags & MS_RDONLY)
7169 return -EROFS;
7170
Chris Mason75eff682008-12-15 15:54:40 -05007171 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05007172 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04007173 binode = list_entry(head->next, struct btrfs_inode,
7174 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007175 inode = igrab(&binode->vfs_inode);
7176 if (!inode)
7177 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05007178 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007179 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007180 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00007181 if (delay_iput)
7182 btrfs_add_delayed_iput(inode);
7183 else
7184 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04007185 }
7186 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05007187 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04007188 }
Chris Mason75eff682008-12-15 15:54:40 -05007189 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04007190
7191 /* the filemap_flush will queue IO into the worker threads, but
7192 * we have to make sure the IO is actually started and that
7193 * ordered extents get created before we return
7194 */
7195 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05007196 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05007197 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04007198 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05007199 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7200 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04007201 }
7202 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04007203 return 0;
7204}
7205
Josef Bacik0019f102010-10-15 15:18:40 -04007206int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
7207 int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04007208{
7209 struct btrfs_inode *binode;
7210 struct inode *inode = NULL;
7211
7212 spin_lock(&root->fs_info->delalloc_lock);
7213 while (!list_empty(&root->fs_info->delalloc_inodes)) {
7214 binode = list_entry(root->fs_info->delalloc_inodes.next,
7215 struct btrfs_inode, delalloc_inodes);
7216 inode = igrab(&binode->vfs_inode);
7217 if (inode) {
7218 list_move_tail(&binode->delalloc_inodes,
7219 &root->fs_info->delalloc_inodes);
7220 break;
7221 }
7222
7223 list_del_init(&binode->delalloc_inodes);
7224 cond_resched_lock(&root->fs_info->delalloc_lock);
7225 }
7226 spin_unlock(&root->fs_info->delalloc_lock);
7227
7228 if (inode) {
Josef Bacik0019f102010-10-15 15:18:40 -04007229 if (sync) {
7230 filemap_write_and_wait(inode->i_mapping);
7231 /*
7232 * We have to do this because compression doesn't
7233 * actually set PG_writeback until it submits the pages
7234 * for IO, which happens in an async thread, so we could
7235 * race and not actually wait for any writeback pages
7236 * because they've not been submitted yet. Technically
7237 * this could still be the case for the ordered stuff
7238 * since the async thread may not have started to do its
7239 * work yet. If this becomes the case then we need to
7240 * figure out a way to make sure that in writepage we
7241 * wait for any async pages to be submitted before
7242 * returning so that fdatawait does what its supposed to
7243 * do.
7244 */
7245 btrfs_wait_ordered_range(inode, 0, (u64)-1);
7246 } else {
7247 filemap_flush(inode->i_mapping);
7248 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04007249 if (delay_iput)
7250 btrfs_add_delayed_iput(inode);
7251 else
7252 iput(inode);
7253 return 1;
7254 }
7255 return 0;
7256}
7257
Chris Mason39279cc2007-06-12 06:35:45 -04007258static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7259 const char *symname)
7260{
7261 struct btrfs_trans_handle *trans;
7262 struct btrfs_root *root = BTRFS_I(dir)->root;
7263 struct btrfs_path *path;
7264 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05007265 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04007266 int err;
7267 int drop_inode = 0;
7268 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04007269 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04007270 int name_len;
7271 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04007272 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04007273 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04007274 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05007275 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007276
7277 name_len = strlen(symname) + 1;
7278 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7279 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05007280
Yan, Zhenga22285a2010-05-16 10:48:46 -04007281 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
7282 if (err)
7283 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04007284 /*
7285 * 2 items for inode item and ref
7286 * 2 items for dir items
7287 * 1 item for xattr if selinux is on
7288 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04007289 trans = btrfs_start_transaction(root, 5);
7290 if (IS_ERR(trans))
7291 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05007292
Josef Bacikaec74772008-07-24 12:12:38 -04007293 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00007294 dentry->d_name.len, dir->i_ino, objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04007295 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007296 if (IS_ERR(inode)) {
7297 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04007298 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04007299 }
Chris Mason39279cc2007-06-12 06:35:45 -04007300
Eric Paris2a7dba32011-02-01 11:05:39 -05007301 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04007302 if (err) {
7303 drop_inode = 1;
7304 goto out_unlock;
7305 }
7306
Josef Bacika1b075d2010-11-19 20:36:11 +00007307 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04007308 if (err)
7309 drop_inode = 1;
7310 else {
7311 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04007312 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04007313 inode->i_fop = &btrfs_file_operations;
7314 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05007315 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04007316 }
Chris Mason39279cc2007-06-12 06:35:45 -04007317 if (drop_inode)
7318 goto out_unlock;
7319
7320 path = btrfs_alloc_path();
7321 BUG_ON(!path);
7322 key.objectid = inode->i_ino;
7323 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007324 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7325 datasize = btrfs_file_extent_calc_inline_size(name_len);
7326 err = btrfs_insert_empty_item(trans, root, path, &key,
7327 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007328 if (err) {
7329 drop_inode = 1;
7330 goto out_unlock;
7331 }
Chris Mason5f39d392007-10-15 16:14:19 -04007332 leaf = path->nodes[0];
7333 ei = btrfs_item_ptr(leaf, path->slots[0],
7334 struct btrfs_file_extent_item);
7335 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7336 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007337 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007338 btrfs_set_file_extent_encryption(leaf, ei, 0);
7339 btrfs_set_file_extent_compression(leaf, ei, 0);
7340 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7341 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7342
Chris Mason39279cc2007-06-12 06:35:45 -04007343 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007344 write_extent_buffer(leaf, symname, ptr, name_len);
7345 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007346 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007347
Chris Mason39279cc2007-06-12 06:35:45 -04007348 inode->i_op = &btrfs_symlink_inode_operations;
7349 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007350 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007351 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007352 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007353 err = btrfs_update_inode(trans, root, inode);
7354 if (err)
7355 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007356
7357out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007358 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04007359 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007360 if (drop_inode) {
7361 inode_dec_link_count(inode);
7362 iput(inode);
7363 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007364 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007365 return err;
7366}
Chris Mason16432982008-04-10 10:23:21 -04007367
Josef Bacik0af3d002010-06-21 14:48:16 -04007368static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7369 u64 start, u64 num_bytes, u64 min_size,
7370 loff_t actual_len, u64 *alloc_hint,
7371 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007372{
Yan Zhengd899e052008-10-30 14:25:28 -04007373 struct btrfs_root *root = BTRFS_I(inode)->root;
7374 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007375 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007376 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007377 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007378 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007379
Josef Bacik0af3d002010-06-21 14:48:16 -04007380 if (trans)
7381 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007382 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007383 if (own_trans) {
7384 trans = btrfs_start_transaction(root, 3);
7385 if (IS_ERR(trans)) {
7386 ret = PTR_ERR(trans);
7387 break;
7388 }
Yan Zhengd899e052008-10-30 14:25:28 -04007389 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007390
Yan, Zhengefa56462010-05-16 10:49:59 -04007391 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7392 0, *alloc_hint, (u64)-1, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007393 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007394 if (own_trans)
7395 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007396 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007397 }
7398
Yan Zhengd899e052008-10-30 14:25:28 -04007399 ret = insert_reserved_file_extent(trans, inode,
7400 cur_offset, ins.objectid,
7401 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007402 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007403 BTRFS_FILE_EXTENT_PREALLOC);
7404 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04007405 btrfs_drop_extent_cache(inode, cur_offset,
7406 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007407
Yan Zhengd899e052008-10-30 14:25:28 -04007408 num_bytes -= ins.offset;
7409 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007410 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007411
Yan Zhengd899e052008-10-30 14:25:28 -04007412 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007413 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007414 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007415 (actual_len > inode->i_size) &&
7416 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007417 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007418 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007419 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007420 i_size = cur_offset;
7421 i_size_write(inode, i_size);
7422 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007423 }
7424
Yan Zhengd899e052008-10-30 14:25:28 -04007425 ret = btrfs_update_inode(trans, root, inode);
7426 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04007427
Josef Bacik0af3d002010-06-21 14:48:16 -04007428 if (own_trans)
7429 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007430 }
Yan Zhengd899e052008-10-30 14:25:28 -04007431 return ret;
7432}
7433
Josef Bacik0af3d002010-06-21 14:48:16 -04007434int btrfs_prealloc_file_range(struct inode *inode, int mode,
7435 u64 start, u64 num_bytes, u64 min_size,
7436 loff_t actual_len, u64 *alloc_hint)
7437{
7438 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7439 min_size, actual_len, alloc_hint,
7440 NULL);
7441}
7442
7443int btrfs_prealloc_file_range_trans(struct inode *inode,
7444 struct btrfs_trans_handle *trans, int mode,
7445 u64 start, u64 num_bytes, u64 min_size,
7446 loff_t actual_len, u64 *alloc_hint)
7447{
7448 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7449 min_size, actual_len, alloc_hint, trans);
7450}
7451
Chris Masone6dcd2d2008-07-17 12:53:50 -04007452static int btrfs_set_page_dirty(struct page *page)
7453{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007454 return __set_page_dirty_nobuffers(page);
7455}
7456
Nick Pigginb74c79e2011-01-07 17:49:58 +11007457static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
Yanfdebe2b2008-01-14 13:26:08 -05007458{
Li Zefanb83cc962010-12-20 16:04:08 +08007459 struct btrfs_root *root = BTRFS_I(inode)->root;
7460
7461 if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7462 return -EROFS;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007463 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05007464 return -EACCES;
Nick Pigginb74c79e2011-01-07 17:49:58 +11007465 return generic_permission(inode, mask, flags, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05007466}
Chris Mason39279cc2007-06-12 06:35:45 -04007467
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007468static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007469 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007470 .lookup = btrfs_lookup,
7471 .create = btrfs_create,
7472 .unlink = btrfs_unlink,
7473 .link = btrfs_link,
7474 .mkdir = btrfs_mkdir,
7475 .rmdir = btrfs_rmdir,
7476 .rename = btrfs_rename,
7477 .symlink = btrfs_symlink,
7478 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007479 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007480 .setxattr = btrfs_setxattr,
7481 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007482 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007483 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007484 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007485};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007486static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007487 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007488 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007489};
Yan, Zheng76dda932009-09-21 16:00:26 -04007490
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007491static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007492 .llseek = generic_file_llseek,
7493 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007494 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007495 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007496#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007497 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007498#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007499 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007500 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007501};
7502
Chris Masond1310b22008-01-24 16:13:08 -05007503static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007504 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007505 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007506 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007507 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007508 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007509 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04007510 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007511 .set_bit_hook = btrfs_set_bit_hook,
7512 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007513 .merge_extent_hook = btrfs_merge_extent_hook,
7514 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007515};
7516
Chris Mason35054392009-01-21 13:11:13 -05007517/*
7518 * btrfs doesn't support the bmap operation because swapfiles
7519 * use bmap to make a mapping of extents in the file. They assume
7520 * these extents won't change over the life of the file and they
7521 * use the bmap result to do IO directly to the drive.
7522 *
7523 * the btrfs bmap call would return logical addresses that aren't
7524 * suitable for IO and they also will change frequently as COW
7525 * operations happen. So, swapfile + btrfs == corruption.
7526 *
7527 * For now we're avoiding this by dropping bmap.
7528 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007529static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007530 .readpage = btrfs_readpage,
7531 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007532 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007533 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -04007534 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007535 .invalidatepage = btrfs_invalidatepage,
7536 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007537 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007538 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007539};
7540
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007541static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007542 .readpage = btrfs_readpage,
7543 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007544 .invalidatepage = btrfs_invalidatepage,
7545 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007546};
7547
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007548static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007549 .getattr = btrfs_getattr,
7550 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007551 .setxattr = btrfs_setxattr,
7552 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007553 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007554 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007555 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007556 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04007557};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007558static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007559 .getattr = btrfs_getattr,
7560 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007561 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007562 .setxattr = btrfs_setxattr,
7563 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007564 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007565 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007566};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007567static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007568 .readlink = generic_readlink,
7569 .follow_link = page_follow_link_light,
7570 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007571 .getattr = btrfs_getattr,
Yanfdebe2b2008-01-14 13:26:08 -05007572 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007573 .setxattr = btrfs_setxattr,
7574 .getxattr = btrfs_getxattr,
7575 .listxattr = btrfs_listxattr,
7576 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007577};
Yan, Zheng76dda932009-09-21 16:00:26 -04007578
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007579const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007580 .d_delete = btrfs_dentry_delete,
7581};