blob: 64daf2acd0d5296a67d320a83e8d48c169f16a4c [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"
Chris Mason39279cc2007-06-12 06:35:45 -040053
54struct btrfs_iget_args {
55 u64 ino;
56 struct btrfs_root *root;
57};
58
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070059static const struct inode_operations btrfs_dir_inode_operations;
60static const struct inode_operations btrfs_symlink_inode_operations;
61static const struct inode_operations btrfs_dir_ro_inode_operations;
62static const struct inode_operations btrfs_special_inode_operations;
63static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070064static const struct address_space_operations btrfs_aops;
65static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070066static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050067static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040068
69static struct kmem_cache *btrfs_inode_cachep;
70struct kmem_cache *btrfs_trans_handle_cachep;
71struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040072struct kmem_cache *btrfs_path_cachep;
73
74#define S_SHIFT 12
75static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
76 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
77 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
78 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
79 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
80 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
81 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
82 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
83};
84
Josef Bacik7b128762008-07-24 12:17:14 -040085static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040086static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050087static noinline int cow_file_range(struct inode *inode,
88 struct page *locked_page,
89 u64 start, u64 end, int *page_started,
90 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040091
Yan, Zhengf34f57a2009-11-12 09:35:27 +000092static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
93 struct inode *inode, struct inode *dir)
Jim Owens0279b4c2009-02-04 09:29:13 -050094{
95 int err;
96
Yan, Zhengf34f57a2009-11-12 09:35:27 +000097 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -050098 if (!err)
Yan, Zhengf34f57a2009-11-12 09:35:27 +000099 err = btrfs_xattr_security_init(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500100 return err;
101}
102
Chris Masond352ac62008-09-29 15:18:18 -0400103/*
Chris Masonc8b97812008-10-29 14:49:59 -0400104 * this does all the hard work for inserting an inline extent into
105 * the btree. The caller should have done a btrfs_drop_extents so that
106 * no overlapping inline items exist in the btree
107 */
Chris Masond3977122009-01-05 21:25:51 -0500108static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400109 struct btrfs_root *root, struct inode *inode,
110 u64 start, size_t size, size_t compressed_size,
111 struct page **compressed_pages)
112{
113 struct btrfs_key key;
114 struct btrfs_path *path;
115 struct extent_buffer *leaf;
116 struct page *page = NULL;
117 char *kaddr;
118 unsigned long ptr;
119 struct btrfs_file_extent_item *ei;
120 int err = 0;
121 int ret;
122 size_t cur_size = size;
123 size_t datasize;
124 unsigned long offset;
125 int use_compress = 0;
126
127 if (compressed_size && compressed_pages) {
128 use_compress = 1;
129 cur_size = compressed_size;
130 }
131
Chris Masond3977122009-01-05 21:25:51 -0500132 path = btrfs_alloc_path();
133 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400134 return -ENOMEM;
135
Chris Masonb9473432009-03-13 11:00:37 -0400136 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400137 btrfs_set_trans_block_group(trans, inode);
138
139 key.objectid = inode->i_ino;
140 key.offset = start;
141 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400142 datasize = btrfs_file_extent_calc_inline_size(cur_size);
143
144 inode_add_bytes(inode, size);
145 ret = btrfs_insert_empty_item(trans, root, path, &key,
146 datasize);
147 BUG_ON(ret);
148 if (ret) {
149 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400150 goto fail;
151 }
152 leaf = path->nodes[0];
153 ei = btrfs_item_ptr(leaf, path->slots[0],
154 struct btrfs_file_extent_item);
155 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
156 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
157 btrfs_set_file_extent_encryption(leaf, ei, 0);
158 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
159 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
160 ptr = btrfs_file_extent_inline_start(ei);
161
162 if (use_compress) {
163 struct page *cpage;
164 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500165 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400166 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500167 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400168 PAGE_CACHE_SIZE);
169
Chris Masonb9473432009-03-13 11:00:37 -0400170 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400171 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400172 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400173
174 i++;
175 ptr += cur_size;
176 compressed_size -= cur_size;
177 }
178 btrfs_set_file_extent_compression(leaf, ei,
179 BTRFS_COMPRESS_ZLIB);
180 } else {
181 page = find_get_page(inode->i_mapping,
182 start >> PAGE_CACHE_SHIFT);
183 btrfs_set_file_extent_compression(leaf, ei, 0);
184 kaddr = kmap_atomic(page, KM_USER0);
185 offset = start & (PAGE_CACHE_SIZE - 1);
186 write_extent_buffer(leaf, kaddr + offset, ptr, size);
187 kunmap_atomic(kaddr, KM_USER0);
188 page_cache_release(page);
189 }
190 btrfs_mark_buffer_dirty(leaf);
191 btrfs_free_path(path);
192
Yan, Zhengc21677542009-11-12 09:34:21 +0000193 /*
194 * we're an inline extent, so nobody can
195 * extend the file past i_size without locking
196 * a page we already have locked.
197 *
198 * We must do any isize and inode updates
199 * before we unlock the pages. Otherwise we
200 * could end up racing with unlink.
201 */
Chris Masonc8b97812008-10-29 14:49:59 -0400202 BTRFS_I(inode)->disk_i_size = inode->i_size;
203 btrfs_update_inode(trans, root, inode);
Yan, Zhengc21677542009-11-12 09:34:21 +0000204
Chris Masonc8b97812008-10-29 14:49:59 -0400205 return 0;
206fail:
207 btrfs_free_path(path);
208 return err;
209}
210
211
212/*
213 * conditionally insert an inline extent into the file. This
214 * does the checks required to make sure the data is small enough
215 * to fit as an inline extent.
216 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400217static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400218 struct btrfs_root *root,
219 struct inode *inode, u64 start, u64 end,
220 size_t compressed_size,
221 struct page **compressed_pages)
222{
223 u64 isize = i_size_read(inode);
224 u64 actual_end = min(end + 1, isize);
225 u64 inline_len = actual_end - start;
226 u64 aligned_end = (end + root->sectorsize - 1) &
227 ~((u64)root->sectorsize - 1);
228 u64 hint_byte;
229 u64 data_len = inline_len;
230 int ret;
231
232 if (compressed_size)
233 data_len = compressed_size;
234
235 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400236 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400237 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
238 (!compressed_size &&
239 (actual_end & (root->sectorsize - 1)) == 0) ||
240 end + 1 < isize ||
241 data_len > root->fs_info->max_inline) {
242 return 1;
243 }
244
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000245 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400246 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 BUG_ON(ret);
248
249 if (isize > actual_end)
250 inline_len = min_t(u64, isize, actual_end);
251 ret = insert_inline_extent(trans, root, inode, start,
252 inline_len, compressed_size,
253 compressed_pages);
254 BUG_ON(ret);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400255 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400256 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400257 return 0;
258}
259
Chris Mason771ed682008-11-06 22:02:51 -0500260struct async_extent {
261 u64 start;
262 u64 ram_size;
263 u64 compressed_size;
264 struct page **pages;
265 unsigned long nr_pages;
266 struct list_head list;
267};
268
269struct async_cow {
270 struct inode *inode;
271 struct btrfs_root *root;
272 struct page *locked_page;
273 u64 start;
274 u64 end;
275 struct list_head extents;
276 struct btrfs_work work;
277};
278
279static noinline int add_async_extent(struct async_cow *cow,
280 u64 start, u64 ram_size,
281 u64 compressed_size,
282 struct page **pages,
283 unsigned long nr_pages)
284{
285 struct async_extent *async_extent;
286
287 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
288 async_extent->start = start;
289 async_extent->ram_size = ram_size;
290 async_extent->compressed_size = compressed_size;
291 async_extent->pages = pages;
292 async_extent->nr_pages = nr_pages;
293 list_add_tail(&async_extent->list, &cow->extents);
294 return 0;
295}
296
Chris Masonc8b97812008-10-29 14:49:59 -0400297/*
Chris Mason771ed682008-11-06 22:02:51 -0500298 * we create compressed extents in two phases. The first
299 * phase compresses a range of pages that have already been
300 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400301 *
Chris Mason771ed682008-11-06 22:02:51 -0500302 * This is done inside an ordered work queue, and the compression
303 * is spread across many cpus. The actual IO submission is step
304 * two, and the ordered work queue takes care of making sure that
305 * happens in the same order things were put onto the queue by
306 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400307 *
Chris Mason771ed682008-11-06 22:02:51 -0500308 * If this code finds it can't get good compression, it puts an
309 * entry onto the work queue to write the uncompressed bytes. This
310 * makes sure that both compressed inodes and uncompressed inodes
311 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400312 */
Chris Mason771ed682008-11-06 22:02:51 -0500313static noinline int compress_file_range(struct inode *inode,
314 struct page *locked_page,
315 u64 start, u64 end,
316 struct async_cow *async_cow,
317 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400318{
319 struct btrfs_root *root = BTRFS_I(inode)->root;
320 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400321 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400322 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400323 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500324 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400325 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400326 struct page **pages = NULL;
327 unsigned long nr_pages;
328 unsigned long nr_pages_ret = 0;
329 unsigned long total_compressed = 0;
330 unsigned long total_in = 0;
331 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500332 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400333 int i;
334 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400335
Chris Mason42dc7ba2008-12-15 11:44:56 -0500336 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400337again:
338 will_compress = 0;
339 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
340 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
341
Chris Masonf03d9302009-02-04 09:31:06 -0500342 /*
343 * we don't want to send crud past the end of i_size through
344 * compression, that's just a waste of CPU time. So, if the
345 * end of the file is before the start of our current
346 * requested range of bytes, we bail out to the uncompressed
347 * cleanup code that can deal with all of this.
348 *
349 * It isn't really the fastest way to fix things, but this is a
350 * very uncommon corner.
351 */
352 if (actual_end <= start)
353 goto cleanup_and_bail_uncompressed;
354
Chris Masonc8b97812008-10-29 14:49:59 -0400355 total_compressed = actual_end - start;
356
357 /* we want to make sure that amount of ram required to uncompress
358 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500359 * of a compressed extent to 128k. This is a crucial number
360 * because it also controls how easily we can spread reads across
361 * cpus for decompression.
362 *
363 * We also want to make sure the amount of IO required to do
364 * a random read is reasonably small, so we limit the size of
365 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400366 */
367 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400368 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500369 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400370 total_in = 0;
371 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400372
Chris Mason771ed682008-11-06 22:02:51 -0500373 /*
374 * we do compression for mount -o compress and when the
375 * inode has not been flagged as nocompress. This flag can
376 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400377 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200378 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500379 (btrfs_test_opt(root, COMPRESS) ||
380 (BTRFS_I(inode)->force_compress))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400381 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400382 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400383
Chris Masonc8b97812008-10-29 14:49:59 -0400384 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
385 total_compressed, pages,
386 nr_pages, &nr_pages_ret,
387 &total_in,
388 &total_compressed,
389 max_compressed);
390
391 if (!ret) {
392 unsigned long offset = total_compressed &
393 (PAGE_CACHE_SIZE - 1);
394 struct page *page = pages[nr_pages_ret - 1];
395 char *kaddr;
396
397 /* zero the tail end of the last page, we might be
398 * sending it down to disk
399 */
400 if (offset) {
401 kaddr = kmap_atomic(page, KM_USER0);
402 memset(kaddr + offset, 0,
403 PAGE_CACHE_SIZE - offset);
404 kunmap_atomic(kaddr, KM_USER0);
405 }
406 will_compress = 1;
407 }
408 }
409 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500410 trans = btrfs_join_transaction(root, 1);
411 BUG_ON(!trans);
412 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400413 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500414
Chris Masonc8b97812008-10-29 14:49:59 -0400415 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500416 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400417 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500418 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400419 */
420 ret = cow_file_range_inline(trans, root, inode,
421 start, end, 0, NULL);
422 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500423 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400424 ret = cow_file_range_inline(trans, root, inode,
425 start, end,
426 total_compressed, pages);
427 }
428 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500429 /*
430 * inline extent creation worked, we don't need
431 * to create any more async work items. Unlock
432 * and free up our temp pages.
433 */
Chris Masonc8b97812008-10-29 14:49:59 -0400434 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400435 &BTRFS_I(inode)->io_tree,
436 start, end, NULL,
437 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400438 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400439 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000440
441 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400442 goto free_pages_out;
443 }
Yan, Zhengc21677542009-11-12 09:34:21 +0000444 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400445 }
446
447 if (will_compress) {
448 /*
449 * we aren't doing an inline extent round the compressed size
450 * up to a block size boundary so the allocator does sane
451 * things
452 */
453 total_compressed = (total_compressed + blocksize - 1) &
454 ~(blocksize - 1);
455
456 /*
457 * one last check to make sure the compression is really a
458 * win, compare the page count read with the blocks on disk
459 */
460 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
461 ~(PAGE_CACHE_SIZE - 1);
462 if (total_compressed >= total_in) {
463 will_compress = 0;
464 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400465 num_bytes = total_in;
466 }
467 }
468 if (!will_compress && pages) {
469 /*
470 * the compression code ran but failed to make things smaller,
471 * free any pages it allocated and our page pointer array
472 */
473 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400474 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400475 page_cache_release(pages[i]);
476 }
477 kfree(pages);
478 pages = NULL;
479 total_compressed = 0;
480 nr_pages_ret = 0;
481
482 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500483 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
484 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500485 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500486 }
Chris Masonc8b97812008-10-29 14:49:59 -0400487 }
Chris Mason771ed682008-11-06 22:02:51 -0500488 if (will_compress) {
489 *num_added += 1;
490
491 /* the async work queues will take care of doing actual
492 * allocation on disk for these compressed pages,
493 * and will submit them to the elevator.
494 */
495 add_async_extent(async_cow, start, num_bytes,
496 total_compressed, pages, nr_pages_ret);
497
Yan, Zheng24ae6362010-12-06 07:02:36 +0000498 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500499 start += num_bytes;
500 pages = NULL;
501 cond_resched();
502 goto again;
503 }
504 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500505cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500506 /*
507 * No compression, but we still need to write the pages in
508 * the file we've been given so far. redirty the locked
509 * page if it corresponds to our extent and set things up
510 * for the async work queue to run cow_file_range to do
511 * the normal delalloc dance
512 */
513 if (page_offset(locked_page) >= start &&
514 page_offset(locked_page) <= end) {
515 __set_page_dirty_nobuffers(locked_page);
516 /* unlocked later on in the async handlers */
517 }
518 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
519 *num_added += 1;
520 }
521
522out:
523 return 0;
524
525free_pages_out:
526 for (i = 0; i < nr_pages_ret; i++) {
527 WARN_ON(pages[i]->mapping);
528 page_cache_release(pages[i]);
529 }
Chris Masond3977122009-01-05 21:25:51 -0500530 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500531
532 goto out;
533}
534
535/*
536 * phase two of compressed writeback. This is the ordered portion
537 * of the code, which only gets called in the order the work was
538 * queued. We walk all the async extents created by compress_file_range
539 * and send them down to the disk.
540 */
541static noinline int submit_compressed_extents(struct inode *inode,
542 struct async_cow *async_cow)
543{
544 struct async_extent *async_extent;
545 u64 alloc_hint = 0;
546 struct btrfs_trans_handle *trans;
547 struct btrfs_key ins;
548 struct extent_map *em;
549 struct btrfs_root *root = BTRFS_I(inode)->root;
550 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
551 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500552 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500553
554 if (list_empty(&async_cow->extents))
555 return 0;
556
Chris Mason771ed682008-11-06 22:02:51 -0500557
Chris Masond3977122009-01-05 21:25:51 -0500558 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500559 async_extent = list_entry(async_cow->extents.next,
560 struct async_extent, list);
561 list_del(&async_extent->list);
562
563 io_tree = &BTRFS_I(inode)->io_tree;
564
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500565retry:
Chris Mason771ed682008-11-06 22:02:51 -0500566 /* did the compression code fall back to uncompressed IO? */
567 if (!async_extent->pages) {
568 int page_started = 0;
569 unsigned long nr_written = 0;
570
571 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000572 async_extent->start +
573 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500574
575 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500576 ret = cow_file_range(inode, async_cow->locked_page,
577 async_extent->start,
578 async_extent->start +
579 async_extent->ram_size - 1,
580 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500581
582 /*
583 * if page_started, cow_file_range inserted an
584 * inline extent and took care of all the unlocking
585 * and IO for us. Otherwise, we need to submit
586 * all those pages down to the drive.
587 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500588 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500589 extent_write_locked_range(io_tree,
590 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500591 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500592 async_extent->ram_size - 1,
593 btrfs_get_extent,
594 WB_SYNC_ALL);
595 kfree(async_extent);
596 cond_resched();
597 continue;
598 }
599
600 lock_extent(io_tree, async_extent->start,
601 async_extent->start + async_extent->ram_size - 1,
602 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500603
Yan, Zhengc21677542009-11-12 09:34:21 +0000604 trans = btrfs_join_transaction(root, 1);
Chris Mason771ed682008-11-06 22:02:51 -0500605 ret = btrfs_reserve_extent(trans, root,
606 async_extent->compressed_size,
607 async_extent->compressed_size,
608 0, alloc_hint,
609 (u64)-1, &ins, 1);
Yan, Zhengc21677542009-11-12 09:34:21 +0000610 btrfs_end_transaction(trans, root);
611
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500612 if (ret) {
613 int i;
614 for (i = 0; i < async_extent->nr_pages; i++) {
615 WARN_ON(async_extent->pages[i]->mapping);
616 page_cache_release(async_extent->pages[i]);
617 }
618 kfree(async_extent->pages);
619 async_extent->nr_pages = 0;
620 async_extent->pages = NULL;
621 unlock_extent(io_tree, async_extent->start,
622 async_extent->start +
623 async_extent->ram_size - 1, GFP_NOFS);
624 goto retry;
625 }
626
Yan, Zhengc21677542009-11-12 09:34:21 +0000627 /*
628 * here we're doing allocation and writeback of the
629 * compressed pages
630 */
631 btrfs_drop_extent_cache(inode, async_extent->start,
632 async_extent->start +
633 async_extent->ram_size - 1, 0);
634
Chris Mason771ed682008-11-06 22:02:51 -0500635 em = alloc_extent_map(GFP_NOFS);
636 em->start = async_extent->start;
637 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500638 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500639
640 em->block_start = ins.objectid;
641 em->block_len = ins.offset;
642 em->bdev = root->fs_info->fs_devices->latest_bdev;
643 set_bit(EXTENT_FLAG_PINNED, &em->flags);
644 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
645
Chris Masond3977122009-01-05 21:25:51 -0500646 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400647 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500648 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400649 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500650 if (ret != -EEXIST) {
651 free_extent_map(em);
652 break;
653 }
654 btrfs_drop_extent_cache(inode, async_extent->start,
655 async_extent->start +
656 async_extent->ram_size - 1, 0);
657 }
658
659 ret = btrfs_add_ordered_extent(inode, async_extent->start,
660 ins.objectid,
661 async_extent->ram_size,
662 ins.offset,
663 BTRFS_ORDERED_COMPRESSED);
664 BUG_ON(ret);
665
Chris Mason771ed682008-11-06 22:02:51 -0500666 /*
667 * clear dirty, set writeback and unlock the pages.
668 */
669 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400670 &BTRFS_I(inode)->io_tree,
671 async_extent->start,
672 async_extent->start +
673 async_extent->ram_size - 1,
674 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
675 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400676 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400677 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500678
679 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500680 async_extent->start,
681 async_extent->ram_size,
682 ins.objectid,
683 ins.offset, async_extent->pages,
684 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500685
686 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500687 alloc_hint = ins.objectid + ins.offset;
688 kfree(async_extent);
689 cond_resched();
690 }
691
Chris Mason771ed682008-11-06 22:02:51 -0500692 return 0;
693}
694
Josef Bacik4b46fce2010-05-23 11:00:55 -0400695static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
696 u64 num_bytes)
697{
698 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
699 struct extent_map *em;
700 u64 alloc_hint = 0;
701
702 read_lock(&em_tree->lock);
703 em = search_extent_mapping(em_tree, start, num_bytes);
704 if (em) {
705 /*
706 * if block start isn't an actual block number then find the
707 * first block in this inode and use that as a hint. If that
708 * block is also bogus then just don't worry about it.
709 */
710 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
711 free_extent_map(em);
712 em = search_extent_mapping(em_tree, 0, 0);
713 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
714 alloc_hint = em->block_start;
715 if (em)
716 free_extent_map(em);
717 } else {
718 alloc_hint = em->block_start;
719 free_extent_map(em);
720 }
721 }
722 read_unlock(&em_tree->lock);
723
724 return alloc_hint;
725}
726
Chris Mason771ed682008-11-06 22:02:51 -0500727/*
728 * when extent_io.c finds a delayed allocation range in the file,
729 * the call backs end up in this code. The basic idea is to
730 * allocate extents on disk for the range, and create ordered data structs
731 * in ram to track those extents.
732 *
733 * locked_page is the page that writepage had locked already. We use
734 * it to make sure we don't do extra locks or unlocks.
735 *
736 * *page_started is set to one if we unlock locked_page and do everything
737 * required to start IO on it. It may be clean and already done with
738 * IO when we return.
739 */
740static noinline int cow_file_range(struct inode *inode,
741 struct page *locked_page,
742 u64 start, u64 end, int *page_started,
743 unsigned long *nr_written,
744 int unlock)
745{
746 struct btrfs_root *root = BTRFS_I(inode)->root;
747 struct btrfs_trans_handle *trans;
748 u64 alloc_hint = 0;
749 u64 num_bytes;
750 unsigned long ram_size;
751 u64 disk_num_bytes;
752 u64 cur_alloc_size;
753 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500754 struct btrfs_key ins;
755 struct extent_map *em;
756 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
757 int ret = 0;
758
Josef Bacik0cb59c92010-07-02 12:14:14 -0400759 BUG_ON(root == root->fs_info->tree_root);
Chris Mason771ed682008-11-06 22:02:51 -0500760 trans = btrfs_join_transaction(root, 1);
761 BUG_ON(!trans);
762 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400763 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500764
Chris Mason771ed682008-11-06 22:02:51 -0500765 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
766 num_bytes = max(blocksize, num_bytes);
767 disk_num_bytes = num_bytes;
768 ret = 0;
769
770 if (start == 0) {
771 /* lets try to make an inline extent */
772 ret = cow_file_range_inline(trans, root, inode,
773 start, end, 0, NULL);
774 if (ret == 0) {
775 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400776 &BTRFS_I(inode)->io_tree,
777 start, end, NULL,
778 EXTENT_CLEAR_UNLOCK_PAGE |
779 EXTENT_CLEAR_UNLOCK |
780 EXTENT_CLEAR_DELALLOC |
781 EXTENT_CLEAR_DIRTY |
782 EXTENT_SET_WRITEBACK |
783 EXTENT_END_WRITEBACK);
Yan, Zhengc21677542009-11-12 09:34:21 +0000784
Chris Mason771ed682008-11-06 22:02:51 -0500785 *nr_written = *nr_written +
786 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
787 *page_started = 1;
788 ret = 0;
789 goto out;
790 }
791 }
Chris Masonc8b97812008-10-29 14:49:59 -0400792
793 BUG_ON(disk_num_bytes >
794 btrfs_super_total_bytes(&root->fs_info->super_copy));
795
Josef Bacik4b46fce2010-05-23 11:00:55 -0400796 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400797 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400798
Chris Masond3977122009-01-05 21:25:51 -0500799 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400800 unsigned long op;
801
Josef Bacik287a0ab2010-03-19 18:07:23 +0000802 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400803 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500804 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400805 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500806 BUG_ON(ret);
807
Chris Masone6dcd2d2008-07-17 12:53:50 -0400808 em = alloc_extent_map(GFP_NOFS);
809 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500810 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500811 ram_size = ins.offset;
812 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400813
Chris Masone6dcd2d2008-07-17 12:53:50 -0400814 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400815 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400816 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400817 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400818
Chris Masond3977122009-01-05 21:25:51 -0500819 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400820 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400821 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400822 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400823 if (ret != -EEXIST) {
824 free_extent_map(em);
825 break;
826 }
827 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400828 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400829 }
830
Chris Mason98d20f62008-04-14 09:46:10 -0400831 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400832 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500833 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400834 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400835
Yan Zheng17d217f2008-12-12 10:03:38 -0500836 if (root->root_key.objectid ==
837 BTRFS_DATA_RELOC_TREE_OBJECTID) {
838 ret = btrfs_reloc_clone_csums(inode, start,
839 cur_alloc_size);
840 BUG_ON(ret);
841 }
842
Chris Masond3977122009-01-05 21:25:51 -0500843 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400844 break;
Chris Masond3977122009-01-05 21:25:51 -0500845
Chris Masonc8b97812008-10-29 14:49:59 -0400846 /* we're not doing compressed IO, don't unlock the first
847 * page (which the caller expects to stay locked), don't
848 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400849 *
850 * Do set the Private2 bit so we know this page was properly
851 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400852 */
Chris Masona791e352009-10-08 11:27:10 -0400853 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
854 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
855 EXTENT_SET_PRIVATE2;
856
Chris Masonc8b97812008-10-29 14:49:59 -0400857 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
858 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400859 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400860 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500861 num_bytes -= cur_alloc_size;
862 alloc_hint = ins.objectid + ins.offset;
863 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400864 }
Chris Masonb888db22007-08-27 16:49:44 -0400865out:
Chris Mason771ed682008-11-06 22:02:51 -0500866 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400867 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400868
Chris Masonbe20aa92007-12-17 20:14:01 -0500869 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500870}
Chris Masonc8b97812008-10-29 14:49:59 -0400871
Chris Mason771ed682008-11-06 22:02:51 -0500872/*
873 * work queue call back to started compression on a file and pages
874 */
875static noinline void async_cow_start(struct btrfs_work *work)
876{
877 struct async_cow *async_cow;
878 int num_added = 0;
879 async_cow = container_of(work, struct async_cow, work);
880
881 compress_file_range(async_cow->inode, async_cow->locked_page,
882 async_cow->start, async_cow->end, async_cow,
883 &num_added);
884 if (num_added == 0)
885 async_cow->inode = NULL;
886}
887
888/*
889 * work queue call back to submit previously compressed pages
890 */
891static noinline void async_cow_submit(struct btrfs_work *work)
892{
893 struct async_cow *async_cow;
894 struct btrfs_root *root;
895 unsigned long nr_pages;
896
897 async_cow = container_of(work, struct async_cow, work);
898
899 root = async_cow->root;
900 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
901 PAGE_CACHE_SHIFT;
902
903 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
904
905 if (atomic_read(&root->fs_info->async_delalloc_pages) <
906 5 * 1042 * 1024 &&
907 waitqueue_active(&root->fs_info->async_submit_wait))
908 wake_up(&root->fs_info->async_submit_wait);
909
Chris Masond3977122009-01-05 21:25:51 -0500910 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500911 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500912}
Chris Masonc8b97812008-10-29 14:49:59 -0400913
Chris Mason771ed682008-11-06 22:02:51 -0500914static noinline void async_cow_free(struct btrfs_work *work)
915{
916 struct async_cow *async_cow;
917 async_cow = container_of(work, struct async_cow, work);
918 kfree(async_cow);
919}
920
921static int cow_file_range_async(struct inode *inode, struct page *locked_page,
922 u64 start, u64 end, int *page_started,
923 unsigned long *nr_written)
924{
925 struct async_cow *async_cow;
926 struct btrfs_root *root = BTRFS_I(inode)->root;
927 unsigned long nr_pages;
928 u64 cur_end;
929 int limit = 10 * 1024 * 1042;
930
Chris Masona3429ab2009-10-08 12:30:20 -0400931 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
932 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500933 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500934 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
935 async_cow->inode = inode;
936 async_cow->root = root;
937 async_cow->locked_page = locked_page;
938 async_cow->start = start;
939
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200940 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500941 cur_end = end;
942 else
943 cur_end = min(end, start + 512 * 1024 - 1);
944
945 async_cow->end = cur_end;
946 INIT_LIST_HEAD(&async_cow->extents);
947
948 async_cow->work.func = async_cow_start;
949 async_cow->work.ordered_func = async_cow_submit;
950 async_cow->work.ordered_free = async_cow_free;
951 async_cow->work.flags = 0;
952
Chris Mason771ed682008-11-06 22:02:51 -0500953 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
954 PAGE_CACHE_SHIFT;
955 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
956
957 btrfs_queue_worker(&root->fs_info->delalloc_workers,
958 &async_cow->work);
959
960 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
961 wait_event(root->fs_info->async_submit_wait,
962 (atomic_read(&root->fs_info->async_delalloc_pages) <
963 limit));
964 }
965
Chris Masond3977122009-01-05 21:25:51 -0500966 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500967 atomic_read(&root->fs_info->async_delalloc_pages)) {
968 wait_event(root->fs_info->async_submit_wait,
969 (atomic_read(&root->fs_info->async_delalloc_pages) ==
970 0));
971 }
972
973 *nr_written += nr_pages;
974 start = cur_end + 1;
975 }
976 *page_started = 1;
977 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500978}
979
Chris Masond3977122009-01-05 21:25:51 -0500980static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500981 u64 bytenr, u64 num_bytes)
982{
983 int ret;
984 struct btrfs_ordered_sum *sums;
985 LIST_HEAD(list);
986
Yan Zheng07d400a2009-01-06 11:42:00 -0500987 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
988 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500989 if (ret == 0 && list_empty(&list))
990 return 0;
991
992 while (!list_empty(&list)) {
993 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
994 list_del(&sums->list);
995 kfree(sums);
996 }
997 return 1;
998}
999
Chris Masond352ac62008-09-29 15:18:18 -04001000/*
1001 * when nowcow writeback call back. This checks for snapshots or COW copies
1002 * of the extents that exist in the file, and COWs the file as required.
1003 *
1004 * If no cow copies or snapshots exist, we write directly to the existing
1005 * blocks on disk
1006 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001007static noinline int run_delalloc_nocow(struct inode *inode,
1008 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001009 u64 start, u64 end, int *page_started, int force,
1010 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001011{
Chris Masonbe20aa92007-12-17 20:14:01 -05001012 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001013 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001014 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001015 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001016 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001017 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001018 u64 cow_start;
1019 u64 cur_offset;
1020 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001022 u64 disk_bytenr;
1023 u64 num_bytes;
1024 int extent_type;
1025 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001026 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001027 int nocow;
1028 int check_prev = 1;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001029 bool nolock = false;
Chris Masonbe20aa92007-12-17 20:14:01 -05001030
1031 path = btrfs_alloc_path();
1032 BUG_ON(!path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001033 if (root == root->fs_info->tree_root) {
1034 nolock = true;
1035 trans = btrfs_join_transaction_nolock(root, 1);
1036 } else {
1037 trans = btrfs_join_transaction(root, 1);
1038 }
Yan Zheng7ea394f2008-08-05 13:05:02 -04001039 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -05001040
Yan Zheng80ff3852008-10-30 14:20:02 -04001041 cow_start = (u64)-1;
1042 cur_offset = start;
1043 while (1) {
1044 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1045 cur_offset, 0);
1046 BUG_ON(ret < 0);
1047 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1048 leaf = path->nodes[0];
1049 btrfs_item_key_to_cpu(leaf, &found_key,
1050 path->slots[0] - 1);
1051 if (found_key.objectid == inode->i_ino &&
1052 found_key.type == BTRFS_EXTENT_DATA_KEY)
1053 path->slots[0]--;
1054 }
1055 check_prev = 0;
1056next_slot:
1057 leaf = path->nodes[0];
1058 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1059 ret = btrfs_next_leaf(root, path);
1060 if (ret < 0)
1061 BUG_ON(1);
1062 if (ret > 0)
1063 break;
1064 leaf = path->nodes[0];
1065 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001066
Yan Zheng80ff3852008-10-30 14:20:02 -04001067 nocow = 0;
1068 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001069 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001070 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001071
Yan Zheng80ff3852008-10-30 14:20:02 -04001072 if (found_key.objectid > inode->i_ino ||
1073 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1074 found_key.offset > end)
1075 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001076
Yan Zheng80ff3852008-10-30 14:20:02 -04001077 if (found_key.offset > cur_offset) {
1078 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001079 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001080 goto out_check;
1081 }
Chris Masonc31f8832008-01-08 15:46:31 -05001082
Yan Zheng80ff3852008-10-30 14:20:02 -04001083 fi = btrfs_item_ptr(leaf, path->slots[0],
1084 struct btrfs_file_extent_item);
1085 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001086
Yan Zhengd899e052008-10-30 14:25:28 -04001087 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1088 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001089 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001090 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001091 extent_end = found_key.offset +
1092 btrfs_file_extent_num_bytes(leaf, fi);
1093 if (extent_end <= start) {
1094 path->slots[0]++;
1095 goto next_slot;
1096 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001097 if (disk_bytenr == 0)
1098 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001099 if (btrfs_file_extent_compression(leaf, fi) ||
1100 btrfs_file_extent_encryption(leaf, fi) ||
1101 btrfs_file_extent_other_encoding(leaf, fi))
1102 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001103 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1104 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001105 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001106 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001107 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001108 found_key.offset -
1109 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001110 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001111 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001112 disk_bytenr += cur_offset - found_key.offset;
1113 num_bytes = min(end + 1, extent_end) - cur_offset;
1114 /*
1115 * force cow if csum exists in the range.
1116 * this ensure that csum for a given extent are
1117 * either valid or do not exist.
1118 */
1119 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1120 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001121 nocow = 1;
1122 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1123 extent_end = found_key.offset +
1124 btrfs_file_extent_inline_len(leaf, fi);
1125 extent_end = ALIGN(extent_end, root->sectorsize);
1126 } else {
1127 BUG_ON(1);
1128 }
1129out_check:
1130 if (extent_end <= start) {
1131 path->slots[0]++;
1132 goto next_slot;
1133 }
1134 if (!nocow) {
1135 if (cow_start == (u64)-1)
1136 cow_start = cur_offset;
1137 cur_offset = extent_end;
1138 if (cur_offset > end)
1139 break;
1140 path->slots[0]++;
1141 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001142 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001143
Yan Zheng7ea394f2008-08-05 13:05:02 -04001144 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001145 if (cow_start != (u64)-1) {
1146 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001147 found_key.offset - 1, page_started,
1148 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001149 BUG_ON(ret);
1150 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001151 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001152
Yan Zhengd899e052008-10-30 14:25:28 -04001153 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1154 struct extent_map *em;
1155 struct extent_map_tree *em_tree;
1156 em_tree = &BTRFS_I(inode)->extent_tree;
1157 em = alloc_extent_map(GFP_NOFS);
1158 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001159 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001160 em->len = num_bytes;
1161 em->block_len = num_bytes;
1162 em->block_start = disk_bytenr;
1163 em->bdev = root->fs_info->fs_devices->latest_bdev;
1164 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1165 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001166 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001167 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001168 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001169 if (ret != -EEXIST) {
1170 free_extent_map(em);
1171 break;
1172 }
1173 btrfs_drop_extent_cache(inode, em->start,
1174 em->start + em->len - 1, 0);
1175 }
1176 type = BTRFS_ORDERED_PREALLOC;
1177 } else {
1178 type = BTRFS_ORDERED_NOCOW;
1179 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001180
1181 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001182 num_bytes, num_bytes, type);
1183 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001184
Yan, Zhengefa56462010-05-16 10:49:59 -04001185 if (root->root_key.objectid ==
1186 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1187 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1188 num_bytes);
1189 BUG_ON(ret);
1190 }
1191
Yan Zhengd899e052008-10-30 14:25:28 -04001192 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001193 cur_offset, cur_offset + num_bytes - 1,
1194 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1195 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1196 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001197 cur_offset = extent_end;
1198 if (cur_offset > end)
1199 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001200 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001201 btrfs_release_path(root, path);
1202
1203 if (cur_offset <= end && cow_start == (u64)-1)
1204 cow_start = cur_offset;
1205 if (cow_start != (u64)-1) {
1206 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001207 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001208 BUG_ON(ret);
1209 }
1210
Josef Bacik0cb59c92010-07-02 12:14:14 -04001211 if (nolock) {
1212 ret = btrfs_end_transaction_nolock(trans, root);
1213 BUG_ON(ret);
1214 } else {
1215 ret = btrfs_end_transaction(trans, root);
1216 BUG_ON(ret);
1217 }
Yan Zheng7ea394f2008-08-05 13:05:02 -04001218 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001219 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001220}
1221
Chris Masond352ac62008-09-29 15:18:18 -04001222/*
1223 * extent_io.c call back to do delayed allocation processing
1224 */
Chris Masonc8b97812008-10-29 14:49:59 -04001225static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001226 u64 start, u64 end, int *page_started,
1227 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001228{
Chris Masonbe20aa92007-12-17 20:14:01 -05001229 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001230 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001231
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001232 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001233 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001234 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001235 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001236 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001237 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001238 else if (!btrfs_test_opt(root, COMPRESS) &&
1239 !(BTRFS_I(inode)->force_compress))
Chris Mason7f366cf2009-03-12 20:12:45 -04001240 ret = cow_file_range(inode, locked_page, start, end,
1241 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001242 else
Chris Mason771ed682008-11-06 22:02:51 -05001243 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001244 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001245 return ret;
1246}
1247
Josef Bacik9ed74f22009-09-11 16:12:44 -04001248static int btrfs_split_extent_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001249 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001250{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001251 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001252 if (!(orig->state & EXTENT_DELALLOC))
1253 return 0;
1254
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001255 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001256 return 0;
1257}
1258
1259/*
1260 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1261 * extents so we can keep track of new extents that are just merged onto old
1262 * extents, such as when we are doing sequential writes, so we can properly
1263 * account for the metadata space we'll need.
1264 */
1265static int btrfs_merge_extent_hook(struct inode *inode,
1266 struct extent_state *new,
1267 struct extent_state *other)
1268{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001269 /* not delalloc, ignore it */
1270 if (!(other->state & EXTENT_DELALLOC))
1271 return 0;
1272
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001273 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001274 return 0;
1275}
1276
Chris Masond352ac62008-09-29 15:18:18 -04001277/*
1278 * extent_io.c set_bit_hook, used to track delayed allocation
1279 * bytes in this file, and to maintain the list of inodes that
1280 * have pending delalloc work to be done.
1281 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001282static int btrfs_set_bit_hook(struct inode *inode,
1283 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001284{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001285
Chris Mason75eff682008-12-15 15:54:40 -05001286 /*
1287 * set_bit and clear bit hooks normally require _irqsave/restore
1288 * but in this case, we are only testeing for the DELALLOC
1289 * bit, which is only set or cleared with irqs on
1290 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001291 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001292 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001293 u64 len = state->end + 1 - state->start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001294 int do_list = (root->root_key.objectid !=
1295 BTRFS_ROOT_TREE_OBJECTID);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001296
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001297 if (*bits & EXTENT_FIRST_DELALLOC)
1298 *bits &= ~EXTENT_FIRST_DELALLOC;
1299 else
1300 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001301
Chris Mason75eff682008-12-15 15:54:40 -05001302 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001303 BTRFS_I(inode)->delalloc_bytes += len;
1304 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001305 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001306 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1307 &root->fs_info->delalloc_inodes);
1308 }
Chris Mason75eff682008-12-15 15:54:40 -05001309 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001310 }
1311 return 0;
1312}
1313
Chris Masond352ac62008-09-29 15:18:18 -04001314/*
1315 * extent_io.c clear_bit_hook, see set_bit_hook for why
1316 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001317static int btrfs_clear_bit_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001318 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001319{
Chris Mason75eff682008-12-15 15:54:40 -05001320 /*
1321 * set_bit and clear bit hooks normally require _irqsave/restore
1322 * but in this case, we are only testeing for the DELALLOC
1323 * bit, which is only set or cleared with irqs on
1324 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001325 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001326 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001327 u64 len = state->end + 1 - state->start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001328 int do_list = (root->root_key.objectid !=
1329 BTRFS_ROOT_TREE_OBJECTID);
Chris Masonbcbfce82008-04-22 13:26:47 -04001330
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001331 if (*bits & EXTENT_FIRST_DELALLOC)
1332 *bits &= ~EXTENT_FIRST_DELALLOC;
1333 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1334 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1335
1336 if (*bits & EXTENT_DO_ACCOUNTING)
1337 btrfs_delalloc_release_metadata(inode, len);
1338
Josef Bacik0cb59c92010-07-02 12:14:14 -04001339 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1340 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001341 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001342
Chris Mason75eff682008-12-15 15:54:40 -05001343 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001344 root->fs_info->delalloc_bytes -= len;
1345 BTRFS_I(inode)->delalloc_bytes -= len;
1346
Josef Bacik0cb59c92010-07-02 12:14:14 -04001347 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001348 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1349 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1350 }
Chris Mason75eff682008-12-15 15:54:40 -05001351 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001352 }
1353 return 0;
1354}
1355
Chris Masond352ac62008-09-29 15:18:18 -04001356/*
1357 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1358 * we don't create bios that span stripes or chunks
1359 */
Chris Mason239b14b2008-03-24 15:02:07 -04001360int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001361 size_t size, struct bio *bio,
1362 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001363{
1364 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1365 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001366 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001367 u64 length = 0;
1368 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001369 int ret;
1370
Chris Mason771ed682008-11-06 22:02:51 -05001371 if (bio_flags & EXTENT_BIO_COMPRESSED)
1372 return 0;
1373
Chris Masonf2d8d742008-04-21 10:03:05 -04001374 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001375 map_tree = &root->fs_info->mapping_tree;
1376 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001377 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001378 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001379
Chris Masond3977122009-01-05 21:25:51 -05001380 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001381 return 1;
Andi Kleen411fc6b2010-10-29 15:14:31 -04001382 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04001383}
1384
Chris Masond352ac62008-09-29 15:18:18 -04001385/*
1386 * in order to insert checksums into the metadata in large chunks,
1387 * we wait until bio submission time. All the pages in the bio are
1388 * checksummed and sums are attached onto the ordered extent record.
1389 *
1390 * At IO completion time the cums attached on the ordered extent record
1391 * are inserted into the btree
1392 */
Chris Masond3977122009-01-05 21:25:51 -05001393static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1394 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001395 unsigned long bio_flags,
1396 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001397{
Chris Mason065631f2008-02-20 12:07:25 -05001398 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001399 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001400
Chris Masond20f7042008-12-08 16:58:54 -05001401 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001402 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001403 return 0;
1404}
Chris Masone0156402008-04-16 11:15:20 -04001405
Chris Mason4a69a412008-11-06 22:03:00 -05001406/*
1407 * in order to insert checksums into the metadata in large chunks,
1408 * we wait until bio submission time. All the pages in the bio are
1409 * checksummed and sums are attached onto the ordered extent record.
1410 *
1411 * At IO completion time the cums attached on the ordered extent record
1412 * are inserted into the btree
1413 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001414static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001415 int mirror_num, unsigned long bio_flags,
1416 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001417{
1418 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001419 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001420}
1421
Chris Masond352ac62008-09-29 15:18:18 -04001422/*
Chris Masoncad321a2008-12-17 14:51:42 -05001423 * extent_io.c submission hook. This does the right thing for csum calculation
1424 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001425 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001426static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001427 int mirror_num, unsigned long bio_flags,
1428 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001429{
1430 struct btrfs_root *root = BTRFS_I(inode)->root;
1431 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001432 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001433
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001434 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001435
Josef Bacik0cb59c92010-07-02 12:14:14 -04001436 if (root == root->fs_info->tree_root)
1437 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1438 else
1439 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001440 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001441
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001442 if (!(rw & REQ_WRITE)) {
Chris Masond20f7042008-12-08 16:58:54 -05001443 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001444 return btrfs_submit_compressed_read(inode, bio,
1445 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001446 } else if (!skip_sum)
1447 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001448 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001449 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001450 /* csum items have already been cloned */
1451 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1452 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001453 /* we're doing a write, do the async checksumming */
1454 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001455 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001456 bio_flags, bio_offset,
1457 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001458 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001459 }
1460
Chris Mason0b86a832008-03-24 15:01:56 -04001461mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001462 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001463}
Chris Mason6885f302008-02-20 16:11:05 -05001464
Chris Masond352ac62008-09-29 15:18:18 -04001465/*
1466 * given a list of ordered sums record them in the inode. This happens
1467 * at IO completion time based on sums calculated at bio submission time.
1468 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001469static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001470 struct inode *inode, u64 file_offset,
1471 struct list_head *list)
1472{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001473 struct btrfs_ordered_sum *sum;
1474
1475 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001476
1477 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001478 btrfs_csum_file_blocks(trans,
1479 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001480 }
1481 return 0;
1482}
1483
Josef Bacik2ac55d42010-02-03 19:33:23 +00001484int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1485 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001486{
Chris Masond3977122009-01-05 21:25:51 -05001487 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001488 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001489 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001490 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001491}
1492
Chris Masond352ac62008-09-29 15:18:18 -04001493/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001494struct btrfs_writepage_fixup {
1495 struct page *page;
1496 struct btrfs_work work;
1497};
1498
Christoph Hellwigb2950862008-12-02 09:54:17 -05001499static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001500{
1501 struct btrfs_writepage_fixup *fixup;
1502 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001503 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001504 struct page *page;
1505 struct inode *inode;
1506 u64 page_start;
1507 u64 page_end;
1508
1509 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1510 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001511again:
Chris Mason247e7432008-07-17 12:53:51 -04001512 lock_page(page);
1513 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1514 ClearPageChecked(page);
1515 goto out_page;
1516 }
1517
1518 inode = page->mapping->host;
1519 page_start = page_offset(page);
1520 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1521
Josef Bacik2ac55d42010-02-03 19:33:23 +00001522 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1523 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001524
1525 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001526 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001527 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001528
1529 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1530 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001531 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1532 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001533 unlock_page(page);
1534 btrfs_start_ordered_extent(inode, ordered, 1);
1535 goto again;
1536 }
Chris Mason247e7432008-07-17 12:53:51 -04001537
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001538 BUG();
Josef Bacik2ac55d42010-02-03 19:33:23 +00001539 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001540 ClearPageChecked(page);
1541out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001542 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1543 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001544out_page:
1545 unlock_page(page);
1546 page_cache_release(page);
1547}
1548
1549/*
1550 * There are a few paths in the higher layers of the kernel that directly
1551 * set the page dirty bit without asking the filesystem if it is a
1552 * good idea. This causes problems because we want to make sure COW
1553 * properly happens and the data=ordered rules are followed.
1554 *
Chris Masonc8b97812008-10-29 14:49:59 -04001555 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001556 * hasn't been properly setup for IO. We kick off an async process
1557 * to fix it up. The async helper will wait for ordered extents, set
1558 * the delalloc bit and make it safe to write the page.
1559 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001560static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001561{
1562 struct inode *inode = page->mapping->host;
1563 struct btrfs_writepage_fixup *fixup;
1564 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001565
Chris Mason8b62b722009-09-02 16:53:46 -04001566 /* this page is properly in the ordered list */
1567 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001568 return 0;
1569
1570 if (PageChecked(page))
1571 return -EAGAIN;
1572
1573 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1574 if (!fixup)
1575 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001576
Chris Mason247e7432008-07-17 12:53:51 -04001577 SetPageChecked(page);
1578 page_cache_get(page);
1579 fixup->work.func = btrfs_writepage_fixup_worker;
1580 fixup->page = page;
1581 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1582 return -EAGAIN;
1583}
1584
Yan Zhengd899e052008-10-30 14:25:28 -04001585static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1586 struct inode *inode, u64 file_pos,
1587 u64 disk_bytenr, u64 disk_num_bytes,
1588 u64 num_bytes, u64 ram_bytes,
1589 u8 compression, u8 encryption,
1590 u16 other_encoding, int extent_type)
1591{
1592 struct btrfs_root *root = BTRFS_I(inode)->root;
1593 struct btrfs_file_extent_item *fi;
1594 struct btrfs_path *path;
1595 struct extent_buffer *leaf;
1596 struct btrfs_key ins;
1597 u64 hint;
1598 int ret;
1599
1600 path = btrfs_alloc_path();
1601 BUG_ON(!path);
1602
Chris Masonb9473432009-03-13 11:00:37 -04001603 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001604
1605 /*
1606 * we may be replacing one extent in the tree with another.
1607 * The new extent is pinned in the extent map, and we don't want
1608 * to drop it from the cache until it is completely in the btree.
1609 *
1610 * So, tell btrfs_drop_extents to leave this extent in the cache.
1611 * the caller is expected to unpin it and allow it to be merged
1612 * with the others.
1613 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001614 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1615 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001616 BUG_ON(ret);
1617
1618 ins.objectid = inode->i_ino;
1619 ins.offset = file_pos;
1620 ins.type = BTRFS_EXTENT_DATA_KEY;
1621 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1622 BUG_ON(ret);
1623 leaf = path->nodes[0];
1624 fi = btrfs_item_ptr(leaf, path->slots[0],
1625 struct btrfs_file_extent_item);
1626 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1627 btrfs_set_file_extent_type(leaf, fi, extent_type);
1628 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1629 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1630 btrfs_set_file_extent_offset(leaf, fi, 0);
1631 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1632 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1633 btrfs_set_file_extent_compression(leaf, fi, compression);
1634 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1635 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001636
1637 btrfs_unlock_up_safe(path, 1);
1638 btrfs_set_lock_blocking(leaf);
1639
Yan Zhengd899e052008-10-30 14:25:28 -04001640 btrfs_mark_buffer_dirty(leaf);
1641
1642 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001643
1644 ins.objectid = disk_bytenr;
1645 ins.offset = disk_num_bytes;
1646 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001647 ret = btrfs_alloc_reserved_file_extent(trans, root,
1648 root->root_key.objectid,
1649 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001650 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001651 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001652
Yan Zhengd899e052008-10-30 14:25:28 -04001653 return 0;
1654}
1655
Chris Mason5d13a982009-03-13 11:41:46 -04001656/*
1657 * helper function for btrfs_finish_ordered_io, this
1658 * just reads in some of the csum leaves to prime them into ram
1659 * before we start the transaction. It limits the amount of btree
1660 * reads required while inside the transaction.
1661 */
Chris Masond352ac62008-09-29 15:18:18 -04001662/* as ordered data IO finishes, this gets called so we can finish
1663 * an ordered extent if the range of bytes in the file it covers are
1664 * fully written.
1665 */
Chris Mason211f90e2008-07-18 11:56:15 -04001666static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001667{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001668 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001669 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001670 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001671 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001672 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04001673 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001674 int ret;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001675 bool nolock = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001676
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001677 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1678 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001679 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001680 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001681 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001682
Josef Bacik0cb59c92010-07-02 12:14:14 -04001683 nolock = (root == root->fs_info->tree_root);
1684
Yan, Zhengc21677542009-11-12 09:34:21 +00001685 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1686 BUG_ON(!list_empty(&ordered_extent->list));
1687 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1688 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001689 if (nolock)
1690 trans = btrfs_join_transaction_nolock(root, 1);
1691 else
1692 trans = btrfs_join_transaction(root, 1);
1693 BUG_ON(!trans);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001694 btrfs_set_trans_block_group(trans, inode);
1695 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00001696 ret = btrfs_update_inode(trans, root, inode);
1697 BUG_ON(ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00001698 }
1699 goto out;
1700 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001701
Josef Bacik2ac55d42010-02-03 19:33:23 +00001702 lock_extent_bits(io_tree, ordered_extent->file_offset,
1703 ordered_extent->file_offset + ordered_extent->len - 1,
1704 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001705
Josef Bacik0cb59c92010-07-02 12:14:14 -04001706 if (nolock)
1707 trans = btrfs_join_transaction_nolock(root, 1);
1708 else
1709 trans = btrfs_join_transaction(root, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001710 btrfs_set_trans_block_group(trans, inode);
1711 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc21677542009-11-12 09:34:21 +00001712
Chris Masonc8b97812008-10-29 14:49:59 -04001713 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001714 compressed = 1;
1715 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1716 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001717 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001718 ordered_extent->file_offset,
1719 ordered_extent->file_offset +
1720 ordered_extent->len);
1721 BUG_ON(ret);
1722 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001723 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001724 ret = insert_reserved_file_extent(trans, inode,
1725 ordered_extent->file_offset,
1726 ordered_extent->start,
1727 ordered_extent->disk_len,
1728 ordered_extent->len,
1729 ordered_extent->len,
1730 compressed, 0, 0,
1731 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001732 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1733 ordered_extent->file_offset,
1734 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001735 BUG_ON(ret);
1736 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001737 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1738 ordered_extent->file_offset +
1739 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1740
Chris Masone6dcd2d2008-07-17 12:53:50 -04001741 add_pending_csums(trans, inode, ordered_extent->file_offset,
1742 &ordered_extent->list);
1743
Yan, Zhengc21677542009-11-12 09:34:21 +00001744 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1745 ret = btrfs_update_inode(trans, root, inode);
1746 BUG_ON(ret);
Yan, Zhengc21677542009-11-12 09:34:21 +00001747out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001748 if (nolock) {
1749 if (trans)
1750 btrfs_end_transaction_nolock(trans, root);
1751 } else {
1752 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1753 if (trans)
1754 btrfs_end_transaction(trans, root);
1755 }
1756
Chris Masone6dcd2d2008-07-17 12:53:50 -04001757 /* once for us */
1758 btrfs_put_ordered_extent(ordered_extent);
1759 /* once for the tree */
1760 btrfs_put_ordered_extent(ordered_extent);
1761
Chris Masone6dcd2d2008-07-17 12:53:50 -04001762 return 0;
1763}
1764
Christoph Hellwigb2950862008-12-02 09:54:17 -05001765static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001766 struct extent_state *state, int uptodate)
1767{
Chris Mason8b62b722009-09-02 16:53:46 -04001768 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001769 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1770}
1771
Chris Masond352ac62008-09-29 15:18:18 -04001772/*
1773 * When IO fails, either with EIO or csum verification fails, we
1774 * try other mirrors that might have a good copy of the data. This
1775 * io_failure_record is used to record state as we go through all the
1776 * mirrors. If another mirror has good data, the page is set up to date
1777 * and things continue. If a good mirror can't be found, the original
1778 * bio end_io callback is called to indicate things have failed.
1779 */
Chris Mason7e383262008-04-09 16:28:12 -04001780struct io_failure_record {
1781 struct page *page;
1782 u64 start;
1783 u64 len;
1784 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001785 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001786 int last_mirror;
1787};
1788
Christoph Hellwigb2950862008-12-02 09:54:17 -05001789static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001790 struct page *page, u64 start, u64 end,
1791 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001792{
1793 struct io_failure_record *failrec = NULL;
1794 u64 private;
1795 struct extent_map *em;
1796 struct inode *inode = page->mapping->host;
1797 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001798 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001799 struct bio *bio;
1800 int num_copies;
1801 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001802 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001803 u64 logical;
1804
1805 ret = get_state_private(failure_tree, start, &private);
1806 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001807 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1808 if (!failrec)
1809 return -ENOMEM;
1810 failrec->start = start;
1811 failrec->len = end - start + 1;
1812 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001813 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001814
Chris Mason890871b2009-09-02 16:24:52 -04001815 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001816 em = lookup_extent_mapping(em_tree, start, failrec->len);
1817 if (em->start > start || em->start + em->len < start) {
1818 free_extent_map(em);
1819 em = NULL;
1820 }
Chris Mason890871b2009-09-02 16:24:52 -04001821 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001822
1823 if (!em || IS_ERR(em)) {
1824 kfree(failrec);
1825 return -EIO;
1826 }
1827 logical = start - em->start;
1828 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001829 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1830 logical = em->block_start;
1831 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1832 }
Chris Mason7e383262008-04-09 16:28:12 -04001833 failrec->logical = logical;
1834 free_extent_map(em);
1835 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1836 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001837 set_state_private(failure_tree, start,
1838 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001839 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001840 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001841 }
1842 num_copies = btrfs_num_copies(
1843 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1844 failrec->logical, failrec->len);
1845 failrec->last_mirror++;
1846 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001847 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001848 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1849 failrec->start,
1850 EXTENT_LOCKED);
1851 if (state && state->start != failrec->start)
1852 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001853 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001854 }
1855 if (!state || failrec->last_mirror > num_copies) {
1856 set_state_private(failure_tree, failrec->start, 0);
1857 clear_extent_bits(failure_tree, failrec->start,
1858 failrec->start + failrec->len - 1,
1859 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1860 kfree(failrec);
1861 return -EIO;
1862 }
1863 bio = bio_alloc(GFP_NOFS, 1);
1864 bio->bi_private = state;
1865 bio->bi_end_io = failed_bio->bi_end_io;
1866 bio->bi_sector = failrec->logical >> 9;
1867 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001868 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001869
Chris Mason7e383262008-04-09 16:28:12 -04001870 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001871 if (failed_bio->bi_rw & REQ_WRITE)
Chris Mason1259ab72008-05-12 13:39:03 -04001872 rw = WRITE;
1873 else
1874 rw = READ;
1875
1876 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001877 failrec->last_mirror,
Chris Masoneaf25d92010-05-25 09:48:28 -04001878 failrec->bio_flags, 0);
Chris Mason1259ab72008-05-12 13:39:03 -04001879 return 0;
1880}
1881
Chris Masond352ac62008-09-29 15:18:18 -04001882/*
1883 * each time an IO finishes, we do a fast check in the IO failure tree
1884 * to see if we need to process or clean up an io_failure_record
1885 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001886static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001887{
1888 u64 private;
1889 u64 private_failure;
1890 struct io_failure_record *failure;
1891 int ret;
1892
1893 private = 0;
1894 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1895 (u64)-1, 1, EXTENT_DIRTY)) {
1896 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1897 start, &private_failure);
1898 if (ret == 0) {
1899 failure = (struct io_failure_record *)(unsigned long)
1900 private_failure;
1901 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1902 failure->start, 0);
1903 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1904 failure->start,
1905 failure->start + failure->len - 1,
1906 EXTENT_DIRTY | EXTENT_LOCKED,
1907 GFP_NOFS);
1908 kfree(failure);
1909 }
1910 }
Chris Mason7e383262008-04-09 16:28:12 -04001911 return 0;
1912}
1913
Chris Masond352ac62008-09-29 15:18:18 -04001914/*
1915 * when reads are done, we need to check csums to verify the data is correct
1916 * if there's a match, we allow the bio to finish. If not, we go through
1917 * the io_failure_record routines to find good copies
1918 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001919static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001920 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001921{
Chris Mason35ebb932007-10-30 16:56:53 -04001922 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001923 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001924 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001925 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001926 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001927 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001928 struct btrfs_root *root = BTRFS_I(inode)->root;
1929 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001930
Chris Masond20f7042008-12-08 16:58:54 -05001931 if (PageChecked(page)) {
1932 ClearPageChecked(page);
1933 goto good;
1934 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001935
1936 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001937 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001938
Yan Zheng17d217f2008-12-12 10:03:38 -05001939 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001940 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001941 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1942 GFP_NOFS);
1943 return 0;
1944 }
1945
Yanc2e639f2008-02-04 08:57:25 -05001946 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001947 private = state->private;
1948 ret = 0;
1949 } else {
1950 ret = get_state_private(io_tree, start, &private);
1951 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001952 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001953 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001954 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001955
Chris Masonff79f812007-10-15 16:22:25 -04001956 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1957 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001958 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001959 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001960
Chris Mason9ab86c82009-01-07 09:48:51 -05001961 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001962good:
Chris Mason7e383262008-04-09 16:28:12 -04001963 /* if the io failure tree for this inode is non-empty,
1964 * check to see if we've recovered from a failed IO
1965 */
Chris Mason1259ab72008-05-12 13:39:03 -04001966 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001967 return 0;
1968
1969zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001970 if (printk_ratelimit()) {
1971 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1972 "private %llu\n", page->mapping->host->i_ino,
1973 (unsigned long long)start, csum,
1974 (unsigned long long)private);
1975 }
Chris Masondb945352007-10-15 16:15:53 -04001976 memset(kaddr + offset, 1, end - start + 1);
1977 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001978 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001979 if (private == 0)
1980 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001981 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001982}
Chris Masonb888db22007-08-27 16:49:44 -04001983
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001984struct delayed_iput {
1985 struct list_head list;
1986 struct inode *inode;
1987};
1988
1989void btrfs_add_delayed_iput(struct inode *inode)
1990{
1991 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1992 struct delayed_iput *delayed;
1993
1994 if (atomic_add_unless(&inode->i_count, -1, 1))
1995 return;
1996
1997 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
1998 delayed->inode = inode;
1999
2000 spin_lock(&fs_info->delayed_iput_lock);
2001 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2002 spin_unlock(&fs_info->delayed_iput_lock);
2003}
2004
2005void btrfs_run_delayed_iputs(struct btrfs_root *root)
2006{
2007 LIST_HEAD(list);
2008 struct btrfs_fs_info *fs_info = root->fs_info;
2009 struct delayed_iput *delayed;
2010 int empty;
2011
2012 spin_lock(&fs_info->delayed_iput_lock);
2013 empty = list_empty(&fs_info->delayed_iputs);
2014 spin_unlock(&fs_info->delayed_iput_lock);
2015 if (empty)
2016 return;
2017
2018 down_read(&root->fs_info->cleanup_work_sem);
2019 spin_lock(&fs_info->delayed_iput_lock);
2020 list_splice_init(&fs_info->delayed_iputs, &list);
2021 spin_unlock(&fs_info->delayed_iput_lock);
2022
2023 while (!list_empty(&list)) {
2024 delayed = list_entry(list.next, struct delayed_iput, list);
2025 list_del(&delayed->list);
2026 iput(delayed->inode);
2027 kfree(delayed);
2028 }
2029 up_read(&root->fs_info->cleanup_work_sem);
2030}
2031
Josef Bacik7b128762008-07-24 12:17:14 -04002032/*
Yan, Zhengd68fc572010-05-16 10:49:58 -04002033 * calculate extra metadata reservation when snapshotting a subvolume
2034 * contains orphan files.
2035 */
2036void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2037 struct btrfs_pending_snapshot *pending,
2038 u64 *bytes_to_reserve)
2039{
2040 struct btrfs_root *root;
2041 struct btrfs_block_rsv *block_rsv;
2042 u64 num_bytes;
2043 int index;
2044
2045 root = pending->root;
2046 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2047 return;
2048
2049 block_rsv = root->orphan_block_rsv;
2050
2051 /* orphan block reservation for the snapshot */
2052 num_bytes = block_rsv->size;
2053
2054 /*
2055 * after the snapshot is created, COWing tree blocks may use more
2056 * space than it frees. So we should make sure there is enough
2057 * reserved space.
2058 */
2059 index = trans->transid & 0x1;
2060 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2061 num_bytes += block_rsv->size -
2062 (block_rsv->reserved + block_rsv->freed[index]);
2063 }
2064
2065 *bytes_to_reserve += num_bytes;
2066}
2067
2068void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2069 struct btrfs_pending_snapshot *pending)
2070{
2071 struct btrfs_root *root = pending->root;
2072 struct btrfs_root *snap = pending->snap;
2073 struct btrfs_block_rsv *block_rsv;
2074 u64 num_bytes;
2075 int index;
2076 int ret;
2077
2078 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2079 return;
2080
2081 /* refill source subvolume's orphan block reservation */
2082 block_rsv = root->orphan_block_rsv;
2083 index = trans->transid & 0x1;
2084 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2085 num_bytes = block_rsv->size -
2086 (block_rsv->reserved + block_rsv->freed[index]);
2087 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2088 root->orphan_block_rsv,
2089 num_bytes);
2090 BUG_ON(ret);
2091 }
2092
2093 /* setup orphan block reservation for the snapshot */
2094 block_rsv = btrfs_alloc_block_rsv(snap);
2095 BUG_ON(!block_rsv);
2096
2097 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2098 snap->orphan_block_rsv = block_rsv;
2099
2100 num_bytes = root->orphan_block_rsv->size;
2101 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2102 block_rsv, num_bytes);
2103 BUG_ON(ret);
2104
2105#if 0
2106 /* insert orphan item for the snapshot */
2107 WARN_ON(!root->orphan_item_inserted);
2108 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2109 snap->root_key.objectid);
2110 BUG_ON(ret);
2111 snap->orphan_item_inserted = 1;
2112#endif
2113}
2114
2115enum btrfs_orphan_cleanup_state {
2116 ORPHAN_CLEANUP_STARTED = 1,
2117 ORPHAN_CLEANUP_DONE = 2,
2118};
2119
2120/*
2121 * This is called in transaction commmit time. If there are no orphan
2122 * files in the subvolume, it removes orphan item and frees block_rsv
2123 * structure.
2124 */
2125void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2126 struct btrfs_root *root)
2127{
2128 int ret;
2129
2130 if (!list_empty(&root->orphan_list) ||
2131 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2132 return;
2133
2134 if (root->orphan_item_inserted &&
2135 btrfs_root_refs(&root->root_item) > 0) {
2136 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2137 root->root_key.objectid);
2138 BUG_ON(ret);
2139 root->orphan_item_inserted = 0;
2140 }
2141
2142 if (root->orphan_block_rsv) {
2143 WARN_ON(root->orphan_block_rsv->size > 0);
2144 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2145 root->orphan_block_rsv = NULL;
2146 }
2147}
2148
2149/*
Josef Bacik7b128762008-07-24 12:17:14 -04002150 * This creates an orphan entry for the given inode in case something goes
2151 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002152 *
2153 * NOTE: caller of this function should reserve 5 units of metadata for
2154 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002155 */
2156int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2157{
2158 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002159 struct btrfs_block_rsv *block_rsv = NULL;
2160 int reserve = 0;
2161 int insert = 0;
2162 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002163
Yan, Zhengd68fc572010-05-16 10:49:58 -04002164 if (!root->orphan_block_rsv) {
2165 block_rsv = btrfs_alloc_block_rsv(root);
2166 BUG_ON(!block_rsv);
Josef Bacik7b128762008-07-24 12:17:14 -04002167 }
2168
Yan, Zhengd68fc572010-05-16 10:49:58 -04002169 spin_lock(&root->orphan_lock);
2170 if (!root->orphan_block_rsv) {
2171 root->orphan_block_rsv = block_rsv;
2172 } else if (block_rsv) {
2173 btrfs_free_block_rsv(root, block_rsv);
2174 block_rsv = NULL;
2175 }
Josef Bacik7b128762008-07-24 12:17:14 -04002176
Yan, Zhengd68fc572010-05-16 10:49:58 -04002177 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2178 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2179#if 0
2180 /*
2181 * For proper ENOSPC handling, we should do orphan
2182 * cleanup when mounting. But this introduces backward
2183 * compatibility issue.
2184 */
2185 if (!xchg(&root->orphan_item_inserted, 1))
2186 insert = 2;
2187 else
2188 insert = 1;
2189#endif
2190 insert = 1;
2191 } else {
2192 WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
2193 }
Josef Bacik7b128762008-07-24 12:17:14 -04002194
Yan, Zhengd68fc572010-05-16 10:49:58 -04002195 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2196 BTRFS_I(inode)->orphan_meta_reserved = 1;
2197 reserve = 1;
2198 }
2199 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002200
Yan, Zhengd68fc572010-05-16 10:49:58 -04002201 if (block_rsv)
2202 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2203
2204 /* grab metadata reservation from transaction handle */
2205 if (reserve) {
2206 ret = btrfs_orphan_reserve_metadata(trans, inode);
2207 BUG_ON(ret);
2208 }
2209
2210 /* insert an orphan item to track this unlinked/truncated file */
2211 if (insert >= 1) {
2212 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2213 BUG_ON(ret);
2214 }
2215
2216 /* insert an orphan item to track subvolume contains orphan files */
2217 if (insert >= 2) {
2218 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2219 root->root_key.objectid);
2220 BUG_ON(ret);
2221 }
2222 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002223}
2224
2225/*
2226 * We have done the truncate/delete so we can go ahead and remove the orphan
2227 * item for this particular inode.
2228 */
2229int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2230{
2231 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002232 int delete_item = 0;
2233 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002234 int ret = 0;
2235
Yan, Zhengd68fc572010-05-16 10:49:58 -04002236 spin_lock(&root->orphan_lock);
2237 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2238 list_del_init(&BTRFS_I(inode)->i_orphan);
2239 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002240 }
2241
Yan, Zhengd68fc572010-05-16 10:49:58 -04002242 if (BTRFS_I(inode)->orphan_meta_reserved) {
2243 BTRFS_I(inode)->orphan_meta_reserved = 0;
2244 release_rsv = 1;
2245 }
2246 spin_unlock(&root->orphan_lock);
2247
2248 if (trans && delete_item) {
2249 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2250 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002251 }
2252
Yan, Zhengd68fc572010-05-16 10:49:58 -04002253 if (release_rsv)
2254 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002255
Yan, Zhengd68fc572010-05-16 10:49:58 -04002256 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002257}
2258
2259/*
2260 * this cleans up any orphans that may be left on the list from the last use
2261 * of this root.
2262 */
2263void btrfs_orphan_cleanup(struct btrfs_root *root)
2264{
2265 struct btrfs_path *path;
2266 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002267 struct btrfs_key key, found_key;
2268 struct btrfs_trans_handle *trans;
2269 struct inode *inode;
2270 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2271
Yan, Zhengd68fc572010-05-16 10:49:58 -04002272 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik7b128762008-07-24 12:17:14 -04002273 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002274
2275 path = btrfs_alloc_path();
2276 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002277 path->reada = -1;
2278
2279 key.objectid = BTRFS_ORPHAN_OBJECTID;
2280 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2281 key.offset = (u64)-1;
2282
Josef Bacik7b128762008-07-24 12:17:14 -04002283 while (1) {
2284 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2285 if (ret < 0) {
2286 printk(KERN_ERR "Error searching slot for orphan: %d"
2287 "\n", ret);
2288 break;
2289 }
2290
2291 /*
2292 * if ret == 0 means we found what we were searching for, which
2293 * is weird, but possible, so only screw with path if we didnt
2294 * find the key and see if we have stuff that matches
2295 */
2296 if (ret > 0) {
2297 if (path->slots[0] == 0)
2298 break;
2299 path->slots[0]--;
2300 }
2301
2302 /* pull out the item */
2303 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002304 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2305
2306 /* make sure the item matches what we want */
2307 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2308 break;
2309 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2310 break;
2311
2312 /* release the path since we're done with it */
2313 btrfs_release_path(root, path);
2314
2315 /*
2316 * this is where we are basically btrfs_lookup, without the
2317 * crossing root thing. we store the inode number in the
2318 * offset of the orphan item.
2319 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002320 found_key.objectid = found_key.offset;
2321 found_key.type = BTRFS_INODE_ITEM_KEY;
2322 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002323 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002324 BUG_ON(IS_ERR(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002325
Josef Bacik7b128762008-07-24 12:17:14 -04002326 /*
2327 * add this inode to the orphan list so btrfs_orphan_del does
2328 * the proper thing when we hit it
2329 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002330 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002331 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002332 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002333
2334 /*
2335 * if this is a bad inode, means we actually succeeded in
2336 * removing the inode, but not the orphan record, which means
2337 * we need to manually delete the orphan since iput will just
2338 * do a destroy_inode
2339 */
2340 if (is_bad_inode(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002341 trans = btrfs_start_transaction(root, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002342 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002343 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002344 iput(inode);
2345 continue;
2346 }
2347
2348 /* if we have links, this was a truncate, lets do that */
2349 if (inode->i_nlink) {
2350 nr_truncate++;
2351 btrfs_truncate(inode);
2352 } else {
2353 nr_unlink++;
2354 }
2355
2356 /* this will do delete_inode and everything for us */
2357 iput(inode);
2358 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002359 btrfs_free_path(path);
2360
2361 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2362
2363 if (root->orphan_block_rsv)
2364 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2365 (u64)-1);
2366
2367 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2368 trans = btrfs_join_transaction(root, 1);
2369 btrfs_end_transaction(trans, root);
2370 }
Josef Bacik7b128762008-07-24 12:17:14 -04002371
2372 if (nr_unlink)
2373 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2374 if (nr_truncate)
2375 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik7b128762008-07-24 12:17:14 -04002376}
2377
Chris Masond352ac62008-09-29 15:18:18 -04002378/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002379 * very simple check to peek ahead in the leaf looking for xattrs. If we
2380 * don't find any xattrs, we know there can't be any acls.
2381 *
2382 * slot is the slot the inode is in, objectid is the objectid of the inode
2383 */
2384static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2385 int slot, u64 objectid)
2386{
2387 u32 nritems = btrfs_header_nritems(leaf);
2388 struct btrfs_key found_key;
2389 int scanned = 0;
2390
2391 slot++;
2392 while (slot < nritems) {
2393 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2394
2395 /* we found a different objectid, there must not be acls */
2396 if (found_key.objectid != objectid)
2397 return 0;
2398
2399 /* we found an xattr, assume we've got an acl */
2400 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2401 return 1;
2402
2403 /*
2404 * we found a key greater than an xattr key, there can't
2405 * be any acls later on
2406 */
2407 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2408 return 0;
2409
2410 slot++;
2411 scanned++;
2412
2413 /*
2414 * it goes inode, inode backrefs, xattrs, extents,
2415 * so if there are a ton of hard links to an inode there can
2416 * be a lot of backrefs. Don't waste time searching too hard,
2417 * this is just an optimization
2418 */
2419 if (scanned >= 8)
2420 break;
2421 }
2422 /* we hit the end of the leaf before we found an xattr or
2423 * something larger than an xattr. We have to assume the inode
2424 * has acls
2425 */
2426 return 1;
2427}
2428
2429/*
Chris Masond352ac62008-09-29 15:18:18 -04002430 * read an inode from the btree into the in-memory inode
2431 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002432static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002433{
2434 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002435 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002436 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002437 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002438 struct btrfs_root *root = BTRFS_I(inode)->root;
2439 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002440 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002441 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002442 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002443 int ret;
2444
2445 path = btrfs_alloc_path();
2446 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002447 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002448
Chris Mason39279cc2007-06-12 06:35:45 -04002449 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002450 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002451 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002452
Chris Mason5f39d392007-10-15 16:14:19 -04002453 leaf = path->nodes[0];
2454 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2455 struct btrfs_inode_item);
2456
2457 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2458 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2459 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2460 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002461 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002462
2463 tspec = btrfs_inode_atime(inode_item);
2464 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2465 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2466
2467 tspec = btrfs_inode_mtime(inode_item);
2468 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2469 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2470
2471 tspec = btrfs_inode_ctime(inode_item);
2472 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2473 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2474
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002475 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002476 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002477 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002478 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002479 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002480 rdev = btrfs_inode_rdev(leaf, inode_item);
2481
Josef Bacikaec74772008-07-24 12:12:38 -04002482 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002483 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002484
Chris Mason5f39d392007-10-15 16:14:19 -04002485 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002486
Chris Mason46a53cc2009-04-27 11:47:50 -04002487 /*
2488 * try to precache a NULL acl entry for files that don't have
2489 * any xattrs or acls
2490 */
2491 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002492 if (!maybe_acls)
2493 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002494
Yan Zhengd2fb3432008-12-11 16:30:39 -05002495 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2496 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002497 btrfs_free_path(path);
2498 inode_item = NULL;
2499
Chris Mason39279cc2007-06-12 06:35:45 -04002500 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002501 case S_IFREG:
2502 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002503 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002504 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002505 inode->i_fop = &btrfs_file_operations;
2506 inode->i_op = &btrfs_file_inode_operations;
2507 break;
2508 case S_IFDIR:
2509 inode->i_fop = &btrfs_dir_file_operations;
2510 if (root == root->fs_info->tree_root)
2511 inode->i_op = &btrfs_dir_ro_inode_operations;
2512 else
2513 inode->i_op = &btrfs_dir_inode_operations;
2514 break;
2515 case S_IFLNK:
2516 inode->i_op = &btrfs_symlink_inode_operations;
2517 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002518 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002519 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002520 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002521 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002522 init_special_inode(inode, inode->i_mode, rdev);
2523 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002524 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002525
2526 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002527 return;
2528
2529make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002530 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002531 make_bad_inode(inode);
2532}
2533
Chris Masond352ac62008-09-29 15:18:18 -04002534/*
2535 * given a leaf and an inode, copy the inode fields into the leaf
2536 */
Chris Masone02119d2008-09-05 16:13:11 -04002537static void fill_inode_item(struct btrfs_trans_handle *trans,
2538 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002539 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002540 struct inode *inode)
2541{
Chris Mason5f39d392007-10-15 16:14:19 -04002542 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2543 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002544 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002545 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2546 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2547
2548 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2549 inode->i_atime.tv_sec);
2550 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2551 inode->i_atime.tv_nsec);
2552
2553 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2554 inode->i_mtime.tv_sec);
2555 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2556 inode->i_mtime.tv_nsec);
2557
2558 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2559 inode->i_ctime.tv_sec);
2560 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2561 inode->i_ctime.tv_nsec);
2562
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002563 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002564 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002565 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002566 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002567 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002568 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002569 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002570}
2571
Chris Masond352ac62008-09-29 15:18:18 -04002572/*
2573 * copy everything in the in-memory inode into the btree.
2574 */
Chris Masond3977122009-01-05 21:25:51 -05002575noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2576 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002577{
2578 struct btrfs_inode_item *inode_item;
2579 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002580 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002581 int ret;
2582
2583 path = btrfs_alloc_path();
2584 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002585 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002586 ret = btrfs_lookup_inode(trans, root, path,
2587 &BTRFS_I(inode)->location, 1);
2588 if (ret) {
2589 if (ret > 0)
2590 ret = -ENOENT;
2591 goto failed;
2592 }
2593
Chris Masonb4ce94d2009-02-04 09:25:08 -05002594 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002595 leaf = path->nodes[0];
2596 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002597 struct btrfs_inode_item);
2598
Chris Masone02119d2008-09-05 16:13:11 -04002599 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002600 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002601 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002602 ret = 0;
2603failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002604 btrfs_free_path(path);
2605 return ret;
2606}
2607
2608
Chris Masond352ac62008-09-29 15:18:18 -04002609/*
2610 * unlink helper that gets used here in inode.c and in the tree logging
2611 * recovery code. It remove a link in a directory with a given name, and
2612 * also drops the back refs in the inode to the directory
2613 */
Chris Masone02119d2008-09-05 16:13:11 -04002614int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2615 struct btrfs_root *root,
2616 struct inode *dir, struct inode *inode,
2617 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002618{
2619 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002620 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002621 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002622 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002623 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002624 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002625
2626 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002627 if (!path) {
2628 ret = -ENOMEM;
2629 goto err;
2630 }
2631
Chris Masonb9473432009-03-13 11:00:37 -04002632 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002633 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2634 name, name_len, -1);
2635 if (IS_ERR(di)) {
2636 ret = PTR_ERR(di);
2637 goto err;
2638 }
2639 if (!di) {
2640 ret = -ENOENT;
2641 goto err;
2642 }
Chris Mason5f39d392007-10-15 16:14:19 -04002643 leaf = path->nodes[0];
2644 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002645 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002646 if (ret)
2647 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002648 btrfs_release_path(root, path);
2649
Josef Bacikaec74772008-07-24 12:12:38 -04002650 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002651 inode->i_ino,
2652 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002653 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002654 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002655 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002656 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002657 goto err;
2658 }
2659
Chris Mason39279cc2007-06-12 06:35:45 -04002660 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002661 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002662 if (IS_ERR(di)) {
2663 ret = PTR_ERR(di);
2664 goto err;
2665 }
2666 if (!di) {
2667 ret = -ENOENT;
2668 goto err;
2669 }
2670 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002671 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002672
Chris Masone02119d2008-09-05 16:13:11 -04002673 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2674 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002675 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002676
2677 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2678 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002679 if (ret == -ENOENT)
2680 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002681err:
2682 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002683 if (ret)
2684 goto out;
2685
2686 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2687 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2688 btrfs_update_inode(trans, root, dir);
2689 btrfs_drop_nlink(inode);
2690 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002691out:
Chris Mason39279cc2007-06-12 06:35:45 -04002692 return ret;
2693}
2694
Yan, Zhenga22285a2010-05-16 10:48:46 -04002695/* helper to check if there is any shared block in the path */
2696static int check_path_shared(struct btrfs_root *root,
2697 struct btrfs_path *path)
2698{
2699 struct extent_buffer *eb;
2700 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00002701 u64 refs = 1;
Andi Kleen411fc6b2010-10-29 15:14:31 -04002702 int uninitialized_var(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002703
2704 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2705 if (!path->nodes[level])
2706 break;
2707 eb = path->nodes[level];
2708 if (!btrfs_block_can_be_shared(root, eb))
2709 continue;
2710 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2711 &refs, NULL);
2712 if (refs > 1)
2713 return 1;
2714 }
Andi Kleen411fc6b2010-10-29 15:14:31 -04002715 return ret; /* XXX callers? */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002716}
2717
2718/*
2719 * helper to start transaction for unlink and rmdir.
2720 *
2721 * unlink and rmdir are special in btrfs, they do not always free space.
2722 * so in enospc case, we should make sure they will free space before
2723 * allowing them to use the global metadata reservation.
2724 */
2725static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2726 struct dentry *dentry)
2727{
2728 struct btrfs_trans_handle *trans;
2729 struct btrfs_root *root = BTRFS_I(dir)->root;
2730 struct btrfs_path *path;
2731 struct btrfs_inode_ref *ref;
2732 struct btrfs_dir_item *di;
2733 struct inode *inode = dentry->d_inode;
2734 u64 index;
2735 int check_link = 1;
2736 int err = -ENOSPC;
2737 int ret;
2738
2739 trans = btrfs_start_transaction(root, 10);
2740 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2741 return trans;
2742
2743 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2744 return ERR_PTR(-ENOSPC);
2745
2746 /* check if there is someone else holds reference */
2747 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2748 return ERR_PTR(-ENOSPC);
2749
2750 if (atomic_read(&inode->i_count) > 2)
2751 return ERR_PTR(-ENOSPC);
2752
2753 if (xchg(&root->fs_info->enospc_unlink, 1))
2754 return ERR_PTR(-ENOSPC);
2755
2756 path = btrfs_alloc_path();
2757 if (!path) {
2758 root->fs_info->enospc_unlink = 0;
2759 return ERR_PTR(-ENOMEM);
2760 }
2761
2762 trans = btrfs_start_transaction(root, 0);
2763 if (IS_ERR(trans)) {
2764 btrfs_free_path(path);
2765 root->fs_info->enospc_unlink = 0;
2766 return trans;
2767 }
2768
2769 path->skip_locking = 1;
2770 path->search_commit_root = 1;
2771
2772 ret = btrfs_lookup_inode(trans, root, path,
2773 &BTRFS_I(dir)->location, 0);
2774 if (ret < 0) {
2775 err = ret;
2776 goto out;
2777 }
2778 if (ret == 0) {
2779 if (check_path_shared(root, path))
2780 goto out;
2781 } else {
2782 check_link = 0;
2783 }
2784 btrfs_release_path(root, path);
2785
2786 ret = btrfs_lookup_inode(trans, root, path,
2787 &BTRFS_I(inode)->location, 0);
2788 if (ret < 0) {
2789 err = ret;
2790 goto out;
2791 }
2792 if (ret == 0) {
2793 if (check_path_shared(root, path))
2794 goto out;
2795 } else {
2796 check_link = 0;
2797 }
2798 btrfs_release_path(root, path);
2799
2800 if (ret == 0 && S_ISREG(inode->i_mode)) {
2801 ret = btrfs_lookup_file_extent(trans, root, path,
2802 inode->i_ino, (u64)-1, 0);
2803 if (ret < 0) {
2804 err = ret;
2805 goto out;
2806 }
2807 BUG_ON(ret == 0);
2808 if (check_path_shared(root, path))
2809 goto out;
2810 btrfs_release_path(root, path);
2811 }
2812
2813 if (!check_link) {
2814 err = 0;
2815 goto out;
2816 }
2817
2818 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2819 dentry->d_name.name, dentry->d_name.len, 0);
2820 if (IS_ERR(di)) {
2821 err = PTR_ERR(di);
2822 goto out;
2823 }
2824 if (di) {
2825 if (check_path_shared(root, path))
2826 goto out;
2827 } else {
2828 err = 0;
2829 goto out;
2830 }
2831 btrfs_release_path(root, path);
2832
2833 ref = btrfs_lookup_inode_ref(trans, root, path,
2834 dentry->d_name.name, dentry->d_name.len,
2835 inode->i_ino, dir->i_ino, 0);
2836 if (IS_ERR(ref)) {
2837 err = PTR_ERR(ref);
2838 goto out;
2839 }
2840 BUG_ON(!ref);
2841 if (check_path_shared(root, path))
2842 goto out;
2843 index = btrfs_inode_ref_index(path->nodes[0], ref);
2844 btrfs_release_path(root, path);
2845
2846 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2847 dentry->d_name.name, dentry->d_name.len, 0);
2848 if (IS_ERR(di)) {
2849 err = PTR_ERR(di);
2850 goto out;
2851 }
2852 BUG_ON(ret == -ENOENT);
2853 if (check_path_shared(root, path))
2854 goto out;
2855
2856 err = 0;
2857out:
2858 btrfs_free_path(path);
2859 if (err) {
2860 btrfs_end_transaction(trans, root);
2861 root->fs_info->enospc_unlink = 0;
2862 return ERR_PTR(err);
2863 }
2864
2865 trans->block_rsv = &root->fs_info->global_block_rsv;
2866 return trans;
2867}
2868
2869static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2870 struct btrfs_root *root)
2871{
2872 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2873 BUG_ON(!root->fs_info->enospc_unlink);
2874 root->fs_info->enospc_unlink = 0;
2875 }
2876 btrfs_end_transaction_throttle(trans, root);
2877}
2878
Chris Mason39279cc2007-06-12 06:35:45 -04002879static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2880{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002881 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002882 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002883 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002884 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002885 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002886
Yan, Zhenga22285a2010-05-16 10:48:46 -04002887 trans = __unlink_start_trans(dir, dentry);
2888 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002889 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002890
Chris Mason39279cc2007-06-12 06:35:45 -04002891 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002892
2893 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2894
Chris Masone02119d2008-09-05 16:13:11 -04002895 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2896 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002897 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002898
Yan, Zhenga22285a2010-05-16 10:48:46 -04002899 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002900 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002901 BUG_ON(ret);
2902 }
Josef Bacik7b128762008-07-24 12:17:14 -04002903
Chris Masond3c2fdc2007-09-17 10:58:06 -04002904 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002905 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002906 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002907 return ret;
2908}
2909
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002910int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2911 struct btrfs_root *root,
2912 struct inode *dir, u64 objectid,
2913 const char *name, int name_len)
2914{
2915 struct btrfs_path *path;
2916 struct extent_buffer *leaf;
2917 struct btrfs_dir_item *di;
2918 struct btrfs_key key;
2919 u64 index;
2920 int ret;
2921
2922 path = btrfs_alloc_path();
2923 if (!path)
2924 return -ENOMEM;
2925
2926 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2927 name, name_len, -1);
2928 BUG_ON(!di || IS_ERR(di));
2929
2930 leaf = path->nodes[0];
2931 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2932 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2933 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2934 BUG_ON(ret);
2935 btrfs_release_path(root, path);
2936
2937 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2938 objectid, root->root_key.objectid,
2939 dir->i_ino, &index, name, name_len);
2940 if (ret < 0) {
2941 BUG_ON(ret != -ENOENT);
2942 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2943 name, name_len);
2944 BUG_ON(!di || IS_ERR(di));
2945
2946 leaf = path->nodes[0];
2947 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2948 btrfs_release_path(root, path);
2949 index = key.offset;
2950 }
2951
2952 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2953 index, name, name_len, -1);
2954 BUG_ON(!di || IS_ERR(di));
2955
2956 leaf = path->nodes[0];
2957 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2958 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2959 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2960 BUG_ON(ret);
2961 btrfs_release_path(root, path);
2962
2963 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2964 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2965 ret = btrfs_update_inode(trans, root, dir);
2966 BUG_ON(ret);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002967
2968 btrfs_free_path(path);
2969 return 0;
2970}
2971
Chris Mason39279cc2007-06-12 06:35:45 -04002972static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2973{
2974 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002975 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002976 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002977 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002978 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002979
Chris Mason3394e162008-11-17 20:42:26 -05002980 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002981 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002982 return -ENOTEMPTY;
2983
Yan, Zhenga22285a2010-05-16 10:48:46 -04002984 trans = __unlink_start_trans(dir, dentry);
2985 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002986 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002987
Chris Mason39279cc2007-06-12 06:35:45 -04002988 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002989
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002990 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2991 err = btrfs_unlink_subvol(trans, root, dir,
2992 BTRFS_I(inode)->location.objectid,
2993 dentry->d_name.name,
2994 dentry->d_name.len);
2995 goto out;
2996 }
2997
Josef Bacik7b128762008-07-24 12:17:14 -04002998 err = btrfs_orphan_add(trans, inode);
2999 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003000 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003001
Chris Mason39279cc2007-06-12 06:35:45 -04003002 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003003 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3004 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003005 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003006 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003007out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003008 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003009 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003010 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003011
Chris Mason39279cc2007-06-12 06:35:45 -04003012 return err;
3013}
3014
Chris Masond20f7042008-12-08 16:58:54 -05003015#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04003016/*
Chris Mason323ac952008-10-01 19:05:46 -04003017 * when truncating bytes in a file, it is possible to avoid reading
3018 * the leaves that contain only checksum items. This can be the
3019 * majority of the IO required to delete a large file, but it must
3020 * be done carefully.
3021 *
3022 * The keys in the level just above the leaves are checked to make sure
3023 * the lowest key in a given leaf is a csum key, and starts at an offset
3024 * after the new size.
3025 *
3026 * Then the key for the next leaf is checked to make sure it also has
3027 * a checksum item for the same file. If it does, we know our target leaf
3028 * contains only checksum items, and it can be safely freed without reading
3029 * it.
3030 *
3031 * This is just an optimization targeted at large files. It may do
3032 * nothing. It will return 0 unless things went badly.
3033 */
3034static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3035 struct btrfs_root *root,
3036 struct btrfs_path *path,
3037 struct inode *inode, u64 new_size)
3038{
3039 struct btrfs_key key;
3040 int ret;
3041 int nritems;
3042 struct btrfs_key found_key;
3043 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003044 struct btrfs_leaf_ref *ref;
3045 u64 leaf_gen;
3046 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04003047
3048 path->lowest_level = 1;
3049 key.objectid = inode->i_ino;
3050 key.type = BTRFS_CSUM_ITEM_KEY;
3051 key.offset = new_size;
3052again:
3053 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3054 if (ret < 0)
3055 goto out;
3056
3057 if (path->nodes[1] == NULL) {
3058 ret = 0;
3059 goto out;
3060 }
3061 ret = 0;
3062 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3063 nritems = btrfs_header_nritems(path->nodes[1]);
3064
3065 if (!nritems)
3066 goto out;
3067
3068 if (path->slots[1] >= nritems)
3069 goto next_node;
3070
3071 /* did we find a key greater than anything we want to delete? */
3072 if (found_key.objectid > inode->i_ino ||
3073 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3074 goto out;
3075
3076 /* we check the next key in the node to make sure the leave contains
3077 * only checksum items. This comparison doesn't work if our
3078 * leaf is the last one in the node
3079 */
3080 if (path->slots[1] + 1 >= nritems) {
3081next_node:
3082 /* search forward from the last key in the node, this
3083 * will bring us into the next node in the tree
3084 */
3085 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3086
3087 /* unlikely, but we inc below, so check to be safe */
3088 if (found_key.offset == (u64)-1)
3089 goto out;
3090
3091 /* search_forward needs a path with locks held, do the
3092 * search again for the original key. It is possible
3093 * this will race with a balance and return a path that
3094 * we could modify, but this drop is just an optimization
3095 * and is allowed to miss some leaves.
3096 */
3097 btrfs_release_path(root, path);
3098 found_key.offset++;
3099
3100 /* setup a max key for search_forward */
3101 other_key.offset = (u64)-1;
3102 other_key.type = key.type;
3103 other_key.objectid = key.objectid;
3104
3105 path->keep_locks = 1;
3106 ret = btrfs_search_forward(root, &found_key, &other_key,
3107 path, 0, 0);
3108 path->keep_locks = 0;
3109 if (ret || found_key.objectid != key.objectid ||
3110 found_key.type != key.type) {
3111 ret = 0;
3112 goto out;
3113 }
3114
3115 key.offset = found_key.offset;
3116 btrfs_release_path(root, path);
3117 cond_resched();
3118 goto again;
3119 }
3120
3121 /* we know there's one more slot after us in the tree,
3122 * read that key so we can verify it is also a checksum item
3123 */
3124 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3125
3126 if (found_key.objectid < inode->i_ino)
3127 goto next_key;
3128
3129 if (found_key.type != key.type || found_key.offset < new_size)
3130 goto next_key;
3131
3132 /*
3133 * if the key for the next leaf isn't a csum key from this objectid,
3134 * we can't be sure there aren't good items inside this leaf.
3135 * Bail out
3136 */
3137 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3138 goto out;
3139
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003140 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3141 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003142 /*
3143 * it is safe to delete this leaf, it contains only
3144 * csum items from this inode at an offset >= new_size
3145 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003146 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04003147 BUG_ON(ret);
3148
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003149 if (root->ref_cows && leaf_gen < trans->transid) {
3150 ref = btrfs_alloc_leaf_ref(root, 0);
3151 if (ref) {
3152 ref->root_gen = root->root_key.offset;
3153 ref->bytenr = leaf_start;
3154 ref->owner = 0;
3155 ref->generation = leaf_gen;
3156 ref->nritems = 0;
3157
Chris Masonbd56b302009-02-04 09:27:02 -05003158 btrfs_sort_leaf_ref(ref);
3159
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003160 ret = btrfs_add_leaf_ref(root, ref, 0);
3161 WARN_ON(ret);
3162 btrfs_free_leaf_ref(root, ref);
3163 } else {
3164 WARN_ON(1);
3165 }
3166 }
Chris Mason323ac952008-10-01 19:05:46 -04003167next_key:
3168 btrfs_release_path(root, path);
3169
3170 if (other_key.objectid == inode->i_ino &&
3171 other_key.type == key.type && other_key.offset > key.offset) {
3172 key.offset = other_key.offset;
3173 cond_resched();
3174 goto again;
3175 }
3176 ret = 0;
3177out:
3178 /* fixup any changes we've made to the path */
3179 path->lowest_level = 0;
3180 path->keep_locks = 0;
3181 btrfs_release_path(root, path);
3182 return ret;
3183}
3184
Chris Masond20f7042008-12-08 16:58:54 -05003185#endif
3186
Chris Mason323ac952008-10-01 19:05:46 -04003187/*
Chris Mason39279cc2007-06-12 06:35:45 -04003188 * this can truncate away extent items, csum items and directory items.
3189 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003190 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003191 *
3192 * csum items that cross the new i_size are truncated to the new size
3193 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003194 *
3195 * min_type is the minimum key type to truncate down to. If set to 0, this
3196 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003197 */
Yan, Zheng80825102009-11-12 09:35:36 +00003198int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3199 struct btrfs_root *root,
3200 struct inode *inode,
3201 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003202{
Chris Mason39279cc2007-06-12 06:35:45 -04003203 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003204 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003205 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003206 struct btrfs_key key;
3207 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003208 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003209 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003210 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003211 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003212 u64 mask = root->sectorsize - 1;
3213 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003214 int found_extent;
3215 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003216 int pending_del_nr = 0;
3217 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003218 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003219 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003220 int ret;
3221 int err = 0;
3222
3223 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003224
Josef Bacik0af3d002010-06-21 14:48:16 -04003225 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003226 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003227
Chris Mason39279cc2007-06-12 06:35:45 -04003228 path = btrfs_alloc_path();
3229 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003230 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003231
Chris Mason39279cc2007-06-12 06:35:45 -04003232 key.objectid = inode->i_ino;
3233 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003234 key.type = (u8)-1;
3235
Chris Mason85e21ba2008-01-29 15:11:36 -05003236search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003237 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003238 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003239 if (ret < 0) {
3240 err = ret;
3241 goto out;
3242 }
Chris Masond3977122009-01-05 21:25:51 -05003243
Chris Mason85e21ba2008-01-29 15:11:36 -05003244 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003245 /* there are no items in the tree for us to truncate, we're
3246 * done
3247 */
Yan, Zheng80825102009-11-12 09:35:36 +00003248 if (path->slots[0] == 0)
3249 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003250 path->slots[0]--;
3251 }
3252
Chris Masond3977122009-01-05 21:25:51 -05003253 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003254 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003255 leaf = path->nodes[0];
3256 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3257 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003258 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003259
Chris Mason5f39d392007-10-15 16:14:19 -04003260 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003261 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003262
Chris Mason85e21ba2008-01-29 15:11:36 -05003263 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003264 break;
3265
Chris Mason5f39d392007-10-15 16:14:19 -04003266 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003267 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003268 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003269 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003270 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003271 encoding = btrfs_file_extent_compression(leaf, fi);
3272 encoding |= btrfs_file_extent_encryption(leaf, fi);
3273 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3274
Chris Mason179e29e2007-11-01 11:28:41 -04003275 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003276 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003277 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003278 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003279 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003280 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003281 }
Yan008630c2007-11-07 13:31:09 -05003282 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003283 }
Yan, Zheng80825102009-11-12 09:35:36 +00003284 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003285 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003286 } else {
3287 if (item_end < new_size)
3288 break;
3289 if (found_key.offset >= new_size)
3290 del_item = 1;
3291 else
3292 del_item = 0;
3293 }
Chris Mason39279cc2007-06-12 06:35:45 -04003294 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003295 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003296 if (found_type != BTRFS_EXTENT_DATA_KEY)
3297 goto delete;
3298
3299 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003300 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003301 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003302 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003303 u64 orig_num_bytes =
3304 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003305 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003306 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -05003307 extent_num_bytes = extent_num_bytes &
3308 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003309 btrfs_set_file_extent_num_bytes(leaf, fi,
3310 extent_num_bytes);
3311 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003312 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003313 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003314 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003315 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003316 } else {
Chris Masondb945352007-10-15 16:15:53 -04003317 extent_num_bytes =
3318 btrfs_file_extent_disk_num_bytes(leaf,
3319 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003320 extent_offset = found_key.offset -
3321 btrfs_file_extent_offset(leaf, fi);
3322
Chris Mason39279cc2007-06-12 06:35:45 -04003323 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003324 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003325 if (extent_start != 0) {
3326 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003327 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003328 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003329 }
3330 }
Chris Mason90692182008-02-08 13:49:28 -05003331 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003332 /*
3333 * we can't truncate inline items that have had
3334 * special encodings
3335 */
3336 if (!del_item &&
3337 btrfs_file_extent_compression(leaf, fi) == 0 &&
3338 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3339 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003340 u32 size = new_size - found_key.offset;
3341
3342 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003343 inode_sub_bytes(inode, item_end + 1 -
3344 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003345 }
3346 size =
3347 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003348 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003349 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003350 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003351 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003352 inode_sub_bytes(inode, item_end + 1 -
3353 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003354 }
Chris Mason39279cc2007-06-12 06:35:45 -04003355 }
Chris Mason179e29e2007-11-01 11:28:41 -04003356delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003357 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003358 if (!pending_del_nr) {
3359 /* no pending yet, add ourselves */
3360 pending_del_slot = path->slots[0];
3361 pending_del_nr = 1;
3362 } else if (pending_del_nr &&
3363 path->slots[0] + 1 == pending_del_slot) {
3364 /* hop on the pending chunk */
3365 pending_del_nr++;
3366 pending_del_slot = path->slots[0];
3367 } else {
Chris Masond3977122009-01-05 21:25:51 -05003368 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003369 }
Chris Mason39279cc2007-06-12 06:35:45 -04003370 } else {
3371 break;
3372 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003373 if (found_extent && (root->ref_cows ||
3374 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003375 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003376 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003377 extent_num_bytes, 0,
3378 btrfs_header_owner(leaf),
3379 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003380 BUG_ON(ret);
3381 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003382
Yan, Zheng80825102009-11-12 09:35:36 +00003383 if (found_type == BTRFS_INODE_ITEM_KEY)
3384 break;
3385
3386 if (path->slots[0] == 0 ||
3387 path->slots[0] != pending_del_slot) {
3388 if (root->ref_cows) {
3389 err = -EAGAIN;
3390 goto out;
3391 }
3392 if (pending_del_nr) {
3393 ret = btrfs_del_items(trans, root, path,
3394 pending_del_slot,
3395 pending_del_nr);
3396 BUG_ON(ret);
3397 pending_del_nr = 0;
3398 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003399 btrfs_release_path(root, path);
3400 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003401 } else {
3402 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003403 }
Chris Mason39279cc2007-06-12 06:35:45 -04003404 }
Yan, Zheng80825102009-11-12 09:35:36 +00003405out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003406 if (pending_del_nr) {
3407 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3408 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003409 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003410 }
Chris Mason39279cc2007-06-12 06:35:45 -04003411 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003412 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003413}
3414
Chris Masona52d9a82007-08-27 16:49:44 -04003415/*
3416 * taken from block_truncate_page, but does cow as it zeros out
3417 * any bytes left in the last page in the file.
3418 */
3419static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3420{
3421 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003422 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003423 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3424 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003425 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003426 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003427 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003428 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3429 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3430 struct page *page;
3431 int ret = 0;
3432 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003433 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003434
3435 if ((offset & (blocksize - 1)) == 0)
3436 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003437 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003438 if (ret)
3439 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003440
3441 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003442again:
Chris Masona52d9a82007-08-27 16:49:44 -04003443 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003444 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003445 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003446 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003447 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003448
3449 page_start = page_offset(page);
3450 page_end = page_start + PAGE_CACHE_SIZE - 1;
3451
Chris Masona52d9a82007-08-27 16:49:44 -04003452 if (!PageUptodate(page)) {
3453 ret = btrfs_readpage(NULL, page);
3454 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003455 if (page->mapping != mapping) {
3456 unlock_page(page);
3457 page_cache_release(page);
3458 goto again;
3459 }
Chris Masona52d9a82007-08-27 16:49:44 -04003460 if (!PageUptodate(page)) {
3461 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003462 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003463 }
3464 }
Chris Mason211c17f2008-05-15 09:13:45 -04003465 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003466
Josef Bacik2ac55d42010-02-03 19:33:23 +00003467 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3468 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003469 set_page_extent_mapped(page);
3470
3471 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3472 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003473 unlock_extent_cached(io_tree, page_start, page_end,
3474 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003475 unlock_page(page);
3476 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003477 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003478 btrfs_put_ordered_extent(ordered);
3479 goto again;
3480 }
3481
Josef Bacik2ac55d42010-02-03 19:33:23 +00003482 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003483 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003484 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003485
Josef Bacik2ac55d42010-02-03 19:33:23 +00003486 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3487 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003488 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003489 unlock_extent_cached(io_tree, page_start, page_end,
3490 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003491 goto out_unlock;
3492 }
3493
Chris Masone6dcd2d2008-07-17 12:53:50 -04003494 ret = 0;
3495 if (offset != PAGE_CACHE_SIZE) {
3496 kaddr = kmap(page);
3497 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3498 flush_dcache_page(page);
3499 kunmap(page);
3500 }
Chris Mason247e7432008-07-17 12:53:51 -04003501 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003502 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003503 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3504 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003505
Chris Mason89642222008-07-24 09:41:53 -04003506out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003507 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003508 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003509 unlock_page(page);
3510 page_cache_release(page);
3511out:
3512 return ret;
3513}
3514
Yan Zheng9036c102008-10-30 14:19:41 -04003515int btrfs_cont_expand(struct inode *inode, loff_t size)
3516{
3517 struct btrfs_trans_handle *trans;
3518 struct btrfs_root *root = BTRFS_I(inode)->root;
3519 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003520 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003521 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003522 u64 mask = root->sectorsize - 1;
3523 u64 hole_start = (inode->i_size + mask) & ~mask;
3524 u64 block_end = (size + mask) & ~mask;
3525 u64 last_byte;
3526 u64 cur_offset;
3527 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003528 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003529
3530 if (size <= hole_start)
3531 return 0;
3532
Yan Zheng9036c102008-10-30 14:19:41 -04003533 while (1) {
3534 struct btrfs_ordered_extent *ordered;
3535 btrfs_wait_ordered_range(inode, hole_start,
3536 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003537 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3538 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003539 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3540 if (!ordered)
3541 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003542 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3543 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003544 btrfs_put_ordered_extent(ordered);
3545 }
3546
Yan Zheng9036c102008-10-30 14:19:41 -04003547 cur_offset = hole_start;
3548 while (1) {
3549 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3550 block_end - cur_offset, 0);
3551 BUG_ON(IS_ERR(em) || !em);
3552 last_byte = min(extent_map_end(em), block_end);
3553 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003554 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003555 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003556 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003557
Yan, Zhenga22285a2010-05-16 10:48:46 -04003558 trans = btrfs_start_transaction(root, 2);
3559 if (IS_ERR(trans)) {
3560 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003561 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003562 }
Yan, Zheng80825102009-11-12 09:35:36 +00003563 btrfs_set_trans_block_group(trans, inode);
3564
3565 err = btrfs_drop_extents(trans, inode, cur_offset,
3566 cur_offset + hole_size,
3567 &hint_byte, 1);
3568 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003569
Yan Zheng9036c102008-10-30 14:19:41 -04003570 err = btrfs_insert_file_extent(trans, root,
3571 inode->i_ino, cur_offset, 0,
3572 0, hole_size, 0, hole_size,
3573 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003574 BUG_ON(err);
3575
Yan Zheng9036c102008-10-30 14:19:41 -04003576 btrfs_drop_extent_cache(inode, hole_start,
3577 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003578
3579 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003580 }
3581 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003582 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003583 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003584 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003585 break;
3586 }
3587
Yan, Zhenga22285a2010-05-16 10:48:46 -04003588 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003589 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3590 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003591 return err;
3592}
3593
Yan, Zheng80825102009-11-12 09:35:36 +00003594static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3595{
3596 struct btrfs_root *root = BTRFS_I(inode)->root;
3597 struct btrfs_trans_handle *trans;
3598 unsigned long nr;
3599 int ret;
3600
3601 if (attr->ia_size == inode->i_size)
3602 return 0;
3603
3604 if (attr->ia_size > inode->i_size) {
3605 unsigned long limit;
3606 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3607 if (attr->ia_size > inode->i_sb->s_maxbytes)
3608 return -EFBIG;
3609 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3610 send_sig(SIGXFSZ, current, 0);
3611 return -EFBIG;
3612 }
3613 }
3614
Yan, Zhengd68fc572010-05-16 10:49:58 -04003615 trans = btrfs_start_transaction(root, 5);
3616 if (IS_ERR(trans))
3617 return PTR_ERR(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00003618
Yan, Zheng80825102009-11-12 09:35:36 +00003619 btrfs_set_trans_block_group(trans, inode);
3620
3621 ret = btrfs_orphan_add(trans, inode);
3622 BUG_ON(ret);
3623
3624 nr = trans->blocks_used;
3625 btrfs_end_transaction(trans, root);
Yan, Zheng80825102009-11-12 09:35:36 +00003626 btrfs_btree_balance_dirty(root, nr);
3627
3628 if (attr->ia_size > inode->i_size) {
3629 ret = btrfs_cont_expand(inode, attr->ia_size);
3630 if (ret) {
3631 btrfs_truncate(inode);
3632 return ret;
3633 }
3634
3635 i_size_write(inode, attr->ia_size);
3636 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3637
Yan, Zhengd68fc572010-05-16 10:49:58 -04003638 trans = btrfs_start_transaction(root, 0);
3639 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003640 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003641 trans->block_rsv = root->orphan_block_rsv;
3642 BUG_ON(!trans->block_rsv);
Yan, Zheng80825102009-11-12 09:35:36 +00003643
3644 ret = btrfs_update_inode(trans, root, inode);
3645 BUG_ON(ret);
3646 if (inode->i_nlink > 0) {
3647 ret = btrfs_orphan_del(trans, inode);
3648 BUG_ON(ret);
3649 }
3650 nr = trans->blocks_used;
3651 btrfs_end_transaction(trans, root);
3652 btrfs_btree_balance_dirty(root, nr);
3653 return 0;
3654 }
3655
3656 /*
3657 * We're truncating a file that used to have good data down to
3658 * zero. Make sure it gets into the ordered flush list so that
3659 * any new writes get down to disk quickly.
3660 */
3661 if (attr->ia_size == 0)
3662 BTRFS_I(inode)->ordered_data_close = 1;
3663
3664 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3665 ret = vmtruncate(inode, attr->ia_size);
3666 BUG_ON(ret);
3667
3668 return 0;
3669}
3670
Chris Mason39279cc2007-06-12 06:35:45 -04003671static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3672{
3673 struct inode *inode = dentry->d_inode;
3674 int err;
3675
3676 err = inode_change_ok(inode, attr);
3677 if (err)
3678 return err;
3679
Chris Mason5a3f23d2009-03-31 13:27:11 -04003680 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003681 err = btrfs_setattr_size(inode, attr);
3682 if (err)
3683 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003684 }
Yan Zheng9036c102008-10-30 14:19:41 -04003685
Christoph Hellwig10257742010-06-04 11:30:02 +02003686 if (attr->ia_valid) {
3687 setattr_copy(inode, attr);
3688 mark_inode_dirty(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003689
Christoph Hellwig10257742010-06-04 11:30:02 +02003690 if (attr->ia_valid & ATTR_MODE)
3691 err = btrfs_acl_chmod(inode);
3692 }
3693
Chris Mason39279cc2007-06-12 06:35:45 -04003694 return err;
3695}
Chris Mason61295eb2008-01-14 16:24:38 -05003696
Al Virobd555972010-06-07 11:35:40 -04003697void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003698{
3699 struct btrfs_trans_handle *trans;
3700 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04003701 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003702 int ret;
3703
3704 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003705 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3706 root == root->fs_info->tree_root))
Al Virobd555972010-06-07 11:35:40 -04003707 goto no_delete;
3708
Chris Mason39279cc2007-06-12 06:35:45 -04003709 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003710 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003711 goto no_delete;
3712 }
Al Virobd555972010-06-07 11:35:40 -04003713 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003714 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003715
Yan, Zhengc71bf092009-11-12 09:34:40 +00003716 if (root->fs_info->log_root_recovering) {
3717 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3718 goto no_delete;
3719 }
3720
Yan, Zheng76dda932009-09-21 16:00:26 -04003721 if (inode->i_nlink > 0) {
3722 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3723 goto no_delete;
3724 }
3725
Chris Masondbe674a2008-07-17 12:54:05 -04003726 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003727
Yan, Zheng80825102009-11-12 09:35:36 +00003728 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003729 trans = btrfs_start_transaction(root, 0);
3730 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003731 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003732 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003733
Yan, Zhengd68fc572010-05-16 10:49:58 -04003734 ret = btrfs_block_rsv_check(trans, root,
3735 root->orphan_block_rsv, 0, 5);
3736 if (ret) {
3737 BUG_ON(ret != -EAGAIN);
3738 ret = btrfs_commit_transaction(trans, root);
3739 BUG_ON(ret);
3740 continue;
3741 }
3742
3743 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003744 if (ret != -EAGAIN)
3745 break;
3746
3747 nr = trans->blocks_used;
3748 btrfs_end_transaction(trans, root);
3749 trans = NULL;
3750 btrfs_btree_balance_dirty(root, nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003751
Josef Bacik7b128762008-07-24 12:17:14 -04003752 }
3753
Yan, Zheng80825102009-11-12 09:35:36 +00003754 if (ret == 0) {
3755 ret = btrfs_orphan_del(trans, inode);
3756 BUG_ON(ret);
3757 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003758
Chris Masond3c2fdc2007-09-17 10:58:06 -04003759 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003760 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003761 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003762no_delete:
Al Virobd555972010-06-07 11:35:40 -04003763 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003764 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003765}
3766
3767/*
3768 * this returns the key found in the dir entry in the location pointer.
3769 * If no dir entries were found, location->objectid is 0.
3770 */
3771static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3772 struct btrfs_key *location)
3773{
3774 const char *name = dentry->d_name.name;
3775 int namelen = dentry->d_name.len;
3776 struct btrfs_dir_item *di;
3777 struct btrfs_path *path;
3778 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003779 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003780
3781 path = btrfs_alloc_path();
3782 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003783
Chris Mason39279cc2007-06-12 06:35:45 -04003784 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3785 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003786 if (IS_ERR(di))
3787 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003788
3789 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003790 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003791
Chris Mason5f39d392007-10-15 16:14:19 -04003792 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003793out:
Chris Mason39279cc2007-06-12 06:35:45 -04003794 btrfs_free_path(path);
3795 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003796out_err:
3797 location->objectid = 0;
3798 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003799}
3800
3801/*
3802 * when we hit a tree root in a directory, the btrfs part of the inode
3803 * needs to be changed to reflect the root directory of the tree root. This
3804 * is kind of like crossing a mount point.
3805 */
3806static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003807 struct inode *dir,
3808 struct dentry *dentry,
3809 struct btrfs_key *location,
3810 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003811{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003812 struct btrfs_path *path;
3813 struct btrfs_root *new_root;
3814 struct btrfs_root_ref *ref;
3815 struct extent_buffer *leaf;
3816 int ret;
3817 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003818
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003819 path = btrfs_alloc_path();
3820 if (!path) {
3821 err = -ENOMEM;
3822 goto out;
3823 }
Chris Mason39279cc2007-06-12 06:35:45 -04003824
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003825 err = -ENOENT;
3826 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3827 BTRFS_I(dir)->root->root_key.objectid,
3828 location->objectid);
3829 if (ret) {
3830 if (ret < 0)
3831 err = ret;
3832 goto out;
3833 }
Chris Mason39279cc2007-06-12 06:35:45 -04003834
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003835 leaf = path->nodes[0];
3836 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3837 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3838 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3839 goto out;
3840
3841 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3842 (unsigned long)(ref + 1),
3843 dentry->d_name.len);
3844 if (ret)
3845 goto out;
3846
3847 btrfs_release_path(root->fs_info->tree_root, path);
3848
3849 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3850 if (IS_ERR(new_root)) {
3851 err = PTR_ERR(new_root);
3852 goto out;
3853 }
3854
3855 if (btrfs_root_refs(&new_root->root_item) == 0) {
3856 err = -ENOENT;
3857 goto out;
3858 }
3859
3860 *sub_root = new_root;
3861 location->objectid = btrfs_root_dirid(&new_root->root_item);
3862 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003863 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003864 err = 0;
3865out:
3866 btrfs_free_path(path);
3867 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003868}
3869
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003870static void inode_tree_add(struct inode *inode)
3871{
3872 struct btrfs_root *root = BTRFS_I(inode)->root;
3873 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003874 struct rb_node **p;
3875 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003876again:
3877 p = &root->inode_tree.rb_node;
3878 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003879
Al Viro1d3382c2010-10-23 15:19:20 -04003880 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04003881 return;
3882
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003883 spin_lock(&root->inode_lock);
3884 while (*p) {
3885 parent = *p;
3886 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3887
3888 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003889 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003890 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003891 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003892 else {
3893 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003894 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003895 rb_erase(parent, &root->inode_tree);
3896 RB_CLEAR_NODE(parent);
3897 spin_unlock(&root->inode_lock);
3898 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003899 }
3900 }
3901 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3902 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3903 spin_unlock(&root->inode_lock);
3904}
3905
3906static void inode_tree_del(struct inode *inode)
3907{
3908 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003909 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003910
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003911 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003912 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003913 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003914 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003915 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003916 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003917 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003918
Josef Bacik0af3d002010-06-21 14:48:16 -04003919 /*
3920 * Free space cache has inodes in the tree root, but the tree root has a
3921 * root_refs of 0, so this could end up dropping the tree root as a
3922 * snapshot, so we need the extra !root->fs_info->tree_root check to
3923 * make sure we don't drop it.
3924 */
3925 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3926 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003927 synchronize_srcu(&root->fs_info->subvol_srcu);
3928 spin_lock(&root->inode_lock);
3929 empty = RB_EMPTY_ROOT(&root->inode_tree);
3930 spin_unlock(&root->inode_lock);
3931 if (empty)
3932 btrfs_add_dead_root(root);
3933 }
3934}
3935
3936int btrfs_invalidate_inodes(struct btrfs_root *root)
3937{
3938 struct rb_node *node;
3939 struct rb_node *prev;
3940 struct btrfs_inode *entry;
3941 struct inode *inode;
3942 u64 objectid = 0;
3943
3944 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3945
3946 spin_lock(&root->inode_lock);
3947again:
3948 node = root->inode_tree.rb_node;
3949 prev = NULL;
3950 while (node) {
3951 prev = node;
3952 entry = rb_entry(node, struct btrfs_inode, rb_node);
3953
3954 if (objectid < entry->vfs_inode.i_ino)
3955 node = node->rb_left;
3956 else if (objectid > entry->vfs_inode.i_ino)
3957 node = node->rb_right;
3958 else
3959 break;
3960 }
3961 if (!node) {
3962 while (prev) {
3963 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3964 if (objectid <= entry->vfs_inode.i_ino) {
3965 node = prev;
3966 break;
3967 }
3968 prev = rb_next(prev);
3969 }
3970 }
3971 while (node) {
3972 entry = rb_entry(node, struct btrfs_inode, rb_node);
3973 objectid = entry->vfs_inode.i_ino + 1;
3974 inode = igrab(&entry->vfs_inode);
3975 if (inode) {
3976 spin_unlock(&root->inode_lock);
3977 if (atomic_read(&inode->i_count) > 1)
3978 d_prune_aliases(inode);
3979 /*
Al Viro45321ac2010-06-07 13:43:19 -04003980 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04003981 * the inode cache when its usage count
3982 * hits zero.
3983 */
3984 iput(inode);
3985 cond_resched();
3986 spin_lock(&root->inode_lock);
3987 goto again;
3988 }
3989
3990 if (cond_resched_lock(&root->inode_lock))
3991 goto again;
3992
3993 node = rb_next(node);
3994 }
3995 spin_unlock(&root->inode_lock);
3996 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003997}
3998
Chris Masone02119d2008-09-05 16:13:11 -04003999static int btrfs_init_locked_inode(struct inode *inode, void *p)
4000{
4001 struct btrfs_iget_args *args = p;
4002 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004003 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004004 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004005 return 0;
4006}
4007
4008static int btrfs_find_actor(struct inode *inode, void *opaque)
4009{
4010 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05004011 return args->ino == inode->i_ino &&
4012 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004013}
4014
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004015static struct inode *btrfs_iget_locked(struct super_block *s,
4016 u64 objectid,
4017 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004018{
4019 struct inode *inode;
4020 struct btrfs_iget_args args;
4021 args.ino = objectid;
4022 args.root = root;
4023
4024 inode = iget5_locked(s, objectid, btrfs_find_actor,
4025 btrfs_init_locked_inode,
4026 (void *)&args);
4027 return inode;
4028}
4029
Balaji Rao1a54ef82008-07-21 02:01:04 +05304030/* Get an inode object given its location and corresponding root.
4031 * Returns in *is_new if the inode was read from disk
4032 */
4033struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004034 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304035{
4036 struct inode *inode;
4037
4038 inode = btrfs_iget_locked(s, location->objectid, root);
4039 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004040 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304041
4042 if (inode->i_state & I_NEW) {
4043 BTRFS_I(inode)->root = root;
4044 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4045 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004046
4047 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304048 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00004049 if (new)
4050 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05304051 }
4052
4053 return inode;
4054}
4055
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004056static struct inode *new_simple_dir(struct super_block *s,
4057 struct btrfs_key *key,
4058 struct btrfs_root *root)
4059{
4060 struct inode *inode = new_inode(s);
4061
4062 if (!inode)
4063 return ERR_PTR(-ENOMEM);
4064
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004065 BTRFS_I(inode)->root = root;
4066 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4067 BTRFS_I(inode)->dummy_inode = 1;
4068
4069 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4070 inode->i_op = &simple_dir_inode_operations;
4071 inode->i_fop = &simple_dir_operations;
4072 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4073 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4074
4075 return inode;
4076}
4077
Chris Mason3de45862008-11-17 21:02:50 -05004078struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004079{
Chris Masond3977122009-01-05 21:25:51 -05004080 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004081 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004082 struct btrfs_root *sub_root = root;
4083 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004084 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004085 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004086
4087 if (dentry->d_name.len > BTRFS_NAME_LEN)
4088 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004089
Chris Mason39279cc2007-06-12 06:35:45 -04004090 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04004091
Chris Mason39279cc2007-06-12 06:35:45 -04004092 if (ret < 0)
4093 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004094
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004095 if (location.objectid == 0)
4096 return NULL;
4097
4098 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004099 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004100 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004101 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004102
4103 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4104
Yan, Zheng76dda932009-09-21 16:00:26 -04004105 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004106 ret = fixup_tree_root_location(root, dir, dentry,
4107 &location, &sub_root);
4108 if (ret < 0) {
4109 if (ret != -ENOENT)
4110 inode = ERR_PTR(ret);
4111 else
4112 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4113 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004114 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004115 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004116 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4117
Yan, Zhengc71bf092009-11-12 09:34:40 +00004118 if (root != sub_root) {
4119 down_read(&root->fs_info->cleanup_work_sem);
4120 if (!(inode->i_sb->s_flags & MS_RDONLY))
4121 btrfs_orphan_cleanup(sub_root);
4122 up_read(&root->fs_info->cleanup_work_sem);
4123 }
4124
Chris Mason3de45862008-11-17 21:02:50 -05004125 return inode;
4126}
4127
Nick Pigginfe15ce42011-01-07 17:49:23 +11004128static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04004129{
4130 struct btrfs_root *root;
4131
Yan, Zhengefefb142009-10-09 09:25:16 -04004132 if (!dentry->d_inode && !IS_ROOT(dentry))
4133 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004134
Yan, Zhengefefb142009-10-09 09:25:16 -04004135 if (dentry->d_inode) {
4136 root = BTRFS_I(dentry->d_inode)->root;
4137 if (btrfs_root_refs(&root->root_item) == 0)
4138 return 1;
4139 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004140 return 0;
4141}
4142
Chris Mason3de45862008-11-17 21:02:50 -05004143static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4144 struct nameidata *nd)
4145{
4146 struct inode *inode;
4147
Chris Mason3de45862008-11-17 21:02:50 -05004148 inode = btrfs_lookup_dentry(dir, dentry);
4149 if (IS_ERR(inode))
4150 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004151
Chris Mason39279cc2007-06-12 06:35:45 -04004152 return d_splice_alias(inode, dentry);
4153}
4154
Chris Mason39279cc2007-06-12 06:35:45 -04004155static unsigned char btrfs_filetype_table[] = {
4156 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4157};
4158
David Woodhousecbdf5a22008-08-06 19:42:33 +01004159static int btrfs_real_readdir(struct file *filp, void *dirent,
4160 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004161{
Chris Mason6da6aba2007-12-18 16:15:09 -05004162 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004163 struct btrfs_root *root = BTRFS_I(inode)->root;
4164 struct btrfs_item *item;
4165 struct btrfs_dir_item *di;
4166 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004167 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004168 struct btrfs_path *path;
4169 int ret;
4170 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04004171 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004172 int slot;
4173 int advance;
4174 unsigned char d_type;
4175 int over = 0;
4176 u32 di_cur;
4177 u32 di_total;
4178 u32 di_len;
4179 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004180 char tmp_name[32];
4181 char *name_ptr;
4182 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04004183
4184 /* FIXME, use a real flag for deciding about the key type */
4185 if (root->fs_info->tree_root == root)
4186 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004187
Chris Mason39544012007-12-12 14:38:19 -05004188 /* special case for "." */
4189 if (filp->f_pos == 0) {
4190 over = filldir(dirent, ".", 1,
4191 1, inode->i_ino,
4192 DT_DIR);
4193 if (over)
4194 return 0;
4195 filp->f_pos = 1;
4196 }
Chris Mason39544012007-12-12 14:38:19 -05004197 /* special case for .., just use the back ref */
4198 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004199 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004200 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004201 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004202 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004203 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004204 filp->f_pos = 2;
4205 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004206 path = btrfs_alloc_path();
4207 path->reada = 2;
4208
Chris Mason39279cc2007-06-12 06:35:45 -04004209 btrfs_set_key_type(&key, key_type);
4210 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01004211 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04004212
Chris Mason39279cc2007-06-12 06:35:45 -04004213 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4214 if (ret < 0)
4215 goto err;
4216 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01004217
4218 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004219 leaf = path->nodes[0];
4220 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004221 slot = path->slots[0];
4222 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01004223 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004224 ret = btrfs_next_leaf(root, path);
4225 if (ret)
4226 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004227 leaf = path->nodes[0];
4228 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004229 slot = path->slots[0];
4230 } else {
4231 slot++;
4232 path->slots[0]++;
4233 }
4234 }
Chris Mason3de45862008-11-17 21:02:50 -05004235
Chris Mason39279cc2007-06-12 06:35:45 -04004236 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004237 item = btrfs_item_nr(leaf, slot);
4238 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4239
4240 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004241 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004242 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004243 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004244 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04004245 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04004246
4247 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01004248
Chris Mason39279cc2007-06-12 06:35:45 -04004249 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4250 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004251 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004252
4253 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004254 struct btrfs_key location;
4255
4256 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004257 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004258 name_ptr = tmp_name;
4259 } else {
4260 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004261 if (!name_ptr) {
4262 ret = -ENOMEM;
4263 goto err;
4264 }
Chris Mason5f39d392007-10-15 16:14:19 -04004265 }
4266 read_extent_buffer(leaf, name_ptr,
4267 (unsigned long)(di + 1), name_len);
4268
4269 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4270 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004271
4272 /* is this a reference to our own snapshot? If so
4273 * skip it
4274 */
4275 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4276 location.objectid == root->root_key.objectid) {
4277 over = 0;
4278 goto skip;
4279 }
Chris Mason5f39d392007-10-15 16:14:19 -04004280 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004281 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004282 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004283
Chris Mason3de45862008-11-17 21:02:50 -05004284skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004285 if (name_ptr != tmp_name)
4286 kfree(name_ptr);
4287
Chris Mason39279cc2007-06-12 06:35:45 -04004288 if (over)
4289 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004290 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004291 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004292 di_cur += di_len;
4293 di = (struct btrfs_dir_item *)((char *)di + di_len);
4294 }
4295 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004296
4297 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004298 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004299 /*
4300 * 32-bit glibc will use getdents64, but then strtol -
4301 * so the last number we can serve is this.
4302 */
4303 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004304 else
4305 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004306nopos:
4307 ret = 0;
4308err:
Chris Mason39279cc2007-06-12 06:35:45 -04004309 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004310 return ret;
4311}
4312
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004313int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004314{
4315 struct btrfs_root *root = BTRFS_I(inode)->root;
4316 struct btrfs_trans_handle *trans;
4317 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004318 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004319
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004320 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004321 return 0;
4322
Josef Bacik0af3d002010-06-21 14:48:16 -04004323 smp_mb();
4324 nolock = (root->fs_info->closing && root == root->fs_info->tree_root);
4325
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004326 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004327 if (nolock)
4328 trans = btrfs_join_transaction_nolock(root, 1);
4329 else
4330 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004331 btrfs_set_trans_block_group(trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04004332 if (nolock)
4333 ret = btrfs_end_transaction_nolock(trans, root);
4334 else
4335 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004336 }
4337 return ret;
4338}
4339
4340/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004341 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004342 * inode changes. But, it is most likely to find the inode in cache.
4343 * FIXME, needs more benchmarking...there are no reasons other than performance
4344 * to keep or drop this code.
4345 */
4346void btrfs_dirty_inode(struct inode *inode)
4347{
4348 struct btrfs_root *root = BTRFS_I(inode)->root;
4349 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004350 int ret;
4351
4352 if (BTRFS_I(inode)->dummy_inode)
4353 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004354
Chris Masonf9295742008-07-17 12:54:14 -04004355 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004356 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004357
4358 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004359 if (ret && ret == -ENOSPC) {
4360 /* whoops, lets try again with the full transaction */
4361 btrfs_end_transaction(trans, root);
4362 trans = btrfs_start_transaction(root, 1);
Chris Mason9aeead72010-05-27 10:23:00 -04004363 if (IS_ERR(trans)) {
4364 if (printk_ratelimit()) {
4365 printk(KERN_ERR "btrfs: fail to "
4366 "dirty inode %lu error %ld\n",
4367 inode->i_ino, PTR_ERR(trans));
4368 }
4369 return;
4370 }
Chris Mason94b60442010-05-26 11:02:00 -04004371 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004372
Chris Mason94b60442010-05-26 11:02:00 -04004373 ret = btrfs_update_inode(trans, root, inode);
4374 if (ret) {
Chris Mason9aeead72010-05-27 10:23:00 -04004375 if (printk_ratelimit()) {
4376 printk(KERN_ERR "btrfs: fail to "
4377 "dirty inode %lu error %d\n",
4378 inode->i_ino, ret);
4379 }
Chris Mason94b60442010-05-26 11:02:00 -04004380 }
4381 }
Chris Mason39279cc2007-06-12 06:35:45 -04004382 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004383}
4384
Chris Masond352ac62008-09-29 15:18:18 -04004385/*
4386 * find the highest existing sequence number in a directory
4387 * and then set the in-memory index_cnt variable to reflect
4388 * free sequence numbers
4389 */
Josef Bacikaec74772008-07-24 12:12:38 -04004390static int btrfs_set_inode_index_count(struct inode *inode)
4391{
4392 struct btrfs_root *root = BTRFS_I(inode)->root;
4393 struct btrfs_key key, found_key;
4394 struct btrfs_path *path;
4395 struct extent_buffer *leaf;
4396 int ret;
4397
4398 key.objectid = inode->i_ino;
4399 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4400 key.offset = (u64)-1;
4401
4402 path = btrfs_alloc_path();
4403 if (!path)
4404 return -ENOMEM;
4405
4406 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4407 if (ret < 0)
4408 goto out;
4409 /* FIXME: we should be able to handle this */
4410 if (ret == 0)
4411 goto out;
4412 ret = 0;
4413
4414 /*
4415 * MAGIC NUMBER EXPLANATION:
4416 * since we search a directory based on f_pos we have to start at 2
4417 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4418 * else has to start at 2
4419 */
4420 if (path->slots[0] == 0) {
4421 BTRFS_I(inode)->index_cnt = 2;
4422 goto out;
4423 }
4424
4425 path->slots[0]--;
4426
4427 leaf = path->nodes[0];
4428 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4429
4430 if (found_key.objectid != inode->i_ino ||
4431 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4432 BTRFS_I(inode)->index_cnt = 2;
4433 goto out;
4434 }
4435
4436 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4437out:
4438 btrfs_free_path(path);
4439 return ret;
4440}
4441
Chris Masond352ac62008-09-29 15:18:18 -04004442/*
4443 * helper to find a free sequence number in a given directory. This current
4444 * code is very simple, later versions will do smarter things in the btree
4445 */
Chris Mason3de45862008-11-17 21:02:50 -05004446int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004447{
4448 int ret = 0;
4449
4450 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4451 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004452 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004453 return ret;
4454 }
4455
Chris Mason00e4e6b2008-08-05 11:18:09 -04004456 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004457 BTRFS_I(dir)->index_cnt++;
4458
4459 return ret;
4460}
4461
Chris Mason39279cc2007-06-12 06:35:45 -04004462static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4463 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004464 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004465 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004466 u64 ref_objectid, u64 objectid,
4467 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004468{
4469 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004470 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004471 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004472 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004473 struct btrfs_inode_ref *ref;
4474 struct btrfs_key key[2];
4475 u32 sizes[2];
4476 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004477 int ret;
4478 int owner;
4479
Chris Mason5f39d392007-10-15 16:14:19 -04004480 path = btrfs_alloc_path();
4481 BUG_ON(!path);
4482
Chris Mason39279cc2007-06-12 06:35:45 -04004483 inode = new_inode(root->fs_info->sb);
4484 if (!inode)
4485 return ERR_PTR(-ENOMEM);
4486
Josef Bacikaec74772008-07-24 12:12:38 -04004487 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004488 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004489 if (ret) {
4490 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004491 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004492 }
Josef Bacikaec74772008-07-24 12:12:38 -04004493 }
4494 /*
4495 * index_cnt is ignored for everything but a dir,
4496 * btrfs_get_inode_index_count has an explanation for the magic
4497 * number
4498 */
4499 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004500 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004501 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004502 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004503 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004504
Chris Mason39279cc2007-06-12 06:35:45 -04004505 if (mode & S_IFDIR)
4506 owner = 0;
4507 else
4508 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004509 BTRFS_I(inode)->block_group =
4510 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004511
4512 key[0].objectid = objectid;
4513 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4514 key[0].offset = 0;
4515
4516 key[1].objectid = objectid;
4517 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4518 key[1].offset = ref_objectid;
4519
4520 sizes[0] = sizeof(struct btrfs_inode_item);
4521 sizes[1] = name_len + sizeof(*ref);
4522
Chris Masonb9473432009-03-13 11:00:37 -04004523 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004524 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4525 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004526 goto fail;
4527
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004528 inode_init_owner(inode, dir, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04004529 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004530 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004531 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004532 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4533 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004534 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004535
4536 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4537 struct btrfs_inode_ref);
4538 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004539 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004540 ptr = (unsigned long)(ref + 1);
4541 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4542
Chris Mason5f39d392007-10-15 16:14:19 -04004543 btrfs_mark_buffer_dirty(path->nodes[0]);
4544 btrfs_free_path(path);
4545
Chris Mason39279cc2007-06-12 06:35:45 -04004546 location = &BTRFS_I(inode)->location;
4547 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004548 location->offset = 0;
4549 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4550
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004551 btrfs_inherit_iflags(inode, dir);
4552
Chris Mason94272162009-07-02 12:26:06 -04004553 if ((mode & S_IFREG)) {
4554 if (btrfs_test_opt(root, NODATASUM))
4555 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4556 if (btrfs_test_opt(root, NODATACOW))
4557 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4558 }
4559
Chris Mason39279cc2007-06-12 06:35:45 -04004560 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004561 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004562 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004563fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004564 if (dir)
4565 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004566 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004567 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004568 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004569}
4570
4571static inline u8 btrfs_inode_type(struct inode *inode)
4572{
4573 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4574}
4575
Chris Masond352ac62008-09-29 15:18:18 -04004576/*
4577 * utility function to add 'inode' into 'parent_inode' with
4578 * a give name and a given sequence number.
4579 * if 'add_backref' is true, also insert a backref from the
4580 * inode to the parent directory.
4581 */
Chris Masone02119d2008-09-05 16:13:11 -04004582int btrfs_add_link(struct btrfs_trans_handle *trans,
4583 struct inode *parent_inode, struct inode *inode,
4584 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004585{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004586 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004587 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004588 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004589
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004590 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4591 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4592 } else {
4593 key.objectid = inode->i_ino;
4594 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4595 key.offset = 0;
4596 }
Chris Mason39279cc2007-06-12 06:35:45 -04004597
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004598 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4599 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4600 key.objectid, root->root_key.objectid,
4601 parent_inode->i_ino,
4602 index, name, name_len);
4603 } else if (add_backref) {
4604 ret = btrfs_insert_inode_ref(trans, root,
4605 name, name_len, inode->i_ino,
4606 parent_inode->i_ino, index);
4607 }
4608
Chris Mason39279cc2007-06-12 06:35:45 -04004609 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004610 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4611 parent_inode->i_ino, &key,
4612 btrfs_inode_type(inode), index);
4613 BUG_ON(ret);
4614
Chris Masondbe674a2008-07-17 12:54:05 -04004615 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004616 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004617 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004618 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004619 }
4620 return ret;
4621}
4622
4623static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004624 struct inode *dir, struct dentry *dentry,
4625 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004626{
Josef Bacika1b075d2010-11-19 20:36:11 +00004627 int err = btrfs_add_link(trans, dir, inode,
4628 dentry->d_name.name, dentry->d_name.len,
4629 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004630 if (!err) {
4631 d_instantiate(dentry, inode);
4632 return 0;
4633 }
4634 if (err > 0)
4635 err = -EEXIST;
4636 return err;
4637}
4638
Josef Bacik618e21d2007-07-11 10:18:17 -04004639static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4640 int mode, dev_t rdev)
4641{
4642 struct btrfs_trans_handle *trans;
4643 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004644 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004645 int err;
4646 int drop_inode = 0;
4647 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004648 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004649 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004650
4651 if (!new_valid_dev(rdev))
4652 return -EINVAL;
4653
Yan, Zhenga22285a2010-05-16 10:48:46 -04004654 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4655 if (err)
4656 return err;
4657
Josef Bacik9ed74f22009-09-11 16:12:44 -04004658 /*
4659 * 2 for inode item and ref
4660 * 2 for dir items
4661 * 1 for xattr if selinux is on
4662 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004663 trans = btrfs_start_transaction(root, 5);
4664 if (IS_ERR(trans))
4665 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004666
Josef Bacik618e21d2007-07-11 10:18:17 -04004667 btrfs_set_trans_block_group(trans, dir);
4668
Josef Bacikaec74772008-07-24 12:12:38 -04004669 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004670 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004671 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004672 err = PTR_ERR(inode);
4673 if (IS_ERR(inode))
4674 goto out_unlock;
4675
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004676 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004677 if (err) {
4678 drop_inode = 1;
4679 goto out_unlock;
4680 }
4681
Josef Bacik618e21d2007-07-11 10:18:17 -04004682 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004683 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004684 if (err)
4685 drop_inode = 1;
4686 else {
4687 inode->i_op = &btrfs_special_inode_operations;
4688 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004689 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004690 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004691 btrfs_update_inode_block_group(trans, inode);
4692 btrfs_update_inode_block_group(trans, dir);
4693out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004694 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004695 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004696 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004697 if (drop_inode) {
4698 inode_dec_link_count(inode);
4699 iput(inode);
4700 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004701 return err;
4702}
4703
Chris Mason39279cc2007-06-12 06:35:45 -04004704static int btrfs_create(struct inode *dir, struct dentry *dentry,
4705 int mode, struct nameidata *nd)
4706{
4707 struct btrfs_trans_handle *trans;
4708 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004709 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004710 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004711 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004712 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004713 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004714 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004715
Yan, Zhenga22285a2010-05-16 10:48:46 -04004716 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4717 if (err)
4718 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004719 /*
4720 * 2 for inode item and ref
4721 * 2 for dir items
4722 * 1 for xattr if selinux is on
4723 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004724 trans = btrfs_start_transaction(root, 5);
4725 if (IS_ERR(trans))
4726 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004727
Chris Mason39279cc2007-06-12 06:35:45 -04004728 btrfs_set_trans_block_group(trans, dir);
4729
Josef Bacikaec74772008-07-24 12:12:38 -04004730 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004731 dentry->d_name.len, dir->i_ino, objectid,
4732 BTRFS_I(dir)->block_group, mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004733 err = PTR_ERR(inode);
4734 if (IS_ERR(inode))
4735 goto out_unlock;
4736
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004737 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004738 if (err) {
4739 drop_inode = 1;
4740 goto out_unlock;
4741 }
4742
Chris Mason39279cc2007-06-12 06:35:45 -04004743 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004744 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004745 if (err)
4746 drop_inode = 1;
4747 else {
4748 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004749 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004750 inode->i_fop = &btrfs_file_operations;
4751 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004752 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004753 }
Chris Mason39279cc2007-06-12 06:35:45 -04004754 btrfs_update_inode_block_group(trans, inode);
4755 btrfs_update_inode_block_group(trans, dir);
4756out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004757 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004758 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004759 if (drop_inode) {
4760 inode_dec_link_count(inode);
4761 iput(inode);
4762 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004763 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004764 return err;
4765}
4766
4767static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4768 struct dentry *dentry)
4769{
4770 struct btrfs_trans_handle *trans;
4771 struct btrfs_root *root = BTRFS_I(dir)->root;
4772 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004773 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004774 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004775 int err;
4776 int drop_inode = 0;
4777
4778 if (inode->i_nlink == 0)
4779 return -ENOENT;
4780
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004781 /* do not allow sys_link's with other subvols of the same device */
4782 if (root->objectid != BTRFS_I(inode)->root->objectid)
4783 return -EPERM;
4784
Josef Bacik9ed74f22009-09-11 16:12:44 -04004785 btrfs_inc_nlink(inode);
Josef Bacikbc1cbf12010-11-23 19:50:59 +00004786 inode->i_ctime = CURRENT_TIME;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004787
Chris Mason3de45862008-11-17 21:02:50 -05004788 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004789 if (err)
4790 goto fail;
4791
Yan, Zhenga22285a2010-05-16 10:48:46 -04004792 /*
4793 * 1 item for inode ref
4794 * 2 items for dir items
4795 */
4796 trans = btrfs_start_transaction(root, 3);
4797 if (IS_ERR(trans)) {
4798 err = PTR_ERR(trans);
4799 goto fail;
4800 }
Chris Mason5f39d392007-10-15 16:14:19 -04004801
Chris Mason39279cc2007-06-12 06:35:45 -04004802 btrfs_set_trans_block_group(trans, dir);
Al Viro7de9c6e2010-10-23 11:11:40 -04004803 ihold(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004804
Josef Bacika1b075d2010-11-19 20:36:11 +00004805 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004806
Yan, Zhenga5719522009-09-24 09:17:31 -04004807 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004808 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004809 } else {
Josef Bacik6a912212010-11-20 09:48:00 +00004810 struct dentry *parent = dget_parent(dentry);
Yan, Zhenga5719522009-09-24 09:17:31 -04004811 btrfs_update_inode_block_group(trans, dir);
4812 err = btrfs_update_inode(trans, root, inode);
4813 BUG_ON(err);
Josef Bacik6a912212010-11-20 09:48:00 +00004814 btrfs_log_new_name(trans, inode, NULL, parent);
4815 dput(parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004816 }
Chris Mason39279cc2007-06-12 06:35:45 -04004817
Chris Masond3c2fdc2007-09-17 10:58:06 -04004818 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004819 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004820fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004821 if (drop_inode) {
4822 inode_dec_link_count(inode);
4823 iput(inode);
4824 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004825 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004826 return err;
4827}
4828
Chris Mason39279cc2007-06-12 06:35:45 -04004829static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4830{
Chris Masonb9d86662008-05-02 16:13:49 -04004831 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004832 struct btrfs_trans_handle *trans;
4833 struct btrfs_root *root = BTRFS_I(dir)->root;
4834 int err = 0;
4835 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004836 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004837 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04004838 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004839
Yan, Zhenga22285a2010-05-16 10:48:46 -04004840 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4841 if (err)
4842 return err;
4843
Josef Bacik9ed74f22009-09-11 16:12:44 -04004844 /*
4845 * 2 items for inode and ref
4846 * 2 items for dir items
4847 * 1 for xattr if selinux is on
4848 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004849 trans = btrfs_start_transaction(root, 5);
4850 if (IS_ERR(trans))
4851 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004852 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004853
Josef Bacikaec74772008-07-24 12:12:38 -04004854 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004855 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004856 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4857 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004858 if (IS_ERR(inode)) {
4859 err = PTR_ERR(inode);
4860 goto out_fail;
4861 }
Chris Mason5f39d392007-10-15 16:14:19 -04004862
Chris Mason39279cc2007-06-12 06:35:45 -04004863 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004864
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004865 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004866 if (err)
4867 goto out_fail;
4868
Chris Mason39279cc2007-06-12 06:35:45 -04004869 inode->i_op = &btrfs_dir_inode_operations;
4870 inode->i_fop = &btrfs_dir_file_operations;
4871 btrfs_set_trans_block_group(trans, inode);
4872
Chris Masondbe674a2008-07-17 12:54:05 -04004873 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004874 err = btrfs_update_inode(trans, root, inode);
4875 if (err)
4876 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004877
Josef Bacika1b075d2010-11-19 20:36:11 +00004878 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4879 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004880 if (err)
4881 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004882
Chris Mason39279cc2007-06-12 06:35:45 -04004883 d_instantiate(dentry, inode);
4884 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004885 btrfs_update_inode_block_group(trans, inode);
4886 btrfs_update_inode_block_group(trans, dir);
4887
4888out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004889 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004890 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004891 if (drop_on_err)
4892 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04004893 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004894 return err;
4895}
4896
Chris Masond352ac62008-09-29 15:18:18 -04004897/* helper for btfs_get_extent. Given an existing extent in the tree,
4898 * and an extent that you want to insert, deal with overlap and insert
4899 * the new extent into the tree.
4900 */
Chris Mason3b951512008-04-17 11:29:12 -04004901static int merge_extent_mapping(struct extent_map_tree *em_tree,
4902 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004903 struct extent_map *em,
4904 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004905{
4906 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004907
Chris Masone6dcd2d2008-07-17 12:53:50 -04004908 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4909 start_diff = map_start - em->start;
4910 em->start = map_start;
4911 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004912 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4913 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004914 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004915 em->block_len -= start_diff;
4916 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004917 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004918}
4919
Chris Masonc8b97812008-10-29 14:49:59 -04004920static noinline int uncompress_inline(struct btrfs_path *path,
4921 struct inode *inode, struct page *page,
4922 size_t pg_offset, u64 extent_offset,
4923 struct btrfs_file_extent_item *item)
4924{
4925 int ret;
4926 struct extent_buffer *leaf = path->nodes[0];
4927 char *tmp;
4928 size_t max_size;
4929 unsigned long inline_size;
4930 unsigned long ptr;
4931
4932 WARN_ON(pg_offset != 0);
4933 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4934 inline_size = btrfs_file_extent_inline_item_len(leaf,
4935 btrfs_item_nr(leaf, path->slots[0]));
4936 tmp = kmalloc(inline_size, GFP_NOFS);
4937 ptr = btrfs_file_extent_inline_start(item);
4938
4939 read_extent_buffer(leaf, tmp, ptr, inline_size);
4940
Chris Mason5b050f02008-11-11 09:34:41 -05004941 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004942 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4943 inline_size, max_size);
4944 if (ret) {
4945 char *kaddr = kmap_atomic(page, KM_USER0);
4946 unsigned long copy_size = min_t(u64,
4947 PAGE_CACHE_SIZE - pg_offset,
4948 max_size - extent_offset);
4949 memset(kaddr + pg_offset, 0, copy_size);
4950 kunmap_atomic(kaddr, KM_USER0);
4951 }
4952 kfree(tmp);
4953 return 0;
4954}
4955
Chris Masond352ac62008-09-29 15:18:18 -04004956/*
4957 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004958 * the ugly parts come from merging extents from the disk with the in-ram
4959 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004960 * where the in-ram extents might be locked pending data=ordered completion.
4961 *
4962 * This also copies inline extents directly into the page.
4963 */
Chris Masond3977122009-01-05 21:25:51 -05004964
Chris Masona52d9a82007-08-27 16:49:44 -04004965struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004966 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004967 int create)
4968{
4969 int ret;
4970 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004971 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004972 u64 extent_start = 0;
4973 u64 extent_end = 0;
4974 u64 objectid = inode->i_ino;
4975 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004976 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004977 struct btrfs_root *root = BTRFS_I(inode)->root;
4978 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004979 struct extent_buffer *leaf;
4980 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004981 struct extent_map *em = NULL;
4982 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004983 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004984 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004985 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004986
Chris Masona52d9a82007-08-27 16:49:44 -04004987again:
Chris Mason890871b2009-09-02 16:24:52 -04004988 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004989 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004990 if (em)
4991 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004992 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004993
Chris Masona52d9a82007-08-27 16:49:44 -04004994 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004995 if (em->start > start || em->start + em->len <= start)
4996 free_extent_map(em);
4997 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004998 free_extent_map(em);
4999 else
5000 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005001 }
Chris Masond1310b22008-01-24 16:13:08 -05005002 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005003 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005004 err = -ENOMEM;
5005 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005006 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005007 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005008 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005009 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005010 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005011 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005012
5013 if (!path) {
5014 path = btrfs_alloc_path();
5015 BUG_ON(!path);
5016 }
5017
Chris Mason179e29e2007-11-01 11:28:41 -04005018 ret = btrfs_lookup_file_extent(trans, root, path,
5019 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005020 if (ret < 0) {
5021 err = ret;
5022 goto out;
5023 }
5024
5025 if (ret != 0) {
5026 if (path->slots[0] == 0)
5027 goto not_found;
5028 path->slots[0]--;
5029 }
5030
Chris Mason5f39d392007-10-15 16:14:19 -04005031 leaf = path->nodes[0];
5032 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005033 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005034 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005035 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5036 found_type = btrfs_key_type(&found_key);
5037 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005038 found_type != BTRFS_EXTENT_DATA_KEY) {
5039 goto not_found;
5040 }
5041
Chris Mason5f39d392007-10-15 16:14:19 -04005042 found_type = btrfs_file_extent_type(leaf, item);
5043 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005044 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005045 if (found_type == BTRFS_FILE_EXTENT_REG ||
5046 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005047 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005048 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005049 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5050 size_t size;
5051 size = btrfs_file_extent_inline_len(leaf, item);
5052 extent_end = (extent_start + size + root->sectorsize - 1) &
5053 ~((u64)root->sectorsize - 1);
5054 }
5055
5056 if (start >= extent_end) {
5057 path->slots[0]++;
5058 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5059 ret = btrfs_next_leaf(root, path);
5060 if (ret < 0) {
5061 err = ret;
5062 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005063 }
Yan Zheng9036c102008-10-30 14:19:41 -04005064 if (ret > 0)
5065 goto not_found;
5066 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005067 }
Yan Zheng9036c102008-10-30 14:19:41 -04005068 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5069 if (found_key.objectid != objectid ||
5070 found_key.type != BTRFS_EXTENT_DATA_KEY)
5071 goto not_found;
5072 if (start + len <= found_key.offset)
5073 goto not_found;
5074 em->start = start;
5075 em->len = found_key.offset - start;
5076 goto not_found_em;
5077 }
5078
Yan Zhengd899e052008-10-30 14:25:28 -04005079 if (found_type == BTRFS_FILE_EXTENT_REG ||
5080 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005081 em->start = extent_start;
5082 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005083 em->orig_start = extent_start -
5084 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005085 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5086 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005087 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005088 goto insert;
5089 }
Chris Masonc8b97812008-10-29 14:49:59 -04005090 if (compressed) {
5091 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5092 em->block_start = bytenr;
5093 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5094 item);
5095 } else {
5096 bytenr += btrfs_file_extent_offset(leaf, item);
5097 em->block_start = bytenr;
5098 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005099 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5100 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005101 }
Chris Masona52d9a82007-08-27 16:49:44 -04005102 goto insert;
5103 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005104 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005105 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005106 size_t size;
5107 size_t extent_offset;
5108 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005109
Chris Masona52d9a82007-08-27 16:49:44 -04005110 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005111 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005112 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005113 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005114 goto out;
5115 }
5116
Yan Zheng9036c102008-10-30 14:19:41 -04005117 size = btrfs_file_extent_inline_len(leaf, item);
5118 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005119 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005120 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005121 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005122 em->len = (copy_size + root->sectorsize - 1) &
5123 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005124 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005125 if (compressed)
5126 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04005127 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005128 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04005129 if (btrfs_file_extent_compression(leaf, item) ==
5130 BTRFS_COMPRESS_ZLIB) {
5131 ret = uncompress_inline(path, inode, page,
5132 pg_offset,
5133 extent_offset, item);
5134 BUG_ON(ret);
5135 } else {
5136 map = kmap(page);
5137 read_extent_buffer(leaf, map + pg_offset, ptr,
5138 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005139 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5140 memset(map + pg_offset + copy_size, 0,
5141 PAGE_CACHE_SIZE - pg_offset -
5142 copy_size);
5143 }
Chris Masonc8b97812008-10-29 14:49:59 -04005144 kunmap(page);
5145 }
Chris Mason179e29e2007-11-01 11:28:41 -04005146 flush_dcache_page(page);
5147 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005148 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005149 if (!trans) {
5150 kunmap(page);
5151 free_extent_map(em);
5152 em = NULL;
5153 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04005154 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04005155 goto again;
5156 }
Chris Masonc8b97812008-10-29 14:49:59 -04005157 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005158 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005159 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005160 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005161 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005162 }
Chris Masond1310b22008-01-24 16:13:08 -05005163 set_extent_uptodate(io_tree, em->start,
5164 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005165 goto insert;
5166 } else {
Chris Masond3977122009-01-05 21:25:51 -05005167 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005168 WARN_ON(1);
5169 }
5170not_found:
5171 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005172 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005173not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005174 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005175 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005176insert:
5177 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05005178 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005179 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5180 "[%llu %llu]\n", (unsigned long long)em->start,
5181 (unsigned long long)em->len,
5182 (unsigned long long)start,
5183 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005184 err = -EIO;
5185 goto out;
5186 }
Chris Masond1310b22008-01-24 16:13:08 -05005187
5188 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005189 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005190 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005191 /* it is possible that someone inserted the extent into the tree
5192 * while we had the lock dropped. It is also possible that
5193 * an overlapping map exists in the tree
5194 */
Chris Masona52d9a82007-08-27 16:49:44 -04005195 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005196 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005197
5198 ret = 0;
5199
Chris Mason3b951512008-04-17 11:29:12 -04005200 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005201 if (existing && (existing->start > start ||
5202 existing->start + existing->len <= start)) {
5203 free_extent_map(existing);
5204 existing = NULL;
5205 }
Chris Mason3b951512008-04-17 11:29:12 -04005206 if (!existing) {
5207 existing = lookup_extent_mapping(em_tree, em->start,
5208 em->len);
5209 if (existing) {
5210 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005211 em, start,
5212 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005213 free_extent_map(existing);
5214 if (err) {
5215 free_extent_map(em);
5216 em = NULL;
5217 }
5218 } else {
5219 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005220 free_extent_map(em);
5221 em = NULL;
5222 }
5223 } else {
5224 free_extent_map(em);
5225 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005226 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005227 }
Chris Masona52d9a82007-08-27 16:49:44 -04005228 }
Chris Mason890871b2009-09-02 16:24:52 -04005229 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005230out:
Chris Masonf4219502008-07-22 11:18:09 -04005231 if (path)
5232 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005233 if (trans) {
5234 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005235 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005236 err = ret;
5237 }
Chris Masona52d9a82007-08-27 16:49:44 -04005238 if (err) {
5239 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005240 return ERR_PTR(err);
5241 }
5242 return em;
5243}
5244
Josef Bacik4b46fce2010-05-23 11:00:55 -04005245static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5246 u64 start, u64 len)
5247{
5248 struct btrfs_root *root = BTRFS_I(inode)->root;
5249 struct btrfs_trans_handle *trans;
5250 struct extent_map *em;
5251 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5252 struct btrfs_key ins;
5253 u64 alloc_hint;
5254 int ret;
5255
5256 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5257
5258 trans = btrfs_join_transaction(root, 0);
5259 if (!trans)
5260 return ERR_PTR(-ENOMEM);
5261
5262 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5263
5264 alloc_hint = get_extent_allocation_hint(inode, start, len);
5265 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5266 alloc_hint, (u64)-1, &ins, 1);
5267 if (ret) {
5268 em = ERR_PTR(ret);
5269 goto out;
5270 }
5271
5272 em = alloc_extent_map(GFP_NOFS);
5273 if (!em) {
5274 em = ERR_PTR(-ENOMEM);
5275 goto out;
5276 }
5277
5278 em->start = start;
5279 em->orig_start = em->start;
5280 em->len = ins.offset;
5281
5282 em->block_start = ins.objectid;
5283 em->block_len = ins.offset;
5284 em->bdev = root->fs_info->fs_devices->latest_bdev;
5285 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5286
5287 while (1) {
5288 write_lock(&em_tree->lock);
5289 ret = add_extent_mapping(em_tree, em);
5290 write_unlock(&em_tree->lock);
5291 if (ret != -EEXIST)
5292 break;
5293 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5294 }
5295
5296 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5297 ins.offset, ins.offset, 0);
5298 if (ret) {
5299 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5300 em = ERR_PTR(ret);
5301 }
5302out:
5303 btrfs_end_transaction(trans, root);
5304 return em;
5305}
5306
Chris Mason46bfbb52010-05-26 11:04:10 -04005307/*
5308 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5309 * block must be cow'd
5310 */
5311static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5312 struct inode *inode, u64 offset, u64 len)
5313{
5314 struct btrfs_path *path;
5315 int ret;
5316 struct extent_buffer *leaf;
5317 struct btrfs_root *root = BTRFS_I(inode)->root;
5318 struct btrfs_file_extent_item *fi;
5319 struct btrfs_key key;
5320 u64 disk_bytenr;
5321 u64 backref_offset;
5322 u64 extent_end;
5323 u64 num_bytes;
5324 int slot;
5325 int found_type;
5326
5327 path = btrfs_alloc_path();
5328 if (!path)
5329 return -ENOMEM;
5330
5331 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
5332 offset, 0);
5333 if (ret < 0)
5334 goto out;
5335
5336 slot = path->slots[0];
5337 if (ret == 1) {
5338 if (slot == 0) {
5339 /* can't find the item, must cow */
5340 ret = 0;
5341 goto out;
5342 }
5343 slot--;
5344 }
5345 ret = 0;
5346 leaf = path->nodes[0];
5347 btrfs_item_key_to_cpu(leaf, &key, slot);
5348 if (key.objectid != inode->i_ino ||
5349 key.type != BTRFS_EXTENT_DATA_KEY) {
5350 /* not our file or wrong item type, must cow */
5351 goto out;
5352 }
5353
5354 if (key.offset > offset) {
5355 /* Wrong offset, must cow */
5356 goto out;
5357 }
5358
5359 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5360 found_type = btrfs_file_extent_type(leaf, fi);
5361 if (found_type != BTRFS_FILE_EXTENT_REG &&
5362 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5363 /* not a regular extent, must cow */
5364 goto out;
5365 }
5366 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5367 backref_offset = btrfs_file_extent_offset(leaf, fi);
5368
5369 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5370 if (extent_end < offset + len) {
5371 /* extent doesn't include our full range, must cow */
5372 goto out;
5373 }
5374
5375 if (btrfs_extent_readonly(root, disk_bytenr))
5376 goto out;
5377
5378 /*
5379 * look for other files referencing this extent, if we
5380 * find any we must cow
5381 */
5382 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
5383 key.offset - backref_offset, disk_bytenr))
5384 goto out;
5385
5386 /*
5387 * adjust disk_bytenr and num_bytes to cover just the bytes
5388 * in this extent we are about to write. If there
5389 * are any csums in that range we have to cow in order
5390 * to keep the csums correct
5391 */
5392 disk_bytenr += backref_offset;
5393 disk_bytenr += offset - key.offset;
5394 num_bytes = min(offset + len, extent_end) - offset;
5395 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5396 goto out;
5397 /*
5398 * all of the above have passed, it is safe to overwrite this extent
5399 * without cow
5400 */
5401 ret = 1;
5402out:
5403 btrfs_free_path(path);
5404 return ret;
5405}
5406
Josef Bacik4b46fce2010-05-23 11:00:55 -04005407static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5408 struct buffer_head *bh_result, int create)
5409{
5410 struct extent_map *em;
5411 struct btrfs_root *root = BTRFS_I(inode)->root;
5412 u64 start = iblock << inode->i_blkbits;
5413 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005414 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005415
5416 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5417 if (IS_ERR(em))
5418 return PTR_ERR(em);
5419
5420 /*
5421 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5422 * io. INLINE is special, and we could probably kludge it in here, but
5423 * it's still buffered so for safety lets just fall back to the generic
5424 * buffered path.
5425 *
5426 * For COMPRESSED we _have_ to read the entire extent in so we can
5427 * decompress it, so there will be buffering required no matter what we
5428 * do, so go ahead and fallback to buffered.
5429 *
5430 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5431 * to buffered IO. Don't blame me, this is the price we pay for using
5432 * the generic code.
5433 */
5434 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5435 em->block_start == EXTENT_MAP_INLINE) {
5436 free_extent_map(em);
5437 return -ENOTBLK;
5438 }
5439
5440 /* Just a good old fashioned hole, return */
5441 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5442 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5443 free_extent_map(em);
5444 /* DIO will do one hole at a time, so just unlock a sector */
5445 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5446 start + root->sectorsize - 1, GFP_NOFS);
5447 return 0;
5448 }
5449
5450 /*
5451 * We don't allocate a new extent in the following cases
5452 *
5453 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5454 * existing extent.
5455 * 2) The extent is marked as PREALLOC. We're good to go here and can
5456 * just use the extent.
5457 *
5458 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005459 if (!create) {
5460 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005461 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005462 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005463
5464 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5465 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5466 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005467 int type;
5468 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005469 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005470
5471 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5472 type = BTRFS_ORDERED_PREALLOC;
5473 else
5474 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005475 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005476 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005477
5478 /*
5479 * we're not going to log anything, but we do need
5480 * to make sure the current transaction stays open
5481 * while we look for nocow cross refs
5482 */
5483 trans = btrfs_join_transaction(root, 0);
5484 if (!trans)
5485 goto must_cow;
5486
5487 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5488 ret = btrfs_add_ordered_extent_dio(inode, start,
5489 block_start, len, len, type);
5490 btrfs_end_transaction(trans, root);
5491 if (ret) {
5492 free_extent_map(em);
5493 return ret;
5494 }
5495 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005496 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005497 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005498 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005499must_cow:
5500 /*
5501 * this will cow the extent, reset the len in case we changed
5502 * it above
5503 */
5504 len = bh_result->b_size;
5505 free_extent_map(em);
5506 em = btrfs_new_extent_direct(inode, start, len);
5507 if (IS_ERR(em))
5508 return PTR_ERR(em);
5509 len = min(len, em->len - (start - em->start));
5510unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005511 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5512 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5513 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005514map:
5515 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5516 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005517 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005518 bh_result->b_bdev = em->bdev;
5519 set_buffer_mapped(bh_result);
5520 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5521 set_buffer_new(bh_result);
5522
5523 free_extent_map(em);
5524
5525 return 0;
5526}
5527
5528struct btrfs_dio_private {
5529 struct inode *inode;
5530 u64 logical_offset;
5531 u64 disk_bytenr;
5532 u64 bytes;
5533 u32 *csums;
5534 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005535
5536 /* number of bios pending for this dio */
5537 atomic_t pending_bios;
5538
5539 /* IO errors */
5540 int errors;
5541
5542 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005543};
5544
5545static void btrfs_endio_direct_read(struct bio *bio, int err)
5546{
Miao Xiee65e1532010-11-22 03:04:43 +00005547 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005548 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5549 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005550 struct inode *inode = dip->inode;
5551 struct btrfs_root *root = BTRFS_I(inode)->root;
5552 u64 start;
5553 u32 *private = dip->csums;
5554
5555 start = dip->logical_offset;
5556 do {
5557 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5558 struct page *page = bvec->bv_page;
5559 char *kaddr;
5560 u32 csum = ~(u32)0;
5561 unsigned long flags;
5562
5563 local_irq_save(flags);
5564 kaddr = kmap_atomic(page, KM_IRQ0);
5565 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5566 csum, bvec->bv_len);
5567 btrfs_csum_final(csum, (char *)&csum);
5568 kunmap_atomic(kaddr, KM_IRQ0);
5569 local_irq_restore(flags);
5570
5571 flush_dcache_page(bvec->bv_page);
5572 if (csum != *private) {
5573 printk(KERN_ERR "btrfs csum failed ino %lu off"
5574 " %llu csum %u private %u\n",
5575 inode->i_ino, (unsigned long long)start,
5576 csum, *private);
5577 err = -EIO;
5578 }
5579 }
5580
5581 start += bvec->bv_len;
5582 private++;
5583 bvec++;
5584 } while (bvec <= bvec_end);
5585
5586 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5587 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5588 bio->bi_private = dip->private;
5589
5590 kfree(dip->csums);
5591 kfree(dip);
5592 dio_end_io(bio, err);
5593}
5594
5595static void btrfs_endio_direct_write(struct bio *bio, int err)
5596{
5597 struct btrfs_dio_private *dip = bio->bi_private;
5598 struct inode *inode = dip->inode;
5599 struct btrfs_root *root = BTRFS_I(inode)->root;
5600 struct btrfs_trans_handle *trans;
5601 struct btrfs_ordered_extent *ordered = NULL;
5602 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005603 u64 ordered_offset = dip->logical_offset;
5604 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005605 int ret;
5606
5607 if (err)
5608 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005609again:
5610 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5611 &ordered_offset,
5612 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005613 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005614 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005615
5616 BUG_ON(!ordered);
5617
5618 trans = btrfs_join_transaction(root, 1);
5619 if (!trans) {
5620 err = -ENOMEM;
5621 goto out;
5622 }
5623 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5624
5625 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5626 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5627 if (!ret)
5628 ret = btrfs_update_inode(trans, root, inode);
5629 err = ret;
5630 goto out;
5631 }
5632
5633 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5634 ordered->file_offset + ordered->len - 1, 0,
5635 &cached_state, GFP_NOFS);
5636
5637 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5638 ret = btrfs_mark_extent_written(trans, inode,
5639 ordered->file_offset,
5640 ordered->file_offset +
5641 ordered->len);
5642 if (ret) {
5643 err = ret;
5644 goto out_unlock;
5645 }
5646 } else {
5647 ret = insert_reserved_file_extent(trans, inode,
5648 ordered->file_offset,
5649 ordered->start,
5650 ordered->disk_len,
5651 ordered->len,
5652 ordered->len,
5653 0, 0, 0,
5654 BTRFS_FILE_EXTENT_REG);
5655 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5656 ordered->file_offset, ordered->len);
5657 if (ret) {
5658 err = ret;
5659 WARN_ON(1);
5660 goto out_unlock;
5661 }
5662 }
5663
5664 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5665 btrfs_ordered_update_i_size(inode, 0, ordered);
5666 btrfs_update_inode(trans, root, inode);
5667out_unlock:
5668 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5669 ordered->file_offset + ordered->len - 1,
5670 &cached_state, GFP_NOFS);
5671out:
5672 btrfs_delalloc_release_metadata(inode, ordered->len);
5673 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005674 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005675 btrfs_put_ordered_extent(ordered);
5676 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05005677
5678out_test:
5679 /*
5680 * our bio might span multiple ordered extents. If we haven't
5681 * completed the accounting for the whole dio, go back and try again
5682 */
5683 if (ordered_offset < dip->logical_offset + dip->bytes) {
5684 ordered_bytes = dip->logical_offset + dip->bytes -
5685 ordered_offset;
5686 goto again;
5687 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005688out_done:
5689 bio->bi_private = dip->private;
5690
5691 kfree(dip->csums);
5692 kfree(dip);
5693 dio_end_io(bio, err);
5694}
5695
Chris Masoneaf25d92010-05-25 09:48:28 -04005696static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5697 struct bio *bio, int mirror_num,
5698 unsigned long bio_flags, u64 offset)
5699{
5700 int ret;
5701 struct btrfs_root *root = BTRFS_I(inode)->root;
5702 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5703 BUG_ON(ret);
5704 return 0;
5705}
5706
Miao Xiee65e1532010-11-22 03:04:43 +00005707static void btrfs_end_dio_bio(struct bio *bio, int err)
5708{
5709 struct btrfs_dio_private *dip = bio->bi_private;
5710
5711 if (err) {
5712 printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00005713 "sector %#Lx len %u err no %d\n",
5714 dip->inode->i_ino, bio->bi_rw,
5715 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00005716 dip->errors = 1;
5717
5718 /*
5719 * before atomic variable goto zero, we must make sure
5720 * dip->errors is perceived to be set.
5721 */
5722 smp_mb__before_atomic_dec();
5723 }
5724
5725 /* if there are more bios still pending for this dio, just exit */
5726 if (!atomic_dec_and_test(&dip->pending_bios))
5727 goto out;
5728
5729 if (dip->errors)
5730 bio_io_error(dip->orig_bio);
5731 else {
5732 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
5733 bio_endio(dip->orig_bio, 0);
5734 }
5735out:
5736 bio_put(bio);
5737}
5738
5739static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
5740 u64 first_sector, gfp_t gfp_flags)
5741{
5742 int nr_vecs = bio_get_nr_vecs(bdev);
5743 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
5744}
5745
5746static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
5747 int rw, u64 file_offset, int skip_sum,
5748 u32 *csums)
5749{
5750 int write = rw & REQ_WRITE;
5751 struct btrfs_root *root = BTRFS_I(inode)->root;
5752 int ret;
5753
5754 bio_get(bio);
5755 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
5756 if (ret)
5757 goto err;
5758
5759 if (write && !skip_sum) {
5760 ret = btrfs_wq_submit_bio(root->fs_info,
5761 inode, rw, bio, 0, 0,
5762 file_offset,
5763 __btrfs_submit_bio_start_direct_io,
5764 __btrfs_submit_bio_done);
5765 goto err;
5766 } else if (!skip_sum)
5767 btrfs_lookup_bio_sums_dio(root, inode, bio,
5768 file_offset, csums);
5769
5770 ret = btrfs_map_bio(root, rw, bio, 0, 1);
5771err:
5772 bio_put(bio);
5773 return ret;
5774}
5775
5776static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
5777 int skip_sum)
5778{
5779 struct inode *inode = dip->inode;
5780 struct btrfs_root *root = BTRFS_I(inode)->root;
5781 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5782 struct bio *bio;
5783 struct bio *orig_bio = dip->orig_bio;
5784 struct bio_vec *bvec = orig_bio->bi_io_vec;
5785 u64 start_sector = orig_bio->bi_sector;
5786 u64 file_offset = dip->logical_offset;
5787 u64 submit_len = 0;
5788 u64 map_length;
5789 int nr_pages = 0;
5790 u32 *csums = dip->csums;
5791 int ret = 0;
5792
5793 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
5794 if (!bio)
5795 return -ENOMEM;
5796 bio->bi_private = dip;
5797 bio->bi_end_io = btrfs_end_dio_bio;
5798 atomic_inc(&dip->pending_bios);
5799
5800 map_length = orig_bio->bi_size;
5801 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5802 &map_length, NULL, 0);
5803 if (ret) {
5804 bio_put(bio);
5805 return -EIO;
5806 }
5807
5808 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
5809 if (unlikely(map_length < submit_len + bvec->bv_len ||
5810 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5811 bvec->bv_offset) < bvec->bv_len)) {
5812 /*
5813 * inc the count before we submit the bio so
5814 * we know the end IO handler won't happen before
5815 * we inc the count. Otherwise, the dip might get freed
5816 * before we're done setting it up
5817 */
5818 atomic_inc(&dip->pending_bios);
5819 ret = __btrfs_submit_dio_bio(bio, inode, rw,
5820 file_offset, skip_sum,
5821 csums);
5822 if (ret) {
5823 bio_put(bio);
5824 atomic_dec(&dip->pending_bios);
5825 goto out_err;
5826 }
5827
5828 if (!skip_sum)
5829 csums = csums + nr_pages;
5830 start_sector += submit_len >> 9;
5831 file_offset += submit_len;
5832
5833 submit_len = 0;
5834 nr_pages = 0;
5835
5836 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
5837 start_sector, GFP_NOFS);
5838 if (!bio)
5839 goto out_err;
5840 bio->bi_private = dip;
5841 bio->bi_end_io = btrfs_end_dio_bio;
5842
5843 map_length = orig_bio->bi_size;
5844 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5845 &map_length, NULL, 0);
5846 if (ret) {
5847 bio_put(bio);
5848 goto out_err;
5849 }
5850 } else {
5851 submit_len += bvec->bv_len;
5852 nr_pages ++;
5853 bvec++;
5854 }
5855 }
5856
5857 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
5858 csums);
5859 if (!ret)
5860 return 0;
5861
5862 bio_put(bio);
5863out_err:
5864 dip->errors = 1;
5865 /*
5866 * before atomic variable goto zero, we must
5867 * make sure dip->errors is perceived to be set.
5868 */
5869 smp_mb__before_atomic_dec();
5870 if (atomic_dec_and_test(&dip->pending_bios))
5871 bio_io_error(dip->orig_bio);
5872
5873 /* bio_end_io() will handle error, so we needn't return it */
5874 return 0;
5875}
5876
Josef Bacik4b46fce2010-05-23 11:00:55 -04005877static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
5878 loff_t file_offset)
5879{
5880 struct btrfs_root *root = BTRFS_I(inode)->root;
5881 struct btrfs_dio_private *dip;
5882 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005883 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005884 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005885 int ret = 0;
5886
5887 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
5888
5889 dip = kmalloc(sizeof(*dip), GFP_NOFS);
5890 if (!dip) {
5891 ret = -ENOMEM;
5892 goto free_ordered;
5893 }
5894 dip->csums = NULL;
5895
5896 if (!skip_sum) {
5897 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
5898 if (!dip->csums) {
5899 ret = -ENOMEM;
5900 goto free_ordered;
5901 }
5902 }
5903
5904 dip->private = bio->bi_private;
5905 dip->inode = inode;
5906 dip->logical_offset = file_offset;
5907
Josef Bacik4b46fce2010-05-23 11:00:55 -04005908 dip->bytes = 0;
5909 do {
5910 dip->bytes += bvec->bv_len;
5911 bvec++;
5912 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
5913
Chris Mason46bfbb52010-05-26 11:04:10 -04005914 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005915 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00005916 dip->errors = 0;
5917 dip->orig_bio = bio;
5918 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005919
5920 if (write)
5921 bio->bi_end_io = btrfs_endio_direct_write;
5922 else
5923 bio->bi_end_io = btrfs_endio_direct_read;
5924
Miao Xiee65e1532010-11-22 03:04:43 +00005925 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
5926 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04005927 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005928free_ordered:
5929 /*
5930 * If this is a write, we need to clean up the reserved space and kill
5931 * the ordered extent.
5932 */
5933 if (write) {
5934 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05005935 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005936 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
5937 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
5938 btrfs_free_reserved_extent(root, ordered->start,
5939 ordered->disk_len);
5940 btrfs_put_ordered_extent(ordered);
5941 btrfs_put_ordered_extent(ordered);
5942 }
5943 bio_endio(bio, ret);
5944}
5945
Chris Mason5a5f79b2010-05-26 21:33:37 -04005946static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
5947 const struct iovec *iov, loff_t offset,
5948 unsigned long nr_segs)
5949{
5950 int seg;
5951 size_t size;
5952 unsigned long addr;
5953 unsigned blocksize_mask = root->sectorsize - 1;
5954 ssize_t retval = -EINVAL;
5955 loff_t end = offset;
5956
5957 if (offset & blocksize_mask)
5958 goto out;
5959
5960 /* Check the memory alignment. Blocks cannot straddle pages */
5961 for (seg = 0; seg < nr_segs; seg++) {
5962 addr = (unsigned long)iov[seg].iov_base;
5963 size = iov[seg].iov_len;
5964 end += size;
5965 if ((addr & blocksize_mask) || (size & blocksize_mask))
5966 goto out;
5967 }
5968 retval = 0;
5969out:
5970 return retval;
5971}
Chris Mason16432982008-04-10 10:23:21 -04005972static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
5973 const struct iovec *iov, loff_t offset,
5974 unsigned long nr_segs)
5975{
Josef Bacik4b46fce2010-05-23 11:00:55 -04005976 struct file *file = iocb->ki_filp;
5977 struct inode *inode = file->f_mapping->host;
5978 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04005979 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005980 u64 lockstart, lockend;
5981 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04005982 int writing = rw & WRITE;
5983 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04005984 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005985
Chris Mason5a5f79b2010-05-26 21:33:37 -04005986 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
5987 offset, nr_segs)) {
5988 return 0;
5989 }
5990
Josef Bacik4b46fce2010-05-23 11:00:55 -04005991 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04005992 lockend = offset + count - 1;
5993
5994 if (writing) {
5995 ret = btrfs_delalloc_reserve_space(inode, count);
5996 if (ret)
5997 goto out;
5998 }
Chris Mason4845e442010-05-25 20:56:50 -04005999
Josef Bacik4b46fce2010-05-23 11:00:55 -04006000 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006001 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6002 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006003 /*
6004 * We're concerned with the entire range that we're going to be
6005 * doing DIO to, so we need to make sure theres no ordered
6006 * extents in this range.
6007 */
6008 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6009 lockend - lockstart + 1);
6010 if (!ordered)
6011 break;
Chris Mason4845e442010-05-25 20:56:50 -04006012 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6013 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006014 btrfs_start_ordered_extent(inode, ordered, 1);
6015 btrfs_put_ordered_extent(ordered);
6016 cond_resched();
6017 }
6018
Chris Mason4845e442010-05-25 20:56:50 -04006019 /*
6020 * we don't use btrfs_set_extent_delalloc because we don't want
6021 * the dirty or uptodate bits
6022 */
6023 if (writing) {
6024 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6025 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6026 EXTENT_DELALLOC, 0, NULL, &cached_state,
6027 GFP_NOFS);
6028 if (ret) {
6029 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6030 lockend, EXTENT_LOCKED | write_bits,
6031 1, 0, &cached_state, GFP_NOFS);
6032 goto out;
6033 }
6034 }
6035
6036 free_extent_state(cached_state);
6037 cached_state = NULL;
6038
Chris Mason5a5f79b2010-05-26 21:33:37 -04006039 ret = __blockdev_direct_IO(rw, iocb, inode,
6040 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6041 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6042 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006043
6044 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006045 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6046 offset + iov_length(iov, nr_segs) - 1,
6047 EXTENT_LOCKED | write_bits, 1, 0,
6048 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006049 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6050 /*
6051 * We're falling back to buffered, unlock the section we didn't
6052 * do IO on.
6053 */
Chris Mason4845e442010-05-25 20:56:50 -04006054 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6055 offset + iov_length(iov, nr_segs) - 1,
6056 EXTENT_LOCKED | write_bits, 1, 0,
6057 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006058 }
Chris Mason4845e442010-05-25 20:56:50 -04006059out:
6060 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006061 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006062}
6063
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006064static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6065 __u64 start, __u64 len)
6066{
6067 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
6068}
6069
Chris Mason9ebefb182007-06-15 13:50:00 -04006070int btrfs_readpage(struct file *file, struct page *page)
6071{
Chris Masond1310b22008-01-24 16:13:08 -05006072 struct extent_io_tree *tree;
6073 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006074 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04006075}
Chris Mason1832a6d2007-12-21 16:27:21 -05006076
Chris Mason39279cc2007-06-12 06:35:45 -04006077static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6078{
Chris Masond1310b22008-01-24 16:13:08 -05006079 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006080
6081
6082 if (current->flags & PF_MEMALLOC) {
6083 redirty_page_for_writepage(wbc, page);
6084 unlock_page(page);
6085 return 0;
6086 }
Chris Masond1310b22008-01-24 16:13:08 -05006087 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006088 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6089}
Chris Mason39279cc2007-06-12 06:35:45 -04006090
Chris Masonf4219502008-07-22 11:18:09 -04006091int btrfs_writepages(struct address_space *mapping,
6092 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006093{
Chris Masond1310b22008-01-24 16:13:08 -05006094 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006095
Chris Masond1310b22008-01-24 16:13:08 -05006096 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006097 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6098}
6099
Chris Mason3ab2fb52007-11-08 10:59:22 -05006100static int
6101btrfs_readpages(struct file *file, struct address_space *mapping,
6102 struct list_head *pages, unsigned nr_pages)
6103{
Chris Masond1310b22008-01-24 16:13:08 -05006104 struct extent_io_tree *tree;
6105 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006106 return extent_readpages(tree, mapping, pages, nr_pages,
6107 btrfs_get_extent);
6108}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006109static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006110{
Chris Masond1310b22008-01-24 16:13:08 -05006111 struct extent_io_tree *tree;
6112 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006113 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006114
Chris Masond1310b22008-01-24 16:13:08 -05006115 tree = &BTRFS_I(page->mapping->host)->io_tree;
6116 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006117 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006118 if (ret == 1) {
6119 ClearPagePrivate(page);
6120 set_page_private(page, 0);
6121 page_cache_release(page);
6122 }
6123 return ret;
6124}
Chris Mason39279cc2007-06-12 06:35:45 -04006125
Chris Masone6dcd2d2008-07-17 12:53:50 -04006126static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6127{
Chris Mason98509cf2008-09-11 15:51:43 -04006128 if (PageWriteback(page) || PageDirty(page))
6129 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006130 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006131}
6132
Chris Masona52d9a82007-08-27 16:49:44 -04006133static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6134{
Chris Masond1310b22008-01-24 16:13:08 -05006135 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006136 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006137 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006138 u64 page_start = page_offset(page);
6139 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006140
Chris Mason8b62b722009-09-02 16:53:46 -04006141
6142 /*
6143 * we have the page locked, so new writeback can't start,
6144 * and the dirty bit won't be cleared while we are here.
6145 *
6146 * Wait for IO on this page so that we can safely clear
6147 * the PagePrivate2 bit and do ordered accounting
6148 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006149 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006150
Chris Masond1310b22008-01-24 16:13:08 -05006151 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006152 if (offset) {
6153 btrfs_releasepage(page, GFP_NOFS);
6154 return;
6155 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006156 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6157 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006158 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6159 page_offset(page));
6160 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006161 /*
6162 * IO on this page will never be started, so we need
6163 * to account for any ordered extents now
6164 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006165 clear_extent_bit(tree, page_start, page_end,
6166 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006167 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006168 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006169 /*
6170 * whoever cleared the private bit is responsible
6171 * for the finish_ordered_io
6172 */
6173 if (TestClearPagePrivate2(page)) {
6174 btrfs_finish_ordered_io(page->mapping->host,
6175 page_start, page_end);
6176 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006177 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006178 cached_state = NULL;
6179 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6180 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006181 }
6182 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006183 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006184 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006185 __btrfs_releasepage(page, GFP_NOFS);
6186
Chris Mason4a096752008-07-21 10:29:44 -04006187 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006188 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006189 ClearPagePrivate(page);
6190 set_page_private(page, 0);
6191 page_cache_release(page);
6192 }
Chris Mason39279cc2007-06-12 06:35:45 -04006193}
6194
Chris Mason9ebefb182007-06-15 13:50:00 -04006195/*
6196 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6197 * called from a page fault handler when a page is first dirtied. Hence we must
6198 * be careful to check for EOF conditions here. We set the page up correctly
6199 * for a written page which means we get ENOSPC checking when writing into
6200 * holes and correct delalloc and unwritten extent mapping on filesystems that
6201 * support these features.
6202 *
6203 * We are not allowed to take the i_mutex here so we have to play games to
6204 * protect against truncate races as the page could now be beyond EOF. Because
6205 * vmtruncate() writes the inode size before removing pages, once we have the
6206 * page lock we can determine safely if the page is beyond EOF. If it is not
6207 * beyond EOF, then the page is guaranteed safe against truncation until we
6208 * unlock the page.
6209 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006210int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006211{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006212 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006213 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006214 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006215 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6216 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006217 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006218 char *kaddr;
6219 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006220 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006221 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04006222 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006223 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006224
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006225 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07006226 if (ret) {
6227 if (ret == -ENOMEM)
6228 ret = VM_FAULT_OOM;
6229 else /* -ENOSPC, -EIO, etc */
6230 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05006231 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07006232 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006233
Nick Piggin56a76f82009-03-31 15:23:23 -07006234 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006235again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006236 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006237 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006238 page_start = page_offset(page);
6239 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006240
Chris Mason9ebefb182007-06-15 13:50:00 -04006241 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006242 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006243 /* page got truncated out from underneath us */
6244 goto out_unlock;
6245 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006246 wait_on_page_writeback(page);
6247
Josef Bacik2ac55d42010-02-03 19:33:23 +00006248 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6249 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006250 set_page_extent_mapped(page);
6251
Chris Masoneb84ae02008-07-17 13:53:27 -04006252 /*
6253 * we can't set the delalloc bits if there are pending ordered
6254 * extents. Drop our locks and wait for them to finish
6255 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006256 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6257 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006258 unlock_extent_cached(io_tree, page_start, page_end,
6259 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006260 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006261 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006262 btrfs_put_ordered_extent(ordered);
6263 goto again;
6264 }
6265
Josef Bacikfbf19082009-10-01 17:10:23 -04006266 /*
6267 * XXX - page_mkwrite gets called every time the page is dirtied, even
6268 * if it was already dirty, so for space accounting reasons we need to
6269 * clear any delalloc bits for the range we are fixing to save. There
6270 * is probably a better way to do this, but for now keep consistent with
6271 * prepare_pages in the normal write path.
6272 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006273 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006274 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006275 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006276
Josef Bacik2ac55d42010-02-03 19:33:23 +00006277 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6278 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006279 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006280 unlock_extent_cached(io_tree, page_start, page_end,
6281 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006282 ret = VM_FAULT_SIGBUS;
6283 goto out_unlock;
6284 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006285 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006286
6287 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006288 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006289 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006290 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006291 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006292
Chris Masone6dcd2d2008-07-17 12:53:50 -04006293 if (zero_start != PAGE_CACHE_SIZE) {
6294 kaddr = kmap(page);
6295 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6296 flush_dcache_page(page);
6297 kunmap(page);
6298 }
Chris Mason247e7432008-07-17 12:53:51 -04006299 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006300 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006301 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006302
Chris Mason257c62e2009-10-13 13:21:08 -04006303 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6304 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6305
Josef Bacik2ac55d42010-02-03 19:33:23 +00006306 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006307
6308out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006309 if (!ret)
6310 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006311 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006312 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05006313out:
Chris Mason9ebefb182007-06-15 13:50:00 -04006314 return ret;
6315}
6316
Chris Mason39279cc2007-06-12 06:35:45 -04006317static void btrfs_truncate(struct inode *inode)
6318{
6319 struct btrfs_root *root = BTRFS_I(inode)->root;
6320 int ret;
6321 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04006322 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006323 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006324
Yan, Zheng80825102009-11-12 09:35:36 +00006325 if (!S_ISREG(inode->i_mode)) {
6326 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04006327 return;
Yan, Zheng80825102009-11-12 09:35:36 +00006328 }
Chris Mason39279cc2007-06-12 06:35:45 -04006329
Josef Bacik5d5e1032009-10-13 16:46:49 -04006330 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6331 if (ret)
6332 return;
Yan, Zheng80825102009-11-12 09:35:36 +00006333
Chris Mason4a096752008-07-21 10:29:44 -04006334 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006335 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006336
Yan, Zhengd68fc572010-05-16 10:49:58 -04006337 trans = btrfs_start_transaction(root, 0);
6338 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00006339 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006340 trans->block_rsv = root->orphan_block_rsv;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006341
6342 /*
6343 * setattr is responsible for setting the ordered_data_close flag,
6344 * but that is only tested during the last file release. That
6345 * could happen well after the next commit, leaving a great big
6346 * window where new writes may get lost if someone chooses to write
6347 * to this file after truncating to zero
6348 *
6349 * The inode doesn't have any dirty data here, and so if we commit
6350 * this is a noop. If someone immediately starts writing to the inode
6351 * it is very likely we'll catch some of their writes in this
6352 * transaction, and the commit will find this file on the ordered
6353 * data list with good things to send down.
6354 *
6355 * This is a best effort solution, there is still a window where
6356 * using truncate to replace the contents of the file will
6357 * end up with a zero length file after a crash.
6358 */
6359 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6360 btrfs_add_ordered_operation(trans, root, inode);
6361
Yan, Zheng80825102009-11-12 09:35:36 +00006362 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006363 if (!trans) {
6364 trans = btrfs_start_transaction(root, 0);
6365 BUG_ON(IS_ERR(trans));
6366 btrfs_set_trans_block_group(trans, inode);
6367 trans->block_rsv = root->orphan_block_rsv;
6368 }
6369
6370 ret = btrfs_block_rsv_check(trans, root,
6371 root->orphan_block_rsv, 0, 5);
6372 if (ret) {
6373 BUG_ON(ret != -EAGAIN);
6374 ret = btrfs_commit_transaction(trans, root);
6375 BUG_ON(ret);
6376 trans = NULL;
6377 continue;
6378 }
6379
Yan, Zheng80825102009-11-12 09:35:36 +00006380 ret = btrfs_truncate_inode_items(trans, root, inode,
6381 inode->i_size,
6382 BTRFS_EXTENT_DATA_KEY);
6383 if (ret != -EAGAIN)
6384 break;
Chris Mason39279cc2007-06-12 06:35:45 -04006385
Yan, Zheng80825102009-11-12 09:35:36 +00006386 ret = btrfs_update_inode(trans, root, inode);
6387 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04006388
Yan, Zheng80825102009-11-12 09:35:36 +00006389 nr = trans->blocks_used;
6390 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006391 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006392 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006393 }
6394
6395 if (ret == 0 && inode->i_nlink > 0) {
6396 ret = btrfs_orphan_del(trans, inode);
6397 BUG_ON(ret);
6398 }
6399
6400 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04006401 BUG_ON(ret);
6402
Josef Bacik7b128762008-07-24 12:17:14 -04006403 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04006404 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006405 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04006406 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04006407}
6408
Sven Wegener3b963622008-06-09 21:57:42 -04006409/*
Chris Masond352ac62008-09-29 15:18:18 -04006410 * create a new subvolume directory/inode (helper for the ioctl).
6411 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006412int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04006413 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006414 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04006415{
Chris Mason39279cc2007-06-12 06:35:45 -04006416 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006417 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006418 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006419
Josef Bacikaec74772008-07-24 12:12:38 -04006420 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006421 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006422 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006423 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006424 inode->i_op = &btrfs_dir_inode_operations;
6425 inode->i_fop = &btrfs_dir_file_operations;
6426
Chris Mason39279cc2007-06-12 06:35:45 -04006427 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006428 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006429
Yan, Zheng76dda932009-09-21 16:00:26 -04006430 err = btrfs_update_inode(trans, new_root, inode);
6431 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006432
Yan, Zheng76dda932009-09-21 16:00:26 -04006433 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006434 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006435}
6436
Chris Masond352ac62008-09-29 15:18:18 -04006437/* helper function for file defrag and space balancing. This
6438 * forces readahead on a given range of bytes in an inode
6439 */
Chris Masonedbd8d42007-12-21 16:27:24 -05006440unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04006441 struct file_ra_state *ra, struct file *file,
6442 pgoff_t offset, pgoff_t last_index)
6443{
Chris Mason8e7bf942008-04-28 09:02:36 -04006444 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04006445
Chris Mason86479a02007-09-10 19:58:16 -04006446 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6447 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04006448}
6449
Chris Mason39279cc2007-06-12 06:35:45 -04006450struct inode *btrfs_alloc_inode(struct super_block *sb)
6451{
6452 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006453 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006454
6455 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6456 if (!ei)
6457 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006458
6459 ei->root = NULL;
6460 ei->space_info = NULL;
6461 ei->generation = 0;
6462 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006463 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006464 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006465 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006466 ei->delalloc_bytes = 0;
6467 ei->reserved_bytes = 0;
6468 ei->disk_i_size = 0;
6469 ei->flags = 0;
6470 ei->index_cnt = (u64)-1;
6471 ei->last_unlink_trans = 0;
6472
Josef Bacik32c00af2009-10-08 13:34:05 -04006473 spin_lock_init(&ei->accounting_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006474 atomic_set(&ei->outstanding_extents, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006475 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006476
6477 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006478 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006479 ei->dummy_inode = 0;
6480 ei->force_compress = 0;
6481
6482 inode = &ei->vfs_inode;
6483 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6484 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6485 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6486 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006487 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006488 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006489 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006490 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006491 RB_CLEAR_NODE(&ei->rb_node);
6492
6493 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006494}
6495
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006496static void btrfs_i_callback(struct rcu_head *head)
6497{
6498 struct inode *inode = container_of(head, struct inode, i_rcu);
6499 INIT_LIST_HEAD(&inode->i_dentry);
6500 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6501}
6502
Chris Mason39279cc2007-06-12 06:35:45 -04006503void btrfs_destroy_inode(struct inode *inode)
6504{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006505 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006506 struct btrfs_root *root = BTRFS_I(inode)->root;
6507
Chris Mason39279cc2007-06-12 06:35:45 -04006508 WARN_ON(!list_empty(&inode->i_dentry));
6509 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006510 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
6511 WARN_ON(BTRFS_I(inode)->reserved_extents);
Chris Mason39279cc2007-06-12 06:35:45 -04006512
Chris Mason5a3f23d2009-03-31 13:27:11 -04006513 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006514 * This can happen where we create an inode, but somebody else also
6515 * created the same inode and we need to destroy the one we already
6516 * created.
6517 */
6518 if (!root)
6519 goto free;
6520
6521 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006522 * Make sure we're properly removed from the ordered operation
6523 * lists.
6524 */
6525 smp_mb();
6526 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6527 spin_lock(&root->fs_info->ordered_extent_lock);
6528 list_del_init(&BTRFS_I(inode)->ordered_operations);
6529 spin_unlock(&root->fs_info->ordered_extent_lock);
6530 }
6531
Josef Bacik0af3d002010-06-21 14:48:16 -04006532 if (root == root->fs_info->tree_root) {
6533 struct btrfs_block_group_cache *block_group;
6534
6535 block_group = btrfs_lookup_block_group(root->fs_info,
6536 BTRFS_I(inode)->block_group);
6537 if (block_group && block_group->inode == inode) {
6538 spin_lock(&block_group->lock);
6539 block_group->inode = NULL;
6540 spin_unlock(&block_group->lock);
6541 btrfs_put_block_group(block_group);
6542 } else if (block_group) {
6543 btrfs_put_block_group(block_group);
6544 }
6545 }
6546
Yan, Zhengd68fc572010-05-16 10:49:58 -04006547 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006548 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00006549 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
6550 inode->i_ino);
6551 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006552 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006553 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006554
Chris Masond3977122009-01-05 21:25:51 -05006555 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006556 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6557 if (!ordered)
6558 break;
6559 else {
Chris Masond3977122009-01-05 21:25:51 -05006560 printk(KERN_ERR "btrfs found ordered "
6561 "extent %llu %llu on inode cleanup\n",
6562 (unsigned long long)ordered->file_offset,
6563 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006564 btrfs_remove_ordered_extent(inode, ordered);
6565 btrfs_put_ordered_extent(ordered);
6566 btrfs_put_ordered_extent(ordered);
6567 }
6568 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006569 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006570 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006571free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11006572 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04006573}
6574
Al Viro45321ac2010-06-07 13:43:19 -04006575int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006576{
6577 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006578
Josef Bacik0af3d002010-06-21 14:48:16 -04006579 if (btrfs_root_refs(&root->root_item) == 0 &&
6580 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04006581 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006582 else
Al Viro45321ac2010-06-07 13:43:19 -04006583 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006584}
6585
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006586static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006587{
6588 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6589
6590 inode_init_once(&ei->vfs_inode);
6591}
6592
6593void btrfs_destroy_cachep(void)
6594{
6595 if (btrfs_inode_cachep)
6596 kmem_cache_destroy(btrfs_inode_cachep);
6597 if (btrfs_trans_handle_cachep)
6598 kmem_cache_destroy(btrfs_trans_handle_cachep);
6599 if (btrfs_transaction_cachep)
6600 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006601 if (btrfs_path_cachep)
6602 kmem_cache_destroy(btrfs_path_cachep);
6603}
6604
6605int btrfs_init_cachep(void)
6606{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006607 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6608 sizeof(struct btrfs_inode), 0,
6609 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006610 if (!btrfs_inode_cachep)
6611 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006612
6613 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6614 sizeof(struct btrfs_trans_handle), 0,
6615 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006616 if (!btrfs_trans_handle_cachep)
6617 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006618
6619 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6620 sizeof(struct btrfs_transaction), 0,
6621 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006622 if (!btrfs_transaction_cachep)
6623 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006624
6625 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6626 sizeof(struct btrfs_path), 0,
6627 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006628 if (!btrfs_path_cachep)
6629 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006630
Chris Mason39279cc2007-06-12 06:35:45 -04006631 return 0;
6632fail:
6633 btrfs_destroy_cachep();
6634 return -ENOMEM;
6635}
6636
6637static int btrfs_getattr(struct vfsmount *mnt,
6638 struct dentry *dentry, struct kstat *stat)
6639{
6640 struct inode *inode = dentry->d_inode;
6641 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05006642 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05006643 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006644 stat->blocks = (inode_get_bytes(inode) +
6645 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04006646 return 0;
6647}
6648
Chris Masond3977122009-01-05 21:25:51 -05006649static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6650 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04006651{
6652 struct btrfs_trans_handle *trans;
6653 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006654 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04006655 struct inode *new_inode = new_dentry->d_inode;
6656 struct inode *old_inode = old_dentry->d_inode;
6657 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006658 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006659 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04006660 int ret;
6661
Yan, Zhengf679a842009-09-24 09:17:31 -04006662 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6663 return -EPERM;
6664
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006665 /* we only allow rename subvolume link between subvolumes */
6666 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05006667 return -EXDEV;
6668
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006669 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
6670 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
6671 return -ENOTEMPTY;
6672
Chris Mason39279cc2007-06-12 06:35:45 -04006673 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006674 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04006675 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006676 /*
6677 * we're using rename to replace one file with another.
6678 * and the replacement file is large. Start IO on it now so
6679 * we don't add too much work to the end of the transaction
6680 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04006681 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04006682 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
6683 filemap_flush(old_inode->i_mapping);
6684
Yan, Zheng76dda932009-09-21 16:00:26 -04006685 /* close the racy window with snapshot create/destroy ioctl */
6686 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6687 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006688 /*
6689 * We want to reserve the absolute worst case amount of items. So if
6690 * both inodes are subvols and we need to unlink them then that would
6691 * require 4 item modifications, but if they are both normal inodes it
6692 * would require 5 item modifications, so we'll assume their normal
6693 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6694 * should cover the worst case number of items we'll modify.
6695 */
6696 trans = btrfs_start_transaction(root, 20);
6697 if (IS_ERR(trans))
6698 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04006699
Yan, Zhenga5719522009-09-24 09:17:31 -04006700 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04006701
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006702 if (dest != root)
6703 btrfs_record_root_in_trans(trans, dest);
6704
Yan, Zhenga5719522009-09-24 09:17:31 -04006705 ret = btrfs_set_inode_index(new_dir, &index);
6706 if (ret)
6707 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006708
Yan, Zhenga5719522009-09-24 09:17:31 -04006709 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006710 /* force full log commit if subvolume involved. */
6711 root->fs_info->last_trans_log_full_commit = trans->transid;
6712 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04006713 ret = btrfs_insert_inode_ref(trans, dest,
6714 new_dentry->d_name.name,
6715 new_dentry->d_name.len,
6716 old_inode->i_ino,
6717 new_dir->i_ino, index);
6718 if (ret)
6719 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006720 /*
6721 * this is an ugly little race, but the rename is required
6722 * to make sure that if we crash, the inode is either at the
6723 * old name or the new one. pinning the log transaction lets
6724 * us make sure we don't allow a log commit to come in after
6725 * we unlink the name but before we add the new name back in.
6726 */
6727 btrfs_pin_log_trans(root);
6728 }
Chris Mason12fcfd22009-03-24 10:24:20 -04006729 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006730 * make sure the inode gets flushed if it is replacing
6731 * something.
6732 */
6733 if (new_inode && new_inode->i_size &&
6734 old_inode && S_ISREG(old_inode->i_mode)) {
6735 btrfs_add_ordered_operation(trans, root, old_inode);
6736 }
6737
Chris Mason39279cc2007-06-12 06:35:45 -04006738 old_dir->i_ctime = old_dir->i_mtime = ctime;
6739 new_dir->i_ctime = new_dir->i_mtime = ctime;
6740 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04006741
Chris Mason12fcfd22009-03-24 10:24:20 -04006742 if (old_dentry->d_parent != new_dentry->d_parent)
6743 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
6744
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006745 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
6746 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
6747 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
6748 old_dentry->d_name.name,
6749 old_dentry->d_name.len);
6750 } else {
6751 btrfs_inc_nlink(old_dentry->d_inode);
6752 ret = btrfs_unlink_inode(trans, root, old_dir,
6753 old_dentry->d_inode,
6754 old_dentry->d_name.name,
6755 old_dentry->d_name.len);
6756 }
6757 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006758
6759 if (new_inode) {
6760 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006761 if (unlikely(new_inode->i_ino ==
6762 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
6763 root_objectid = BTRFS_I(new_inode)->location.objectid;
6764 ret = btrfs_unlink_subvol(trans, dest, new_dir,
6765 root_objectid,
6766 new_dentry->d_name.name,
6767 new_dentry->d_name.len);
6768 BUG_ON(new_inode->i_nlink == 0);
6769 } else {
6770 ret = btrfs_unlink_inode(trans, dest, new_dir,
6771 new_dentry->d_inode,
6772 new_dentry->d_name.name,
6773 new_dentry->d_name.len);
6774 }
6775 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006776 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04006777 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006778 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006779 }
Chris Mason39279cc2007-06-12 06:35:45 -04006780 }
Josef Bacikaec74772008-07-24 12:12:38 -04006781
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006782 ret = btrfs_add_link(trans, new_dir, old_inode,
6783 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04006784 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006785 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006786
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006787 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik6a912212010-11-20 09:48:00 +00006788 struct dentry *parent = dget_parent(new_dentry);
6789 btrfs_log_new_name(trans, old_inode, old_dir, parent);
6790 dput(parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006791 btrfs_end_log_trans(root);
6792 }
Chris Mason39279cc2007-06-12 06:35:45 -04006793out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04006794 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006795
Yan, Zheng76dda932009-09-21 16:00:26 -04006796 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6797 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006798
Chris Mason39279cc2007-06-12 06:35:45 -04006799 return ret;
6800}
6801
Chris Masond352ac62008-09-29 15:18:18 -04006802/*
6803 * some fairly slow code that needs optimization. This walks the list
6804 * of all the inodes with pending delalloc and forces them to disk.
6805 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006806int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04006807{
6808 struct list_head *head = &root->fs_info->delalloc_inodes;
6809 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006810 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04006811
Yan Zhengc146afa2008-11-12 14:34:12 -05006812 if (root->fs_info->sb->s_flags & MS_RDONLY)
6813 return -EROFS;
6814
Chris Mason75eff682008-12-15 15:54:40 -05006815 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05006816 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04006817 binode = list_entry(head->next, struct btrfs_inode,
6818 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006819 inode = igrab(&binode->vfs_inode);
6820 if (!inode)
6821 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05006822 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006823 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04006824 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006825 if (delay_iput)
6826 btrfs_add_delayed_iput(inode);
6827 else
6828 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006829 }
6830 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05006831 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04006832 }
Chris Mason75eff682008-12-15 15:54:40 -05006833 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04006834
6835 /* the filemap_flush will queue IO into the worker threads, but
6836 * we have to make sure the IO is actually started and that
6837 * ordered extents get created before we return
6838 */
6839 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05006840 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05006841 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04006842 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05006843 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
6844 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04006845 }
6846 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04006847 return 0;
6848}
6849
Josef Bacik0019f102010-10-15 15:18:40 -04006850int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
6851 int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04006852{
6853 struct btrfs_inode *binode;
6854 struct inode *inode = NULL;
6855
6856 spin_lock(&root->fs_info->delalloc_lock);
6857 while (!list_empty(&root->fs_info->delalloc_inodes)) {
6858 binode = list_entry(root->fs_info->delalloc_inodes.next,
6859 struct btrfs_inode, delalloc_inodes);
6860 inode = igrab(&binode->vfs_inode);
6861 if (inode) {
6862 list_move_tail(&binode->delalloc_inodes,
6863 &root->fs_info->delalloc_inodes);
6864 break;
6865 }
6866
6867 list_del_init(&binode->delalloc_inodes);
6868 cond_resched_lock(&root->fs_info->delalloc_lock);
6869 }
6870 spin_unlock(&root->fs_info->delalloc_lock);
6871
6872 if (inode) {
Josef Bacik0019f102010-10-15 15:18:40 -04006873 if (sync) {
6874 filemap_write_and_wait(inode->i_mapping);
6875 /*
6876 * We have to do this because compression doesn't
6877 * actually set PG_writeback until it submits the pages
6878 * for IO, which happens in an async thread, so we could
6879 * race and not actually wait for any writeback pages
6880 * because they've not been submitted yet. Technically
6881 * this could still be the case for the ordered stuff
6882 * since the async thread may not have started to do its
6883 * work yet. If this becomes the case then we need to
6884 * figure out a way to make sure that in writepage we
6885 * wait for any async pages to be submitted before
6886 * returning so that fdatawait does what its supposed to
6887 * do.
6888 */
6889 btrfs_wait_ordered_range(inode, 0, (u64)-1);
6890 } else {
6891 filemap_flush(inode->i_mapping);
6892 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04006893 if (delay_iput)
6894 btrfs_add_delayed_iput(inode);
6895 else
6896 iput(inode);
6897 return 1;
6898 }
6899 return 0;
6900}
6901
Chris Mason39279cc2007-06-12 06:35:45 -04006902static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
6903 const char *symname)
6904{
6905 struct btrfs_trans_handle *trans;
6906 struct btrfs_root *root = BTRFS_I(dir)->root;
6907 struct btrfs_path *path;
6908 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05006909 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006910 int err;
6911 int drop_inode = 0;
6912 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006913 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04006914 int name_len;
6915 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04006916 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006917 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04006918 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05006919 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006920
6921 name_len = strlen(symname) + 1;
6922 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
6923 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05006924
Yan, Zhenga22285a2010-05-16 10:48:46 -04006925 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
6926 if (err)
6927 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006928 /*
6929 * 2 items for inode item and ref
6930 * 2 items for dir items
6931 * 1 item for xattr if selinux is on
6932 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006933 trans = btrfs_start_transaction(root, 5);
6934 if (IS_ERR(trans))
6935 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006936
Chris Mason39279cc2007-06-12 06:35:45 -04006937 btrfs_set_trans_block_group(trans, dir);
6938
Josef Bacikaec74772008-07-24 12:12:38 -04006939 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00006940 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04006941 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
6942 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006943 err = PTR_ERR(inode);
6944 if (IS_ERR(inode))
6945 goto out_unlock;
6946
Yan, Zhengf34f57a2009-11-12 09:35:27 +00006947 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04006948 if (err) {
6949 drop_inode = 1;
6950 goto out_unlock;
6951 }
6952
Chris Mason39279cc2007-06-12 06:35:45 -04006953 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00006954 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006955 if (err)
6956 drop_inode = 1;
6957 else {
6958 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04006959 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04006960 inode->i_fop = &btrfs_file_operations;
6961 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05006962 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04006963 }
Chris Mason39279cc2007-06-12 06:35:45 -04006964 btrfs_update_inode_block_group(trans, inode);
6965 btrfs_update_inode_block_group(trans, dir);
6966 if (drop_inode)
6967 goto out_unlock;
6968
6969 path = btrfs_alloc_path();
6970 BUG_ON(!path);
6971 key.objectid = inode->i_ino;
6972 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006973 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
6974 datasize = btrfs_file_extent_calc_inline_size(name_len);
6975 err = btrfs_insert_empty_item(trans, root, path, &key,
6976 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006977 if (err) {
6978 drop_inode = 1;
6979 goto out_unlock;
6980 }
Chris Mason5f39d392007-10-15 16:14:19 -04006981 leaf = path->nodes[0];
6982 ei = btrfs_item_ptr(leaf, path->slots[0],
6983 struct btrfs_file_extent_item);
6984 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
6985 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04006986 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04006987 btrfs_set_file_extent_encryption(leaf, ei, 0);
6988 btrfs_set_file_extent_compression(leaf, ei, 0);
6989 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
6990 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
6991
Chris Mason39279cc2007-06-12 06:35:45 -04006992 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04006993 write_extent_buffer(leaf, symname, ptr, name_len);
6994 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04006995 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04006996
Chris Mason39279cc2007-06-12 06:35:45 -04006997 inode->i_op = &btrfs_symlink_inode_operations;
6998 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04006999 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007000 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007001 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007002 err = btrfs_update_inode(trans, root, inode);
7003 if (err)
7004 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007005
7006out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04007007 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04007008 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007009 if (drop_inode) {
7010 inode_dec_link_count(inode);
7011 iput(inode);
7012 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04007013 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007014 return err;
7015}
Chris Mason16432982008-04-10 10:23:21 -04007016
Josef Bacik0af3d002010-06-21 14:48:16 -04007017static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7018 u64 start, u64 num_bytes, u64 min_size,
7019 loff_t actual_len, u64 *alloc_hint,
7020 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007021{
Yan Zhengd899e052008-10-30 14:25:28 -04007022 struct btrfs_root *root = BTRFS_I(inode)->root;
7023 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007024 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007025 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007026 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007027 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007028
Josef Bacik0af3d002010-06-21 14:48:16 -04007029 if (trans)
7030 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007031 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007032 if (own_trans) {
7033 trans = btrfs_start_transaction(root, 3);
7034 if (IS_ERR(trans)) {
7035 ret = PTR_ERR(trans);
7036 break;
7037 }
Yan Zhengd899e052008-10-30 14:25:28 -04007038 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007039
Yan, Zhengefa56462010-05-16 10:49:59 -04007040 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7041 0, *alloc_hint, (u64)-1, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007042 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007043 if (own_trans)
7044 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007045 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007046 }
7047
Yan Zhengd899e052008-10-30 14:25:28 -04007048 ret = insert_reserved_file_extent(trans, inode,
7049 cur_offset, ins.objectid,
7050 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007051 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007052 BTRFS_FILE_EXTENT_PREALLOC);
7053 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04007054 btrfs_drop_extent_cache(inode, cur_offset,
7055 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007056
Yan Zhengd899e052008-10-30 14:25:28 -04007057 num_bytes -= ins.offset;
7058 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007059 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007060
Yan Zhengd899e052008-10-30 14:25:28 -04007061 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007062 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007063 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007064 (actual_len > inode->i_size) &&
7065 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007066 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007067 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007068 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007069 i_size = cur_offset;
7070 i_size_write(inode, i_size);
7071 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007072 }
7073
Yan Zhengd899e052008-10-30 14:25:28 -04007074 ret = btrfs_update_inode(trans, root, inode);
7075 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04007076
Josef Bacik0af3d002010-06-21 14:48:16 -04007077 if (own_trans)
7078 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007079 }
Yan Zhengd899e052008-10-30 14:25:28 -04007080 return ret;
7081}
7082
Josef Bacik0af3d002010-06-21 14:48:16 -04007083int btrfs_prealloc_file_range(struct inode *inode, int mode,
7084 u64 start, u64 num_bytes, u64 min_size,
7085 loff_t actual_len, u64 *alloc_hint)
7086{
7087 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7088 min_size, actual_len, alloc_hint,
7089 NULL);
7090}
7091
7092int btrfs_prealloc_file_range_trans(struct inode *inode,
7093 struct btrfs_trans_handle *trans, int mode,
7094 u64 start, u64 num_bytes, u64 min_size,
7095 loff_t actual_len, u64 *alloc_hint)
7096{
7097 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7098 min_size, actual_len, alloc_hint, trans);
7099}
7100
Yan Zhengd899e052008-10-30 14:25:28 -04007101static long btrfs_fallocate(struct inode *inode, int mode,
7102 loff_t offset, loff_t len)
7103{
Josef Bacik2ac55d42010-02-03 19:33:23 +00007104 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04007105 u64 cur_offset;
7106 u64 last_byte;
7107 u64 alloc_start;
7108 u64 alloc_end;
7109 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04007110 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04007111 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
7112 struct extent_map *em;
7113 int ret;
7114
7115 alloc_start = offset & ~mask;
7116 alloc_end = (offset + len + mask) & ~mask;
7117
Josef Bacik23a85192010-11-17 20:46:19 -05007118 /* We only support the FALLOC_FL_KEEP_SIZE mode */
Christoph Hellwig64c23e82011-01-14 13:07:30 +01007119 if (mode & ~FALLOC_FL_KEEP_SIZE)
Josef Bacik23a85192010-11-17 20:46:19 -05007120 return -EOPNOTSUPP;
7121
Chris Mason546888d2009-04-21 11:53:38 -04007122 /*
7123 * wait for ordered IO before we have any locks. We'll loop again
7124 * below with the locks held.
7125 */
7126 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
7127
Yan Zhengd899e052008-10-30 14:25:28 -04007128 mutex_lock(&inode->i_mutex);
Josef Bacik0ed42a62010-11-22 18:55:39 +00007129 ret = inode_newsize_ok(inode, alloc_end);
7130 if (ret)
7131 goto out;
7132
Yan Zhengd899e052008-10-30 14:25:28 -04007133 if (alloc_start > inode->i_size) {
7134 ret = btrfs_cont_expand(inode, alloc_start);
7135 if (ret)
7136 goto out;
7137 }
7138
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007139 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
Josef Bacika970b0a2009-06-27 21:07:34 -04007140 if (ret)
7141 goto out;
7142
Chris Masone980b502009-04-24 14:39:24 -04007143 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04007144 while (1) {
7145 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04007146
Chris Mason546888d2009-04-21 11:53:38 -04007147 /* the extent lock is ordered inside the running
7148 * transaction
7149 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007150 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
7151 locked_end, 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04007152 ordered = btrfs_lookup_first_ordered_extent(inode,
7153 alloc_end - 1);
7154 if (ordered &&
7155 ordered->file_offset + ordered->len > alloc_start &&
7156 ordered->file_offset < alloc_end) {
7157 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007158 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7159 alloc_start, locked_end,
7160 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04007161 /*
7162 * we can't wait on the range with the transaction
7163 * running or with the extent lock held
7164 */
Yan Zhengd899e052008-10-30 14:25:28 -04007165 btrfs_wait_ordered_range(inode, alloc_start,
7166 alloc_end - alloc_start);
7167 } else {
7168 if (ordered)
7169 btrfs_put_ordered_extent(ordered);
7170 break;
7171 }
7172 }
7173
7174 cur_offset = alloc_start;
7175 while (1) {
7176 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
7177 alloc_end - cur_offset, 0);
7178 BUG_ON(IS_ERR(em) || !em);
7179 last_byte = min(extent_map_end(em), alloc_end);
7180 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007181 if (em->block_start == EXTENT_MAP_HOLE ||
7182 (cur_offset >= inode->i_size &&
7183 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
Josef Bacik83609772010-06-07 18:26:37 +00007184 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04007185 last_byte - cur_offset,
7186 1 << inode->i_blkbits,
7187 offset + len,
7188 &alloc_hint);
Yan Zhengd899e052008-10-30 14:25:28 -04007189 if (ret < 0) {
7190 free_extent_map(em);
7191 break;
7192 }
7193 }
Yan Zhengd899e052008-10-30 14:25:28 -04007194 free_extent_map(em);
7195
7196 cur_offset = last_byte;
7197 if (cur_offset >= alloc_end) {
7198 ret = 0;
7199 break;
7200 }
7201 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00007202 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
7203 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04007204
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007205 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04007206out:
7207 mutex_unlock(&inode->i_mutex);
7208 return ret;
7209}
7210
Chris Masone6dcd2d2008-07-17 12:53:50 -04007211static int btrfs_set_page_dirty(struct page *page)
7212{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007213 return __set_page_dirty_nobuffers(page);
7214}
7215
Nick Pigginb74c79e2011-01-07 17:49:58 +11007216static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
Yanfdebe2b2008-01-14 13:26:08 -05007217{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007218 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05007219 return -EACCES;
Nick Pigginb74c79e2011-01-07 17:49:58 +11007220 return generic_permission(inode, mask, flags, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05007221}
Chris Mason39279cc2007-06-12 06:35:45 -04007222
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007223static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007224 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007225 .lookup = btrfs_lookup,
7226 .create = btrfs_create,
7227 .unlink = btrfs_unlink,
7228 .link = btrfs_link,
7229 .mkdir = btrfs_mkdir,
7230 .rmdir = btrfs_rmdir,
7231 .rename = btrfs_rename,
7232 .symlink = btrfs_symlink,
7233 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007234 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007235 .setxattr = btrfs_setxattr,
7236 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007237 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007238 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007239 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007240};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007241static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007242 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007243 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007244};
Yan, Zheng76dda932009-09-21 16:00:26 -04007245
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007246static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007247 .llseek = generic_file_llseek,
7248 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007249 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007250 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007251#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007252 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007253#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007254 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007255 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007256};
7257
Chris Masond1310b22008-01-24 16:13:08 -05007258static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007259 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007260 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007261 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007262 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007263 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007264 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04007265 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007266 .set_bit_hook = btrfs_set_bit_hook,
7267 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007268 .merge_extent_hook = btrfs_merge_extent_hook,
7269 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007270};
7271
Chris Mason35054392009-01-21 13:11:13 -05007272/*
7273 * btrfs doesn't support the bmap operation because swapfiles
7274 * use bmap to make a mapping of extents in the file. They assume
7275 * these extents won't change over the life of the file and they
7276 * use the bmap result to do IO directly to the drive.
7277 *
7278 * the btrfs bmap call would return logical addresses that aren't
7279 * suitable for IO and they also will change frequently as COW
7280 * operations happen. So, swapfile + btrfs == corruption.
7281 *
7282 * For now we're avoiding this by dropping bmap.
7283 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007284static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007285 .readpage = btrfs_readpage,
7286 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007287 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007288 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04007289 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04007290 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007291 .invalidatepage = btrfs_invalidatepage,
7292 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007293 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007294 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007295};
7296
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007297static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007298 .readpage = btrfs_readpage,
7299 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007300 .invalidatepage = btrfs_invalidatepage,
7301 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007302};
7303
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007304static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007305 .truncate = btrfs_truncate,
7306 .getattr = btrfs_getattr,
7307 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007308 .setxattr = btrfs_setxattr,
7309 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007310 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007311 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007312 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04007313 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007314 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04007315};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007316static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007317 .getattr = btrfs_getattr,
7318 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007319 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007320 .setxattr = btrfs_setxattr,
7321 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007322 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007323 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007324};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007325static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007326 .readlink = generic_readlink,
7327 .follow_link = page_follow_link_light,
7328 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007329 .getattr = btrfs_getattr,
Yanfdebe2b2008-01-14 13:26:08 -05007330 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007331 .setxattr = btrfs_setxattr,
7332 .getxattr = btrfs_getxattr,
7333 .listxattr = btrfs_listxattr,
7334 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007335};
Yan, Zheng76dda932009-09-21 16:00:26 -04007336
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007337const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007338 .d_delete = btrfs_dentry_delete,
7339};