blob: 3a6edc4c564224861485cd9260e024fc17af75d1 [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, Zhengc2167752009-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, Zhengc2167752009-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 Masonf03d9301f2009-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, Zhengc2167752009-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, Zhengc2167752009-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 Masonf03d9301f2009-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, Zhengc2167752009-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, Zhengc2167752009-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, Zhengc2167752009-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, Zhengc2167752009-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);
Miao Xieb897abe2011-01-26 16:19:22 +08001547 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001548}
1549
1550/*
1551 * There are a few paths in the higher layers of the kernel that directly
1552 * set the page dirty bit without asking the filesystem if it is a
1553 * good idea. This causes problems because we want to make sure COW
1554 * properly happens and the data=ordered rules are followed.
1555 *
Chris Masonc8b97812008-10-29 14:49:59 -04001556 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001557 * hasn't been properly setup for IO. We kick off an async process
1558 * to fix it up. The async helper will wait for ordered extents, set
1559 * the delalloc bit and make it safe to write the page.
1560 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001561static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001562{
1563 struct inode *inode = page->mapping->host;
1564 struct btrfs_writepage_fixup *fixup;
1565 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001566
Chris Mason8b62b722009-09-02 16:53:46 -04001567 /* this page is properly in the ordered list */
1568 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001569 return 0;
1570
1571 if (PageChecked(page))
1572 return -EAGAIN;
1573
1574 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1575 if (!fixup)
1576 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001577
Chris Mason247e7432008-07-17 12:53:51 -04001578 SetPageChecked(page);
1579 page_cache_get(page);
1580 fixup->work.func = btrfs_writepage_fixup_worker;
1581 fixup->page = page;
1582 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1583 return -EAGAIN;
1584}
1585
Yan Zhengd899e052008-10-30 14:25:28 -04001586static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1587 struct inode *inode, u64 file_pos,
1588 u64 disk_bytenr, u64 disk_num_bytes,
1589 u64 num_bytes, u64 ram_bytes,
1590 u8 compression, u8 encryption,
1591 u16 other_encoding, int extent_type)
1592{
1593 struct btrfs_root *root = BTRFS_I(inode)->root;
1594 struct btrfs_file_extent_item *fi;
1595 struct btrfs_path *path;
1596 struct extent_buffer *leaf;
1597 struct btrfs_key ins;
1598 u64 hint;
1599 int ret;
1600
1601 path = btrfs_alloc_path();
1602 BUG_ON(!path);
1603
Chris Masonb9473432009-03-13 11:00:37 -04001604 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001605
1606 /*
1607 * we may be replacing one extent in the tree with another.
1608 * The new extent is pinned in the extent map, and we don't want
1609 * to drop it from the cache until it is completely in the btree.
1610 *
1611 * So, tell btrfs_drop_extents to leave this extent in the cache.
1612 * the caller is expected to unpin it and allow it to be merged
1613 * with the others.
1614 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001615 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1616 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001617 BUG_ON(ret);
1618
1619 ins.objectid = inode->i_ino;
1620 ins.offset = file_pos;
1621 ins.type = BTRFS_EXTENT_DATA_KEY;
1622 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1623 BUG_ON(ret);
1624 leaf = path->nodes[0];
1625 fi = btrfs_item_ptr(leaf, path->slots[0],
1626 struct btrfs_file_extent_item);
1627 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1628 btrfs_set_file_extent_type(leaf, fi, extent_type);
1629 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1630 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1631 btrfs_set_file_extent_offset(leaf, fi, 0);
1632 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1633 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1634 btrfs_set_file_extent_compression(leaf, fi, compression);
1635 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1636 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001637
1638 btrfs_unlock_up_safe(path, 1);
1639 btrfs_set_lock_blocking(leaf);
1640
Yan Zhengd899e052008-10-30 14:25:28 -04001641 btrfs_mark_buffer_dirty(leaf);
1642
1643 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001644
1645 ins.objectid = disk_bytenr;
1646 ins.offset = disk_num_bytes;
1647 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001648 ret = btrfs_alloc_reserved_file_extent(trans, root,
1649 root->root_key.objectid,
1650 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001651 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001652 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001653
Yan Zhengd899e052008-10-30 14:25:28 -04001654 return 0;
1655}
1656
Chris Mason5d13a982009-03-13 11:41:46 -04001657/*
1658 * helper function for btrfs_finish_ordered_io, this
1659 * just reads in some of the csum leaves to prime them into ram
1660 * before we start the transaction. It limits the amount of btree
1661 * reads required while inside the transaction.
1662 */
Chris Masond352ac62008-09-29 15:18:18 -04001663/* as ordered data IO finishes, this gets called so we can finish
1664 * an ordered extent if the range of bytes in the file it covers are
1665 * fully written.
1666 */
Chris Mason211f90e2008-07-18 11:56:15 -04001667static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001668{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001669 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001670 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001671 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001672 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001673 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04001674 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001675 int ret;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001676 bool nolock = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001677
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001678 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1679 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001680 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001681 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001682 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001683
Josef Bacik0cb59c92010-07-02 12:14:14 -04001684 nolock = (root == root->fs_info->tree_root);
1685
Yan, Zhengc2167752009-11-12 09:34:21 +00001686 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1687 BUG_ON(!list_empty(&ordered_extent->list));
1688 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1689 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001690 if (nolock)
1691 trans = btrfs_join_transaction_nolock(root, 1);
1692 else
1693 trans = btrfs_join_transaction(root, 1);
1694 BUG_ON(!trans);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001695 btrfs_set_trans_block_group(trans, inode);
1696 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001697 ret = btrfs_update_inode(trans, root, inode);
1698 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001699 }
1700 goto out;
1701 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001702
Josef Bacik2ac55d42010-02-03 19:33:23 +00001703 lock_extent_bits(io_tree, ordered_extent->file_offset,
1704 ordered_extent->file_offset + ordered_extent->len - 1,
1705 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001706
Josef Bacik0cb59c92010-07-02 12:14:14 -04001707 if (nolock)
1708 trans = btrfs_join_transaction_nolock(root, 1);
1709 else
1710 trans = btrfs_join_transaction(root, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001711 btrfs_set_trans_block_group(trans, inode);
1712 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001713
Chris Masonc8b97812008-10-29 14:49:59 -04001714 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001715 compressed = 1;
1716 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1717 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001718 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001719 ordered_extent->file_offset,
1720 ordered_extent->file_offset +
1721 ordered_extent->len);
1722 BUG_ON(ret);
1723 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001724 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001725 ret = insert_reserved_file_extent(trans, inode,
1726 ordered_extent->file_offset,
1727 ordered_extent->start,
1728 ordered_extent->disk_len,
1729 ordered_extent->len,
1730 ordered_extent->len,
1731 compressed, 0, 0,
1732 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001733 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1734 ordered_extent->file_offset,
1735 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001736 BUG_ON(ret);
1737 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001738 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1739 ordered_extent->file_offset +
1740 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1741
Chris Masone6dcd2d2008-07-17 12:53:50 -04001742 add_pending_csums(trans, inode, ordered_extent->file_offset,
1743 &ordered_extent->list);
1744
Yan, Zhengc2167752009-11-12 09:34:21 +00001745 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1746 ret = btrfs_update_inode(trans, root, inode);
1747 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001748out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001749 if (nolock) {
1750 if (trans)
1751 btrfs_end_transaction_nolock(trans, root);
1752 } else {
1753 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1754 if (trans)
1755 btrfs_end_transaction(trans, root);
1756 }
1757
Chris Masone6dcd2d2008-07-17 12:53:50 -04001758 /* once for us */
1759 btrfs_put_ordered_extent(ordered_extent);
1760 /* once for the tree */
1761 btrfs_put_ordered_extent(ordered_extent);
1762
Chris Masone6dcd2d2008-07-17 12:53:50 -04001763 return 0;
1764}
1765
Christoph Hellwigb2950862008-12-02 09:54:17 -05001766static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001767 struct extent_state *state, int uptodate)
1768{
Chris Mason8b62b722009-09-02 16:53:46 -04001769 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001770 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1771}
1772
Chris Masond352ac62008-09-29 15:18:18 -04001773/*
1774 * When IO fails, either with EIO or csum verification fails, we
1775 * try other mirrors that might have a good copy of the data. This
1776 * io_failure_record is used to record state as we go through all the
1777 * mirrors. If another mirror has good data, the page is set up to date
1778 * and things continue. If a good mirror can't be found, the original
1779 * bio end_io callback is called to indicate things have failed.
1780 */
Chris Mason7e383262008-04-09 16:28:12 -04001781struct io_failure_record {
1782 struct page *page;
1783 u64 start;
1784 u64 len;
1785 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001786 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001787 int last_mirror;
1788};
1789
Christoph Hellwigb2950862008-12-02 09:54:17 -05001790static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001791 struct page *page, u64 start, u64 end,
1792 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001793{
1794 struct io_failure_record *failrec = NULL;
1795 u64 private;
1796 struct extent_map *em;
1797 struct inode *inode = page->mapping->host;
1798 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001799 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001800 struct bio *bio;
1801 int num_copies;
1802 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001803 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001804 u64 logical;
1805
1806 ret = get_state_private(failure_tree, start, &private);
1807 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001808 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1809 if (!failrec)
1810 return -ENOMEM;
1811 failrec->start = start;
1812 failrec->len = end - start + 1;
1813 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001814 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001815
Chris Mason890871b2009-09-02 16:24:52 -04001816 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001817 em = lookup_extent_mapping(em_tree, start, failrec->len);
1818 if (em->start > start || em->start + em->len < start) {
1819 free_extent_map(em);
1820 em = NULL;
1821 }
Chris Mason890871b2009-09-02 16:24:52 -04001822 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001823
1824 if (!em || IS_ERR(em)) {
1825 kfree(failrec);
1826 return -EIO;
1827 }
1828 logical = start - em->start;
1829 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001830 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1831 logical = em->block_start;
1832 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1833 }
Chris Mason7e383262008-04-09 16:28:12 -04001834 failrec->logical = logical;
1835 free_extent_map(em);
1836 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1837 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001838 set_state_private(failure_tree, start,
1839 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001840 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001841 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001842 }
1843 num_copies = btrfs_num_copies(
1844 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1845 failrec->logical, failrec->len);
1846 failrec->last_mirror++;
1847 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001848 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001849 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1850 failrec->start,
1851 EXTENT_LOCKED);
1852 if (state && state->start != failrec->start)
1853 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001854 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001855 }
1856 if (!state || failrec->last_mirror > num_copies) {
1857 set_state_private(failure_tree, failrec->start, 0);
1858 clear_extent_bits(failure_tree, failrec->start,
1859 failrec->start + failrec->len - 1,
1860 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1861 kfree(failrec);
1862 return -EIO;
1863 }
1864 bio = bio_alloc(GFP_NOFS, 1);
1865 bio->bi_private = state;
1866 bio->bi_end_io = failed_bio->bi_end_io;
1867 bio->bi_sector = failrec->logical >> 9;
1868 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001869 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001870
Chris Mason7e383262008-04-09 16:28:12 -04001871 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001872 if (failed_bio->bi_rw & REQ_WRITE)
Chris Mason1259ab72008-05-12 13:39:03 -04001873 rw = WRITE;
1874 else
1875 rw = READ;
1876
1877 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001878 failrec->last_mirror,
Chris Masoneaf25d92010-05-25 09:48:28 -04001879 failrec->bio_flags, 0);
Chris Mason1259ab72008-05-12 13:39:03 -04001880 return 0;
1881}
1882
Chris Masond352ac62008-09-29 15:18:18 -04001883/*
1884 * each time an IO finishes, we do a fast check in the IO failure tree
1885 * to see if we need to process or clean up an io_failure_record
1886 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001887static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001888{
1889 u64 private;
1890 u64 private_failure;
1891 struct io_failure_record *failure;
1892 int ret;
1893
1894 private = 0;
1895 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1896 (u64)-1, 1, EXTENT_DIRTY)) {
1897 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1898 start, &private_failure);
1899 if (ret == 0) {
1900 failure = (struct io_failure_record *)(unsigned long)
1901 private_failure;
1902 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1903 failure->start, 0);
1904 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1905 failure->start,
1906 failure->start + failure->len - 1,
1907 EXTENT_DIRTY | EXTENT_LOCKED,
1908 GFP_NOFS);
1909 kfree(failure);
1910 }
1911 }
Chris Mason7e383262008-04-09 16:28:12 -04001912 return 0;
1913}
1914
Chris Masond352ac62008-09-29 15:18:18 -04001915/*
1916 * when reads are done, we need to check csums to verify the data is correct
1917 * if there's a match, we allow the bio to finish. If not, we go through
1918 * the io_failure_record routines to find good copies
1919 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001920static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001921 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001922{
Chris Mason35ebb932007-10-30 16:56:53 -04001923 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001924 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001925 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001926 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001927 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001928 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001929 struct btrfs_root *root = BTRFS_I(inode)->root;
1930 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001931
Chris Masond20f7042008-12-08 16:58:54 -05001932 if (PageChecked(page)) {
1933 ClearPageChecked(page);
1934 goto good;
1935 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001936
1937 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001938 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001939
Yan Zheng17d217f2008-12-12 10:03:38 -05001940 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001941 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001942 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1943 GFP_NOFS);
1944 return 0;
1945 }
1946
Yanc2e639f2008-02-04 08:57:25 -05001947 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001948 private = state->private;
1949 ret = 0;
1950 } else {
1951 ret = get_state_private(io_tree, start, &private);
1952 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001953 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001954 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001955 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001956
Chris Masonff79f812007-10-15 16:22:25 -04001957 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1958 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001959 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001960 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001961
Chris Mason9ab86c82009-01-07 09:48:51 -05001962 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001963good:
Chris Mason7e383262008-04-09 16:28:12 -04001964 /* if the io failure tree for this inode is non-empty,
1965 * check to see if we've recovered from a failed IO
1966 */
Chris Mason1259ab72008-05-12 13:39:03 -04001967 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001968 return 0;
1969
1970zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001971 if (printk_ratelimit()) {
1972 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1973 "private %llu\n", page->mapping->host->i_ino,
1974 (unsigned long long)start, csum,
1975 (unsigned long long)private);
1976 }
Chris Masondb945352007-10-15 16:15:53 -04001977 memset(kaddr + offset, 1, end - start + 1);
1978 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001979 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001980 if (private == 0)
1981 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001982 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001983}
Chris Masonb888db22007-08-27 16:49:44 -04001984
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001985struct delayed_iput {
1986 struct list_head list;
1987 struct inode *inode;
1988};
1989
1990void btrfs_add_delayed_iput(struct inode *inode)
1991{
1992 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1993 struct delayed_iput *delayed;
1994
1995 if (atomic_add_unless(&inode->i_count, -1, 1))
1996 return;
1997
1998 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
1999 delayed->inode = inode;
2000
2001 spin_lock(&fs_info->delayed_iput_lock);
2002 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2003 spin_unlock(&fs_info->delayed_iput_lock);
2004}
2005
2006void btrfs_run_delayed_iputs(struct btrfs_root *root)
2007{
2008 LIST_HEAD(list);
2009 struct btrfs_fs_info *fs_info = root->fs_info;
2010 struct delayed_iput *delayed;
2011 int empty;
2012
2013 spin_lock(&fs_info->delayed_iput_lock);
2014 empty = list_empty(&fs_info->delayed_iputs);
2015 spin_unlock(&fs_info->delayed_iput_lock);
2016 if (empty)
2017 return;
2018
2019 down_read(&root->fs_info->cleanup_work_sem);
2020 spin_lock(&fs_info->delayed_iput_lock);
2021 list_splice_init(&fs_info->delayed_iputs, &list);
2022 spin_unlock(&fs_info->delayed_iput_lock);
2023
2024 while (!list_empty(&list)) {
2025 delayed = list_entry(list.next, struct delayed_iput, list);
2026 list_del(&delayed->list);
2027 iput(delayed->inode);
2028 kfree(delayed);
2029 }
2030 up_read(&root->fs_info->cleanup_work_sem);
2031}
2032
Josef Bacik7b128762008-07-24 12:17:14 -04002033/*
Yan, Zhengd68fc572010-05-16 10:49:58 -04002034 * calculate extra metadata reservation when snapshotting a subvolume
2035 * contains orphan files.
2036 */
2037void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2038 struct btrfs_pending_snapshot *pending,
2039 u64 *bytes_to_reserve)
2040{
2041 struct btrfs_root *root;
2042 struct btrfs_block_rsv *block_rsv;
2043 u64 num_bytes;
2044 int index;
2045
2046 root = pending->root;
2047 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2048 return;
2049
2050 block_rsv = root->orphan_block_rsv;
2051
2052 /* orphan block reservation for the snapshot */
2053 num_bytes = block_rsv->size;
2054
2055 /*
2056 * after the snapshot is created, COWing tree blocks may use more
2057 * space than it frees. So we should make sure there is enough
2058 * reserved space.
2059 */
2060 index = trans->transid & 0x1;
2061 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2062 num_bytes += block_rsv->size -
2063 (block_rsv->reserved + block_rsv->freed[index]);
2064 }
2065
2066 *bytes_to_reserve += num_bytes;
2067}
2068
2069void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2070 struct btrfs_pending_snapshot *pending)
2071{
2072 struct btrfs_root *root = pending->root;
2073 struct btrfs_root *snap = pending->snap;
2074 struct btrfs_block_rsv *block_rsv;
2075 u64 num_bytes;
2076 int index;
2077 int ret;
2078
2079 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2080 return;
2081
2082 /* refill source subvolume's orphan block reservation */
2083 block_rsv = root->orphan_block_rsv;
2084 index = trans->transid & 0x1;
2085 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2086 num_bytes = block_rsv->size -
2087 (block_rsv->reserved + block_rsv->freed[index]);
2088 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2089 root->orphan_block_rsv,
2090 num_bytes);
2091 BUG_ON(ret);
2092 }
2093
2094 /* setup orphan block reservation for the snapshot */
2095 block_rsv = btrfs_alloc_block_rsv(snap);
2096 BUG_ON(!block_rsv);
2097
2098 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2099 snap->orphan_block_rsv = block_rsv;
2100
2101 num_bytes = root->orphan_block_rsv->size;
2102 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2103 block_rsv, num_bytes);
2104 BUG_ON(ret);
2105
2106#if 0
2107 /* insert orphan item for the snapshot */
2108 WARN_ON(!root->orphan_item_inserted);
2109 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2110 snap->root_key.objectid);
2111 BUG_ON(ret);
2112 snap->orphan_item_inserted = 1;
2113#endif
2114}
2115
2116enum btrfs_orphan_cleanup_state {
2117 ORPHAN_CLEANUP_STARTED = 1,
2118 ORPHAN_CLEANUP_DONE = 2,
2119};
2120
2121/*
2122 * This is called in transaction commmit time. If there are no orphan
2123 * files in the subvolume, it removes orphan item and frees block_rsv
2124 * structure.
2125 */
2126void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2127 struct btrfs_root *root)
2128{
2129 int ret;
2130
2131 if (!list_empty(&root->orphan_list) ||
2132 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2133 return;
2134
2135 if (root->orphan_item_inserted &&
2136 btrfs_root_refs(&root->root_item) > 0) {
2137 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2138 root->root_key.objectid);
2139 BUG_ON(ret);
2140 root->orphan_item_inserted = 0;
2141 }
2142
2143 if (root->orphan_block_rsv) {
2144 WARN_ON(root->orphan_block_rsv->size > 0);
2145 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2146 root->orphan_block_rsv = NULL;
2147 }
2148}
2149
2150/*
Josef Bacik7b128762008-07-24 12:17:14 -04002151 * This creates an orphan entry for the given inode in case something goes
2152 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002153 *
2154 * NOTE: caller of this function should reserve 5 units of metadata for
2155 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002156 */
2157int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2158{
2159 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002160 struct btrfs_block_rsv *block_rsv = NULL;
2161 int reserve = 0;
2162 int insert = 0;
2163 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002164
Yan, Zhengd68fc572010-05-16 10:49:58 -04002165 if (!root->orphan_block_rsv) {
2166 block_rsv = btrfs_alloc_block_rsv(root);
2167 BUG_ON(!block_rsv);
Josef Bacik7b128762008-07-24 12:17:14 -04002168 }
2169
Yan, Zhengd68fc572010-05-16 10:49:58 -04002170 spin_lock(&root->orphan_lock);
2171 if (!root->orphan_block_rsv) {
2172 root->orphan_block_rsv = block_rsv;
2173 } else if (block_rsv) {
2174 btrfs_free_block_rsv(root, block_rsv);
2175 block_rsv = NULL;
2176 }
Josef Bacik7b128762008-07-24 12:17:14 -04002177
Yan, Zhengd68fc572010-05-16 10:49:58 -04002178 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2179 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2180#if 0
2181 /*
2182 * For proper ENOSPC handling, we should do orphan
2183 * cleanup when mounting. But this introduces backward
2184 * compatibility issue.
2185 */
2186 if (!xchg(&root->orphan_item_inserted, 1))
2187 insert = 2;
2188 else
2189 insert = 1;
2190#endif
2191 insert = 1;
2192 } else {
2193 WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
2194 }
Josef Bacik7b128762008-07-24 12:17:14 -04002195
Yan, Zhengd68fc572010-05-16 10:49:58 -04002196 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2197 BTRFS_I(inode)->orphan_meta_reserved = 1;
2198 reserve = 1;
2199 }
2200 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002201
Yan, Zhengd68fc572010-05-16 10:49:58 -04002202 if (block_rsv)
2203 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2204
2205 /* grab metadata reservation from transaction handle */
2206 if (reserve) {
2207 ret = btrfs_orphan_reserve_metadata(trans, inode);
2208 BUG_ON(ret);
2209 }
2210
2211 /* insert an orphan item to track this unlinked/truncated file */
2212 if (insert >= 1) {
2213 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2214 BUG_ON(ret);
2215 }
2216
2217 /* insert an orphan item to track subvolume contains orphan files */
2218 if (insert >= 2) {
2219 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2220 root->root_key.objectid);
2221 BUG_ON(ret);
2222 }
2223 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002224}
2225
2226/*
2227 * We have done the truncate/delete so we can go ahead and remove the orphan
2228 * item for this particular inode.
2229 */
2230int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2231{
2232 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002233 int delete_item = 0;
2234 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002235 int ret = 0;
2236
Yan, Zhengd68fc572010-05-16 10:49:58 -04002237 spin_lock(&root->orphan_lock);
2238 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2239 list_del_init(&BTRFS_I(inode)->i_orphan);
2240 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002241 }
2242
Yan, Zhengd68fc572010-05-16 10:49:58 -04002243 if (BTRFS_I(inode)->orphan_meta_reserved) {
2244 BTRFS_I(inode)->orphan_meta_reserved = 0;
2245 release_rsv = 1;
2246 }
2247 spin_unlock(&root->orphan_lock);
2248
2249 if (trans && delete_item) {
2250 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2251 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002252 }
2253
Yan, Zhengd68fc572010-05-16 10:49:58 -04002254 if (release_rsv)
2255 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002256
Yan, Zhengd68fc572010-05-16 10:49:58 -04002257 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002258}
2259
2260/*
2261 * this cleans up any orphans that may be left on the list from the last use
2262 * of this root.
2263 */
2264void btrfs_orphan_cleanup(struct btrfs_root *root)
2265{
2266 struct btrfs_path *path;
2267 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002268 struct btrfs_key key, found_key;
2269 struct btrfs_trans_handle *trans;
2270 struct inode *inode;
2271 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2272
Yan, Zhengd68fc572010-05-16 10:49:58 -04002273 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik7b128762008-07-24 12:17:14 -04002274 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002275
2276 path = btrfs_alloc_path();
2277 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002278 path->reada = -1;
2279
2280 key.objectid = BTRFS_ORPHAN_OBJECTID;
2281 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2282 key.offset = (u64)-1;
2283
Josef Bacik7b128762008-07-24 12:17:14 -04002284 while (1) {
2285 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2286 if (ret < 0) {
2287 printk(KERN_ERR "Error searching slot for orphan: %d"
2288 "\n", ret);
2289 break;
2290 }
2291
2292 /*
2293 * if ret == 0 means we found what we were searching for, which
2294 * is weird, but possible, so only screw with path if we didnt
2295 * find the key and see if we have stuff that matches
2296 */
2297 if (ret > 0) {
2298 if (path->slots[0] == 0)
2299 break;
2300 path->slots[0]--;
2301 }
2302
2303 /* pull out the item */
2304 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002305 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2306
2307 /* make sure the item matches what we want */
2308 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2309 break;
2310 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2311 break;
2312
2313 /* release the path since we're done with it */
2314 btrfs_release_path(root, path);
2315
2316 /*
2317 * this is where we are basically btrfs_lookup, without the
2318 * crossing root thing. we store the inode number in the
2319 * offset of the orphan item.
2320 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002321 found_key.objectid = found_key.offset;
2322 found_key.type = BTRFS_INODE_ITEM_KEY;
2323 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002324 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002325 BUG_ON(IS_ERR(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002326
Josef Bacik7b128762008-07-24 12:17:14 -04002327 /*
2328 * add this inode to the orphan list so btrfs_orphan_del does
2329 * the proper thing when we hit it
2330 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002331 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002332 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002333 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002334
2335 /*
2336 * if this is a bad inode, means we actually succeeded in
2337 * removing the inode, but not the orphan record, which means
2338 * we need to manually delete the orphan since iput will just
2339 * do a destroy_inode
2340 */
2341 if (is_bad_inode(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002342 trans = btrfs_start_transaction(root, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002343 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002344 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002345 iput(inode);
2346 continue;
2347 }
2348
2349 /* if we have links, this was a truncate, lets do that */
2350 if (inode->i_nlink) {
2351 nr_truncate++;
2352 btrfs_truncate(inode);
2353 } else {
2354 nr_unlink++;
2355 }
2356
2357 /* this will do delete_inode and everything for us */
2358 iput(inode);
2359 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002360 btrfs_free_path(path);
2361
2362 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2363
2364 if (root->orphan_block_rsv)
2365 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2366 (u64)-1);
2367
2368 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2369 trans = btrfs_join_transaction(root, 1);
2370 btrfs_end_transaction(trans, root);
2371 }
Josef Bacik7b128762008-07-24 12:17:14 -04002372
2373 if (nr_unlink)
2374 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2375 if (nr_truncate)
2376 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik7b128762008-07-24 12:17:14 -04002377}
2378
Chris Masond352ac62008-09-29 15:18:18 -04002379/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002380 * very simple check to peek ahead in the leaf looking for xattrs. If we
2381 * don't find any xattrs, we know there can't be any acls.
2382 *
2383 * slot is the slot the inode is in, objectid is the objectid of the inode
2384 */
2385static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2386 int slot, u64 objectid)
2387{
2388 u32 nritems = btrfs_header_nritems(leaf);
2389 struct btrfs_key found_key;
2390 int scanned = 0;
2391
2392 slot++;
2393 while (slot < nritems) {
2394 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2395
2396 /* we found a different objectid, there must not be acls */
2397 if (found_key.objectid != objectid)
2398 return 0;
2399
2400 /* we found an xattr, assume we've got an acl */
2401 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2402 return 1;
2403
2404 /*
2405 * we found a key greater than an xattr key, there can't
2406 * be any acls later on
2407 */
2408 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2409 return 0;
2410
2411 slot++;
2412 scanned++;
2413
2414 /*
2415 * it goes inode, inode backrefs, xattrs, extents,
2416 * so if there are a ton of hard links to an inode there can
2417 * be a lot of backrefs. Don't waste time searching too hard,
2418 * this is just an optimization
2419 */
2420 if (scanned >= 8)
2421 break;
2422 }
2423 /* we hit the end of the leaf before we found an xattr or
2424 * something larger than an xattr. We have to assume the inode
2425 * has acls
2426 */
2427 return 1;
2428}
2429
2430/*
Chris Masond352ac62008-09-29 15:18:18 -04002431 * read an inode from the btree into the in-memory inode
2432 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002433static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002434{
2435 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002436 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002437 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002438 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002439 struct btrfs_root *root = BTRFS_I(inode)->root;
2440 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002441 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002442 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002443 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002444 int ret;
2445
2446 path = btrfs_alloc_path();
2447 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002448 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002449
Chris Mason39279cc2007-06-12 06:35:45 -04002450 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002451 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002452 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002453
Chris Mason5f39d392007-10-15 16:14:19 -04002454 leaf = path->nodes[0];
2455 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2456 struct btrfs_inode_item);
2457
2458 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2459 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2460 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2461 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002462 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002463
2464 tspec = btrfs_inode_atime(inode_item);
2465 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2466 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2467
2468 tspec = btrfs_inode_mtime(inode_item);
2469 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2470 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2471
2472 tspec = btrfs_inode_ctime(inode_item);
2473 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2474 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2475
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002476 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002477 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002478 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002479 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002480 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002481 rdev = btrfs_inode_rdev(leaf, inode_item);
2482
Josef Bacikaec74772008-07-24 12:12:38 -04002483 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002484 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002485
Chris Mason5f39d392007-10-15 16:14:19 -04002486 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002487
Chris Mason46a53cc2009-04-27 11:47:50 -04002488 /*
2489 * try to precache a NULL acl entry for files that don't have
2490 * any xattrs or acls
2491 */
2492 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002493 if (!maybe_acls)
2494 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002495
Yan Zhengd2fb3432008-12-11 16:30:39 -05002496 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2497 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002498 btrfs_free_path(path);
2499 inode_item = NULL;
2500
Chris Mason39279cc2007-06-12 06:35:45 -04002501 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002502 case S_IFREG:
2503 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002504 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002505 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002506 inode->i_fop = &btrfs_file_operations;
2507 inode->i_op = &btrfs_file_inode_operations;
2508 break;
2509 case S_IFDIR:
2510 inode->i_fop = &btrfs_dir_file_operations;
2511 if (root == root->fs_info->tree_root)
2512 inode->i_op = &btrfs_dir_ro_inode_operations;
2513 else
2514 inode->i_op = &btrfs_dir_inode_operations;
2515 break;
2516 case S_IFLNK:
2517 inode->i_op = &btrfs_symlink_inode_operations;
2518 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002519 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002520 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002521 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002522 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002523 init_special_inode(inode, inode->i_mode, rdev);
2524 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002525 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002526
2527 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002528 return;
2529
2530make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002531 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002532 make_bad_inode(inode);
2533}
2534
Chris Masond352ac62008-09-29 15:18:18 -04002535/*
2536 * given a leaf and an inode, copy the inode fields into the leaf
2537 */
Chris Masone02119d2008-09-05 16:13:11 -04002538static void fill_inode_item(struct btrfs_trans_handle *trans,
2539 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002540 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002541 struct inode *inode)
2542{
Chris Mason5f39d392007-10-15 16:14:19 -04002543 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2544 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002545 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002546 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2547 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2548
2549 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2550 inode->i_atime.tv_sec);
2551 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2552 inode->i_atime.tv_nsec);
2553
2554 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2555 inode->i_mtime.tv_sec);
2556 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2557 inode->i_mtime.tv_nsec);
2558
2559 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2560 inode->i_ctime.tv_sec);
2561 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2562 inode->i_ctime.tv_nsec);
2563
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002564 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002565 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002566 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002567 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002568 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002569 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002570 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002571}
2572
Chris Masond352ac62008-09-29 15:18:18 -04002573/*
2574 * copy everything in the in-memory inode into the btree.
2575 */
Chris Masond3977122009-01-05 21:25:51 -05002576noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2577 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002578{
2579 struct btrfs_inode_item *inode_item;
2580 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002581 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002582 int ret;
2583
2584 path = btrfs_alloc_path();
2585 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002586 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002587 ret = btrfs_lookup_inode(trans, root, path,
2588 &BTRFS_I(inode)->location, 1);
2589 if (ret) {
2590 if (ret > 0)
2591 ret = -ENOENT;
2592 goto failed;
2593 }
2594
Chris Masonb4ce94d2009-02-04 09:25:08 -05002595 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002596 leaf = path->nodes[0];
2597 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002598 struct btrfs_inode_item);
2599
Chris Masone02119d2008-09-05 16:13:11 -04002600 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002601 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002602 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002603 ret = 0;
2604failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002605 btrfs_free_path(path);
2606 return ret;
2607}
2608
2609
Chris Masond352ac62008-09-29 15:18:18 -04002610/*
2611 * unlink helper that gets used here in inode.c and in the tree logging
2612 * recovery code. It remove a link in a directory with a given name, and
2613 * also drops the back refs in the inode to the directory
2614 */
Chris Masone02119d2008-09-05 16:13:11 -04002615int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2616 struct btrfs_root *root,
2617 struct inode *dir, struct inode *inode,
2618 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002619{
2620 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002621 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002622 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002623 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002624 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002625 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002626
2627 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002628 if (!path) {
2629 ret = -ENOMEM;
2630 goto err;
2631 }
2632
Chris Masonb9473432009-03-13 11:00:37 -04002633 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002634 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2635 name, name_len, -1);
2636 if (IS_ERR(di)) {
2637 ret = PTR_ERR(di);
2638 goto err;
2639 }
2640 if (!di) {
2641 ret = -ENOENT;
2642 goto err;
2643 }
Chris Mason5f39d392007-10-15 16:14:19 -04002644 leaf = path->nodes[0];
2645 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002646 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002647 if (ret)
2648 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002649 btrfs_release_path(root, path);
2650
Josef Bacikaec74772008-07-24 12:12:38 -04002651 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002652 inode->i_ino,
2653 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002654 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002655 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002656 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002657 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002658 goto err;
2659 }
2660
Chris Mason39279cc2007-06-12 06:35:45 -04002661 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002662 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002663 if (IS_ERR(di)) {
2664 ret = PTR_ERR(di);
2665 goto err;
2666 }
2667 if (!di) {
2668 ret = -ENOENT;
2669 goto err;
2670 }
2671 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002672 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002673
Chris Masone02119d2008-09-05 16:13:11 -04002674 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2675 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002676 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002677
2678 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2679 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002680 if (ret == -ENOENT)
2681 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002682err:
2683 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002684 if (ret)
2685 goto out;
2686
2687 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2688 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2689 btrfs_update_inode(trans, root, dir);
2690 btrfs_drop_nlink(inode);
2691 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002692out:
Chris Mason39279cc2007-06-12 06:35:45 -04002693 return ret;
2694}
2695
Yan, Zhenga22285a2010-05-16 10:48:46 -04002696/* helper to check if there is any shared block in the path */
2697static int check_path_shared(struct btrfs_root *root,
2698 struct btrfs_path *path)
2699{
2700 struct extent_buffer *eb;
2701 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002702 u64 refs = 1;
Andi Kleen411fc6b2010-10-29 15:14:31 -04002703 int uninitialized_var(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002704
2705 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2706 if (!path->nodes[level])
2707 break;
2708 eb = path->nodes[level];
2709 if (!btrfs_block_can_be_shared(root, eb))
2710 continue;
2711 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2712 &refs, NULL);
2713 if (refs > 1)
2714 return 1;
2715 }
Andi Kleen411fc6b2010-10-29 15:14:31 -04002716 return ret; /* XXX callers? */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002717}
2718
2719/*
2720 * helper to start transaction for unlink and rmdir.
2721 *
2722 * unlink and rmdir are special in btrfs, they do not always free space.
2723 * so in enospc case, we should make sure they will free space before
2724 * allowing them to use the global metadata reservation.
2725 */
2726static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2727 struct dentry *dentry)
2728{
2729 struct btrfs_trans_handle *trans;
2730 struct btrfs_root *root = BTRFS_I(dir)->root;
2731 struct btrfs_path *path;
2732 struct btrfs_inode_ref *ref;
2733 struct btrfs_dir_item *di;
2734 struct inode *inode = dentry->d_inode;
2735 u64 index;
2736 int check_link = 1;
2737 int err = -ENOSPC;
2738 int ret;
2739
2740 trans = btrfs_start_transaction(root, 10);
2741 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2742 return trans;
2743
2744 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2745 return ERR_PTR(-ENOSPC);
2746
2747 /* check if there is someone else holds reference */
2748 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2749 return ERR_PTR(-ENOSPC);
2750
2751 if (atomic_read(&inode->i_count) > 2)
2752 return ERR_PTR(-ENOSPC);
2753
2754 if (xchg(&root->fs_info->enospc_unlink, 1))
2755 return ERR_PTR(-ENOSPC);
2756
2757 path = btrfs_alloc_path();
2758 if (!path) {
2759 root->fs_info->enospc_unlink = 0;
2760 return ERR_PTR(-ENOMEM);
2761 }
2762
2763 trans = btrfs_start_transaction(root, 0);
2764 if (IS_ERR(trans)) {
2765 btrfs_free_path(path);
2766 root->fs_info->enospc_unlink = 0;
2767 return trans;
2768 }
2769
2770 path->skip_locking = 1;
2771 path->search_commit_root = 1;
2772
2773 ret = btrfs_lookup_inode(trans, root, path,
2774 &BTRFS_I(dir)->location, 0);
2775 if (ret < 0) {
2776 err = ret;
2777 goto out;
2778 }
2779 if (ret == 0) {
2780 if (check_path_shared(root, path))
2781 goto out;
2782 } else {
2783 check_link = 0;
2784 }
2785 btrfs_release_path(root, path);
2786
2787 ret = btrfs_lookup_inode(trans, root, path,
2788 &BTRFS_I(inode)->location, 0);
2789 if (ret < 0) {
2790 err = ret;
2791 goto out;
2792 }
2793 if (ret == 0) {
2794 if (check_path_shared(root, path))
2795 goto out;
2796 } else {
2797 check_link = 0;
2798 }
2799 btrfs_release_path(root, path);
2800
2801 if (ret == 0 && S_ISREG(inode->i_mode)) {
2802 ret = btrfs_lookup_file_extent(trans, root, path,
2803 inode->i_ino, (u64)-1, 0);
2804 if (ret < 0) {
2805 err = ret;
2806 goto out;
2807 }
2808 BUG_ON(ret == 0);
2809 if (check_path_shared(root, path))
2810 goto out;
2811 btrfs_release_path(root, path);
2812 }
2813
2814 if (!check_link) {
2815 err = 0;
2816 goto out;
2817 }
2818
2819 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2820 dentry->d_name.name, dentry->d_name.len, 0);
2821 if (IS_ERR(di)) {
2822 err = PTR_ERR(di);
2823 goto out;
2824 }
2825 if (di) {
2826 if (check_path_shared(root, path))
2827 goto out;
2828 } else {
2829 err = 0;
2830 goto out;
2831 }
2832 btrfs_release_path(root, path);
2833
2834 ref = btrfs_lookup_inode_ref(trans, root, path,
2835 dentry->d_name.name, dentry->d_name.len,
2836 inode->i_ino, dir->i_ino, 0);
2837 if (IS_ERR(ref)) {
2838 err = PTR_ERR(ref);
2839 goto out;
2840 }
2841 BUG_ON(!ref);
2842 if (check_path_shared(root, path))
2843 goto out;
2844 index = btrfs_inode_ref_index(path->nodes[0], ref);
2845 btrfs_release_path(root, path);
2846
2847 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2848 dentry->d_name.name, dentry->d_name.len, 0);
2849 if (IS_ERR(di)) {
2850 err = PTR_ERR(di);
2851 goto out;
2852 }
2853 BUG_ON(ret == -ENOENT);
2854 if (check_path_shared(root, path))
2855 goto out;
2856
2857 err = 0;
2858out:
2859 btrfs_free_path(path);
2860 if (err) {
2861 btrfs_end_transaction(trans, root);
2862 root->fs_info->enospc_unlink = 0;
2863 return ERR_PTR(err);
2864 }
2865
2866 trans->block_rsv = &root->fs_info->global_block_rsv;
2867 return trans;
2868}
2869
2870static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2871 struct btrfs_root *root)
2872{
2873 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2874 BUG_ON(!root->fs_info->enospc_unlink);
2875 root->fs_info->enospc_unlink = 0;
2876 }
2877 btrfs_end_transaction_throttle(trans, root);
2878}
2879
Chris Mason39279cc2007-06-12 06:35:45 -04002880static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2881{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002882 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002883 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002884 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002885 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002886 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002887
Yan, Zhenga22285a2010-05-16 10:48:46 -04002888 trans = __unlink_start_trans(dir, dentry);
2889 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002890 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002891
Chris Mason39279cc2007-06-12 06:35:45 -04002892 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002893
2894 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2895
Chris Masone02119d2008-09-05 16:13:11 -04002896 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2897 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002898 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002899
Yan, Zhenga22285a2010-05-16 10:48:46 -04002900 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002901 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002902 BUG_ON(ret);
2903 }
Josef Bacik7b128762008-07-24 12:17:14 -04002904
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002905 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002906 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002907 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002908 return ret;
2909}
2910
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002911int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2912 struct btrfs_root *root,
2913 struct inode *dir, u64 objectid,
2914 const char *name, int name_len)
2915{
2916 struct btrfs_path *path;
2917 struct extent_buffer *leaf;
2918 struct btrfs_dir_item *di;
2919 struct btrfs_key key;
2920 u64 index;
2921 int ret;
2922
2923 path = btrfs_alloc_path();
2924 if (!path)
2925 return -ENOMEM;
2926
2927 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2928 name, name_len, -1);
2929 BUG_ON(!di || IS_ERR(di));
2930
2931 leaf = path->nodes[0];
2932 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2933 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2934 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2935 BUG_ON(ret);
2936 btrfs_release_path(root, path);
2937
2938 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2939 objectid, root->root_key.objectid,
2940 dir->i_ino, &index, name, name_len);
2941 if (ret < 0) {
2942 BUG_ON(ret != -ENOENT);
2943 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2944 name, name_len);
2945 BUG_ON(!di || IS_ERR(di));
2946
2947 leaf = path->nodes[0];
2948 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2949 btrfs_release_path(root, path);
2950 index = key.offset;
2951 }
2952
2953 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2954 index, name, name_len, -1);
2955 BUG_ON(!di || IS_ERR(di));
2956
2957 leaf = path->nodes[0];
2958 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2959 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2960 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2961 BUG_ON(ret);
2962 btrfs_release_path(root, path);
2963
2964 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2965 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2966 ret = btrfs_update_inode(trans, root, dir);
2967 BUG_ON(ret);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002968
2969 btrfs_free_path(path);
2970 return 0;
2971}
2972
Chris Mason39279cc2007-06-12 06:35:45 -04002973static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2974{
2975 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002976 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002977 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002978 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002979 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002980
Chris Mason3394e162008-11-17 20:42:26 -05002981 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002982 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002983 return -ENOTEMPTY;
2984
Yan, Zhenga22285a2010-05-16 10:48:46 -04002985 trans = __unlink_start_trans(dir, dentry);
2986 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002987 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002988
Chris Mason39279cc2007-06-12 06:35:45 -04002989 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002990
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002991 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2992 err = btrfs_unlink_subvol(trans, root, dir,
2993 BTRFS_I(inode)->location.objectid,
2994 dentry->d_name.name,
2995 dentry->d_name.len);
2996 goto out;
2997 }
2998
Josef Bacik7b128762008-07-24 12:17:14 -04002999 err = btrfs_orphan_add(trans, inode);
3000 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003001 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003002
Chris Mason39279cc2007-06-12 06:35:45 -04003003 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003004 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3005 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003006 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003007 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003008out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003009 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003010 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003011 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003012
Chris Mason39279cc2007-06-12 06:35:45 -04003013 return err;
3014}
3015
Chris Masond20f7042008-12-08 16:58:54 -05003016#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04003017/*
Chris Mason323ac952008-10-01 19:05:46 -04003018 * when truncating bytes in a file, it is possible to avoid reading
3019 * the leaves that contain only checksum items. This can be the
3020 * majority of the IO required to delete a large file, but it must
3021 * be done carefully.
3022 *
3023 * The keys in the level just above the leaves are checked to make sure
3024 * the lowest key in a given leaf is a csum key, and starts at an offset
3025 * after the new size.
3026 *
3027 * Then the key for the next leaf is checked to make sure it also has
3028 * a checksum item for the same file. If it does, we know our target leaf
3029 * contains only checksum items, and it can be safely freed without reading
3030 * it.
3031 *
3032 * This is just an optimization targeted at large files. It may do
3033 * nothing. It will return 0 unless things went badly.
3034 */
3035static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3036 struct btrfs_root *root,
3037 struct btrfs_path *path,
3038 struct inode *inode, u64 new_size)
3039{
3040 struct btrfs_key key;
3041 int ret;
3042 int nritems;
3043 struct btrfs_key found_key;
3044 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003045 struct btrfs_leaf_ref *ref;
3046 u64 leaf_gen;
3047 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04003048
3049 path->lowest_level = 1;
3050 key.objectid = inode->i_ino;
3051 key.type = BTRFS_CSUM_ITEM_KEY;
3052 key.offset = new_size;
3053again:
3054 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3055 if (ret < 0)
3056 goto out;
3057
3058 if (path->nodes[1] == NULL) {
3059 ret = 0;
3060 goto out;
3061 }
3062 ret = 0;
3063 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3064 nritems = btrfs_header_nritems(path->nodes[1]);
3065
3066 if (!nritems)
3067 goto out;
3068
3069 if (path->slots[1] >= nritems)
3070 goto next_node;
3071
3072 /* did we find a key greater than anything we want to delete? */
3073 if (found_key.objectid > inode->i_ino ||
3074 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3075 goto out;
3076
3077 /* we check the next key in the node to make sure the leave contains
3078 * only checksum items. This comparison doesn't work if our
3079 * leaf is the last one in the node
3080 */
3081 if (path->slots[1] + 1 >= nritems) {
3082next_node:
3083 /* search forward from the last key in the node, this
3084 * will bring us into the next node in the tree
3085 */
3086 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3087
3088 /* unlikely, but we inc below, so check to be safe */
3089 if (found_key.offset == (u64)-1)
3090 goto out;
3091
3092 /* search_forward needs a path with locks held, do the
3093 * search again for the original key. It is possible
3094 * this will race with a balance and return a path that
3095 * we could modify, but this drop is just an optimization
3096 * and is allowed to miss some leaves.
3097 */
3098 btrfs_release_path(root, path);
3099 found_key.offset++;
3100
3101 /* setup a max key for search_forward */
3102 other_key.offset = (u64)-1;
3103 other_key.type = key.type;
3104 other_key.objectid = key.objectid;
3105
3106 path->keep_locks = 1;
3107 ret = btrfs_search_forward(root, &found_key, &other_key,
3108 path, 0, 0);
3109 path->keep_locks = 0;
3110 if (ret || found_key.objectid != key.objectid ||
3111 found_key.type != key.type) {
3112 ret = 0;
3113 goto out;
3114 }
3115
3116 key.offset = found_key.offset;
3117 btrfs_release_path(root, path);
3118 cond_resched();
3119 goto again;
3120 }
3121
3122 /* we know there's one more slot after us in the tree,
3123 * read that key so we can verify it is also a checksum item
3124 */
3125 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3126
3127 if (found_key.objectid < inode->i_ino)
3128 goto next_key;
3129
3130 if (found_key.type != key.type || found_key.offset < new_size)
3131 goto next_key;
3132
3133 /*
3134 * if the key for the next leaf isn't a csum key from this objectid,
3135 * we can't be sure there aren't good items inside this leaf.
3136 * Bail out
3137 */
3138 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3139 goto out;
3140
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003141 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3142 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003143 /*
3144 * it is safe to delete this leaf, it contains only
3145 * csum items from this inode at an offset >= new_size
3146 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003147 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04003148 BUG_ON(ret);
3149
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003150 if (root->ref_cows && leaf_gen < trans->transid) {
3151 ref = btrfs_alloc_leaf_ref(root, 0);
3152 if (ref) {
3153 ref->root_gen = root->root_key.offset;
3154 ref->bytenr = leaf_start;
3155 ref->owner = 0;
3156 ref->generation = leaf_gen;
3157 ref->nritems = 0;
3158
Chris Masonbd56b302009-02-04 09:27:02 -05003159 btrfs_sort_leaf_ref(ref);
3160
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003161 ret = btrfs_add_leaf_ref(root, ref, 0);
3162 WARN_ON(ret);
3163 btrfs_free_leaf_ref(root, ref);
3164 } else {
3165 WARN_ON(1);
3166 }
3167 }
Chris Mason323ac952008-10-01 19:05:46 -04003168next_key:
3169 btrfs_release_path(root, path);
3170
3171 if (other_key.objectid == inode->i_ino &&
3172 other_key.type == key.type && other_key.offset > key.offset) {
3173 key.offset = other_key.offset;
3174 cond_resched();
3175 goto again;
3176 }
3177 ret = 0;
3178out:
3179 /* fixup any changes we've made to the path */
3180 path->lowest_level = 0;
3181 path->keep_locks = 0;
3182 btrfs_release_path(root, path);
3183 return ret;
3184}
3185
Chris Masond20f7042008-12-08 16:58:54 -05003186#endif
3187
Chris Mason323ac952008-10-01 19:05:46 -04003188/*
Chris Mason39279cc2007-06-12 06:35:45 -04003189 * this can truncate away extent items, csum items and directory items.
3190 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003191 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003192 *
3193 * csum items that cross the new i_size are truncated to the new size
3194 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003195 *
3196 * min_type is the minimum key type to truncate down to. If set to 0, this
3197 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003198 */
Yan, Zheng80825102009-11-12 09:35:36 +00003199int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3200 struct btrfs_root *root,
3201 struct inode *inode,
3202 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003203{
Chris Mason39279cc2007-06-12 06:35:45 -04003204 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003205 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003206 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003207 struct btrfs_key key;
3208 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003209 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003210 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003211 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003212 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003213 u64 mask = root->sectorsize - 1;
3214 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003215 int found_extent;
3216 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003217 int pending_del_nr = 0;
3218 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003219 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003220 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003221 int ret;
3222 int err = 0;
3223
3224 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003225
Josef Bacik0af3d002010-06-21 14:48:16 -04003226 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003227 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003228
Chris Mason39279cc2007-06-12 06:35:45 -04003229 path = btrfs_alloc_path();
3230 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003231 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003232
Chris Mason39279cc2007-06-12 06:35:45 -04003233 key.objectid = inode->i_ino;
3234 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003235 key.type = (u8)-1;
3236
Chris Mason85e21ba2008-01-29 15:11:36 -05003237search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003238 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003239 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003240 if (ret < 0) {
3241 err = ret;
3242 goto out;
3243 }
Chris Masond3977122009-01-05 21:25:51 -05003244
Chris Mason85e21ba2008-01-29 15:11:36 -05003245 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003246 /* there are no items in the tree for us to truncate, we're
3247 * done
3248 */
Yan, Zheng80825102009-11-12 09:35:36 +00003249 if (path->slots[0] == 0)
3250 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003251 path->slots[0]--;
3252 }
3253
Chris Masond3977122009-01-05 21:25:51 -05003254 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003255 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003256 leaf = path->nodes[0];
3257 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3258 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003259 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003260
Chris Mason5f39d392007-10-15 16:14:19 -04003261 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003262 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003263
Chris Mason85e21ba2008-01-29 15:11:36 -05003264 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003265 break;
3266
Chris Mason5f39d392007-10-15 16:14:19 -04003267 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003268 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003269 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003270 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003271 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003272 encoding = btrfs_file_extent_compression(leaf, fi);
3273 encoding |= btrfs_file_extent_encryption(leaf, fi);
3274 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3275
Chris Mason179e29e2007-11-01 11:28:41 -04003276 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003277 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003278 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003279 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003280 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003281 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003282 }
Yan008630c2007-11-07 13:31:09 -05003283 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003284 }
Yan, Zheng80825102009-11-12 09:35:36 +00003285 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003286 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003287 } else {
3288 if (item_end < new_size)
3289 break;
3290 if (found_key.offset >= new_size)
3291 del_item = 1;
3292 else
3293 del_item = 0;
3294 }
Chris Mason39279cc2007-06-12 06:35:45 -04003295 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003296 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003297 if (found_type != BTRFS_EXTENT_DATA_KEY)
3298 goto delete;
3299
3300 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003301 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003302 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003303 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003304 u64 orig_num_bytes =
3305 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003306 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003307 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003308 extent_num_bytes = extent_num_bytes &
3309 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003310 btrfs_set_file_extent_num_bytes(leaf, fi,
3311 extent_num_bytes);
3312 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003313 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003314 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003315 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003316 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003317 } else {
Chris Masondb945352007-10-15 16:15:53 -04003318 extent_num_bytes =
3319 btrfs_file_extent_disk_num_bytes(leaf,
3320 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003321 extent_offset = found_key.offset -
3322 btrfs_file_extent_offset(leaf, fi);
3323
Chris Mason39279cc2007-06-12 06:35:45 -04003324 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003325 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003326 if (extent_start != 0) {
3327 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003328 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003329 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003330 }
3331 }
Chris Mason90692182008-02-08 13:49:28 -05003332 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003333 /*
3334 * we can't truncate inline items that have had
3335 * special encodings
3336 */
3337 if (!del_item &&
3338 btrfs_file_extent_compression(leaf, fi) == 0 &&
3339 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3340 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003341 u32 size = new_size - found_key.offset;
3342
3343 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003344 inode_sub_bytes(inode, item_end + 1 -
3345 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003346 }
3347 size =
3348 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003349 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003350 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003351 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003352 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003353 inode_sub_bytes(inode, item_end + 1 -
3354 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003355 }
Chris Mason39279cc2007-06-12 06:35:45 -04003356 }
Chris Mason179e29e2007-11-01 11:28:41 -04003357delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003358 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003359 if (!pending_del_nr) {
3360 /* no pending yet, add ourselves */
3361 pending_del_slot = path->slots[0];
3362 pending_del_nr = 1;
3363 } else if (pending_del_nr &&
3364 path->slots[0] + 1 == pending_del_slot) {
3365 /* hop on the pending chunk */
3366 pending_del_nr++;
3367 pending_del_slot = path->slots[0];
3368 } else {
Chris Masond3977122009-01-05 21:25:51 -05003369 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003370 }
Chris Mason39279cc2007-06-12 06:35:45 -04003371 } else {
3372 break;
3373 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003374 if (found_extent && (root->ref_cows ||
3375 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003376 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003377 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003378 extent_num_bytes, 0,
3379 btrfs_header_owner(leaf),
3380 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003381 BUG_ON(ret);
3382 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003383
Yan, Zheng80825102009-11-12 09:35:36 +00003384 if (found_type == BTRFS_INODE_ITEM_KEY)
3385 break;
3386
3387 if (path->slots[0] == 0 ||
3388 path->slots[0] != pending_del_slot) {
3389 if (root->ref_cows) {
3390 err = -EAGAIN;
3391 goto out;
3392 }
3393 if (pending_del_nr) {
3394 ret = btrfs_del_items(trans, root, path,
3395 pending_del_slot,
3396 pending_del_nr);
3397 BUG_ON(ret);
3398 pending_del_nr = 0;
3399 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003400 btrfs_release_path(root, path);
3401 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003402 } else {
3403 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003404 }
Chris Mason39279cc2007-06-12 06:35:45 -04003405 }
Yan, Zheng80825102009-11-12 09:35:36 +00003406out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003407 if (pending_del_nr) {
3408 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3409 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003410 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003411 }
Chris Mason39279cc2007-06-12 06:35:45 -04003412 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003413 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003414}
3415
Chris Masona52d9a82007-08-27 16:49:44 -04003416/*
3417 * taken from block_truncate_page, but does cow as it zeros out
3418 * any bytes left in the last page in the file.
3419 */
3420static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3421{
3422 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003423 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003424 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3425 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003426 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003427 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003428 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003429 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3430 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3431 struct page *page;
3432 int ret = 0;
3433 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003434 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003435
3436 if ((offset & (blocksize - 1)) == 0)
3437 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003438 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003439 if (ret)
3440 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003441
3442 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003443again:
Chris Masona52d9a82007-08-27 16:49:44 -04003444 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003445 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003446 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003447 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003448 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003449
3450 page_start = page_offset(page);
3451 page_end = page_start + PAGE_CACHE_SIZE - 1;
3452
Chris Masona52d9a82007-08-27 16:49:44 -04003453 if (!PageUptodate(page)) {
3454 ret = btrfs_readpage(NULL, page);
3455 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003456 if (page->mapping != mapping) {
3457 unlock_page(page);
3458 page_cache_release(page);
3459 goto again;
3460 }
Chris Masona52d9a82007-08-27 16:49:44 -04003461 if (!PageUptodate(page)) {
3462 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003463 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003464 }
3465 }
Chris Mason211c17f2008-05-15 09:13:45 -04003466 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003467
Josef Bacik2ac55d42010-02-03 19:33:23 +00003468 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3469 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003470 set_page_extent_mapped(page);
3471
3472 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3473 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003474 unlock_extent_cached(io_tree, page_start, page_end,
3475 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003476 unlock_page(page);
3477 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003478 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003479 btrfs_put_ordered_extent(ordered);
3480 goto again;
3481 }
3482
Josef Bacik2ac55d42010-02-03 19:33:23 +00003483 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003484 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003485 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003486
Josef Bacik2ac55d42010-02-03 19:33:23 +00003487 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3488 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003489 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003490 unlock_extent_cached(io_tree, page_start, page_end,
3491 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003492 goto out_unlock;
3493 }
3494
Chris Masone6dcd2d2008-07-17 12:53:50 -04003495 ret = 0;
3496 if (offset != PAGE_CACHE_SIZE) {
3497 kaddr = kmap(page);
3498 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3499 flush_dcache_page(page);
3500 kunmap(page);
3501 }
Chris Mason247e7432008-07-17 12:53:51 -04003502 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003503 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003504 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3505 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003506
Chris Mason89642222008-07-24 09:41:53 -04003507out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003508 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003509 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003510 unlock_page(page);
3511 page_cache_release(page);
3512out:
3513 return ret;
3514}
3515
Yan Zheng9036c102008-10-30 14:19:41 -04003516int btrfs_cont_expand(struct inode *inode, loff_t size)
3517{
3518 struct btrfs_trans_handle *trans;
3519 struct btrfs_root *root = BTRFS_I(inode)->root;
3520 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003521 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003522 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003523 u64 mask = root->sectorsize - 1;
3524 u64 hole_start = (inode->i_size + mask) & ~mask;
3525 u64 block_end = (size + mask) & ~mask;
3526 u64 last_byte;
3527 u64 cur_offset;
3528 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003529 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003530
3531 if (size <= hole_start)
3532 return 0;
3533
Yan Zheng9036c102008-10-30 14:19:41 -04003534 while (1) {
3535 struct btrfs_ordered_extent *ordered;
3536 btrfs_wait_ordered_range(inode, hole_start,
3537 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003538 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3539 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003540 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3541 if (!ordered)
3542 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003543 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3544 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003545 btrfs_put_ordered_extent(ordered);
3546 }
3547
Yan Zheng9036c102008-10-30 14:19:41 -04003548 cur_offset = hole_start;
3549 while (1) {
3550 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3551 block_end - cur_offset, 0);
3552 BUG_ON(IS_ERR(em) || !em);
3553 last_byte = min(extent_map_end(em), block_end);
3554 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003555 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003556 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003557 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003558
Yan, Zhenga22285a2010-05-16 10:48:46 -04003559 trans = btrfs_start_transaction(root, 2);
3560 if (IS_ERR(trans)) {
3561 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003562 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003563 }
Yan, Zheng80825102009-11-12 09:35:36 +00003564 btrfs_set_trans_block_group(trans, inode);
3565
3566 err = btrfs_drop_extents(trans, inode, cur_offset,
3567 cur_offset + hole_size,
3568 &hint_byte, 1);
3569 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003570
Yan Zheng9036c102008-10-30 14:19:41 -04003571 err = btrfs_insert_file_extent(trans, root,
3572 inode->i_ino, cur_offset, 0,
3573 0, hole_size, 0, hole_size,
3574 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003575 BUG_ON(err);
3576
Yan Zheng9036c102008-10-30 14:19:41 -04003577 btrfs_drop_extent_cache(inode, hole_start,
3578 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003579
3580 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003581 }
3582 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003583 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003584 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003585 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003586 break;
3587 }
3588
Yan, Zhenga22285a2010-05-16 10:48:46 -04003589 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003590 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3591 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003592 return err;
3593}
3594
Yan, Zheng80825102009-11-12 09:35:36 +00003595static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3596{
3597 struct btrfs_root *root = BTRFS_I(inode)->root;
3598 struct btrfs_trans_handle *trans;
3599 unsigned long nr;
3600 int ret;
3601
3602 if (attr->ia_size == inode->i_size)
3603 return 0;
3604
3605 if (attr->ia_size > inode->i_size) {
3606 unsigned long limit;
3607 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3608 if (attr->ia_size > inode->i_sb->s_maxbytes)
3609 return -EFBIG;
3610 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3611 send_sig(SIGXFSZ, current, 0);
3612 return -EFBIG;
3613 }
3614 }
3615
Yan, Zhengd68fc572010-05-16 10:49:58 -04003616 trans = btrfs_start_transaction(root, 5);
3617 if (IS_ERR(trans))
3618 return PTR_ERR(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00003619
Yan, Zheng80825102009-11-12 09:35:36 +00003620 btrfs_set_trans_block_group(trans, inode);
3621
3622 ret = btrfs_orphan_add(trans, inode);
3623 BUG_ON(ret);
3624
3625 nr = trans->blocks_used;
3626 btrfs_end_transaction(trans, root);
Yan, Zheng80825102009-11-12 09:35:36 +00003627 btrfs_btree_balance_dirty(root, nr);
3628
3629 if (attr->ia_size > inode->i_size) {
3630 ret = btrfs_cont_expand(inode, attr->ia_size);
3631 if (ret) {
3632 btrfs_truncate(inode);
3633 return ret;
3634 }
3635
3636 i_size_write(inode, attr->ia_size);
3637 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3638
Yan, Zhengd68fc572010-05-16 10:49:58 -04003639 trans = btrfs_start_transaction(root, 0);
3640 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003641 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003642 trans->block_rsv = root->orphan_block_rsv;
3643 BUG_ON(!trans->block_rsv);
Yan, Zheng80825102009-11-12 09:35:36 +00003644
3645 ret = btrfs_update_inode(trans, root, inode);
3646 BUG_ON(ret);
3647 if (inode->i_nlink > 0) {
3648 ret = btrfs_orphan_del(trans, inode);
3649 BUG_ON(ret);
3650 }
3651 nr = trans->blocks_used;
3652 btrfs_end_transaction(trans, root);
3653 btrfs_btree_balance_dirty(root, nr);
3654 return 0;
3655 }
3656
3657 /*
3658 * We're truncating a file that used to have good data down to
3659 * zero. Make sure it gets into the ordered flush list so that
3660 * any new writes get down to disk quickly.
3661 */
3662 if (attr->ia_size == 0)
3663 BTRFS_I(inode)->ordered_data_close = 1;
3664
3665 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3666 ret = vmtruncate(inode, attr->ia_size);
3667 BUG_ON(ret);
3668
3669 return 0;
3670}
3671
Chris Mason39279cc2007-06-12 06:35:45 -04003672static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3673{
3674 struct inode *inode = dentry->d_inode;
3675 int err;
3676
3677 err = inode_change_ok(inode, attr);
3678 if (err)
3679 return err;
3680
Chris Mason5a3f23d2009-03-31 13:27:11 -04003681 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003682 err = btrfs_setattr_size(inode, attr);
3683 if (err)
3684 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003685 }
Yan Zheng9036c102008-10-30 14:19:41 -04003686
Christoph Hellwig10257742010-06-04 11:30:02 +02003687 if (attr->ia_valid) {
3688 setattr_copy(inode, attr);
3689 mark_inode_dirty(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003690
Christoph Hellwig10257742010-06-04 11:30:02 +02003691 if (attr->ia_valid & ATTR_MODE)
3692 err = btrfs_acl_chmod(inode);
3693 }
3694
Chris Mason39279cc2007-06-12 06:35:45 -04003695 return err;
3696}
Chris Mason61295eb2008-01-14 16:24:38 -05003697
Al Virobd555972010-06-07 11:35:40 -04003698void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003699{
3700 struct btrfs_trans_handle *trans;
3701 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003702 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003703 int ret;
3704
3705 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003706 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3707 root == root->fs_info->tree_root))
Al Virobd555972010-06-07 11:35:40 -04003708 goto no_delete;
3709
Chris Mason39279cc2007-06-12 06:35:45 -04003710 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003711 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003712 goto no_delete;
3713 }
Al Virobd555972010-06-07 11:35:40 -04003714 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003715 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003716
Yan, Zhengc71bf092009-11-12 09:34:40 +00003717 if (root->fs_info->log_root_recovering) {
3718 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3719 goto no_delete;
3720 }
3721
Yan, Zheng76dda932009-09-21 16:00:26 -04003722 if (inode->i_nlink > 0) {
3723 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3724 goto no_delete;
3725 }
3726
Chris Masondbe674a2008-07-17 12:54:05 -04003727 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003728
Yan, Zheng80825102009-11-12 09:35:36 +00003729 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003730 trans = btrfs_start_transaction(root, 0);
3731 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003732 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003733 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003734
Yan, Zhengd68fc572010-05-16 10:49:58 -04003735 ret = btrfs_block_rsv_check(trans, root,
3736 root->orphan_block_rsv, 0, 5);
3737 if (ret) {
3738 BUG_ON(ret != -EAGAIN);
3739 ret = btrfs_commit_transaction(trans, root);
3740 BUG_ON(ret);
3741 continue;
3742 }
3743
3744 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003745 if (ret != -EAGAIN)
3746 break;
3747
3748 nr = trans->blocks_used;
3749 btrfs_end_transaction(trans, root);
3750 trans = NULL;
3751 btrfs_btree_balance_dirty(root, nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003752
Josef Bacik7b128762008-07-24 12:17:14 -04003753 }
3754
Yan, Zheng80825102009-11-12 09:35:36 +00003755 if (ret == 0) {
3756 ret = btrfs_orphan_del(trans, inode);
3757 BUG_ON(ret);
3758 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003759
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003760 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003761 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003762 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003763no_delete:
Al Virobd555972010-06-07 11:35:40 -04003764 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003765 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003766}
3767
3768/*
3769 * this returns the key found in the dir entry in the location pointer.
3770 * If no dir entries were found, location->objectid is 0.
3771 */
3772static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3773 struct btrfs_key *location)
3774{
3775 const char *name = dentry->d_name.name;
3776 int namelen = dentry->d_name.len;
3777 struct btrfs_dir_item *di;
3778 struct btrfs_path *path;
3779 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003780 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003781
3782 path = btrfs_alloc_path();
3783 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003784
Chris Mason39279cc2007-06-12 06:35:45 -04003785 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3786 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003787 if (IS_ERR(di))
3788 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003789
3790 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003791 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003792
Chris Mason5f39d392007-10-15 16:14:19 -04003793 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003794out:
Chris Mason39279cc2007-06-12 06:35:45 -04003795 btrfs_free_path(path);
3796 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003797out_err:
3798 location->objectid = 0;
3799 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003800}
3801
3802/*
3803 * when we hit a tree root in a directory, the btrfs part of the inode
3804 * needs to be changed to reflect the root directory of the tree root. This
3805 * is kind of like crossing a mount point.
3806 */
3807static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003808 struct inode *dir,
3809 struct dentry *dentry,
3810 struct btrfs_key *location,
3811 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003812{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003813 struct btrfs_path *path;
3814 struct btrfs_root *new_root;
3815 struct btrfs_root_ref *ref;
3816 struct extent_buffer *leaf;
3817 int ret;
3818 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003819
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003820 path = btrfs_alloc_path();
3821 if (!path) {
3822 err = -ENOMEM;
3823 goto out;
3824 }
Chris Mason39279cc2007-06-12 06:35:45 -04003825
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003826 err = -ENOENT;
3827 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3828 BTRFS_I(dir)->root->root_key.objectid,
3829 location->objectid);
3830 if (ret) {
3831 if (ret < 0)
3832 err = ret;
3833 goto out;
3834 }
Chris Mason39279cc2007-06-12 06:35:45 -04003835
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003836 leaf = path->nodes[0];
3837 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3838 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3839 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3840 goto out;
3841
3842 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3843 (unsigned long)(ref + 1),
3844 dentry->d_name.len);
3845 if (ret)
3846 goto out;
3847
3848 btrfs_release_path(root->fs_info->tree_root, path);
3849
3850 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3851 if (IS_ERR(new_root)) {
3852 err = PTR_ERR(new_root);
3853 goto out;
3854 }
3855
3856 if (btrfs_root_refs(&new_root->root_item) == 0) {
3857 err = -ENOENT;
3858 goto out;
3859 }
3860
3861 *sub_root = new_root;
3862 location->objectid = btrfs_root_dirid(&new_root->root_item);
3863 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003864 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003865 err = 0;
3866out:
3867 btrfs_free_path(path);
3868 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003869}
3870
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003871static void inode_tree_add(struct inode *inode)
3872{
3873 struct btrfs_root *root = BTRFS_I(inode)->root;
3874 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003875 struct rb_node **p;
3876 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003877again:
3878 p = &root->inode_tree.rb_node;
3879 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003880
Yan, Zheng76dda932009-09-21 16:00:26 -04003881 if (hlist_unhashed(&inode->i_hash))
3882 return;
3883
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003884 spin_lock(&root->inode_lock);
3885 while (*p) {
3886 parent = *p;
3887 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3888
3889 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003890 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003891 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003892 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003893 else {
3894 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003895 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003896 rb_erase(parent, &root->inode_tree);
3897 RB_CLEAR_NODE(parent);
3898 spin_unlock(&root->inode_lock);
3899 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003900 }
3901 }
3902 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3903 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3904 spin_unlock(&root->inode_lock);
3905}
3906
3907static void inode_tree_del(struct inode *inode)
3908{
3909 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003910 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003911
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003912 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003913 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003914 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003915 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003916 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003917 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003918 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003919
Josef Bacik0af3d002010-06-21 14:48:16 -04003920 /*
3921 * Free space cache has inodes in the tree root, but the tree root has a
3922 * root_refs of 0, so this could end up dropping the tree root as a
3923 * snapshot, so we need the extra !root->fs_info->tree_root check to
3924 * make sure we don't drop it.
3925 */
3926 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3927 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003928 synchronize_srcu(&root->fs_info->subvol_srcu);
3929 spin_lock(&root->inode_lock);
3930 empty = RB_EMPTY_ROOT(&root->inode_tree);
3931 spin_unlock(&root->inode_lock);
3932 if (empty)
3933 btrfs_add_dead_root(root);
3934 }
3935}
3936
3937int btrfs_invalidate_inodes(struct btrfs_root *root)
3938{
3939 struct rb_node *node;
3940 struct rb_node *prev;
3941 struct btrfs_inode *entry;
3942 struct inode *inode;
3943 u64 objectid = 0;
3944
3945 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3946
3947 spin_lock(&root->inode_lock);
3948again:
3949 node = root->inode_tree.rb_node;
3950 prev = NULL;
3951 while (node) {
3952 prev = node;
3953 entry = rb_entry(node, struct btrfs_inode, rb_node);
3954
3955 if (objectid < entry->vfs_inode.i_ino)
3956 node = node->rb_left;
3957 else if (objectid > entry->vfs_inode.i_ino)
3958 node = node->rb_right;
3959 else
3960 break;
3961 }
3962 if (!node) {
3963 while (prev) {
3964 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3965 if (objectid <= entry->vfs_inode.i_ino) {
3966 node = prev;
3967 break;
3968 }
3969 prev = rb_next(prev);
3970 }
3971 }
3972 while (node) {
3973 entry = rb_entry(node, struct btrfs_inode, rb_node);
3974 objectid = entry->vfs_inode.i_ino + 1;
3975 inode = igrab(&entry->vfs_inode);
3976 if (inode) {
3977 spin_unlock(&root->inode_lock);
3978 if (atomic_read(&inode->i_count) > 1)
3979 d_prune_aliases(inode);
3980 /*
Al Viro45321ac2010-06-07 13:43:19 -04003981 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04003982 * the inode cache when its usage count
3983 * hits zero.
3984 */
3985 iput(inode);
3986 cond_resched();
3987 spin_lock(&root->inode_lock);
3988 goto again;
3989 }
3990
3991 if (cond_resched_lock(&root->inode_lock))
3992 goto again;
3993
3994 node = rb_next(node);
3995 }
3996 spin_unlock(&root->inode_lock);
3997 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003998}
3999
Chris Masone02119d2008-09-05 16:13:11 -04004000static int btrfs_init_locked_inode(struct inode *inode, void *p)
4001{
4002 struct btrfs_iget_args *args = p;
4003 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004004 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004005 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004006 return 0;
4007}
4008
4009static int btrfs_find_actor(struct inode *inode, void *opaque)
4010{
4011 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05004012 return args->ino == inode->i_ino &&
4013 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004014}
4015
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004016static struct inode *btrfs_iget_locked(struct super_block *s,
4017 u64 objectid,
4018 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004019{
4020 struct inode *inode;
4021 struct btrfs_iget_args args;
4022 args.ino = objectid;
4023 args.root = root;
4024
4025 inode = iget5_locked(s, objectid, btrfs_find_actor,
4026 btrfs_init_locked_inode,
4027 (void *)&args);
4028 return inode;
4029}
4030
Balaji Rao1a54ef82008-07-21 02:01:04 +05304031/* Get an inode object given its location and corresponding root.
4032 * Returns in *is_new if the inode was read from disk
4033 */
4034struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004035 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304036{
4037 struct inode *inode;
4038
4039 inode = btrfs_iget_locked(s, location->objectid, root);
4040 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004041 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304042
4043 if (inode->i_state & I_NEW) {
4044 BTRFS_I(inode)->root = root;
4045 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4046 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004047
4048 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304049 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00004050 if (new)
4051 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05304052 }
4053
4054 return inode;
4055}
4056
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004057static struct inode *new_simple_dir(struct super_block *s,
4058 struct btrfs_key *key,
4059 struct btrfs_root *root)
4060{
4061 struct inode *inode = new_inode(s);
4062
4063 if (!inode)
4064 return ERR_PTR(-ENOMEM);
4065
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004066 BTRFS_I(inode)->root = root;
4067 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4068 BTRFS_I(inode)->dummy_inode = 1;
4069
4070 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4071 inode->i_op = &simple_dir_inode_operations;
4072 inode->i_fop = &simple_dir_operations;
4073 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4074 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4075
4076 return inode;
4077}
4078
Chris Mason3de45862008-11-17 21:02:50 -05004079struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004080{
Chris Masond3977122009-01-05 21:25:51 -05004081 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004082 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004083 struct btrfs_root *sub_root = root;
4084 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004085 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004086 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004087
Yan, Zheng76dda932009-09-21 16:00:26 -04004088 dentry->d_op = &btrfs_dentry_operations;
4089
Chris Mason39279cc2007-06-12 06:35:45 -04004090 if (dentry->d_name.len > BTRFS_NAME_LEN)
4091 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004092
Chris Mason39279cc2007-06-12 06:35:45 -04004093 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04004094
Chris Mason39279cc2007-06-12 06:35:45 -04004095 if (ret < 0)
4096 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004097
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004098 if (location.objectid == 0)
4099 return NULL;
4100
4101 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004102 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004103 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004104 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004105
4106 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4107
Yan, Zheng76dda932009-09-21 16:00:26 -04004108 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004109 ret = fixup_tree_root_location(root, dir, dentry,
4110 &location, &sub_root);
4111 if (ret < 0) {
4112 if (ret != -ENOENT)
4113 inode = ERR_PTR(ret);
4114 else
4115 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4116 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004117 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004118 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004119 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4120
Yan, Zhengc71bf092009-11-12 09:34:40 +00004121 if (root != sub_root) {
4122 down_read(&root->fs_info->cleanup_work_sem);
4123 if (!(inode->i_sb->s_flags & MS_RDONLY))
4124 btrfs_orphan_cleanup(sub_root);
4125 up_read(&root->fs_info->cleanup_work_sem);
4126 }
4127
Chris Mason3de45862008-11-17 21:02:50 -05004128 return inode;
4129}
4130
Yan, Zheng76dda932009-09-21 16:00:26 -04004131static int btrfs_dentry_delete(struct dentry *dentry)
4132{
4133 struct btrfs_root *root;
4134
Yan, Zhengefefb142009-10-09 09:25:16 -04004135 if (!dentry->d_inode && !IS_ROOT(dentry))
4136 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004137
Yan, Zhengefefb142009-10-09 09:25:16 -04004138 if (dentry->d_inode) {
4139 root = BTRFS_I(dentry->d_inode)->root;
4140 if (btrfs_root_refs(&root->root_item) == 0)
4141 return 1;
4142 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004143 return 0;
4144}
4145
Chris Mason3de45862008-11-17 21:02:50 -05004146static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4147 struct nameidata *nd)
4148{
4149 struct inode *inode;
4150
Chris Mason3de45862008-11-17 21:02:50 -05004151 inode = btrfs_lookup_dentry(dir, dentry);
4152 if (IS_ERR(inode))
4153 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004154
Chris Mason39279cc2007-06-12 06:35:45 -04004155 return d_splice_alias(inode, dentry);
4156}
4157
Chris Mason39279cc2007-06-12 06:35:45 -04004158static unsigned char btrfs_filetype_table[] = {
4159 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4160};
4161
David Woodhousecbdf5a22008-08-06 19:42:33 +01004162static int btrfs_real_readdir(struct file *filp, void *dirent,
4163 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004164{
Chris Mason6da6aba2007-12-18 16:15:09 -05004165 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004166 struct btrfs_root *root = BTRFS_I(inode)->root;
4167 struct btrfs_item *item;
4168 struct btrfs_dir_item *di;
4169 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004170 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004171 struct btrfs_path *path;
4172 int ret;
4173 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04004174 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004175 int slot;
4176 int advance;
4177 unsigned char d_type;
4178 int over = 0;
4179 u32 di_cur;
4180 u32 di_total;
4181 u32 di_len;
4182 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004183 char tmp_name[32];
4184 char *name_ptr;
4185 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04004186
4187 /* FIXME, use a real flag for deciding about the key type */
4188 if (root->fs_info->tree_root == root)
4189 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004190
Chris Mason39544012007-12-12 14:38:19 -05004191 /* special case for "." */
4192 if (filp->f_pos == 0) {
4193 over = filldir(dirent, ".", 1,
4194 1, inode->i_ino,
4195 DT_DIR);
4196 if (over)
4197 return 0;
4198 filp->f_pos = 1;
4199 }
Chris Mason39544012007-12-12 14:38:19 -05004200 /* special case for .., just use the back ref */
4201 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004202 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004203 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004204 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004205 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004206 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004207 filp->f_pos = 2;
4208 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004209 path = btrfs_alloc_path();
4210 path->reada = 2;
4211
Chris Mason39279cc2007-06-12 06:35:45 -04004212 btrfs_set_key_type(&key, key_type);
4213 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01004214 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04004215
Chris Mason39279cc2007-06-12 06:35:45 -04004216 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4217 if (ret < 0)
4218 goto err;
4219 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01004220
4221 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004222 leaf = path->nodes[0];
4223 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004224 slot = path->slots[0];
4225 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01004226 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004227 ret = btrfs_next_leaf(root, path);
4228 if (ret)
4229 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004230 leaf = path->nodes[0];
4231 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004232 slot = path->slots[0];
4233 } else {
4234 slot++;
4235 path->slots[0]++;
4236 }
4237 }
Chris Mason3de45862008-11-17 21:02:50 -05004238
Chris Mason39279cc2007-06-12 06:35:45 -04004239 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004240 item = btrfs_item_nr(leaf, slot);
4241 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4242
4243 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004244 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004245 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004246 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004247 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04004248 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04004249
4250 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01004251
Chris Mason39279cc2007-06-12 06:35:45 -04004252 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4253 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004254 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004255
4256 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004257 struct btrfs_key location;
4258
4259 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004260 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004261 name_ptr = tmp_name;
4262 } else {
4263 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004264 if (!name_ptr) {
4265 ret = -ENOMEM;
4266 goto err;
4267 }
Chris Mason5f39d392007-10-15 16:14:19 -04004268 }
4269 read_extent_buffer(leaf, name_ptr,
4270 (unsigned long)(di + 1), name_len);
4271
4272 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4273 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004274
4275 /* is this a reference to our own snapshot? If so
4276 * skip it
4277 */
4278 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4279 location.objectid == root->root_key.objectid) {
4280 over = 0;
4281 goto skip;
4282 }
Chris Mason5f39d392007-10-15 16:14:19 -04004283 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004284 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004285 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004286
Chris Mason3de45862008-11-17 21:02:50 -05004287skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004288 if (name_ptr != tmp_name)
4289 kfree(name_ptr);
4290
Chris Mason39279cc2007-06-12 06:35:45 -04004291 if (over)
4292 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004293 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004294 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004295 di_cur += di_len;
4296 di = (struct btrfs_dir_item *)((char *)di + di_len);
4297 }
4298 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004299
4300 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004301 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004302 /*
4303 * 32-bit glibc will use getdents64, but then strtol -
4304 * so the last number we can serve is this.
4305 */
4306 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004307 else
4308 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004309nopos:
4310 ret = 0;
4311err:
Chris Mason39279cc2007-06-12 06:35:45 -04004312 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004313 return ret;
4314}
4315
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004316int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004317{
4318 struct btrfs_root *root = BTRFS_I(inode)->root;
4319 struct btrfs_trans_handle *trans;
4320 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004321 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004322
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004323 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004324 return 0;
4325
Josef Bacik0af3d002010-06-21 14:48:16 -04004326 smp_mb();
4327 nolock = (root->fs_info->closing && root == root->fs_info->tree_root);
4328
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004329 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004330 if (nolock)
4331 trans = btrfs_join_transaction_nolock(root, 1);
4332 else
4333 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004334 btrfs_set_trans_block_group(trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04004335 if (nolock)
4336 ret = btrfs_end_transaction_nolock(trans, root);
4337 else
4338 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004339 }
4340 return ret;
4341}
4342
4343/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004344 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004345 * inode changes. But, it is most likely to find the inode in cache.
4346 * FIXME, needs more benchmarking...there are no reasons other than performance
4347 * to keep or drop this code.
4348 */
4349void btrfs_dirty_inode(struct inode *inode)
4350{
4351 struct btrfs_root *root = BTRFS_I(inode)->root;
4352 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004353 int ret;
4354
4355 if (BTRFS_I(inode)->dummy_inode)
4356 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004357
Chris Masonf9295742008-07-17 12:54:14 -04004358 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004359 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004360
4361 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004362 if (ret && ret == -ENOSPC) {
4363 /* whoops, lets try again with the full transaction */
4364 btrfs_end_transaction(trans, root);
4365 trans = btrfs_start_transaction(root, 1);
Chris Mason9aeead72010-05-27 10:23:00 -04004366 if (IS_ERR(trans)) {
4367 if (printk_ratelimit()) {
4368 printk(KERN_ERR "btrfs: fail to "
4369 "dirty inode %lu error %ld\n",
4370 inode->i_ino, PTR_ERR(trans));
4371 }
4372 return;
4373 }
Chris Mason94b60442010-05-26 11:02:00 -04004374 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004375
Chris Mason94b60442010-05-26 11:02:00 -04004376 ret = btrfs_update_inode(trans, root, inode);
4377 if (ret) {
Chris Mason9aeead72010-05-27 10:23:00 -04004378 if (printk_ratelimit()) {
4379 printk(KERN_ERR "btrfs: fail to "
4380 "dirty inode %lu error %d\n",
4381 inode->i_ino, ret);
4382 }
Chris Mason94b60442010-05-26 11:02:00 -04004383 }
4384 }
Chris Mason39279cc2007-06-12 06:35:45 -04004385 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004386}
4387
Chris Masond352ac62008-09-29 15:18:18 -04004388/*
4389 * find the highest existing sequence number in a directory
4390 * and then set the in-memory index_cnt variable to reflect
4391 * free sequence numbers
4392 */
Josef Bacikaec74772008-07-24 12:12:38 -04004393static int btrfs_set_inode_index_count(struct inode *inode)
4394{
4395 struct btrfs_root *root = BTRFS_I(inode)->root;
4396 struct btrfs_key key, found_key;
4397 struct btrfs_path *path;
4398 struct extent_buffer *leaf;
4399 int ret;
4400
4401 key.objectid = inode->i_ino;
4402 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4403 key.offset = (u64)-1;
4404
4405 path = btrfs_alloc_path();
4406 if (!path)
4407 return -ENOMEM;
4408
4409 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4410 if (ret < 0)
4411 goto out;
4412 /* FIXME: we should be able to handle this */
4413 if (ret == 0)
4414 goto out;
4415 ret = 0;
4416
4417 /*
4418 * MAGIC NUMBER EXPLANATION:
4419 * since we search a directory based on f_pos we have to start at 2
4420 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4421 * else has to start at 2
4422 */
4423 if (path->slots[0] == 0) {
4424 BTRFS_I(inode)->index_cnt = 2;
4425 goto out;
4426 }
4427
4428 path->slots[0]--;
4429
4430 leaf = path->nodes[0];
4431 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4432
4433 if (found_key.objectid != inode->i_ino ||
4434 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4435 BTRFS_I(inode)->index_cnt = 2;
4436 goto out;
4437 }
4438
4439 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4440out:
4441 btrfs_free_path(path);
4442 return ret;
4443}
4444
Chris Masond352ac62008-09-29 15:18:18 -04004445/*
4446 * helper to find a free sequence number in a given directory. This current
4447 * code is very simple, later versions will do smarter things in the btree
4448 */
Chris Mason3de45862008-11-17 21:02:50 -05004449int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004450{
4451 int ret = 0;
4452
4453 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4454 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004455 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004456 return ret;
4457 }
4458
Chris Mason00e4e6b2008-08-05 11:18:09 -04004459 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004460 BTRFS_I(dir)->index_cnt++;
4461
4462 return ret;
4463}
4464
Chris Mason39279cc2007-06-12 06:35:45 -04004465static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4466 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004467 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004468 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004469 u64 ref_objectid, u64 objectid,
4470 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004471{
4472 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004473 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004474 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004475 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004476 struct btrfs_inode_ref *ref;
4477 struct btrfs_key key[2];
4478 u32 sizes[2];
4479 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004480 int ret;
4481 int owner;
4482
Chris Mason5f39d392007-10-15 16:14:19 -04004483 path = btrfs_alloc_path();
4484 BUG_ON(!path);
4485
Chris Mason39279cc2007-06-12 06:35:45 -04004486 inode = new_inode(root->fs_info->sb);
4487 if (!inode)
4488 return ERR_PTR(-ENOMEM);
4489
Josef Bacikaec74772008-07-24 12:12:38 -04004490 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004491 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004492 if (ret) {
4493 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004494 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004495 }
Josef Bacikaec74772008-07-24 12:12:38 -04004496 }
4497 /*
4498 * index_cnt is ignored for everything but a dir,
4499 * btrfs_get_inode_index_count has an explanation for the magic
4500 * number
4501 */
4502 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004503 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004504 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004505 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004506 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004507
Chris Mason39279cc2007-06-12 06:35:45 -04004508 if (mode & S_IFDIR)
4509 owner = 0;
4510 else
4511 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004512 BTRFS_I(inode)->block_group =
4513 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004514
4515 key[0].objectid = objectid;
4516 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4517 key[0].offset = 0;
4518
4519 key[1].objectid = objectid;
4520 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4521 key[1].offset = ref_objectid;
4522
4523 sizes[0] = sizeof(struct btrfs_inode_item);
4524 sizes[1] = name_len + sizeof(*ref);
4525
Chris Masonb9473432009-03-13 11:00:37 -04004526 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004527 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4528 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004529 goto fail;
4530
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004531 inode_init_owner(inode, dir, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04004532 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004533 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004534 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004535 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4536 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004537 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004538
4539 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4540 struct btrfs_inode_ref);
4541 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004542 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004543 ptr = (unsigned long)(ref + 1);
4544 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4545
Chris Mason5f39d392007-10-15 16:14:19 -04004546 btrfs_mark_buffer_dirty(path->nodes[0]);
4547 btrfs_free_path(path);
4548
Chris Mason39279cc2007-06-12 06:35:45 -04004549 location = &BTRFS_I(inode)->location;
4550 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004551 location->offset = 0;
4552 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4553
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004554 btrfs_inherit_iflags(inode, dir);
4555
Chris Mason94272162009-07-02 12:26:06 -04004556 if ((mode & S_IFREG)) {
4557 if (btrfs_test_opt(root, NODATASUM))
4558 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4559 if (btrfs_test_opt(root, NODATACOW))
4560 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4561 }
4562
Chris Mason39279cc2007-06-12 06:35:45 -04004563 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004564 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004565 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004566fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004567 if (dir)
4568 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004569 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004570 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004571 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004572}
4573
4574static inline u8 btrfs_inode_type(struct inode *inode)
4575{
4576 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4577}
4578
Chris Masond352ac62008-09-29 15:18:18 -04004579/*
4580 * utility function to add 'inode' into 'parent_inode' with
4581 * a give name and a given sequence number.
4582 * if 'add_backref' is true, also insert a backref from the
4583 * inode to the parent directory.
4584 */
Chris Masone02119d2008-09-05 16:13:11 -04004585int btrfs_add_link(struct btrfs_trans_handle *trans,
4586 struct inode *parent_inode, struct inode *inode,
4587 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004588{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004589 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004590 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004591 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004592
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004593 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4594 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4595 } else {
4596 key.objectid = inode->i_ino;
4597 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4598 key.offset = 0;
4599 }
Chris Mason39279cc2007-06-12 06:35:45 -04004600
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004601 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4602 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4603 key.objectid, root->root_key.objectid,
4604 parent_inode->i_ino,
4605 index, name, name_len);
4606 } else if (add_backref) {
4607 ret = btrfs_insert_inode_ref(trans, root,
4608 name, name_len, inode->i_ino,
4609 parent_inode->i_ino, index);
4610 }
4611
Chris Mason39279cc2007-06-12 06:35:45 -04004612 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004613 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4614 parent_inode->i_ino, &key,
4615 btrfs_inode_type(inode), index);
4616 BUG_ON(ret);
4617
Chris Masondbe674a2008-07-17 12:54:05 -04004618 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004619 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004620 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004621 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004622 }
4623 return ret;
4624}
4625
4626static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004627 struct inode *dir, struct dentry *dentry,
4628 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004629{
Josef Bacika1b075d2010-11-19 20:36:11 +00004630 int err = btrfs_add_link(trans, dir, inode,
4631 dentry->d_name.name, dentry->d_name.len,
4632 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004633 if (!err) {
4634 d_instantiate(dentry, inode);
4635 return 0;
4636 }
4637 if (err > 0)
4638 err = -EEXIST;
4639 return err;
4640}
4641
Josef Bacik618e21d2007-07-11 10:18:17 -04004642static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4643 int mode, dev_t rdev)
4644{
4645 struct btrfs_trans_handle *trans;
4646 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004647 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004648 int err;
4649 int drop_inode = 0;
4650 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004651 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004652 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004653
4654 if (!new_valid_dev(rdev))
4655 return -EINVAL;
4656
Yan, Zhenga22285a2010-05-16 10:48:46 -04004657 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4658 if (err)
4659 return err;
4660
Josef Bacik9ed74f22009-09-11 16:12:44 -04004661 /*
4662 * 2 for inode item and ref
4663 * 2 for dir items
4664 * 1 for xattr if selinux is on
4665 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004666 trans = btrfs_start_transaction(root, 5);
4667 if (IS_ERR(trans))
4668 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004669
Josef Bacik618e21d2007-07-11 10:18:17 -04004670 btrfs_set_trans_block_group(trans, dir);
4671
Josef Bacikaec74772008-07-24 12:12:38 -04004672 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004673 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004674 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004675 err = PTR_ERR(inode);
4676 if (IS_ERR(inode))
4677 goto out_unlock;
4678
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004679 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004680 if (err) {
4681 drop_inode = 1;
4682 goto out_unlock;
4683 }
4684
Josef Bacik618e21d2007-07-11 10:18:17 -04004685 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004686 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004687 if (err)
4688 drop_inode = 1;
4689 else {
4690 inode->i_op = &btrfs_special_inode_operations;
4691 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004692 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004693 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004694 btrfs_update_inode_block_group(trans, inode);
4695 btrfs_update_inode_block_group(trans, dir);
4696out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004697 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004698 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004699 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004700 if (drop_inode) {
4701 inode_dec_link_count(inode);
4702 iput(inode);
4703 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004704 return err;
4705}
4706
Chris Mason39279cc2007-06-12 06:35:45 -04004707static int btrfs_create(struct inode *dir, struct dentry *dentry,
4708 int mode, struct nameidata *nd)
4709{
4710 struct btrfs_trans_handle *trans;
4711 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004712 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004713 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004714 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004715 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004716 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004717 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004718
Yan, Zhenga22285a2010-05-16 10:48:46 -04004719 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4720 if (err)
4721 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004722 /*
4723 * 2 for inode item and ref
4724 * 2 for dir items
4725 * 1 for xattr if selinux is on
4726 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004727 trans = btrfs_start_transaction(root, 5);
4728 if (IS_ERR(trans))
4729 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004730
Chris Mason39279cc2007-06-12 06:35:45 -04004731 btrfs_set_trans_block_group(trans, dir);
4732
Josef Bacikaec74772008-07-24 12:12:38 -04004733 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004734 dentry->d_name.len, dir->i_ino, objectid,
4735 BTRFS_I(dir)->block_group, mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004736 err = PTR_ERR(inode);
4737 if (IS_ERR(inode))
4738 goto out_unlock;
4739
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004740 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004741 if (err) {
4742 drop_inode = 1;
4743 goto out_unlock;
4744 }
4745
Chris Mason39279cc2007-06-12 06:35:45 -04004746 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004747 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004748 if (err)
4749 drop_inode = 1;
4750 else {
4751 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004752 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004753 inode->i_fop = &btrfs_file_operations;
4754 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004755 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004756 }
Chris Mason39279cc2007-06-12 06:35:45 -04004757 btrfs_update_inode_block_group(trans, inode);
4758 btrfs_update_inode_block_group(trans, dir);
4759out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004760 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004761 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004762 if (drop_inode) {
4763 inode_dec_link_count(inode);
4764 iput(inode);
4765 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004766 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004767 return err;
4768}
4769
4770static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4771 struct dentry *dentry)
4772{
4773 struct btrfs_trans_handle *trans;
4774 struct btrfs_root *root = BTRFS_I(dir)->root;
4775 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004776 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004777 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004778 int err;
4779 int drop_inode = 0;
4780
4781 if (inode->i_nlink == 0)
4782 return -ENOENT;
4783
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004784 /* do not allow sys_link's with other subvols of the same device */
4785 if (root->objectid != BTRFS_I(inode)->root->objectid)
4786 return -EPERM;
4787
Josef Bacik9ed74f22009-09-11 16:12:44 -04004788 btrfs_inc_nlink(inode);
Josef Bacikbc1cbf12010-11-23 19:50:59 +00004789 inode->i_ctime = CURRENT_TIME;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004790
Chris Mason3de45862008-11-17 21:02:50 -05004791 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004792 if (err)
4793 goto fail;
4794
Yan, Zhenga22285a2010-05-16 10:48:46 -04004795 /*
4796 * 1 item for inode ref
4797 * 2 items for dir items
4798 */
4799 trans = btrfs_start_transaction(root, 3);
4800 if (IS_ERR(trans)) {
4801 err = PTR_ERR(trans);
4802 goto fail;
4803 }
Chris Mason5f39d392007-10-15 16:14:19 -04004804
Chris Mason39279cc2007-06-12 06:35:45 -04004805 btrfs_set_trans_block_group(trans, dir);
4806 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004807
Josef Bacika1b075d2010-11-19 20:36:11 +00004808 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004809
Yan, Zhenga5719522009-09-24 09:17:31 -04004810 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004811 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004812 } else {
Josef Bacik6a912212010-11-20 09:48:00 +00004813 struct dentry *parent = dget_parent(dentry);
Yan, Zhenga5719522009-09-24 09:17:31 -04004814 btrfs_update_inode_block_group(trans, dir);
4815 err = btrfs_update_inode(trans, root, inode);
4816 BUG_ON(err);
Josef Bacik6a912212010-11-20 09:48:00 +00004817 btrfs_log_new_name(trans, inode, NULL, parent);
4818 dput(parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004819 }
Chris Mason39279cc2007-06-12 06:35:45 -04004820
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004821 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004822 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004823fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004824 if (drop_inode) {
4825 inode_dec_link_count(inode);
4826 iput(inode);
4827 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004828 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004829 return err;
4830}
4831
Chris Mason39279cc2007-06-12 06:35:45 -04004832static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4833{
Chris Masonb9d86662008-05-02 16:13:49 -04004834 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004835 struct btrfs_trans_handle *trans;
4836 struct btrfs_root *root = BTRFS_I(dir)->root;
4837 int err = 0;
4838 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004839 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004840 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004841 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004842
Yan, Zhenga22285a2010-05-16 10:48:46 -04004843 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4844 if (err)
4845 return err;
4846
Josef Bacik9ed74f22009-09-11 16:12:44 -04004847 /*
4848 * 2 items for inode and ref
4849 * 2 items for dir items
4850 * 1 for xattr if selinux is on
4851 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004852 trans = btrfs_start_transaction(root, 5);
4853 if (IS_ERR(trans))
4854 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004855 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004856
Josef Bacikaec74772008-07-24 12:12:38 -04004857 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004858 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004859 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4860 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004861 if (IS_ERR(inode)) {
4862 err = PTR_ERR(inode);
4863 goto out_fail;
4864 }
Chris Mason5f39d392007-10-15 16:14:19 -04004865
Chris Mason39279cc2007-06-12 06:35:45 -04004866 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004867
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004868 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004869 if (err)
4870 goto out_fail;
4871
Chris Mason39279cc2007-06-12 06:35:45 -04004872 inode->i_op = &btrfs_dir_inode_operations;
4873 inode->i_fop = &btrfs_dir_file_operations;
4874 btrfs_set_trans_block_group(trans, inode);
4875
Chris Masondbe674a2008-07-17 12:54:05 -04004876 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004877 err = btrfs_update_inode(trans, root, inode);
4878 if (err)
4879 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004880
Josef Bacika1b075d2010-11-19 20:36:11 +00004881 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4882 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004883 if (err)
4884 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004885
Chris Mason39279cc2007-06-12 06:35:45 -04004886 d_instantiate(dentry, inode);
4887 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004888 btrfs_update_inode_block_group(trans, inode);
4889 btrfs_update_inode_block_group(trans, dir);
4890
4891out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004892 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004893 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004894 if (drop_on_err)
4895 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004896 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004897 return err;
4898}
4899
Chris Masond352ac62008-09-29 15:18:18 -04004900/* helper for btfs_get_extent. Given an existing extent in the tree,
4901 * and an extent that you want to insert, deal with overlap and insert
4902 * the new extent into the tree.
4903 */
Chris Mason3b951512008-04-17 11:29:12 -04004904static int merge_extent_mapping(struct extent_map_tree *em_tree,
4905 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004906 struct extent_map *em,
4907 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004908{
4909 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004910
Chris Masone6dcd2d2008-07-17 12:53:50 -04004911 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4912 start_diff = map_start - em->start;
4913 em->start = map_start;
4914 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004915 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4916 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004917 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004918 em->block_len -= start_diff;
4919 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004920 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004921}
4922
Chris Masonc8b97812008-10-29 14:49:59 -04004923static noinline int uncompress_inline(struct btrfs_path *path,
4924 struct inode *inode, struct page *page,
4925 size_t pg_offset, u64 extent_offset,
4926 struct btrfs_file_extent_item *item)
4927{
4928 int ret;
4929 struct extent_buffer *leaf = path->nodes[0];
4930 char *tmp;
4931 size_t max_size;
4932 unsigned long inline_size;
4933 unsigned long ptr;
4934
4935 WARN_ON(pg_offset != 0);
4936 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4937 inline_size = btrfs_file_extent_inline_item_len(leaf,
4938 btrfs_item_nr(leaf, path->slots[0]));
4939 tmp = kmalloc(inline_size, GFP_NOFS);
4940 ptr = btrfs_file_extent_inline_start(item);
4941
4942 read_extent_buffer(leaf, tmp, ptr, inline_size);
4943
Chris Mason5b050f02008-11-11 09:34:41 -05004944 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004945 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4946 inline_size, max_size);
4947 if (ret) {
4948 char *kaddr = kmap_atomic(page, KM_USER0);
4949 unsigned long copy_size = min_t(u64,
4950 PAGE_CACHE_SIZE - pg_offset,
4951 max_size - extent_offset);
4952 memset(kaddr + pg_offset, 0, copy_size);
4953 kunmap_atomic(kaddr, KM_USER0);
4954 }
4955 kfree(tmp);
4956 return 0;
4957}
4958
Chris Masond352ac62008-09-29 15:18:18 -04004959/*
4960 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004961 * the ugly parts come from merging extents from the disk with the in-ram
4962 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004963 * where the in-ram extents might be locked pending data=ordered completion.
4964 *
4965 * This also copies inline extents directly into the page.
4966 */
Chris Masond3977122009-01-05 21:25:51 -05004967
Chris Masona52d9a82007-08-27 16:49:44 -04004968struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004969 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004970 int create)
4971{
4972 int ret;
4973 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004974 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004975 u64 extent_start = 0;
4976 u64 extent_end = 0;
4977 u64 objectid = inode->i_ino;
4978 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004979 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004980 struct btrfs_root *root = BTRFS_I(inode)->root;
4981 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004982 struct extent_buffer *leaf;
4983 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004984 struct extent_map *em = NULL;
4985 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004986 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004987 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004988 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004989
Chris Masona52d9a82007-08-27 16:49:44 -04004990again:
Chris Mason890871b2009-09-02 16:24:52 -04004991 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004992 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004993 if (em)
4994 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004995 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004996
Chris Masona52d9a82007-08-27 16:49:44 -04004997 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004998 if (em->start > start || em->start + em->len <= start)
4999 free_extent_map(em);
5000 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005001 free_extent_map(em);
5002 else
5003 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005004 }
Chris Masond1310b22008-01-24 16:13:08 -05005005 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005006 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005007 err = -ENOMEM;
5008 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005009 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005010 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005011 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005012 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005013 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005014 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005015
5016 if (!path) {
5017 path = btrfs_alloc_path();
5018 BUG_ON(!path);
5019 }
5020
Chris Mason179e29e2007-11-01 11:28:41 -04005021 ret = btrfs_lookup_file_extent(trans, root, path,
5022 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005023 if (ret < 0) {
5024 err = ret;
5025 goto out;
5026 }
5027
5028 if (ret != 0) {
5029 if (path->slots[0] == 0)
5030 goto not_found;
5031 path->slots[0]--;
5032 }
5033
Chris Mason5f39d392007-10-15 16:14:19 -04005034 leaf = path->nodes[0];
5035 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005036 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005037 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005038 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5039 found_type = btrfs_key_type(&found_key);
5040 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005041 found_type != BTRFS_EXTENT_DATA_KEY) {
5042 goto not_found;
5043 }
5044
Chris Mason5f39d392007-10-15 16:14:19 -04005045 found_type = btrfs_file_extent_type(leaf, item);
5046 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005047 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005048 if (found_type == BTRFS_FILE_EXTENT_REG ||
5049 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005050 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005051 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005052 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5053 size_t size;
5054 size = btrfs_file_extent_inline_len(leaf, item);
5055 extent_end = (extent_start + size + root->sectorsize - 1) &
5056 ~((u64)root->sectorsize - 1);
5057 }
5058
5059 if (start >= extent_end) {
5060 path->slots[0]++;
5061 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5062 ret = btrfs_next_leaf(root, path);
5063 if (ret < 0) {
5064 err = ret;
5065 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005066 }
Yan Zheng9036c102008-10-30 14:19:41 -04005067 if (ret > 0)
5068 goto not_found;
5069 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005070 }
Yan Zheng9036c102008-10-30 14:19:41 -04005071 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5072 if (found_key.objectid != objectid ||
5073 found_key.type != BTRFS_EXTENT_DATA_KEY)
5074 goto not_found;
5075 if (start + len <= found_key.offset)
5076 goto not_found;
5077 em->start = start;
5078 em->len = found_key.offset - start;
5079 goto not_found_em;
5080 }
5081
Yan Zhengd899e052008-10-30 14:25:28 -04005082 if (found_type == BTRFS_FILE_EXTENT_REG ||
5083 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005084 em->start = extent_start;
5085 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005086 em->orig_start = extent_start -
5087 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005088 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5089 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005090 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005091 goto insert;
5092 }
Chris Masonc8b97812008-10-29 14:49:59 -04005093 if (compressed) {
5094 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5095 em->block_start = bytenr;
5096 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5097 item);
5098 } else {
5099 bytenr += btrfs_file_extent_offset(leaf, item);
5100 em->block_start = bytenr;
5101 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005102 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5103 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005104 }
Chris Masona52d9a82007-08-27 16:49:44 -04005105 goto insert;
5106 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005107 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005108 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005109 size_t size;
5110 size_t extent_offset;
5111 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005112
Chris Masona52d9a82007-08-27 16:49:44 -04005113 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005114 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005115 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005116 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005117 goto out;
5118 }
5119
Yan Zheng9036c102008-10-30 14:19:41 -04005120 size = btrfs_file_extent_inline_len(leaf, item);
5121 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005122 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005123 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005124 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005125 em->len = (copy_size + root->sectorsize - 1) &
5126 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005127 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005128 if (compressed)
5129 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04005130 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005131 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04005132 if (btrfs_file_extent_compression(leaf, item) ==
5133 BTRFS_COMPRESS_ZLIB) {
5134 ret = uncompress_inline(path, inode, page,
5135 pg_offset,
5136 extent_offset, item);
5137 BUG_ON(ret);
5138 } else {
5139 map = kmap(page);
5140 read_extent_buffer(leaf, map + pg_offset, ptr,
5141 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005142 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5143 memset(map + pg_offset + copy_size, 0,
5144 PAGE_CACHE_SIZE - pg_offset -
5145 copy_size);
5146 }
Chris Masonc8b97812008-10-29 14:49:59 -04005147 kunmap(page);
5148 }
Chris Mason179e29e2007-11-01 11:28:41 -04005149 flush_dcache_page(page);
5150 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005151 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005152 if (!trans) {
5153 kunmap(page);
5154 free_extent_map(em);
5155 em = NULL;
5156 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04005157 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04005158 goto again;
5159 }
Chris Masonc8b97812008-10-29 14:49:59 -04005160 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005161 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005162 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005163 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005164 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005165 }
Chris Masond1310b22008-01-24 16:13:08 -05005166 set_extent_uptodate(io_tree, em->start,
5167 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005168 goto insert;
5169 } else {
Chris Masond3977122009-01-05 21:25:51 -05005170 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005171 WARN_ON(1);
5172 }
5173not_found:
5174 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005175 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005176not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005177 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005178 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005179insert:
5180 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05005181 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005182 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5183 "[%llu %llu]\n", (unsigned long long)em->start,
5184 (unsigned long long)em->len,
5185 (unsigned long long)start,
5186 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005187 err = -EIO;
5188 goto out;
5189 }
Chris Masond1310b22008-01-24 16:13:08 -05005190
5191 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005192 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005193 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005194 /* it is possible that someone inserted the extent into the tree
5195 * while we had the lock dropped. It is also possible that
5196 * an overlapping map exists in the tree
5197 */
Chris Masona52d9a82007-08-27 16:49:44 -04005198 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005199 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005200
5201 ret = 0;
5202
Chris Mason3b951512008-04-17 11:29:12 -04005203 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005204 if (existing && (existing->start > start ||
5205 existing->start + existing->len <= start)) {
5206 free_extent_map(existing);
5207 existing = NULL;
5208 }
Chris Mason3b951512008-04-17 11:29:12 -04005209 if (!existing) {
5210 existing = lookup_extent_mapping(em_tree, em->start,
5211 em->len);
5212 if (existing) {
5213 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005214 em, start,
5215 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005216 free_extent_map(existing);
5217 if (err) {
5218 free_extent_map(em);
5219 em = NULL;
5220 }
5221 } else {
5222 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005223 free_extent_map(em);
5224 em = NULL;
5225 }
5226 } else {
5227 free_extent_map(em);
5228 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005229 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005230 }
Chris Masona52d9a82007-08-27 16:49:44 -04005231 }
Chris Mason890871b2009-09-02 16:24:52 -04005232 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005233out:
Chris Masonf4219502008-07-22 11:18:09 -04005234 if (path)
5235 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005236 if (trans) {
5237 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005238 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005239 err = ret;
5240 }
Chris Masona52d9a82007-08-27 16:49:44 -04005241 if (err) {
5242 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005243 return ERR_PTR(err);
5244 }
5245 return em;
5246}
5247
Josef Bacik4b46fce2010-05-23 11:00:55 -04005248static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5249 u64 start, u64 len)
5250{
5251 struct btrfs_root *root = BTRFS_I(inode)->root;
5252 struct btrfs_trans_handle *trans;
5253 struct extent_map *em;
5254 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5255 struct btrfs_key ins;
5256 u64 alloc_hint;
5257 int ret;
5258
5259 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5260
5261 trans = btrfs_join_transaction(root, 0);
5262 if (!trans)
5263 return ERR_PTR(-ENOMEM);
5264
5265 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5266
5267 alloc_hint = get_extent_allocation_hint(inode, start, len);
5268 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5269 alloc_hint, (u64)-1, &ins, 1);
5270 if (ret) {
5271 em = ERR_PTR(ret);
5272 goto out;
5273 }
5274
5275 em = alloc_extent_map(GFP_NOFS);
5276 if (!em) {
5277 em = ERR_PTR(-ENOMEM);
5278 goto out;
5279 }
5280
5281 em->start = start;
5282 em->orig_start = em->start;
5283 em->len = ins.offset;
5284
5285 em->block_start = ins.objectid;
5286 em->block_len = ins.offset;
5287 em->bdev = root->fs_info->fs_devices->latest_bdev;
5288 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5289
5290 while (1) {
5291 write_lock(&em_tree->lock);
5292 ret = add_extent_mapping(em_tree, em);
5293 write_unlock(&em_tree->lock);
5294 if (ret != -EEXIST)
5295 break;
5296 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5297 }
5298
5299 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5300 ins.offset, ins.offset, 0);
5301 if (ret) {
5302 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5303 em = ERR_PTR(ret);
5304 }
5305out:
5306 btrfs_end_transaction(trans, root);
5307 return em;
5308}
5309
Chris Mason46bfbb52010-05-26 11:04:10 -04005310/*
5311 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5312 * block must be cow'd
5313 */
5314static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5315 struct inode *inode, u64 offset, u64 len)
5316{
5317 struct btrfs_path *path;
5318 int ret;
5319 struct extent_buffer *leaf;
5320 struct btrfs_root *root = BTRFS_I(inode)->root;
5321 struct btrfs_file_extent_item *fi;
5322 struct btrfs_key key;
5323 u64 disk_bytenr;
5324 u64 backref_offset;
5325 u64 extent_end;
5326 u64 num_bytes;
5327 int slot;
5328 int found_type;
5329
5330 path = btrfs_alloc_path();
5331 if (!path)
5332 return -ENOMEM;
5333
5334 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
5335 offset, 0);
5336 if (ret < 0)
5337 goto out;
5338
5339 slot = path->slots[0];
5340 if (ret == 1) {
5341 if (slot == 0) {
5342 /* can't find the item, must cow */
5343 ret = 0;
5344 goto out;
5345 }
5346 slot--;
5347 }
5348 ret = 0;
5349 leaf = path->nodes[0];
5350 btrfs_item_key_to_cpu(leaf, &key, slot);
5351 if (key.objectid != inode->i_ino ||
5352 key.type != BTRFS_EXTENT_DATA_KEY) {
5353 /* not our file or wrong item type, must cow */
5354 goto out;
5355 }
5356
5357 if (key.offset > offset) {
5358 /* Wrong offset, must cow */
5359 goto out;
5360 }
5361
5362 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5363 found_type = btrfs_file_extent_type(leaf, fi);
5364 if (found_type != BTRFS_FILE_EXTENT_REG &&
5365 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5366 /* not a regular extent, must cow */
5367 goto out;
5368 }
5369 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5370 backref_offset = btrfs_file_extent_offset(leaf, fi);
5371
5372 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5373 if (extent_end < offset + len) {
5374 /* extent doesn't include our full range, must cow */
5375 goto out;
5376 }
5377
5378 if (btrfs_extent_readonly(root, disk_bytenr))
5379 goto out;
5380
5381 /*
5382 * look for other files referencing this extent, if we
5383 * find any we must cow
5384 */
5385 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
5386 key.offset - backref_offset, disk_bytenr))
5387 goto out;
5388
5389 /*
5390 * adjust disk_bytenr and num_bytes to cover just the bytes
5391 * in this extent we are about to write. If there
5392 * are any csums in that range we have to cow in order
5393 * to keep the csums correct
5394 */
5395 disk_bytenr += backref_offset;
5396 disk_bytenr += offset - key.offset;
5397 num_bytes = min(offset + len, extent_end) - offset;
5398 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5399 goto out;
5400 /*
5401 * all of the above have passed, it is safe to overwrite this extent
5402 * without cow
5403 */
5404 ret = 1;
5405out:
5406 btrfs_free_path(path);
5407 return ret;
5408}
5409
Josef Bacik4b46fce2010-05-23 11:00:55 -04005410static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5411 struct buffer_head *bh_result, int create)
5412{
5413 struct extent_map *em;
5414 struct btrfs_root *root = BTRFS_I(inode)->root;
5415 u64 start = iblock << inode->i_blkbits;
5416 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005417 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005418
5419 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5420 if (IS_ERR(em))
5421 return PTR_ERR(em);
5422
5423 /*
5424 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5425 * io. INLINE is special, and we could probably kludge it in here, but
5426 * it's still buffered so for safety lets just fall back to the generic
5427 * buffered path.
5428 *
5429 * For COMPRESSED we _have_ to read the entire extent in so we can
5430 * decompress it, so there will be buffering required no matter what we
5431 * do, so go ahead and fallback to buffered.
5432 *
5433 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5434 * to buffered IO. Don't blame me, this is the price we pay for using
5435 * the generic code.
5436 */
5437 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5438 em->block_start == EXTENT_MAP_INLINE) {
5439 free_extent_map(em);
5440 return -ENOTBLK;
5441 }
5442
5443 /* Just a good old fashioned hole, return */
5444 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5445 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5446 free_extent_map(em);
5447 /* DIO will do one hole at a time, so just unlock a sector */
5448 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5449 start + root->sectorsize - 1, GFP_NOFS);
5450 return 0;
5451 }
5452
5453 /*
5454 * We don't allocate a new extent in the following cases
5455 *
5456 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5457 * existing extent.
5458 * 2) The extent is marked as PREALLOC. We're good to go here and can
5459 * just use the extent.
5460 *
5461 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005462 if (!create) {
5463 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005464 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005465 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005466
5467 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5468 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5469 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005470 int type;
5471 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005472 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005473
5474 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5475 type = BTRFS_ORDERED_PREALLOC;
5476 else
5477 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005478 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005479 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005480
5481 /*
5482 * we're not going to log anything, but we do need
5483 * to make sure the current transaction stays open
5484 * while we look for nocow cross refs
5485 */
5486 trans = btrfs_join_transaction(root, 0);
5487 if (!trans)
5488 goto must_cow;
5489
5490 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5491 ret = btrfs_add_ordered_extent_dio(inode, start,
5492 block_start, len, len, type);
5493 btrfs_end_transaction(trans, root);
5494 if (ret) {
5495 free_extent_map(em);
5496 return ret;
5497 }
5498 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005499 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005500 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005501 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005502must_cow:
5503 /*
5504 * this will cow the extent, reset the len in case we changed
5505 * it above
5506 */
5507 len = bh_result->b_size;
5508 free_extent_map(em);
5509 em = btrfs_new_extent_direct(inode, start, len);
5510 if (IS_ERR(em))
5511 return PTR_ERR(em);
5512 len = min(len, em->len - (start - em->start));
5513unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005514 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5515 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5516 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005517map:
5518 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5519 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005520 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005521 bh_result->b_bdev = em->bdev;
5522 set_buffer_mapped(bh_result);
5523 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5524 set_buffer_new(bh_result);
5525
5526 free_extent_map(em);
5527
5528 return 0;
5529}
5530
5531struct btrfs_dio_private {
5532 struct inode *inode;
5533 u64 logical_offset;
5534 u64 disk_bytenr;
5535 u64 bytes;
5536 u32 *csums;
5537 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005538
5539 /* number of bios pending for this dio */
5540 atomic_t pending_bios;
5541
5542 /* IO errors */
5543 int errors;
5544
5545 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005546};
5547
5548static void btrfs_endio_direct_read(struct bio *bio, int err)
5549{
Miao Xiee65e1532010-11-22 03:04:43 +00005550 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005551 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5552 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005553 struct inode *inode = dip->inode;
5554 struct btrfs_root *root = BTRFS_I(inode)->root;
5555 u64 start;
5556 u32 *private = dip->csums;
5557
5558 start = dip->logical_offset;
5559 do {
5560 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5561 struct page *page = bvec->bv_page;
5562 char *kaddr;
5563 u32 csum = ~(u32)0;
5564 unsigned long flags;
5565
5566 local_irq_save(flags);
5567 kaddr = kmap_atomic(page, KM_IRQ0);
5568 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5569 csum, bvec->bv_len);
5570 btrfs_csum_final(csum, (char *)&csum);
5571 kunmap_atomic(kaddr, KM_IRQ0);
5572 local_irq_restore(flags);
5573
5574 flush_dcache_page(bvec->bv_page);
5575 if (csum != *private) {
5576 printk(KERN_ERR "btrfs csum failed ino %lu off"
5577 " %llu csum %u private %u\n",
5578 inode->i_ino, (unsigned long long)start,
5579 csum, *private);
5580 err = -EIO;
5581 }
5582 }
5583
5584 start += bvec->bv_len;
5585 private++;
5586 bvec++;
5587 } while (bvec <= bvec_end);
5588
5589 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5590 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5591 bio->bi_private = dip->private;
5592
5593 kfree(dip->csums);
5594 kfree(dip);
5595 dio_end_io(bio, err);
5596}
5597
5598static void btrfs_endio_direct_write(struct bio *bio, int err)
5599{
5600 struct btrfs_dio_private *dip = bio->bi_private;
5601 struct inode *inode = dip->inode;
5602 struct btrfs_root *root = BTRFS_I(inode)->root;
5603 struct btrfs_trans_handle *trans;
5604 struct btrfs_ordered_extent *ordered = NULL;
5605 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005606 u64 ordered_offset = dip->logical_offset;
5607 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005608 int ret;
5609
5610 if (err)
5611 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005612again:
5613 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5614 &ordered_offset,
5615 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005616 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005617 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005618
5619 BUG_ON(!ordered);
5620
5621 trans = btrfs_join_transaction(root, 1);
5622 if (!trans) {
5623 err = -ENOMEM;
5624 goto out;
5625 }
5626 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5627
5628 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5629 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5630 if (!ret)
5631 ret = btrfs_update_inode(trans, root, inode);
5632 err = ret;
5633 goto out;
5634 }
5635
5636 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5637 ordered->file_offset + ordered->len - 1, 0,
5638 &cached_state, GFP_NOFS);
5639
5640 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5641 ret = btrfs_mark_extent_written(trans, inode,
5642 ordered->file_offset,
5643 ordered->file_offset +
5644 ordered->len);
5645 if (ret) {
5646 err = ret;
5647 goto out_unlock;
5648 }
5649 } else {
5650 ret = insert_reserved_file_extent(trans, inode,
5651 ordered->file_offset,
5652 ordered->start,
5653 ordered->disk_len,
5654 ordered->len,
5655 ordered->len,
5656 0, 0, 0,
5657 BTRFS_FILE_EXTENT_REG);
5658 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5659 ordered->file_offset, ordered->len);
5660 if (ret) {
5661 err = ret;
5662 WARN_ON(1);
5663 goto out_unlock;
5664 }
5665 }
5666
5667 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5668 btrfs_ordered_update_i_size(inode, 0, ordered);
5669 btrfs_update_inode(trans, root, inode);
5670out_unlock:
5671 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5672 ordered->file_offset + ordered->len - 1,
5673 &cached_state, GFP_NOFS);
5674out:
5675 btrfs_delalloc_release_metadata(inode, ordered->len);
5676 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005677 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005678 btrfs_put_ordered_extent(ordered);
5679 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05005680
5681out_test:
5682 /*
5683 * our bio might span multiple ordered extents. If we haven't
5684 * completed the accounting for the whole dio, go back and try again
5685 */
5686 if (ordered_offset < dip->logical_offset + dip->bytes) {
5687 ordered_bytes = dip->logical_offset + dip->bytes -
5688 ordered_offset;
5689 goto again;
5690 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005691out_done:
5692 bio->bi_private = dip->private;
5693
5694 kfree(dip->csums);
5695 kfree(dip);
5696 dio_end_io(bio, err);
5697}
5698
Chris Masoneaf25d92010-05-25 09:48:28 -04005699static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5700 struct bio *bio, int mirror_num,
5701 unsigned long bio_flags, u64 offset)
5702{
5703 int ret;
5704 struct btrfs_root *root = BTRFS_I(inode)->root;
5705 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5706 BUG_ON(ret);
5707 return 0;
5708}
5709
Miao Xiee65e1532010-11-22 03:04:43 +00005710static void btrfs_end_dio_bio(struct bio *bio, int err)
5711{
5712 struct btrfs_dio_private *dip = bio->bi_private;
5713
5714 if (err) {
5715 printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00005716 "sector %#Lx len %u err no %d\n",
5717 dip->inode->i_ino, bio->bi_rw,
5718 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00005719 dip->errors = 1;
5720
5721 /*
5722 * before atomic variable goto zero, we must make sure
5723 * dip->errors is perceived to be set.
5724 */
5725 smp_mb__before_atomic_dec();
5726 }
5727
5728 /* if there are more bios still pending for this dio, just exit */
5729 if (!atomic_dec_and_test(&dip->pending_bios))
5730 goto out;
5731
5732 if (dip->errors)
5733 bio_io_error(dip->orig_bio);
5734 else {
5735 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
5736 bio_endio(dip->orig_bio, 0);
5737 }
5738out:
5739 bio_put(bio);
5740}
5741
5742static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
5743 u64 first_sector, gfp_t gfp_flags)
5744{
5745 int nr_vecs = bio_get_nr_vecs(bdev);
5746 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
5747}
5748
5749static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
5750 int rw, u64 file_offset, int skip_sum,
5751 u32 *csums)
5752{
5753 int write = rw & REQ_WRITE;
5754 struct btrfs_root *root = BTRFS_I(inode)->root;
5755 int ret;
5756
5757 bio_get(bio);
5758 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
5759 if (ret)
5760 goto err;
5761
5762 if (write && !skip_sum) {
5763 ret = btrfs_wq_submit_bio(root->fs_info,
5764 inode, rw, bio, 0, 0,
5765 file_offset,
5766 __btrfs_submit_bio_start_direct_io,
5767 __btrfs_submit_bio_done);
5768 goto err;
5769 } else if (!skip_sum)
5770 btrfs_lookup_bio_sums_dio(root, inode, bio,
5771 file_offset, csums);
5772
5773 ret = btrfs_map_bio(root, rw, bio, 0, 1);
5774err:
5775 bio_put(bio);
5776 return ret;
5777}
5778
5779static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
5780 int skip_sum)
5781{
5782 struct inode *inode = dip->inode;
5783 struct btrfs_root *root = BTRFS_I(inode)->root;
5784 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5785 struct bio *bio;
5786 struct bio *orig_bio = dip->orig_bio;
5787 struct bio_vec *bvec = orig_bio->bi_io_vec;
5788 u64 start_sector = orig_bio->bi_sector;
5789 u64 file_offset = dip->logical_offset;
5790 u64 submit_len = 0;
5791 u64 map_length;
5792 int nr_pages = 0;
5793 u32 *csums = dip->csums;
5794 int ret = 0;
5795
5796 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
5797 if (!bio)
5798 return -ENOMEM;
5799 bio->bi_private = dip;
5800 bio->bi_end_io = btrfs_end_dio_bio;
5801 atomic_inc(&dip->pending_bios);
5802
5803 map_length = orig_bio->bi_size;
5804 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5805 &map_length, NULL, 0);
5806 if (ret) {
5807 bio_put(bio);
5808 return -EIO;
5809 }
5810
5811 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
5812 if (unlikely(map_length < submit_len + bvec->bv_len ||
5813 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5814 bvec->bv_offset) < bvec->bv_len)) {
5815 /*
5816 * inc the count before we submit the bio so
5817 * we know the end IO handler won't happen before
5818 * we inc the count. Otherwise, the dip might get freed
5819 * before we're done setting it up
5820 */
5821 atomic_inc(&dip->pending_bios);
5822 ret = __btrfs_submit_dio_bio(bio, inode, rw,
5823 file_offset, skip_sum,
5824 csums);
5825 if (ret) {
5826 bio_put(bio);
5827 atomic_dec(&dip->pending_bios);
5828 goto out_err;
5829 }
5830
5831 if (!skip_sum)
5832 csums = csums + nr_pages;
5833 start_sector += submit_len >> 9;
5834 file_offset += submit_len;
5835
5836 submit_len = 0;
5837 nr_pages = 0;
5838
5839 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
5840 start_sector, GFP_NOFS);
5841 if (!bio)
5842 goto out_err;
5843 bio->bi_private = dip;
5844 bio->bi_end_io = btrfs_end_dio_bio;
5845
5846 map_length = orig_bio->bi_size;
5847 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5848 &map_length, NULL, 0);
5849 if (ret) {
5850 bio_put(bio);
5851 goto out_err;
5852 }
5853 } else {
5854 submit_len += bvec->bv_len;
5855 nr_pages ++;
5856 bvec++;
5857 }
5858 }
5859
5860 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
5861 csums);
5862 if (!ret)
5863 return 0;
5864
5865 bio_put(bio);
5866out_err:
5867 dip->errors = 1;
5868 /*
5869 * before atomic variable goto zero, we must
5870 * make sure dip->errors is perceived to be set.
5871 */
5872 smp_mb__before_atomic_dec();
5873 if (atomic_dec_and_test(&dip->pending_bios))
5874 bio_io_error(dip->orig_bio);
5875
5876 /* bio_end_io() will handle error, so we needn't return it */
5877 return 0;
5878}
5879
Josef Bacik4b46fce2010-05-23 11:00:55 -04005880static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
5881 loff_t file_offset)
5882{
5883 struct btrfs_root *root = BTRFS_I(inode)->root;
5884 struct btrfs_dio_private *dip;
5885 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005886 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005887 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005888 int ret = 0;
5889
5890 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
5891
5892 dip = kmalloc(sizeof(*dip), GFP_NOFS);
5893 if (!dip) {
5894 ret = -ENOMEM;
5895 goto free_ordered;
5896 }
5897 dip->csums = NULL;
5898
5899 if (!skip_sum) {
5900 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
5901 if (!dip->csums) {
5902 ret = -ENOMEM;
5903 goto free_ordered;
5904 }
5905 }
5906
5907 dip->private = bio->bi_private;
5908 dip->inode = inode;
5909 dip->logical_offset = file_offset;
5910
Josef Bacik4b46fce2010-05-23 11:00:55 -04005911 dip->bytes = 0;
5912 do {
5913 dip->bytes += bvec->bv_len;
5914 bvec++;
5915 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
5916
Chris Mason46bfbb52010-05-26 11:04:10 -04005917 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005918 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00005919 dip->errors = 0;
5920 dip->orig_bio = bio;
5921 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005922
5923 if (write)
5924 bio->bi_end_io = btrfs_endio_direct_write;
5925 else
5926 bio->bi_end_io = btrfs_endio_direct_read;
5927
Miao Xiee65e1532010-11-22 03:04:43 +00005928 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
5929 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04005930 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005931free_ordered:
5932 /*
5933 * If this is a write, we need to clean up the reserved space and kill
5934 * the ordered extent.
5935 */
5936 if (write) {
5937 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05005938 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005939 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
5940 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
5941 btrfs_free_reserved_extent(root, ordered->start,
5942 ordered->disk_len);
5943 btrfs_put_ordered_extent(ordered);
5944 btrfs_put_ordered_extent(ordered);
5945 }
5946 bio_endio(bio, ret);
5947}
5948
Chris Mason5a5f79b2010-05-26 21:33:37 -04005949static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
5950 const struct iovec *iov, loff_t offset,
5951 unsigned long nr_segs)
5952{
5953 int seg;
5954 size_t size;
5955 unsigned long addr;
5956 unsigned blocksize_mask = root->sectorsize - 1;
5957 ssize_t retval = -EINVAL;
5958 loff_t end = offset;
5959
5960 if (offset & blocksize_mask)
5961 goto out;
5962
5963 /* Check the memory alignment. Blocks cannot straddle pages */
5964 for (seg = 0; seg < nr_segs; seg++) {
5965 addr = (unsigned long)iov[seg].iov_base;
5966 size = iov[seg].iov_len;
5967 end += size;
5968 if ((addr & blocksize_mask) || (size & blocksize_mask))
5969 goto out;
5970 }
5971 retval = 0;
5972out:
5973 return retval;
5974}
Chris Mason16432982008-04-10 10:23:21 -04005975static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
5976 const struct iovec *iov, loff_t offset,
5977 unsigned long nr_segs)
5978{
Josef Bacik4b46fce2010-05-23 11:00:55 -04005979 struct file *file = iocb->ki_filp;
5980 struct inode *inode = file->f_mapping->host;
5981 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04005982 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005983 u64 lockstart, lockend;
5984 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04005985 int writing = rw & WRITE;
5986 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04005987 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005988
Chris Mason5a5f79b2010-05-26 21:33:37 -04005989 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
5990 offset, nr_segs)) {
5991 return 0;
5992 }
5993
Josef Bacik4b46fce2010-05-23 11:00:55 -04005994 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04005995 lockend = offset + count - 1;
5996
5997 if (writing) {
5998 ret = btrfs_delalloc_reserve_space(inode, count);
5999 if (ret)
6000 goto out;
6001 }
Chris Mason4845e442010-05-25 20:56:50 -04006002
Josef Bacik4b46fce2010-05-23 11:00:55 -04006003 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006004 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6005 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006006 /*
6007 * We're concerned with the entire range that we're going to be
6008 * doing DIO to, so we need to make sure theres no ordered
6009 * extents in this range.
6010 */
6011 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6012 lockend - lockstart + 1);
6013 if (!ordered)
6014 break;
Chris Mason4845e442010-05-25 20:56:50 -04006015 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6016 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006017 btrfs_start_ordered_extent(inode, ordered, 1);
6018 btrfs_put_ordered_extent(ordered);
6019 cond_resched();
6020 }
6021
Chris Mason4845e442010-05-25 20:56:50 -04006022 /*
6023 * we don't use btrfs_set_extent_delalloc because we don't want
6024 * the dirty or uptodate bits
6025 */
6026 if (writing) {
6027 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6028 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6029 EXTENT_DELALLOC, 0, NULL, &cached_state,
6030 GFP_NOFS);
6031 if (ret) {
6032 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6033 lockend, EXTENT_LOCKED | write_bits,
6034 1, 0, &cached_state, GFP_NOFS);
6035 goto out;
6036 }
6037 }
6038
6039 free_extent_state(cached_state);
6040 cached_state = NULL;
6041
Chris Mason5a5f79b2010-05-26 21:33:37 -04006042 ret = __blockdev_direct_IO(rw, iocb, inode,
6043 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6044 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6045 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006046
6047 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006048 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6049 offset + iov_length(iov, nr_segs) - 1,
6050 EXTENT_LOCKED | write_bits, 1, 0,
6051 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006052 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6053 /*
6054 * We're falling back to buffered, unlock the section we didn't
6055 * do IO on.
6056 */
Chris Mason4845e442010-05-25 20:56:50 -04006057 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6058 offset + iov_length(iov, nr_segs) - 1,
6059 EXTENT_LOCKED | write_bits, 1, 0,
6060 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006061 }
Chris Mason4845e442010-05-25 20:56:50 -04006062out:
6063 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006064 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006065}
6066
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006067static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6068 __u64 start, __u64 len)
6069{
6070 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
6071}
6072
Chris Mason9ebefb182007-06-15 13:50:00 -04006073int btrfs_readpage(struct file *file, struct page *page)
6074{
Chris Masond1310b22008-01-24 16:13:08 -05006075 struct extent_io_tree *tree;
6076 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006077 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04006078}
Chris Mason1832a6d2007-12-21 16:27:21 -05006079
Chris Mason39279cc2007-06-12 06:35:45 -04006080static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6081{
Chris Masond1310b22008-01-24 16:13:08 -05006082 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006083
6084
6085 if (current->flags & PF_MEMALLOC) {
6086 redirty_page_for_writepage(wbc, page);
6087 unlock_page(page);
6088 return 0;
6089 }
Chris Masond1310b22008-01-24 16:13:08 -05006090 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006091 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6092}
Chris Mason39279cc2007-06-12 06:35:45 -04006093
Chris Masonf4219502008-07-22 11:18:09 -04006094int btrfs_writepages(struct address_space *mapping,
6095 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006096{
Chris Masond1310b22008-01-24 16:13:08 -05006097 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006098
Chris Masond1310b22008-01-24 16:13:08 -05006099 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006100 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6101}
6102
Chris Mason3ab2fb52007-11-08 10:59:22 -05006103static int
6104btrfs_readpages(struct file *file, struct address_space *mapping,
6105 struct list_head *pages, unsigned nr_pages)
6106{
Chris Masond1310b22008-01-24 16:13:08 -05006107 struct extent_io_tree *tree;
6108 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006109 return extent_readpages(tree, mapping, pages, nr_pages,
6110 btrfs_get_extent);
6111}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006112static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006113{
Chris Masond1310b22008-01-24 16:13:08 -05006114 struct extent_io_tree *tree;
6115 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006116 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006117
Chris Masond1310b22008-01-24 16:13:08 -05006118 tree = &BTRFS_I(page->mapping->host)->io_tree;
6119 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006120 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006121 if (ret == 1) {
6122 ClearPagePrivate(page);
6123 set_page_private(page, 0);
6124 page_cache_release(page);
6125 }
6126 return ret;
6127}
Chris Mason39279cc2007-06-12 06:35:45 -04006128
Chris Masone6dcd2d2008-07-17 12:53:50 -04006129static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6130{
Chris Mason98509cf2008-09-11 15:51:43 -04006131 if (PageWriteback(page) || PageDirty(page))
6132 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006133 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006134}
6135
Chris Masona52d9a82007-08-27 16:49:44 -04006136static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6137{
Chris Masond1310b22008-01-24 16:13:08 -05006138 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006139 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006140 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006141 u64 page_start = page_offset(page);
6142 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006143
Chris Mason8b62b722009-09-02 16:53:46 -04006144
6145 /*
6146 * we have the page locked, so new writeback can't start,
6147 * and the dirty bit won't be cleared while we are here.
6148 *
6149 * Wait for IO on this page so that we can safely clear
6150 * the PagePrivate2 bit and do ordered accounting
6151 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006152 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006153
Chris Masond1310b22008-01-24 16:13:08 -05006154 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006155 if (offset) {
6156 btrfs_releasepage(page, GFP_NOFS);
6157 return;
6158 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006159 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6160 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006161 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6162 page_offset(page));
6163 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006164 /*
6165 * IO on this page will never be started, so we need
6166 * to account for any ordered extents now
6167 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006168 clear_extent_bit(tree, page_start, page_end,
6169 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006170 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006171 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006172 /*
6173 * whoever cleared the private bit is responsible
6174 * for the finish_ordered_io
6175 */
6176 if (TestClearPagePrivate2(page)) {
6177 btrfs_finish_ordered_io(page->mapping->host,
6178 page_start, page_end);
6179 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006180 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006181 cached_state = NULL;
6182 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6183 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006184 }
6185 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006186 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006187 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006188 __btrfs_releasepage(page, GFP_NOFS);
6189
Chris Mason4a096752008-07-21 10:29:44 -04006190 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006191 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006192 ClearPagePrivate(page);
6193 set_page_private(page, 0);
6194 page_cache_release(page);
6195 }
Chris Mason39279cc2007-06-12 06:35:45 -04006196}
6197
Chris Mason9ebefb182007-06-15 13:50:00 -04006198/*
6199 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6200 * called from a page fault handler when a page is first dirtied. Hence we must
6201 * be careful to check for EOF conditions here. We set the page up correctly
6202 * for a written page which means we get ENOSPC checking when writing into
6203 * holes and correct delalloc and unwritten extent mapping on filesystems that
6204 * support these features.
6205 *
6206 * We are not allowed to take the i_mutex here so we have to play games to
6207 * protect against truncate races as the page could now be beyond EOF. Because
6208 * vmtruncate() writes the inode size before removing pages, once we have the
6209 * page lock we can determine safely if the page is beyond EOF. If it is not
6210 * beyond EOF, then the page is guaranteed safe against truncation until we
6211 * unlock the page.
6212 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006213int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006214{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006215 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006216 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006217 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006218 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6219 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006220 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006221 char *kaddr;
6222 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006223 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006224 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04006225 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006226 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006227
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006228 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07006229 if (ret) {
6230 if (ret == -ENOMEM)
6231 ret = VM_FAULT_OOM;
6232 else /* -ENOSPC, -EIO, etc */
6233 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05006234 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07006235 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006236
Nick Piggin56a76f82009-03-31 15:23:23 -07006237 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006238again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006239 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006240 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006241 page_start = page_offset(page);
6242 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006243
Chris Mason9ebefb182007-06-15 13:50:00 -04006244 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006245 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006246 /* page got truncated out from underneath us */
6247 goto out_unlock;
6248 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006249 wait_on_page_writeback(page);
6250
Josef Bacik2ac55d42010-02-03 19:33:23 +00006251 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6252 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006253 set_page_extent_mapped(page);
6254
Chris Masoneb84ae02008-07-17 13:53:27 -04006255 /*
6256 * we can't set the delalloc bits if there are pending ordered
6257 * extents. Drop our locks and wait for them to finish
6258 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006259 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6260 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006261 unlock_extent_cached(io_tree, page_start, page_end,
6262 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006263 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006264 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006265 btrfs_put_ordered_extent(ordered);
6266 goto again;
6267 }
6268
Josef Bacikfbf19082009-10-01 17:10:23 -04006269 /*
6270 * XXX - page_mkwrite gets called every time the page is dirtied, even
6271 * if it was already dirty, so for space accounting reasons we need to
6272 * clear any delalloc bits for the range we are fixing to save. There
6273 * is probably a better way to do this, but for now keep consistent with
6274 * prepare_pages in the normal write path.
6275 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006276 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006277 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006278 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006279
Josef Bacik2ac55d42010-02-03 19:33:23 +00006280 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6281 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006282 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006283 unlock_extent_cached(io_tree, page_start, page_end,
6284 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006285 ret = VM_FAULT_SIGBUS;
6286 goto out_unlock;
6287 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006288 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006289
6290 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006291 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006292 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006293 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006294 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006295
Chris Masone6dcd2d2008-07-17 12:53:50 -04006296 if (zero_start != PAGE_CACHE_SIZE) {
6297 kaddr = kmap(page);
6298 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6299 flush_dcache_page(page);
6300 kunmap(page);
6301 }
Chris Mason247e7432008-07-17 12:53:51 -04006302 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006303 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006304 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006305
Chris Mason257c62e2009-10-13 13:21:08 -04006306 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6307 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6308
Josef Bacik2ac55d42010-02-03 19:33:23 +00006309 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006310
6311out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006312 if (!ret)
6313 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006314 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006315 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05006316out:
Chris Mason9ebefb182007-06-15 13:50:00 -04006317 return ret;
6318}
6319
Chris Mason39279cc2007-06-12 06:35:45 -04006320static void btrfs_truncate(struct inode *inode)
6321{
6322 struct btrfs_root *root = BTRFS_I(inode)->root;
6323 int ret;
6324 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006325 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006326 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006327
Yan, Zheng80825102009-11-12 09:35:36 +00006328 if (!S_ISREG(inode->i_mode)) {
6329 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04006330 return;
Yan, Zheng80825102009-11-12 09:35:36 +00006331 }
Chris Mason39279cc2007-06-12 06:35:45 -04006332
Josef Bacik5d5e1032009-10-13 16:46:49 -04006333 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6334 if (ret)
6335 return;
Yan, Zheng80825102009-11-12 09:35:36 +00006336
Chris Mason4a096752008-07-21 10:29:44 -04006337 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006338 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006339
Yan, Zhengd68fc572010-05-16 10:49:58 -04006340 trans = btrfs_start_transaction(root, 0);
6341 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00006342 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006343 trans->block_rsv = root->orphan_block_rsv;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006344
6345 /*
6346 * setattr is responsible for setting the ordered_data_close flag,
6347 * but that is only tested during the last file release. That
6348 * could happen well after the next commit, leaving a great big
6349 * window where new writes may get lost if someone chooses to write
6350 * to this file after truncating to zero
6351 *
6352 * The inode doesn't have any dirty data here, and so if we commit
6353 * this is a noop. If someone immediately starts writing to the inode
6354 * it is very likely we'll catch some of their writes in this
6355 * transaction, and the commit will find this file on the ordered
6356 * data list with good things to send down.
6357 *
6358 * This is a best effort solution, there is still a window where
6359 * using truncate to replace the contents of the file will
6360 * end up with a zero length file after a crash.
6361 */
6362 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6363 btrfs_add_ordered_operation(trans, root, inode);
6364
Yan, Zheng80825102009-11-12 09:35:36 +00006365 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006366 if (!trans) {
6367 trans = btrfs_start_transaction(root, 0);
6368 BUG_ON(IS_ERR(trans));
6369 btrfs_set_trans_block_group(trans, inode);
6370 trans->block_rsv = root->orphan_block_rsv;
6371 }
6372
6373 ret = btrfs_block_rsv_check(trans, root,
6374 root->orphan_block_rsv, 0, 5);
6375 if (ret) {
6376 BUG_ON(ret != -EAGAIN);
6377 ret = btrfs_commit_transaction(trans, root);
6378 BUG_ON(ret);
6379 trans = NULL;
6380 continue;
6381 }
6382
Yan, Zheng80825102009-11-12 09:35:36 +00006383 ret = btrfs_truncate_inode_items(trans, root, inode,
6384 inode->i_size,
6385 BTRFS_EXTENT_DATA_KEY);
6386 if (ret != -EAGAIN)
6387 break;
Chris Mason39279cc2007-06-12 06:35:45 -04006388
Yan, Zheng80825102009-11-12 09:35:36 +00006389 ret = btrfs_update_inode(trans, root, inode);
6390 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04006391
Yan, Zheng80825102009-11-12 09:35:36 +00006392 nr = trans->blocks_used;
6393 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006394 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006395 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006396 }
6397
6398 if (ret == 0 && inode->i_nlink > 0) {
6399 ret = btrfs_orphan_del(trans, inode);
6400 BUG_ON(ret);
6401 }
6402
6403 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04006404 BUG_ON(ret);
6405
Josef Bacik7b128762008-07-24 12:17:14 -04006406 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04006407 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006408 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006409 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04006410}
6411
Sven Wegener3b963622008-06-09 21:57:42 -04006412/*
Chris Masond352ac62008-09-29 15:18:18 -04006413 * create a new subvolume directory/inode (helper for the ioctl).
6414 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006415int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04006416 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006417 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04006418{
Chris Mason39279cc2007-06-12 06:35:45 -04006419 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006420 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006421 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006422
Josef Bacikaec74772008-07-24 12:12:38 -04006423 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006424 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006425 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006426 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006427 inode->i_op = &btrfs_dir_inode_operations;
6428 inode->i_fop = &btrfs_dir_file_operations;
6429
Chris Mason39279cc2007-06-12 06:35:45 -04006430 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006431 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006432
Yan, Zheng76dda932009-09-21 16:00:26 -04006433 err = btrfs_update_inode(trans, new_root, inode);
6434 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006435
Yan, Zheng76dda932009-09-21 16:00:26 -04006436 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006437 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006438}
6439
Chris Masond352ac62008-09-29 15:18:18 -04006440/* helper function for file defrag and space balancing. This
6441 * forces readahead on a given range of bytes in an inode
6442 */
Chris Masonedbd8d42007-12-21 16:27:24 -05006443unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04006444 struct file_ra_state *ra, struct file *file,
6445 pgoff_t offset, pgoff_t last_index)
6446{
Chris Mason8e7bf942008-04-28 09:02:36 -04006447 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04006448
Chris Mason86479a02007-09-10 19:58:16 -04006449 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6450 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04006451}
6452
Chris Mason39279cc2007-06-12 06:35:45 -04006453struct inode *btrfs_alloc_inode(struct super_block *sb)
6454{
6455 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006456 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006457
6458 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6459 if (!ei)
6460 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006461
6462 ei->root = NULL;
6463 ei->space_info = NULL;
6464 ei->generation = 0;
6465 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006466 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006467 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006468 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006469 ei->delalloc_bytes = 0;
6470 ei->reserved_bytes = 0;
6471 ei->disk_i_size = 0;
6472 ei->flags = 0;
6473 ei->index_cnt = (u64)-1;
6474 ei->last_unlink_trans = 0;
6475
Josef Bacik32c00af2009-10-08 13:34:05 -04006476 spin_lock_init(&ei->accounting_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006477 atomic_set(&ei->outstanding_extents, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006478 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006479
6480 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006481 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006482 ei->dummy_inode = 0;
6483 ei->force_compress = 0;
6484
6485 inode = &ei->vfs_inode;
6486 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6487 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6488 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6489 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006490 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006491 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006492 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006493 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006494 RB_CLEAR_NODE(&ei->rb_node);
6495
6496 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006497}
6498
6499void btrfs_destroy_inode(struct inode *inode)
6500{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006501 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006502 struct btrfs_root *root = BTRFS_I(inode)->root;
6503
Chris Mason39279cc2007-06-12 06:35:45 -04006504 WARN_ON(!list_empty(&inode->i_dentry));
6505 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006506 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
6507 WARN_ON(BTRFS_I(inode)->reserved_extents);
Chris Mason39279cc2007-06-12 06:35:45 -04006508
Chris Mason5a3f23d2009-03-31 13:27:11 -04006509 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006510 * This can happen where we create an inode, but somebody else also
6511 * created the same inode and we need to destroy the one we already
6512 * created.
6513 */
6514 if (!root)
6515 goto free;
6516
6517 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006518 * Make sure we're properly removed from the ordered operation
6519 * lists.
6520 */
6521 smp_mb();
6522 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6523 spin_lock(&root->fs_info->ordered_extent_lock);
6524 list_del_init(&BTRFS_I(inode)->ordered_operations);
6525 spin_unlock(&root->fs_info->ordered_extent_lock);
6526 }
6527
Josef Bacik0af3d002010-06-21 14:48:16 -04006528 if (root == root->fs_info->tree_root) {
6529 struct btrfs_block_group_cache *block_group;
6530
6531 block_group = btrfs_lookup_block_group(root->fs_info,
6532 BTRFS_I(inode)->block_group);
6533 if (block_group && block_group->inode == inode) {
6534 spin_lock(&block_group->lock);
6535 block_group->inode = NULL;
6536 spin_unlock(&block_group->lock);
6537 btrfs_put_block_group(block_group);
6538 } else if (block_group) {
6539 btrfs_put_block_group(block_group);
6540 }
6541 }
6542
Yan, Zhengd68fc572010-05-16 10:49:58 -04006543 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006544 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00006545 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
6546 inode->i_ino);
6547 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006548 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006549 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006550
Chris Masond3977122009-01-05 21:25:51 -05006551 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006552 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6553 if (!ordered)
6554 break;
6555 else {
Chris Masond3977122009-01-05 21:25:51 -05006556 printk(KERN_ERR "btrfs found ordered "
6557 "extent %llu %llu on inode cleanup\n",
6558 (unsigned long long)ordered->file_offset,
6559 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006560 btrfs_remove_ordered_extent(inode, ordered);
6561 btrfs_put_ordered_extent(ordered);
6562 btrfs_put_ordered_extent(ordered);
6563 }
6564 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006565 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006566 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006567free:
Chris Mason39279cc2007-06-12 06:35:45 -04006568 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6569}
6570
Al Viro45321ac2010-06-07 13:43:19 -04006571int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006572{
6573 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006574
Josef Bacik0af3d002010-06-21 14:48:16 -04006575 if (btrfs_root_refs(&root->root_item) == 0 &&
6576 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04006577 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006578 else
Al Viro45321ac2010-06-07 13:43:19 -04006579 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006580}
6581
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006582static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006583{
6584 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6585
6586 inode_init_once(&ei->vfs_inode);
6587}
6588
6589void btrfs_destroy_cachep(void)
6590{
6591 if (btrfs_inode_cachep)
6592 kmem_cache_destroy(btrfs_inode_cachep);
6593 if (btrfs_trans_handle_cachep)
6594 kmem_cache_destroy(btrfs_trans_handle_cachep);
6595 if (btrfs_transaction_cachep)
6596 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006597 if (btrfs_path_cachep)
6598 kmem_cache_destroy(btrfs_path_cachep);
6599}
6600
6601int btrfs_init_cachep(void)
6602{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006603 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6604 sizeof(struct btrfs_inode), 0,
6605 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006606 if (!btrfs_inode_cachep)
6607 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006608
6609 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6610 sizeof(struct btrfs_trans_handle), 0,
6611 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006612 if (!btrfs_trans_handle_cachep)
6613 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006614
6615 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6616 sizeof(struct btrfs_transaction), 0,
6617 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006618 if (!btrfs_transaction_cachep)
6619 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006620
6621 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6622 sizeof(struct btrfs_path), 0,
6623 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006624 if (!btrfs_path_cachep)
6625 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006626
Chris Mason39279cc2007-06-12 06:35:45 -04006627 return 0;
6628fail:
6629 btrfs_destroy_cachep();
6630 return -ENOMEM;
6631}
6632
6633static int btrfs_getattr(struct vfsmount *mnt,
6634 struct dentry *dentry, struct kstat *stat)
6635{
6636 struct inode *inode = dentry->d_inode;
6637 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05006638 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05006639 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006640 stat->blocks = (inode_get_bytes(inode) +
6641 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04006642 return 0;
6643}
6644
Chris Masond3977122009-01-05 21:25:51 -05006645static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6646 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04006647{
6648 struct btrfs_trans_handle *trans;
6649 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006650 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04006651 struct inode *new_inode = new_dentry->d_inode;
6652 struct inode *old_inode = old_dentry->d_inode;
6653 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006654 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006655 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04006656 int ret;
6657
Yan, Zhengf679a842009-09-24 09:17:31 -04006658 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6659 return -EPERM;
6660
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006661 /* we only allow rename subvolume link between subvolumes */
6662 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05006663 return -EXDEV;
6664
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006665 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
6666 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
6667 return -ENOTEMPTY;
6668
Chris Mason39279cc2007-06-12 06:35:45 -04006669 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006670 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04006671 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006672 /*
6673 * we're using rename to replace one file with another.
6674 * and the replacement file is large. Start IO on it now so
6675 * we don't add too much work to the end of the transaction
6676 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04006677 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04006678 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
6679 filemap_flush(old_inode->i_mapping);
6680
Yan, Zheng76dda932009-09-21 16:00:26 -04006681 /* close the racy window with snapshot create/destroy ioctl */
6682 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6683 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006684 /*
6685 * We want to reserve the absolute worst case amount of items. So if
6686 * both inodes are subvols and we need to unlink them then that would
6687 * require 4 item modifications, but if they are both normal inodes it
6688 * would require 5 item modifications, so we'll assume their normal
6689 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6690 * should cover the worst case number of items we'll modify.
6691 */
6692 trans = btrfs_start_transaction(root, 20);
6693 if (IS_ERR(trans))
6694 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04006695
Yan, Zhenga5719522009-09-24 09:17:31 -04006696 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04006697
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006698 if (dest != root)
6699 btrfs_record_root_in_trans(trans, dest);
6700
Yan, Zhenga5719522009-09-24 09:17:31 -04006701 ret = btrfs_set_inode_index(new_dir, &index);
6702 if (ret)
6703 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006704
Yan, Zhenga5719522009-09-24 09:17:31 -04006705 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006706 /* force full log commit if subvolume involved. */
6707 root->fs_info->last_trans_log_full_commit = trans->transid;
6708 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04006709 ret = btrfs_insert_inode_ref(trans, dest,
6710 new_dentry->d_name.name,
6711 new_dentry->d_name.len,
6712 old_inode->i_ino,
6713 new_dir->i_ino, index);
6714 if (ret)
6715 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006716 /*
6717 * this is an ugly little race, but the rename is required
6718 * to make sure that if we crash, the inode is either at the
6719 * old name or the new one. pinning the log transaction lets
6720 * us make sure we don't allow a log commit to come in after
6721 * we unlink the name but before we add the new name back in.
6722 */
6723 btrfs_pin_log_trans(root);
6724 }
Chris Mason12fcfd22009-03-24 10:24:20 -04006725 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006726 * make sure the inode gets flushed if it is replacing
6727 * something.
6728 */
6729 if (new_inode && new_inode->i_size &&
6730 old_inode && S_ISREG(old_inode->i_mode)) {
6731 btrfs_add_ordered_operation(trans, root, old_inode);
6732 }
6733
Chris Mason39279cc2007-06-12 06:35:45 -04006734 old_dir->i_ctime = old_dir->i_mtime = ctime;
6735 new_dir->i_ctime = new_dir->i_mtime = ctime;
6736 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04006737
Chris Mason12fcfd22009-03-24 10:24:20 -04006738 if (old_dentry->d_parent != new_dentry->d_parent)
6739 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
6740
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006741 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
6742 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
6743 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
6744 old_dentry->d_name.name,
6745 old_dentry->d_name.len);
6746 } else {
6747 btrfs_inc_nlink(old_dentry->d_inode);
6748 ret = btrfs_unlink_inode(trans, root, old_dir,
6749 old_dentry->d_inode,
6750 old_dentry->d_name.name,
6751 old_dentry->d_name.len);
6752 }
6753 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006754
6755 if (new_inode) {
6756 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006757 if (unlikely(new_inode->i_ino ==
6758 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
6759 root_objectid = BTRFS_I(new_inode)->location.objectid;
6760 ret = btrfs_unlink_subvol(trans, dest, new_dir,
6761 root_objectid,
6762 new_dentry->d_name.name,
6763 new_dentry->d_name.len);
6764 BUG_ON(new_inode->i_nlink == 0);
6765 } else {
6766 ret = btrfs_unlink_inode(trans, dest, new_dir,
6767 new_dentry->d_inode,
6768 new_dentry->d_name.name,
6769 new_dentry->d_name.len);
6770 }
6771 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006772 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04006773 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006774 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006775 }
Chris Mason39279cc2007-06-12 06:35:45 -04006776 }
Josef Bacikaec74772008-07-24 12:12:38 -04006777
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006778 ret = btrfs_add_link(trans, new_dir, old_inode,
6779 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04006780 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006781 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006782
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006783 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik6a912212010-11-20 09:48:00 +00006784 struct dentry *parent = dget_parent(new_dentry);
6785 btrfs_log_new_name(trans, old_inode, old_dir, parent);
6786 dput(parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006787 btrfs_end_log_trans(root);
6788 }
Chris Mason39279cc2007-06-12 06:35:45 -04006789out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04006790 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006791
Yan, Zheng76dda932009-09-21 16:00:26 -04006792 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6793 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006794
Chris Mason39279cc2007-06-12 06:35:45 -04006795 return ret;
6796}
6797
Chris Masond352ac62008-09-29 15:18:18 -04006798/*
6799 * some fairly slow code that needs optimization. This walks the list
6800 * of all the inodes with pending delalloc and forces them to disk.
6801 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006802int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04006803{
6804 struct list_head *head = &root->fs_info->delalloc_inodes;
6805 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006806 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04006807
Yan Zhengc146afa2008-11-12 14:34:12 -05006808 if (root->fs_info->sb->s_flags & MS_RDONLY)
6809 return -EROFS;
6810
Chris Mason75eff682008-12-15 15:54:40 -05006811 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05006812 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04006813 binode = list_entry(head->next, struct btrfs_inode,
6814 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006815 inode = igrab(&binode->vfs_inode);
6816 if (!inode)
6817 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05006818 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006819 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04006820 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006821 if (delay_iput)
6822 btrfs_add_delayed_iput(inode);
6823 else
6824 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006825 }
6826 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05006827 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04006828 }
Chris Mason75eff682008-12-15 15:54:40 -05006829 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04006830
6831 /* the filemap_flush will queue IO into the worker threads, but
6832 * we have to make sure the IO is actually started and that
6833 * ordered extents get created before we return
6834 */
6835 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05006836 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05006837 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04006838 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05006839 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
6840 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04006841 }
6842 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04006843 return 0;
6844}
6845
Josef Bacik0019f102010-10-15 15:18:40 -04006846int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
6847 int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04006848{
6849 struct btrfs_inode *binode;
6850 struct inode *inode = NULL;
6851
6852 spin_lock(&root->fs_info->delalloc_lock);
6853 while (!list_empty(&root->fs_info->delalloc_inodes)) {
6854 binode = list_entry(root->fs_info->delalloc_inodes.next,
6855 struct btrfs_inode, delalloc_inodes);
6856 inode = igrab(&binode->vfs_inode);
6857 if (inode) {
6858 list_move_tail(&binode->delalloc_inodes,
6859 &root->fs_info->delalloc_inodes);
6860 break;
6861 }
6862
6863 list_del_init(&binode->delalloc_inodes);
6864 cond_resched_lock(&root->fs_info->delalloc_lock);
6865 }
6866 spin_unlock(&root->fs_info->delalloc_lock);
6867
6868 if (inode) {
Josef Bacik0019f102010-10-15 15:18:40 -04006869 if (sync) {
6870 filemap_write_and_wait(inode->i_mapping);
6871 /*
6872 * We have to do this because compression doesn't
6873 * actually set PG_writeback until it submits the pages
6874 * for IO, which happens in an async thread, so we could
6875 * race and not actually wait for any writeback pages
6876 * because they've not been submitted yet. Technically
6877 * this could still be the case for the ordered stuff
6878 * since the async thread may not have started to do its
6879 * work yet. If this becomes the case then we need to
6880 * figure out a way to make sure that in writepage we
6881 * wait for any async pages to be submitted before
6882 * returning so that fdatawait does what its supposed to
6883 * do.
6884 */
6885 btrfs_wait_ordered_range(inode, 0, (u64)-1);
6886 } else {
6887 filemap_flush(inode->i_mapping);
6888 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04006889 if (delay_iput)
6890 btrfs_add_delayed_iput(inode);
6891 else
6892 iput(inode);
6893 return 1;
6894 }
6895 return 0;
6896}
6897
Chris Mason39279cc2007-06-12 06:35:45 -04006898static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
6899 const char *symname)
6900{
6901 struct btrfs_trans_handle *trans;
6902 struct btrfs_root *root = BTRFS_I(dir)->root;
6903 struct btrfs_path *path;
6904 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05006905 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006906 int err;
6907 int drop_inode = 0;
6908 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006909 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04006910 int name_len;
6911 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04006912 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006913 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04006914 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05006915 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006916
6917 name_len = strlen(symname) + 1;
6918 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
6919 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05006920
Yan, Zhenga22285a2010-05-16 10:48:46 -04006921 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
6922 if (err)
6923 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006924 /*
6925 * 2 items for inode item and ref
6926 * 2 items for dir items
6927 * 1 item for xattr if selinux is on
6928 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006929 trans = btrfs_start_transaction(root, 5);
6930 if (IS_ERR(trans))
6931 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006932
Chris Mason39279cc2007-06-12 06:35:45 -04006933 btrfs_set_trans_block_group(trans, dir);
6934
Josef Bacikaec74772008-07-24 12:12:38 -04006935 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00006936 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04006937 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
6938 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006939 err = PTR_ERR(inode);
6940 if (IS_ERR(inode))
6941 goto out_unlock;
6942
Yan, Zhengf34f57a2009-11-12 09:35:27 +00006943 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04006944 if (err) {
6945 drop_inode = 1;
6946 goto out_unlock;
6947 }
6948
Chris Mason39279cc2007-06-12 06:35:45 -04006949 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00006950 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006951 if (err)
6952 drop_inode = 1;
6953 else {
6954 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04006955 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04006956 inode->i_fop = &btrfs_file_operations;
6957 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05006958 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04006959 }
Chris Mason39279cc2007-06-12 06:35:45 -04006960 btrfs_update_inode_block_group(trans, inode);
6961 btrfs_update_inode_block_group(trans, dir);
6962 if (drop_inode)
6963 goto out_unlock;
6964
6965 path = btrfs_alloc_path();
6966 BUG_ON(!path);
6967 key.objectid = inode->i_ino;
6968 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006969 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
6970 datasize = btrfs_file_extent_calc_inline_size(name_len);
6971 err = btrfs_insert_empty_item(trans, root, path, &key,
6972 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006973 if (err) {
6974 drop_inode = 1;
6975 goto out_unlock;
6976 }
Chris Mason5f39d392007-10-15 16:14:19 -04006977 leaf = path->nodes[0];
6978 ei = btrfs_item_ptr(leaf, path->slots[0],
6979 struct btrfs_file_extent_item);
6980 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
6981 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04006982 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04006983 btrfs_set_file_extent_encryption(leaf, ei, 0);
6984 btrfs_set_file_extent_compression(leaf, ei, 0);
6985 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
6986 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
6987
Chris Mason39279cc2007-06-12 06:35:45 -04006988 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04006989 write_extent_buffer(leaf, symname, ptr, name_len);
6990 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04006991 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04006992
Chris Mason39279cc2007-06-12 06:35:45 -04006993 inode->i_op = &btrfs_symlink_inode_operations;
6994 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04006995 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04006996 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04006997 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04006998 err = btrfs_update_inode(trans, root, inode);
6999 if (err)
7000 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007001
7002out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007003 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04007004 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007005 if (drop_inode) {
7006 inode_dec_link_count(inode);
7007 iput(inode);
7008 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007009 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007010 return err;
7011}
Chris Mason16432982008-04-10 10:23:21 -04007012
Josef Bacik0af3d002010-06-21 14:48:16 -04007013static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7014 u64 start, u64 num_bytes, u64 min_size,
7015 loff_t actual_len, u64 *alloc_hint,
7016 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007017{
Yan Zhengd899e052008-10-30 14:25:28 -04007018 struct btrfs_root *root = BTRFS_I(inode)->root;
7019 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007020 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007021 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007022 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007023 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007024
Josef Bacik0af3d002010-06-21 14:48:16 -04007025 if (trans)
7026 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007027 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007028 if (own_trans) {
7029 trans = btrfs_start_transaction(root, 3);
7030 if (IS_ERR(trans)) {
7031 ret = PTR_ERR(trans);
7032 break;
7033 }
Yan Zhengd899e052008-10-30 14:25:28 -04007034 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007035
Yan, Zhengefa56462010-05-16 10:49:59 -04007036 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7037 0, *alloc_hint, (u64)-1, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007038 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007039 if (own_trans)
7040 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007041 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007042 }
7043
Yan Zhengd899e052008-10-30 14:25:28 -04007044 ret = insert_reserved_file_extent(trans, inode,
7045 cur_offset, ins.objectid,
7046 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007047 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007048 BTRFS_FILE_EXTENT_PREALLOC);
7049 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04007050 btrfs_drop_extent_cache(inode, cur_offset,
7051 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007052
Yan Zhengd899e052008-10-30 14:25:28 -04007053 num_bytes -= ins.offset;
7054 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007055 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007056
Yan Zhengd899e052008-10-30 14:25:28 -04007057 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007058 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007059 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007060 (actual_len > inode->i_size) &&
7061 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007062 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007063 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007064 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007065 i_size = cur_offset;
7066 i_size_write(inode, i_size);
7067 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007068 }
7069
Yan Zhengd899e052008-10-30 14:25:28 -04007070 ret = btrfs_update_inode(trans, root, inode);
7071 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04007072
Josef Bacik0af3d002010-06-21 14:48:16 -04007073 if (own_trans)
7074 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007075 }
Yan Zhengd899e052008-10-30 14:25:28 -04007076 return ret;
7077}
7078
Josef Bacik0af3d002010-06-21 14:48:16 -04007079int btrfs_prealloc_file_range(struct inode *inode, int mode,
7080 u64 start, u64 num_bytes, u64 min_size,
7081 loff_t actual_len, u64 *alloc_hint)
7082{
7083 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7084 min_size, actual_len, alloc_hint,
7085 NULL);
7086}
7087
7088int btrfs_prealloc_file_range_trans(struct inode *inode,
7089 struct btrfs_trans_handle *trans, int mode,
7090 u64 start, u64 num_bytes, u64 min_size,
7091 loff_t actual_len, u64 *alloc_hint)
7092{
7093 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7094 min_size, actual_len, alloc_hint, trans);
7095}
7096
Yan Zhengd899e052008-10-30 14:25:28 -04007097static long btrfs_fallocate(struct inode *inode, int mode,
7098 loff_t offset, loff_t len)
7099{
Josef Bacik2ac55d42010-02-03 19:33:23 +00007100 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04007101 u64 cur_offset;
7102 u64 last_byte;
7103 u64 alloc_start;
7104 u64 alloc_end;
7105 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04007106 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04007107 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
7108 struct extent_map *em;
7109 int ret;
7110
7111 alloc_start = offset & ~mask;
7112 alloc_end = (offset + len + mask) & ~mask;
7113
Chris Mason546888d2009-04-21 11:53:38 -04007114 /*
7115 * wait for ordered IO before we have any locks. We'll loop again
7116 * below with the locks held.
7117 */
7118 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
7119
Yan Zhengd899e052008-10-30 14:25:28 -04007120 mutex_lock(&inode->i_mutex);
Josef Bacik0ed42a62010-11-22 18:55:39 +00007121 ret = inode_newsize_ok(inode, alloc_end);
7122 if (ret)
7123 goto out;
7124
Yan Zhengd899e052008-10-30 14:25:28 -04007125 if (alloc_start > inode->i_size) {
7126 ret = btrfs_cont_expand(inode, alloc_start);
7127 if (ret)
7128 goto out;
7129 }
7130
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007131 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
Josef Bacika970b0a2009-06-27 21:07:34 -04007132 if (ret)
7133 goto out;
7134
Chris Masone980b502009-04-24 14:39:24 -04007135 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04007136 while (1) {
7137 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04007138
Chris Mason546888d2009-04-21 11:53:38 -04007139 /* the extent lock is ordered inside the running
7140 * transaction
7141 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007142 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
7143 locked_end, 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04007144 ordered = btrfs_lookup_first_ordered_extent(inode,
7145 alloc_end - 1);
7146 if (ordered &&
7147 ordered->file_offset + ordered->len > alloc_start &&
7148 ordered->file_offset < alloc_end) {
7149 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007150 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7151 alloc_start, locked_end,
7152 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04007153 /*
7154 * we can't wait on the range with the transaction
7155 * running or with the extent lock held
7156 */
Yan Zhengd899e052008-10-30 14:25:28 -04007157 btrfs_wait_ordered_range(inode, alloc_start,
7158 alloc_end - alloc_start);
7159 } else {
7160 if (ordered)
7161 btrfs_put_ordered_extent(ordered);
7162 break;
7163 }
7164 }
7165
7166 cur_offset = alloc_start;
7167 while (1) {
7168 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
7169 alloc_end - cur_offset, 0);
7170 BUG_ON(IS_ERR(em) || !em);
7171 last_byte = min(extent_map_end(em), alloc_end);
7172 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007173 if (em->block_start == EXTENT_MAP_HOLE ||
7174 (cur_offset >= inode->i_size &&
7175 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
Josef Bacik83609772010-06-07 18:26:37 +00007176 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04007177 last_byte - cur_offset,
7178 1 << inode->i_blkbits,
7179 offset + len,
7180 &alloc_hint);
Yan Zhengd899e052008-10-30 14:25:28 -04007181 if (ret < 0) {
7182 free_extent_map(em);
7183 break;
7184 }
7185 }
Yan Zhengd899e052008-10-30 14:25:28 -04007186 free_extent_map(em);
7187
7188 cur_offset = last_byte;
7189 if (cur_offset >= alloc_end) {
7190 ret = 0;
7191 break;
7192 }
7193 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00007194 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
7195 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04007196
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007197 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04007198out:
7199 mutex_unlock(&inode->i_mutex);
7200 return ret;
7201}
7202
Chris Masone6dcd2d2008-07-17 12:53:50 -04007203static int btrfs_set_page_dirty(struct page *page)
7204{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007205 return __set_page_dirty_nobuffers(page);
7206}
7207
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007208static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007209{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007210 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05007211 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04007212 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05007213}
Chris Mason39279cc2007-06-12 06:35:45 -04007214
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007215static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007216 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007217 .lookup = btrfs_lookup,
7218 .create = btrfs_create,
7219 .unlink = btrfs_unlink,
7220 .link = btrfs_link,
7221 .mkdir = btrfs_mkdir,
7222 .rmdir = btrfs_rmdir,
7223 .rename = btrfs_rename,
7224 .symlink = btrfs_symlink,
7225 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007226 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007227 .setxattr = btrfs_setxattr,
7228 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007229 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007230 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007231 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007232};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007233static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007234 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007235 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007236};
Yan, Zheng76dda932009-09-21 16:00:26 -04007237
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007238static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007239 .llseek = generic_file_llseek,
7240 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007241 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007242 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007243#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007244 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007245#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007246 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007247 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007248};
7249
Chris Masond1310b22008-01-24 16:13:08 -05007250static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007251 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007252 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007253 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007254 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007255 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007256 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04007257 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007258 .set_bit_hook = btrfs_set_bit_hook,
7259 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007260 .merge_extent_hook = btrfs_merge_extent_hook,
7261 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007262};
7263
Chris Mason35054392009-01-21 13:11:13 -05007264/*
7265 * btrfs doesn't support the bmap operation because swapfiles
7266 * use bmap to make a mapping of extents in the file. They assume
7267 * these extents won't change over the life of the file and they
7268 * use the bmap result to do IO directly to the drive.
7269 *
7270 * the btrfs bmap call would return logical addresses that aren't
7271 * suitable for IO and they also will change frequently as COW
7272 * operations happen. So, swapfile + btrfs == corruption.
7273 *
7274 * For now we're avoiding this by dropping bmap.
7275 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007276static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007277 .readpage = btrfs_readpage,
7278 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007279 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007280 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04007281 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04007282 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007283 .invalidatepage = btrfs_invalidatepage,
7284 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007285 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007286 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007287};
7288
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007289static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007290 .readpage = btrfs_readpage,
7291 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007292 .invalidatepage = btrfs_invalidatepage,
7293 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007294};
7295
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007296static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007297 .truncate = btrfs_truncate,
7298 .getattr = btrfs_getattr,
7299 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007300 .setxattr = btrfs_setxattr,
7301 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007302 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007303 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007304 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04007305 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007306 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04007307};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007308static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007309 .getattr = btrfs_getattr,
7310 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007311 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007312 .setxattr = btrfs_setxattr,
7313 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007314 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007315 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007316};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007317static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007318 .readlink = generic_readlink,
7319 .follow_link = page_follow_link_light,
7320 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007321 .getattr = btrfs_getattr,
Yanfdebe2b2008-01-14 13:26:08 -05007322 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007323 .setxattr = btrfs_setxattr,
7324 .getxattr = btrfs_getxattr,
7325 .listxattr = btrfs_listxattr,
7326 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007327};
Yan, Zheng76dda932009-09-21 16:00:26 -04007328
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007329const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007330 .d_delete = btrfs_dentry_delete,
7331};