blob: a85b90c86cb0e321fd91cd3bfb312e6b393194e9 [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050039#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040046#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040047#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040048#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040049#include "tree-log.h"
Chris Masonc8b97812008-10-29 14:49:59 -040050#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050051#include "locking.h"
Chris Mason39279cc2007-06-12 06:35:45 -040052
53struct btrfs_iget_args {
54 u64 ino;
55 struct btrfs_root *root;
56};
57
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070058static const struct inode_operations btrfs_dir_inode_operations;
59static const struct inode_operations btrfs_symlink_inode_operations;
60static const struct inode_operations btrfs_dir_ro_inode_operations;
61static const struct inode_operations btrfs_special_inode_operations;
62static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070063static const struct address_space_operations btrfs_aops;
64static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070065static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050066static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040067
68static struct kmem_cache *btrfs_inode_cachep;
69struct kmem_cache *btrfs_trans_handle_cachep;
70struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040071struct kmem_cache *btrfs_path_cachep;
72
73#define S_SHIFT 12
74static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
75 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
76 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
77 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
78 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
79 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
80 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
81 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
82};
83
Josef Bacik7b128762008-07-24 12:17:14 -040084static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040085static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050086static noinline int cow_file_range(struct inode *inode,
87 struct page *locked_page,
88 u64 start, u64 end, int *page_started,
89 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040090
Yan, Zhengf34f57a2009-11-12 09:35:27 +000091static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
92 struct inode *inode, struct inode *dir)
Jim Owens0279b4c2009-02-04 09:29:13 -050093{
94 int err;
95
Yan, Zhengf34f57a2009-11-12 09:35:27 +000096 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -050097 if (!err)
Yan, Zhengf34f57a2009-11-12 09:35:27 +000098 err = btrfs_xattr_security_init(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -050099 return err;
100}
101
Chris Masond352ac62008-09-29 15:18:18 -0400102/*
Chris Masonc8b97812008-10-29 14:49:59 -0400103 * this does all the hard work for inserting an inline extent into
104 * the btree. The caller should have done a btrfs_drop_extents so that
105 * no overlapping inline items exist in the btree
106 */
Chris Masond3977122009-01-05 21:25:51 -0500107static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400108 struct btrfs_root *root, struct inode *inode,
109 u64 start, size_t size, size_t compressed_size,
110 struct page **compressed_pages)
111{
112 struct btrfs_key key;
113 struct btrfs_path *path;
114 struct extent_buffer *leaf;
115 struct page *page = NULL;
116 char *kaddr;
117 unsigned long ptr;
118 struct btrfs_file_extent_item *ei;
119 int err = 0;
120 int ret;
121 size_t cur_size = size;
122 size_t datasize;
123 unsigned long offset;
124 int use_compress = 0;
125
126 if (compressed_size && compressed_pages) {
127 use_compress = 1;
128 cur_size = compressed_size;
129 }
130
Chris Masond3977122009-01-05 21:25:51 -0500131 path = btrfs_alloc_path();
132 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400133 return -ENOMEM;
134
Chris Masonb9473432009-03-13 11:00:37 -0400135 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400136 btrfs_set_trans_block_group(trans, inode);
137
138 key.objectid = inode->i_ino;
139 key.offset = start;
140 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400141 datasize = btrfs_file_extent_calc_inline_size(cur_size);
142
143 inode_add_bytes(inode, size);
144 ret = btrfs_insert_empty_item(trans, root, path, &key,
145 datasize);
146 BUG_ON(ret);
147 if (ret) {
148 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400149 goto fail;
150 }
151 leaf = path->nodes[0];
152 ei = btrfs_item_ptr(leaf, path->slots[0],
153 struct btrfs_file_extent_item);
154 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
155 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
156 btrfs_set_file_extent_encryption(leaf, ei, 0);
157 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
158 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
159 ptr = btrfs_file_extent_inline_start(ei);
160
161 if (use_compress) {
162 struct page *cpage;
163 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500164 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400165 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500166 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400167 PAGE_CACHE_SIZE);
168
Chris Masonb9473432009-03-13 11:00:37 -0400169 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400170 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400171 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400172
173 i++;
174 ptr += cur_size;
175 compressed_size -= cur_size;
176 }
177 btrfs_set_file_extent_compression(leaf, ei,
178 BTRFS_COMPRESS_ZLIB);
179 } else {
180 page = find_get_page(inode->i_mapping,
181 start >> PAGE_CACHE_SHIFT);
182 btrfs_set_file_extent_compression(leaf, ei, 0);
183 kaddr = kmap_atomic(page, KM_USER0);
184 offset = start & (PAGE_CACHE_SIZE - 1);
185 write_extent_buffer(leaf, kaddr + offset, ptr, size);
186 kunmap_atomic(kaddr, KM_USER0);
187 page_cache_release(page);
188 }
189 btrfs_mark_buffer_dirty(leaf);
190 btrfs_free_path(path);
191
Yan, Zhengc2167752009-11-12 09:34:21 +0000192 /*
193 * we're an inline extent, so nobody can
194 * extend the file past i_size without locking
195 * a page we already have locked.
196 *
197 * We must do any isize and inode updates
198 * before we unlock the pages. Otherwise we
199 * could end up racing with unlink.
200 */
Chris Masonc8b97812008-10-29 14:49:59 -0400201 BTRFS_I(inode)->disk_i_size = inode->i_size;
202 btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000203
Chris Masonc8b97812008-10-29 14:49:59 -0400204 return 0;
205fail:
206 btrfs_free_path(path);
207 return err;
208}
209
210
211/*
212 * conditionally insert an inline extent into the file. This
213 * does the checks required to make sure the data is small enough
214 * to fit as an inline extent.
215 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400216static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400217 struct btrfs_root *root,
218 struct inode *inode, u64 start, u64 end,
219 size_t compressed_size,
220 struct page **compressed_pages)
221{
222 u64 isize = i_size_read(inode);
223 u64 actual_end = min(end + 1, isize);
224 u64 inline_len = actual_end - start;
225 u64 aligned_end = (end + root->sectorsize - 1) &
226 ~((u64)root->sectorsize - 1);
227 u64 hint_byte;
228 u64 data_len = inline_len;
229 int ret;
230
231 if (compressed_size)
232 data_len = compressed_size;
233
234 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400235 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400236 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
237 (!compressed_size &&
238 (actual_end & (root->sectorsize - 1)) == 0) ||
239 end + 1 < isize ||
240 data_len > root->fs_info->max_inline) {
241 return 1;
242 }
243
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000244 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400245 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400246 BUG_ON(ret);
247
248 if (isize > actual_end)
249 inline_len = min_t(u64, isize, actual_end);
250 ret = insert_inline_extent(trans, root, inode, start,
251 inline_len, compressed_size,
252 compressed_pages);
253 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -0400254 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400255 return 0;
256}
257
Chris Mason771ed682008-11-06 22:02:51 -0500258struct async_extent {
259 u64 start;
260 u64 ram_size;
261 u64 compressed_size;
262 struct page **pages;
263 unsigned long nr_pages;
264 struct list_head list;
265};
266
267struct async_cow {
268 struct inode *inode;
269 struct btrfs_root *root;
270 struct page *locked_page;
271 u64 start;
272 u64 end;
273 struct list_head extents;
274 struct btrfs_work work;
275};
276
277static noinline int add_async_extent(struct async_cow *cow,
278 u64 start, u64 ram_size,
279 u64 compressed_size,
280 struct page **pages,
281 unsigned long nr_pages)
282{
283 struct async_extent *async_extent;
284
285 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
286 async_extent->start = start;
287 async_extent->ram_size = ram_size;
288 async_extent->compressed_size = compressed_size;
289 async_extent->pages = pages;
290 async_extent->nr_pages = nr_pages;
291 list_add_tail(&async_extent->list, &cow->extents);
292 return 0;
293}
294
Chris Masonc8b97812008-10-29 14:49:59 -0400295/*
Chris Mason771ed682008-11-06 22:02:51 -0500296 * we create compressed extents in two phases. The first
297 * phase compresses a range of pages that have already been
298 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400299 *
Chris Mason771ed682008-11-06 22:02:51 -0500300 * This is done inside an ordered work queue, and the compression
301 * is spread across many cpus. The actual IO submission is step
302 * two, and the ordered work queue takes care of making sure that
303 * happens in the same order things were put onto the queue by
304 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400305 *
Chris Mason771ed682008-11-06 22:02:51 -0500306 * If this code finds it can't get good compression, it puts an
307 * entry onto the work queue to write the uncompressed bytes. This
308 * makes sure that both compressed inodes and uncompressed inodes
309 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400310 */
Chris Mason771ed682008-11-06 22:02:51 -0500311static noinline int compress_file_range(struct inode *inode,
312 struct page *locked_page,
313 u64 start, u64 end,
314 struct async_cow *async_cow,
315 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400316{
317 struct btrfs_root *root = BTRFS_I(inode)->root;
318 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400319 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400320 u64 orig_start;
321 u64 disk_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 Masonc8b97812008-10-29 14:49:59 -0400336 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500337
Chris Mason42dc7ba2008-12-15 11:44:56 -0500338 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400339again:
340 will_compress = 0;
341 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
342 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
343
Chris Masonf03d9302009-02-04 09:31:06 -0500344 /*
345 * we don't want to send crud past the end of i_size through
346 * compression, that's just a waste of CPU time. So, if the
347 * end of the file is before the start of our current
348 * requested range of bytes, we bail out to the uncompressed
349 * cleanup code that can deal with all of this.
350 *
351 * It isn't really the fastest way to fix things, but this is a
352 * very uncommon corner.
353 */
354 if (actual_end <= start)
355 goto cleanup_and_bail_uncompressed;
356
Chris Masonc8b97812008-10-29 14:49:59 -0400357 total_compressed = actual_end - start;
358
359 /* we want to make sure that amount of ram required to uncompress
360 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500361 * of a compressed extent to 128k. This is a crucial number
362 * because it also controls how easily we can spread reads across
363 * cpus for decompression.
364 *
365 * We also want to make sure the amount of IO required to do
366 * a random read is reasonably small, so we limit the size of
367 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400368 */
369 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400370 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500371 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400372 disk_num_bytes = num_bytes;
373 total_in = 0;
374 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400375
Chris Mason771ed682008-11-06 22:02:51 -0500376 /*
377 * we do compression for mount -o compress and when the
378 * inode has not been flagged as nocompress. This flag can
379 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400380 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200381 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500382 (btrfs_test_opt(root, COMPRESS) ||
383 (BTRFS_I(inode)->force_compress))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400384 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400385 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400386
Chris Masonc8b97812008-10-29 14:49:59 -0400387 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
388 total_compressed, pages,
389 nr_pages, &nr_pages_ret,
390 &total_in,
391 &total_compressed,
392 max_compressed);
393
394 if (!ret) {
395 unsigned long offset = total_compressed &
396 (PAGE_CACHE_SIZE - 1);
397 struct page *page = pages[nr_pages_ret - 1];
398 char *kaddr;
399
400 /* zero the tail end of the last page, we might be
401 * sending it down to disk
402 */
403 if (offset) {
404 kaddr = kmap_atomic(page, KM_USER0);
405 memset(kaddr + offset, 0,
406 PAGE_CACHE_SIZE - offset);
407 kunmap_atomic(kaddr, KM_USER0);
408 }
409 will_compress = 1;
410 }
411 }
412 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500413 trans = btrfs_join_transaction(root, 1);
414 BUG_ON(!trans);
415 btrfs_set_trans_block_group(trans, inode);
416
Chris Masonc8b97812008-10-29 14:49:59 -0400417 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500418 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400419 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500420 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400421 */
422 ret = cow_file_range_inline(trans, root, inode,
423 start, end, 0, NULL);
424 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500425 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400426 ret = cow_file_range_inline(trans, root, inode,
427 start, end,
428 total_compressed, pages);
429 }
430 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500431 /*
432 * inline extent creation worked, we don't need
433 * to create any more async work items. Unlock
434 * and free up our temp pages.
435 */
Chris Masonc8b97812008-10-29 14:49:59 -0400436 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400437 &BTRFS_I(inode)->io_tree,
438 start, end, NULL,
439 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400440 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400441 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400442 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000443
444 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400445 goto free_pages_out;
446 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000447 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400448 }
449
450 if (will_compress) {
451 /*
452 * we aren't doing an inline extent round the compressed size
453 * up to a block size boundary so the allocator does sane
454 * things
455 */
456 total_compressed = (total_compressed + blocksize - 1) &
457 ~(blocksize - 1);
458
459 /*
460 * one last check to make sure the compression is really a
461 * win, compare the page count read with the blocks on disk
462 */
463 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
464 ~(PAGE_CACHE_SIZE - 1);
465 if (total_compressed >= total_in) {
466 will_compress = 0;
467 } else {
468 disk_num_bytes = total_compressed;
469 num_bytes = total_in;
470 }
471 }
472 if (!will_compress && pages) {
473 /*
474 * the compression code ran but failed to make things smaller,
475 * free any pages it allocated and our page pointer array
476 */
477 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400478 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 page_cache_release(pages[i]);
480 }
481 kfree(pages);
482 pages = NULL;
483 total_compressed = 0;
484 nr_pages_ret = 0;
485
486 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500487 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
488 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500489 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500490 }
Chris Masonc8b97812008-10-29 14:49:59 -0400491 }
Chris Mason771ed682008-11-06 22:02:51 -0500492 if (will_compress) {
493 *num_added += 1;
494
495 /* the async work queues will take care of doing actual
496 * allocation on disk for these compressed pages,
497 * and will submit them to the elevator.
498 */
499 add_async_extent(async_cow, start, num_bytes,
500 total_compressed, pages, nr_pages_ret);
501
Chris Mason42dc7ba2008-12-15 11:44:56 -0500502 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500503 start += num_bytes;
504 pages = NULL;
505 cond_resched();
506 goto again;
507 }
508 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500509cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500510 /*
511 * No compression, but we still need to write the pages in
512 * the file we've been given so far. redirty the locked
513 * page if it corresponds to our extent and set things up
514 * for the async work queue to run cow_file_range to do
515 * the normal delalloc dance
516 */
517 if (page_offset(locked_page) >= start &&
518 page_offset(locked_page) <= end) {
519 __set_page_dirty_nobuffers(locked_page);
520 /* unlocked later on in the async handlers */
521 }
522 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
523 *num_added += 1;
524 }
525
526out:
527 return 0;
528
529free_pages_out:
530 for (i = 0; i < nr_pages_ret; i++) {
531 WARN_ON(pages[i]->mapping);
532 page_cache_release(pages[i]);
533 }
Chris Masond3977122009-01-05 21:25:51 -0500534 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500535
536 goto out;
537}
538
539/*
540 * phase two of compressed writeback. This is the ordered portion
541 * of the code, which only gets called in the order the work was
542 * queued. We walk all the async extents created by compress_file_range
543 * and send them down to the disk.
544 */
545static noinline int submit_compressed_extents(struct inode *inode,
546 struct async_cow *async_cow)
547{
548 struct async_extent *async_extent;
549 u64 alloc_hint = 0;
550 struct btrfs_trans_handle *trans;
551 struct btrfs_key ins;
552 struct extent_map *em;
553 struct btrfs_root *root = BTRFS_I(inode)->root;
554 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
555 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee2009-11-10 21:23:48 -0500556 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500557
558 if (list_empty(&async_cow->extents))
559 return 0;
560
Chris Mason771ed682008-11-06 22:02:51 -0500561
Chris Masond3977122009-01-05 21:25:51 -0500562 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500563 async_extent = list_entry(async_cow->extents.next,
564 struct async_extent, list);
565 list_del(&async_extent->list);
566
567 io_tree = &BTRFS_I(inode)->io_tree;
568
Josef Bacikf5a84ee2009-11-10 21:23:48 -0500569retry:
Chris Mason771ed682008-11-06 22:02:51 -0500570 /* did the compression code fall back to uncompressed IO? */
571 if (!async_extent->pages) {
572 int page_started = 0;
573 unsigned long nr_written = 0;
574
575 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000576 async_extent->start +
577 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500578
579 /* allocate blocks */
Josef Bacikf5a84ee2009-11-10 21:23:48 -0500580 ret = cow_file_range(inode, async_cow->locked_page,
581 async_extent->start,
582 async_extent->start +
583 async_extent->ram_size - 1,
584 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500585
586 /*
587 * if page_started, cow_file_range inserted an
588 * inline extent and took care of all the unlocking
589 * and IO for us. Otherwise, we need to submit
590 * all those pages down to the drive.
591 */
Josef Bacikf5a84ee2009-11-10 21:23:48 -0500592 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500593 extent_write_locked_range(io_tree,
594 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500595 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500596 async_extent->ram_size - 1,
597 btrfs_get_extent,
598 WB_SYNC_ALL);
599 kfree(async_extent);
600 cond_resched();
601 continue;
602 }
603
604 lock_extent(io_tree, async_extent->start,
605 async_extent->start + async_extent->ram_size - 1,
606 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500607
Yan, Zhengc2167752009-11-12 09:34:21 +0000608 trans = btrfs_join_transaction(root, 1);
Chris Mason771ed682008-11-06 22:02:51 -0500609 ret = btrfs_reserve_extent(trans, root,
610 async_extent->compressed_size,
611 async_extent->compressed_size,
612 0, alloc_hint,
613 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000614 btrfs_end_transaction(trans, root);
615
Josef Bacikf5a84ee2009-11-10 21:23:48 -0500616 if (ret) {
617 int i;
618 for (i = 0; i < async_extent->nr_pages; i++) {
619 WARN_ON(async_extent->pages[i]->mapping);
620 page_cache_release(async_extent->pages[i]);
621 }
622 kfree(async_extent->pages);
623 async_extent->nr_pages = 0;
624 async_extent->pages = NULL;
625 unlock_extent(io_tree, async_extent->start,
626 async_extent->start +
627 async_extent->ram_size - 1, GFP_NOFS);
628 goto retry;
629 }
630
Yan, Zhengc2167752009-11-12 09:34:21 +0000631 /*
632 * here we're doing allocation and writeback of the
633 * compressed pages
634 */
635 btrfs_drop_extent_cache(inode, async_extent->start,
636 async_extent->start +
637 async_extent->ram_size - 1, 0);
638
Chris Mason771ed682008-11-06 22:02:51 -0500639 em = alloc_extent_map(GFP_NOFS);
640 em->start = async_extent->start;
641 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500642 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500643
644 em->block_start = ins.objectid;
645 em->block_len = ins.offset;
646 em->bdev = root->fs_info->fs_devices->latest_bdev;
647 set_bit(EXTENT_FLAG_PINNED, &em->flags);
648 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
649
Chris Masond3977122009-01-05 21:25:51 -0500650 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400651 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500652 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400653 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500654 if (ret != -EEXIST) {
655 free_extent_map(em);
656 break;
657 }
658 btrfs_drop_extent_cache(inode, async_extent->start,
659 async_extent->start +
660 async_extent->ram_size - 1, 0);
661 }
662
663 ret = btrfs_add_ordered_extent(inode, async_extent->start,
664 ins.objectid,
665 async_extent->ram_size,
666 ins.offset,
667 BTRFS_ORDERED_COMPRESSED);
668 BUG_ON(ret);
669
Chris Mason771ed682008-11-06 22:02:51 -0500670 /*
671 * clear dirty, set writeback and unlock the pages.
672 */
673 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400674 &BTRFS_I(inode)->io_tree,
675 async_extent->start,
676 async_extent->start +
677 async_extent->ram_size - 1,
678 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
679 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400680 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400681 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500682
683 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500684 async_extent->start,
685 async_extent->ram_size,
686 ins.objectid,
687 ins.offset, async_extent->pages,
688 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500689
690 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500691 alloc_hint = ins.objectid + ins.offset;
692 kfree(async_extent);
693 cond_resched();
694 }
695
Chris Mason771ed682008-11-06 22:02:51 -0500696 return 0;
697}
698
699/*
700 * when extent_io.c finds a delayed allocation range in the file,
701 * the call backs end up in this code. The basic idea is to
702 * allocate extents on disk for the range, and create ordered data structs
703 * in ram to track those extents.
704 *
705 * locked_page is the page that writepage had locked already. We use
706 * it to make sure we don't do extra locks or unlocks.
707 *
708 * *page_started is set to one if we unlock locked_page and do everything
709 * required to start IO on it. It may be clean and already done with
710 * IO when we return.
711 */
712static noinline int cow_file_range(struct inode *inode,
713 struct page *locked_page,
714 u64 start, u64 end, int *page_started,
715 unsigned long *nr_written,
716 int unlock)
717{
718 struct btrfs_root *root = BTRFS_I(inode)->root;
719 struct btrfs_trans_handle *trans;
720 u64 alloc_hint = 0;
721 u64 num_bytes;
722 unsigned long ram_size;
723 u64 disk_num_bytes;
724 u64 cur_alloc_size;
725 u64 blocksize = root->sectorsize;
726 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500727 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500728 struct btrfs_key ins;
729 struct extent_map *em;
730 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
731 int ret = 0;
732
733 trans = btrfs_join_transaction(root, 1);
734 BUG_ON(!trans);
735 btrfs_set_trans_block_group(trans, inode);
736
Chris Mason42dc7ba2008-12-15 11:44:56 -0500737 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500738
739 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
740 num_bytes = max(blocksize, num_bytes);
741 disk_num_bytes = num_bytes;
742 ret = 0;
743
744 if (start == 0) {
745 /* lets try to make an inline extent */
746 ret = cow_file_range_inline(trans, root, inode,
747 start, end, 0, NULL);
748 if (ret == 0) {
749 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400750 &BTRFS_I(inode)->io_tree,
751 start, end, NULL,
752 EXTENT_CLEAR_UNLOCK_PAGE |
753 EXTENT_CLEAR_UNLOCK |
754 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400755 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400756 EXTENT_CLEAR_DIRTY |
757 EXTENT_SET_WRITEBACK |
758 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000759
Chris Mason771ed682008-11-06 22:02:51 -0500760 *nr_written = *nr_written +
761 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
762 *page_started = 1;
763 ret = 0;
764 goto out;
765 }
766 }
Chris Masonc8b97812008-10-29 14:49:59 -0400767
768 BUG_ON(disk_num_bytes >
769 btrfs_super_total_bytes(&root->fs_info->super_copy));
770
Chris Masonb917b7c2009-09-18 16:07:03 -0400771
772 read_lock(&BTRFS_I(inode)->extent_tree.lock);
773 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
774 start, num_bytes);
775 if (em) {
Josef Bacik6346c932009-11-10 21:23:47 -0500776 /*
777 * if block start isn't an actual block number then find the
778 * first block in this inode and use that as a hint. If that
779 * block is also bogus then just don't worry about it.
780 */
781 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
782 free_extent_map(em);
783 em = search_extent_mapping(em_tree, 0, 0);
784 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
785 alloc_hint = em->block_start;
786 if (em)
787 free_extent_map(em);
788 } else {
789 alloc_hint = em->block_start;
790 free_extent_map(em);
791 }
Chris Masonb917b7c2009-09-18 16:07:03 -0400792 }
793 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400794 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400795
Chris Masond3977122009-01-05 21:25:51 -0500796 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400797 unsigned long op;
798
Josef Bacik287a0ab2010-03-19 18:07:23 +0000799 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400800 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500801 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400802 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500803 BUG_ON(ret);
804
Chris Masone6dcd2d2008-07-17 12:53:50 -0400805 em = alloc_extent_map(GFP_NOFS);
806 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500807 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500808 ram_size = ins.offset;
809 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400810
Chris Masone6dcd2d2008-07-17 12:53:50 -0400811 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400812 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400813 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400814 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400815
Chris Masond3977122009-01-05 21:25:51 -0500816 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400817 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400818 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400819 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400820 if (ret != -EEXIST) {
821 free_extent_map(em);
822 break;
823 }
824 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400825 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400826 }
827
Chris Mason98d20f62008-04-14 09:46:10 -0400828 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400829 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500830 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400831 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400832
Yan Zheng17d217f2008-12-12 10:03:38 -0500833 if (root->root_key.objectid ==
834 BTRFS_DATA_RELOC_TREE_OBJECTID) {
835 ret = btrfs_reloc_clone_csums(inode, start,
836 cur_alloc_size);
837 BUG_ON(ret);
838 }
839
Chris Masond3977122009-01-05 21:25:51 -0500840 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400841 break;
Chris Masond3977122009-01-05 21:25:51 -0500842
Chris Masonc8b97812008-10-29 14:49:59 -0400843 /* we're not doing compressed IO, don't unlock the first
844 * page (which the caller expects to stay locked), don't
845 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400846 *
847 * Do set the Private2 bit so we know this page was properly
848 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400849 */
Chris Masona791e352009-10-08 11:27:10 -0400850 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
851 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
852 EXTENT_SET_PRIVATE2;
853
Chris Masonc8b97812008-10-29 14:49:59 -0400854 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
855 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400856 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400857 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500858 num_bytes -= cur_alloc_size;
859 alloc_hint = ins.objectid + ins.offset;
860 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400861 }
Chris Masonb888db22007-08-27 16:49:44 -0400862out:
Chris Mason771ed682008-11-06 22:02:51 -0500863 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400864 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400865
Chris Masonbe20aa92007-12-17 20:14:01 -0500866 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500867}
Chris Masonc8b97812008-10-29 14:49:59 -0400868
Chris Mason771ed682008-11-06 22:02:51 -0500869/*
870 * work queue call back to started compression on a file and pages
871 */
872static noinline void async_cow_start(struct btrfs_work *work)
873{
874 struct async_cow *async_cow;
875 int num_added = 0;
876 async_cow = container_of(work, struct async_cow, work);
877
878 compress_file_range(async_cow->inode, async_cow->locked_page,
879 async_cow->start, async_cow->end, async_cow,
880 &num_added);
881 if (num_added == 0)
882 async_cow->inode = NULL;
883}
884
885/*
886 * work queue call back to submit previously compressed pages
887 */
888static noinline void async_cow_submit(struct btrfs_work *work)
889{
890 struct async_cow *async_cow;
891 struct btrfs_root *root;
892 unsigned long nr_pages;
893
894 async_cow = container_of(work, struct async_cow, work);
895
896 root = async_cow->root;
897 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
898 PAGE_CACHE_SHIFT;
899
900 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
901
902 if (atomic_read(&root->fs_info->async_delalloc_pages) <
903 5 * 1042 * 1024 &&
904 waitqueue_active(&root->fs_info->async_submit_wait))
905 wake_up(&root->fs_info->async_submit_wait);
906
Chris Masond3977122009-01-05 21:25:51 -0500907 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500908 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500909}
Chris Masonc8b97812008-10-29 14:49:59 -0400910
Chris Mason771ed682008-11-06 22:02:51 -0500911static noinline void async_cow_free(struct btrfs_work *work)
912{
913 struct async_cow *async_cow;
914 async_cow = container_of(work, struct async_cow, work);
915 kfree(async_cow);
916}
917
918static int cow_file_range_async(struct inode *inode, struct page *locked_page,
919 u64 start, u64 end, int *page_started,
920 unsigned long *nr_written)
921{
922 struct async_cow *async_cow;
923 struct btrfs_root *root = BTRFS_I(inode)->root;
924 unsigned long nr_pages;
925 u64 cur_end;
926 int limit = 10 * 1024 * 1042;
927
Chris Masona3429ab2009-10-08 12:30:20 -0400928 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
929 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500930 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500931 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
932 async_cow->inode = inode;
933 async_cow->root = root;
934 async_cow->locked_page = locked_page;
935 async_cow->start = start;
936
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200937 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500938 cur_end = end;
939 else
940 cur_end = min(end, start + 512 * 1024 - 1);
941
942 async_cow->end = cur_end;
943 INIT_LIST_HEAD(&async_cow->extents);
944
945 async_cow->work.func = async_cow_start;
946 async_cow->work.ordered_func = async_cow_submit;
947 async_cow->work.ordered_free = async_cow_free;
948 async_cow->work.flags = 0;
949
Chris Mason771ed682008-11-06 22:02:51 -0500950 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
951 PAGE_CACHE_SHIFT;
952 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
953
954 btrfs_queue_worker(&root->fs_info->delalloc_workers,
955 &async_cow->work);
956
957 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
958 wait_event(root->fs_info->async_submit_wait,
959 (atomic_read(&root->fs_info->async_delalloc_pages) <
960 limit));
961 }
962
Chris Masond3977122009-01-05 21:25:51 -0500963 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500964 atomic_read(&root->fs_info->async_delalloc_pages)) {
965 wait_event(root->fs_info->async_submit_wait,
966 (atomic_read(&root->fs_info->async_delalloc_pages) ==
967 0));
968 }
969
970 *nr_written += nr_pages;
971 start = cur_end + 1;
972 }
973 *page_started = 1;
974 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500975}
976
Chris Masond3977122009-01-05 21:25:51 -0500977static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500978 u64 bytenr, u64 num_bytes)
979{
980 int ret;
981 struct btrfs_ordered_sum *sums;
982 LIST_HEAD(list);
983
Yan Zheng07d400a2009-01-06 11:42:00 -0500984 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
985 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500986 if (ret == 0 && list_empty(&list))
987 return 0;
988
989 while (!list_empty(&list)) {
990 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
991 list_del(&sums->list);
992 kfree(sums);
993 }
994 return 1;
995}
996
Chris Masond352ac62008-09-29 15:18:18 -0400997/*
998 * when nowcow writeback call back. This checks for snapshots or COW copies
999 * of the extents that exist in the file, and COWs the file as required.
1000 *
1001 * If no cow copies or snapshots exist, we write directly to the existing
1002 * blocks on disk
1003 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001004static noinline int run_delalloc_nocow(struct inode *inode,
1005 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001006 u64 start, u64 end, int *page_started, int force,
1007 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001008{
Chris Masonbe20aa92007-12-17 20:14:01 -05001009 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001010 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001011 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001012 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001013 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001014 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001015 u64 cow_start;
1016 u64 cur_offset;
1017 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001018 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001019 u64 disk_bytenr;
1020 u64 num_bytes;
1021 int extent_type;
1022 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001023 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001024 int nocow;
1025 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -05001026
1027 path = btrfs_alloc_path();
1028 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001029 trans = btrfs_join_transaction(root, 1);
1030 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -05001031
Yan Zheng80ff3852008-10-30 14:20:02 -04001032 cow_start = (u64)-1;
1033 cur_offset = start;
1034 while (1) {
1035 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1036 cur_offset, 0);
1037 BUG_ON(ret < 0);
1038 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1039 leaf = path->nodes[0];
1040 btrfs_item_key_to_cpu(leaf, &found_key,
1041 path->slots[0] - 1);
1042 if (found_key.objectid == inode->i_ino &&
1043 found_key.type == BTRFS_EXTENT_DATA_KEY)
1044 path->slots[0]--;
1045 }
1046 check_prev = 0;
1047next_slot:
1048 leaf = path->nodes[0];
1049 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1050 ret = btrfs_next_leaf(root, path);
1051 if (ret < 0)
1052 BUG_ON(1);
1053 if (ret > 0)
1054 break;
1055 leaf = path->nodes[0];
1056 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001057
Yan Zheng80ff3852008-10-30 14:20:02 -04001058 nocow = 0;
1059 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001060 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001061 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001062
Yan Zheng80ff3852008-10-30 14:20:02 -04001063 if (found_key.objectid > inode->i_ino ||
1064 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1065 found_key.offset > end)
1066 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001067
Yan Zheng80ff3852008-10-30 14:20:02 -04001068 if (found_key.offset > cur_offset) {
1069 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001070 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001071 goto out_check;
1072 }
Chris Masonc31f8832008-01-08 15:46:31 -05001073
Yan Zheng80ff3852008-10-30 14:20:02 -04001074 fi = btrfs_item_ptr(leaf, path->slots[0],
1075 struct btrfs_file_extent_item);
1076 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001077
Yan Zhengd899e052008-10-30 14:25:28 -04001078 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1079 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001080 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001081 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001082 extent_end = found_key.offset +
1083 btrfs_file_extent_num_bytes(leaf, fi);
1084 if (extent_end <= start) {
1085 path->slots[0]++;
1086 goto next_slot;
1087 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001088 if (disk_bytenr == 0)
1089 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001090 if (btrfs_file_extent_compression(leaf, fi) ||
1091 btrfs_file_extent_encryption(leaf, fi) ||
1092 btrfs_file_extent_other_encoding(leaf, fi))
1093 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001094 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1095 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001096 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001097 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001098 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001099 found_key.offset -
1100 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001101 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001102 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001103 disk_bytenr += cur_offset - found_key.offset;
1104 num_bytes = min(end + 1, extent_end) - cur_offset;
1105 /*
1106 * force cow if csum exists in the range.
1107 * this ensure that csum for a given extent are
1108 * either valid or do not exist.
1109 */
1110 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1111 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001112 nocow = 1;
1113 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1114 extent_end = found_key.offset +
1115 btrfs_file_extent_inline_len(leaf, fi);
1116 extent_end = ALIGN(extent_end, root->sectorsize);
1117 } else {
1118 BUG_ON(1);
1119 }
1120out_check:
1121 if (extent_end <= start) {
1122 path->slots[0]++;
1123 goto next_slot;
1124 }
1125 if (!nocow) {
1126 if (cow_start == (u64)-1)
1127 cow_start = cur_offset;
1128 cur_offset = extent_end;
1129 if (cur_offset > end)
1130 break;
1131 path->slots[0]++;
1132 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001133 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001134
Yan Zheng7ea394f2008-08-05 13:05:02 -04001135 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001136 if (cow_start != (u64)-1) {
1137 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001138 found_key.offset - 1, page_started,
1139 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001140 BUG_ON(ret);
1141 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001142 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001143
Yan Zhengd899e052008-10-30 14:25:28 -04001144 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1145 struct extent_map *em;
1146 struct extent_map_tree *em_tree;
1147 em_tree = &BTRFS_I(inode)->extent_tree;
1148 em = alloc_extent_map(GFP_NOFS);
1149 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001150 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001151 em->len = num_bytes;
1152 em->block_len = num_bytes;
1153 em->block_start = disk_bytenr;
1154 em->bdev = root->fs_info->fs_devices->latest_bdev;
1155 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1156 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001157 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001158 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001159 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001160 if (ret != -EEXIST) {
1161 free_extent_map(em);
1162 break;
1163 }
1164 btrfs_drop_extent_cache(inode, em->start,
1165 em->start + em->len - 1, 0);
1166 }
1167 type = BTRFS_ORDERED_PREALLOC;
1168 } else {
1169 type = BTRFS_ORDERED_NOCOW;
1170 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001171
1172 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001173 num_bytes, num_bytes, type);
1174 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001175
Yan Zhengd899e052008-10-30 14:25:28 -04001176 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001177 cur_offset, cur_offset + num_bytes - 1,
1178 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1179 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1180 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001181 cur_offset = extent_end;
1182 if (cur_offset > end)
1183 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001184 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001185 btrfs_release_path(root, path);
1186
1187 if (cur_offset <= end && cow_start == (u64)-1)
1188 cow_start = cur_offset;
1189 if (cow_start != (u64)-1) {
1190 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001191 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001192 BUG_ON(ret);
1193 }
1194
1195 ret = btrfs_end_transaction(trans, root);
1196 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001197 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001198 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001199}
1200
Chris Masond352ac62008-09-29 15:18:18 -04001201/*
1202 * extent_io.c call back to do delayed allocation processing
1203 */
Chris Masonc8b97812008-10-29 14:49:59 -04001204static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001205 u64 start, u64 end, int *page_started,
1206 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001207{
Chris Masonbe20aa92007-12-17 20:14:01 -05001208 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001209 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001210
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001211 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001212 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001213 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001214 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001215 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001216 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001217 else if (!btrfs_test_opt(root, COMPRESS) &&
1218 !(BTRFS_I(inode)->force_compress))
Chris Mason7f366cf2009-03-12 20:12:45 -04001219 ret = cow_file_range(inode, locked_page, start, end,
1220 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001221 else
Chris Mason771ed682008-11-06 22:02:51 -05001222 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001223 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001224 return ret;
1225}
1226
Josef Bacik9ed74f22009-09-11 16:12:44 -04001227static int btrfs_split_extent_hook(struct inode *inode,
1228 struct extent_state *orig, u64 split)
1229{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001230 if (!(orig->state & EXTENT_DELALLOC))
1231 return 0;
1232
Josef Bacik32c00af2009-10-08 13:34:05 -04001233 spin_lock(&BTRFS_I(inode)->accounting_lock);
1234 BTRFS_I(inode)->outstanding_extents++;
1235 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001236
1237 return 0;
1238}
1239
1240/*
1241 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1242 * extents so we can keep track of new extents that are just merged onto old
1243 * extents, such as when we are doing sequential writes, so we can properly
1244 * account for the metadata space we'll need.
1245 */
1246static int btrfs_merge_extent_hook(struct inode *inode,
1247 struct extent_state *new,
1248 struct extent_state *other)
1249{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001250 /* not delalloc, ignore it */
1251 if (!(other->state & EXTENT_DELALLOC))
1252 return 0;
1253
Josef Bacik32c00af2009-10-08 13:34:05 -04001254 spin_lock(&BTRFS_I(inode)->accounting_lock);
1255 BTRFS_I(inode)->outstanding_extents--;
1256 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001257
1258 return 0;
1259}
1260
Chris Masond352ac62008-09-29 15:18:18 -04001261/*
1262 * extent_io.c set_bit_hook, used to track delayed allocation
1263 * bytes in this file, and to maintain the list of inodes that
1264 * have pending delalloc work to be done.
1265 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001266static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001267 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001268{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001269
Chris Mason75eff682008-12-15 15:54:40 -05001270 /*
1271 * set_bit and clear bit hooks normally require _irqsave/restore
1272 * but in this case, we are only testeing for the DELALLOC
1273 * bit, which is only set or cleared with irqs on
1274 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001275 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001276 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001277
Josef Bacik32c00af2009-10-08 13:34:05 -04001278 spin_lock(&BTRFS_I(inode)->accounting_lock);
1279 BTRFS_I(inode)->outstanding_extents++;
1280 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik6a632092009-02-20 11:00:09 -05001281 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001282
Chris Mason75eff682008-12-15 15:54:40 -05001283 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001284 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001285 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001286 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1287 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1288 &root->fs_info->delalloc_inodes);
1289 }
Chris Mason75eff682008-12-15 15:54:40 -05001290 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001291 }
1292 return 0;
1293}
1294
Chris Masond352ac62008-09-29 15:18:18 -04001295/*
1296 * extent_io.c clear_bit_hook, see set_bit_hook for why
1297 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001298static int btrfs_clear_bit_hook(struct inode *inode,
1299 struct extent_state *state, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001300{
Chris Mason75eff682008-12-15 15:54:40 -05001301 /*
1302 * set_bit and clear bit hooks normally require _irqsave/restore
1303 * but in this case, we are only testeing for the DELALLOC
1304 * bit, which is only set or cleared with irqs on
1305 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001306 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001307 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001308
Josef Bacik32c00af2009-10-08 13:34:05 -04001309 if (bits & EXTENT_DO_ACCOUNTING) {
1310 spin_lock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001311 WARN_ON(!BTRFS_I(inode)->outstanding_extents);
Josef Bacik32c00af2009-10-08 13:34:05 -04001312 BTRFS_I(inode)->outstanding_extents--;
1313 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1314 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1315 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04001316
Chris Mason75eff682008-12-15 15:54:40 -05001317 spin_lock(&root->fs_info->delalloc_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001318 if (state->end - state->start + 1 >
1319 root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001320 printk(KERN_INFO "btrfs warning: delalloc account "
1321 "%llu %llu\n",
Josef Bacik9ed74f22009-09-11 16:12:44 -04001322 (unsigned long long)
1323 state->end - state->start + 1,
Chris Masond3977122009-01-05 21:25:51 -05001324 (unsigned long long)
1325 root->fs_info->delalloc_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001326 btrfs_delalloc_free_space(root, inode, (u64)-1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001327 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001328 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001329 } else {
Josef Bacik6a632092009-02-20 11:00:09 -05001330 btrfs_delalloc_free_space(root, inode,
Josef Bacik9ed74f22009-09-11 16:12:44 -04001331 state->end -
1332 state->start + 1);
1333 root->fs_info->delalloc_bytes -= state->end -
1334 state->start + 1;
1335 BTRFS_I(inode)->delalloc_bytes -= state->end -
1336 state->start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001337 }
Chris Masonea8c2812008-08-04 23:17:27 -04001338 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1339 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1340 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1341 }
Chris Mason75eff682008-12-15 15:54:40 -05001342 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001343 }
1344 return 0;
1345}
1346
Chris Masond352ac62008-09-29 15:18:18 -04001347/*
1348 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1349 * we don't create bios that span stripes or chunks
1350 */
Chris Mason239b14b2008-03-24 15:02:07 -04001351int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001352 size_t size, struct bio *bio,
1353 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001354{
1355 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1356 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001357 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001358 u64 length = 0;
1359 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001360 int ret;
1361
Chris Mason771ed682008-11-06 22:02:51 -05001362 if (bio_flags & EXTENT_BIO_COMPRESSED)
1363 return 0;
1364
Chris Masonf2d8d742008-04-21 10:03:05 -04001365 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001366 map_tree = &root->fs_info->mapping_tree;
1367 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001368 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001369 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001370
Chris Masond3977122009-01-05 21:25:51 -05001371 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001372 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001373 return 0;
1374}
1375
Chris Masond352ac62008-09-29 15:18:18 -04001376/*
1377 * in order to insert checksums into the metadata in large chunks,
1378 * we wait until bio submission time. All the pages in the bio are
1379 * checksummed and sums are attached onto the ordered extent record.
1380 *
1381 * At IO completion time the cums attached on the ordered extent record
1382 * are inserted into the btree
1383 */
Chris Masond3977122009-01-05 21:25:51 -05001384static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1385 struct bio *bio, int mirror_num,
1386 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001387{
Chris Mason065631f2008-02-20 12:07:25 -05001388 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001389 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001390
Chris Masond20f7042008-12-08 16:58:54 -05001391 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001392 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001393 return 0;
1394}
Chris Masone0156402008-04-16 11:15:20 -04001395
Chris Mason4a69a412008-11-06 22:03:00 -05001396/*
1397 * in order to insert checksums into the metadata in large chunks,
1398 * we wait until bio submission time. All the pages in the bio are
1399 * checksummed and sums are attached onto the ordered extent record.
1400 *
1401 * At IO completion time the cums attached on the ordered extent record
1402 * are inserted into the btree
1403 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001404static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001405 int mirror_num, unsigned long bio_flags)
1406{
1407 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001408 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001409}
1410
Chris Masond352ac62008-09-29 15:18:18 -04001411/*
Chris Masoncad321a2008-12-17 14:51:42 -05001412 * extent_io.c submission hook. This does the right thing for csum calculation
1413 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001414 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001415static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001416 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001417{
1418 struct btrfs_root *root = BTRFS_I(inode)->root;
1419 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001420 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001421
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001422 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001423
Chris Masone6dcd2d2008-07-17 12:53:50 -04001424 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1425 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001426
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001427 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001428 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001429 return btrfs_submit_compressed_read(inode, bio,
1430 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001431 } else if (!skip_sum)
1432 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001433 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001434 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001435 /* csum items have already been cloned */
1436 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1437 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001438 /* we're doing a write, do the async checksumming */
1439 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001440 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001441 bio_flags, __btrfs_submit_bio_start,
1442 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001443 }
1444
Chris Mason0b86a832008-03-24 15:01:56 -04001445mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001446 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001447}
Chris Mason6885f302008-02-20 16:11:05 -05001448
Chris Masond352ac62008-09-29 15:18:18 -04001449/*
1450 * given a list of ordered sums record them in the inode. This happens
1451 * at IO completion time based on sums calculated at bio submission time.
1452 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001453static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001454 struct inode *inode, u64 file_offset,
1455 struct list_head *list)
1456{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001457 struct btrfs_ordered_sum *sum;
1458
1459 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001460
1461 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001462 btrfs_csum_file_blocks(trans,
1463 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001464 }
1465 return 0;
1466}
1467
Josef Bacik2ac55d42010-02-03 19:33:23 +00001468int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1469 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001470{
Chris Masond3977122009-01-05 21:25:51 -05001471 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001472 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001473 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001474 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001475}
1476
Chris Masond352ac62008-09-29 15:18:18 -04001477/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001478struct btrfs_writepage_fixup {
1479 struct page *page;
1480 struct btrfs_work work;
1481};
1482
Christoph Hellwigb2950862008-12-02 09:54:17 -05001483static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001484{
1485 struct btrfs_writepage_fixup *fixup;
1486 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001487 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001488 struct page *page;
1489 struct inode *inode;
1490 u64 page_start;
1491 u64 page_end;
1492
1493 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1494 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001495again:
Chris Mason247e7432008-07-17 12:53:51 -04001496 lock_page(page);
1497 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1498 ClearPageChecked(page);
1499 goto out_page;
1500 }
1501
1502 inode = page->mapping->host;
1503 page_start = page_offset(page);
1504 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1505
Josef Bacik2ac55d42010-02-03 19:33:23 +00001506 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1507 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001508
1509 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001510 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001511 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001512
1513 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1514 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001515 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1516 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001517 unlock_page(page);
1518 btrfs_start_ordered_extent(inode, ordered, 1);
1519 goto again;
1520 }
Chris Mason247e7432008-07-17 12:53:51 -04001521
Josef Bacik2ac55d42010-02-03 19:33:23 +00001522 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001523 ClearPageChecked(page);
1524out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001525 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1526 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001527out_page:
1528 unlock_page(page);
1529 page_cache_release(page);
1530}
1531
1532/*
1533 * There are a few paths in the higher layers of the kernel that directly
1534 * set the page dirty bit without asking the filesystem if it is a
1535 * good idea. This causes problems because we want to make sure COW
1536 * properly happens and the data=ordered rules are followed.
1537 *
Chris Masonc8b97812008-10-29 14:49:59 -04001538 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001539 * hasn't been properly setup for IO. We kick off an async process
1540 * to fix it up. The async helper will wait for ordered extents, set
1541 * the delalloc bit and make it safe to write the page.
1542 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001543static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001544{
1545 struct inode *inode = page->mapping->host;
1546 struct btrfs_writepage_fixup *fixup;
1547 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001548
Chris Mason8b62b722009-09-02 16:53:46 -04001549 /* this page is properly in the ordered list */
1550 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001551 return 0;
1552
1553 if (PageChecked(page))
1554 return -EAGAIN;
1555
1556 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1557 if (!fixup)
1558 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001559
Chris Mason247e7432008-07-17 12:53:51 -04001560 SetPageChecked(page);
1561 page_cache_get(page);
1562 fixup->work.func = btrfs_writepage_fixup_worker;
1563 fixup->page = page;
1564 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1565 return -EAGAIN;
1566}
1567
Yan Zhengd899e052008-10-30 14:25:28 -04001568static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1569 struct inode *inode, u64 file_pos,
1570 u64 disk_bytenr, u64 disk_num_bytes,
1571 u64 num_bytes, u64 ram_bytes,
1572 u8 compression, u8 encryption,
1573 u16 other_encoding, int extent_type)
1574{
1575 struct btrfs_root *root = BTRFS_I(inode)->root;
1576 struct btrfs_file_extent_item *fi;
1577 struct btrfs_path *path;
1578 struct extent_buffer *leaf;
1579 struct btrfs_key ins;
1580 u64 hint;
1581 int ret;
1582
1583 path = btrfs_alloc_path();
1584 BUG_ON(!path);
1585
Chris Masonb9473432009-03-13 11:00:37 -04001586 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001587
1588 /*
1589 * we may be replacing one extent in the tree with another.
1590 * The new extent is pinned in the extent map, and we don't want
1591 * to drop it from the cache until it is completely in the btree.
1592 *
1593 * So, tell btrfs_drop_extents to leave this extent in the cache.
1594 * the caller is expected to unpin it and allow it to be merged
1595 * with the others.
1596 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001597 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1598 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001599 BUG_ON(ret);
1600
1601 ins.objectid = inode->i_ino;
1602 ins.offset = file_pos;
1603 ins.type = BTRFS_EXTENT_DATA_KEY;
1604 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1605 BUG_ON(ret);
1606 leaf = path->nodes[0];
1607 fi = btrfs_item_ptr(leaf, path->slots[0],
1608 struct btrfs_file_extent_item);
1609 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1610 btrfs_set_file_extent_type(leaf, fi, extent_type);
1611 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1612 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1613 btrfs_set_file_extent_offset(leaf, fi, 0);
1614 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1615 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1616 btrfs_set_file_extent_compression(leaf, fi, compression);
1617 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1618 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001619
1620 btrfs_unlock_up_safe(path, 1);
1621 btrfs_set_lock_blocking(leaf);
1622
Yan Zhengd899e052008-10-30 14:25:28 -04001623 btrfs_mark_buffer_dirty(leaf);
1624
1625 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001626
1627 ins.objectid = disk_bytenr;
1628 ins.offset = disk_num_bytes;
1629 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001630 ret = btrfs_alloc_reserved_file_extent(trans, root,
1631 root->root_key.objectid,
1632 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001633 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001634 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001635
Yan Zhengd899e052008-10-30 14:25:28 -04001636 return 0;
1637}
1638
Chris Mason5d13a982009-03-13 11:41:46 -04001639/*
1640 * helper function for btrfs_finish_ordered_io, this
1641 * just reads in some of the csum leaves to prime them into ram
1642 * before we start the transaction. It limits the amount of btree
1643 * reads required while inside the transaction.
1644 */
Chris Masond352ac62008-09-29 15:18:18 -04001645/* as ordered data IO finishes, this gets called so we can finish
1646 * an ordered extent if the range of bytes in the file it covers are
1647 * fully written.
1648 */
Chris Mason211f90e2008-07-18 11:56:15 -04001649static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001650{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001651 struct btrfs_root *root = BTRFS_I(inode)->root;
1652 struct btrfs_trans_handle *trans;
Chris Mason5d13a982009-03-13 11:41:46 -04001653 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001654 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001655 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04001656 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001657 int ret;
1658
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001659 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1660 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001661 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001662 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001663 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001664
Yan, Zhengc2167752009-11-12 09:34:21 +00001665 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1666 BUG_ON(!list_empty(&ordered_extent->list));
1667 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1668 if (!ret) {
1669 trans = btrfs_join_transaction(root, 1);
1670 ret = btrfs_update_inode(trans, root, inode);
1671 BUG_ON(ret);
1672 btrfs_end_transaction(trans, root);
1673 }
1674 goto out;
1675 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001676
Josef Bacik2ac55d42010-02-03 19:33:23 +00001677 lock_extent_bits(io_tree, ordered_extent->file_offset,
1678 ordered_extent->file_offset + ordered_extent->len - 1,
1679 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001680
Yan, Zhengc2167752009-11-12 09:34:21 +00001681 trans = btrfs_join_transaction(root, 1);
1682
Chris Masonc8b97812008-10-29 14:49:59 -04001683 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001684 compressed = 1;
1685 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1686 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001687 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001688 ordered_extent->file_offset,
1689 ordered_extent->file_offset +
1690 ordered_extent->len);
1691 BUG_ON(ret);
1692 } else {
1693 ret = insert_reserved_file_extent(trans, inode,
1694 ordered_extent->file_offset,
1695 ordered_extent->start,
1696 ordered_extent->disk_len,
1697 ordered_extent->len,
1698 ordered_extent->len,
1699 compressed, 0, 0,
1700 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001701 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1702 ordered_extent->file_offset,
1703 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001704 BUG_ON(ret);
1705 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001706 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1707 ordered_extent->file_offset +
1708 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1709
Chris Masone6dcd2d2008-07-17 12:53:50 -04001710 add_pending_csums(trans, inode, ordered_extent->file_offset,
1711 &ordered_extent->list);
1712
Yan, Zhengc2167752009-11-12 09:34:21 +00001713 /* this also removes the ordered extent from the tree */
1714 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1715 ret = btrfs_update_inode(trans, root, inode);
1716 BUG_ON(ret);
1717 btrfs_end_transaction(trans, root);
1718out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001719 /* once for us */
1720 btrfs_put_ordered_extent(ordered_extent);
1721 /* once for the tree */
1722 btrfs_put_ordered_extent(ordered_extent);
1723
Chris Masone6dcd2d2008-07-17 12:53:50 -04001724 return 0;
1725}
1726
Christoph Hellwigb2950862008-12-02 09:54:17 -05001727static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001728 struct extent_state *state, int uptodate)
1729{
Chris Mason8b62b722009-09-02 16:53:46 -04001730 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001731 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1732}
1733
Chris Masond352ac62008-09-29 15:18:18 -04001734/*
1735 * When IO fails, either with EIO or csum verification fails, we
1736 * try other mirrors that might have a good copy of the data. This
1737 * io_failure_record is used to record state as we go through all the
1738 * mirrors. If another mirror has good data, the page is set up to date
1739 * and things continue. If a good mirror can't be found, the original
1740 * bio end_io callback is called to indicate things have failed.
1741 */
Chris Mason7e383262008-04-09 16:28:12 -04001742struct io_failure_record {
1743 struct page *page;
1744 u64 start;
1745 u64 len;
1746 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001747 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001748 int last_mirror;
1749};
1750
Christoph Hellwigb2950862008-12-02 09:54:17 -05001751static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001752 struct page *page, u64 start, u64 end,
1753 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001754{
1755 struct io_failure_record *failrec = NULL;
1756 u64 private;
1757 struct extent_map *em;
1758 struct inode *inode = page->mapping->host;
1759 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001760 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001761 struct bio *bio;
1762 int num_copies;
1763 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001764 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001765 u64 logical;
1766
1767 ret = get_state_private(failure_tree, start, &private);
1768 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001769 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1770 if (!failrec)
1771 return -ENOMEM;
1772 failrec->start = start;
1773 failrec->len = end - start + 1;
1774 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001775 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001776
Chris Mason890871b2009-09-02 16:24:52 -04001777 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001778 em = lookup_extent_mapping(em_tree, start, failrec->len);
1779 if (em->start > start || em->start + em->len < start) {
1780 free_extent_map(em);
1781 em = NULL;
1782 }
Chris Mason890871b2009-09-02 16:24:52 -04001783 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001784
1785 if (!em || IS_ERR(em)) {
1786 kfree(failrec);
1787 return -EIO;
1788 }
1789 logical = start - em->start;
1790 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001791 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1792 logical = em->block_start;
1793 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1794 }
Chris Mason7e383262008-04-09 16:28:12 -04001795 failrec->logical = logical;
1796 free_extent_map(em);
1797 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1798 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001799 set_state_private(failure_tree, start,
1800 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001801 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001802 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001803 }
1804 num_copies = btrfs_num_copies(
1805 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1806 failrec->logical, failrec->len);
1807 failrec->last_mirror++;
1808 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001809 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001810 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1811 failrec->start,
1812 EXTENT_LOCKED);
1813 if (state && state->start != failrec->start)
1814 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001815 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001816 }
1817 if (!state || failrec->last_mirror > num_copies) {
1818 set_state_private(failure_tree, failrec->start, 0);
1819 clear_extent_bits(failure_tree, failrec->start,
1820 failrec->start + failrec->len - 1,
1821 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1822 kfree(failrec);
1823 return -EIO;
1824 }
1825 bio = bio_alloc(GFP_NOFS, 1);
1826 bio->bi_private = state;
1827 bio->bi_end_io = failed_bio->bi_end_io;
1828 bio->bi_sector = failrec->logical >> 9;
1829 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001830 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001831
Chris Mason7e383262008-04-09 16:28:12 -04001832 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001833 if (failed_bio->bi_rw & (1 << BIO_RW))
1834 rw = WRITE;
1835 else
1836 rw = READ;
1837
1838 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001839 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001840 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001841 return 0;
1842}
1843
Chris Masond352ac62008-09-29 15:18:18 -04001844/*
1845 * each time an IO finishes, we do a fast check in the IO failure tree
1846 * to see if we need to process or clean up an io_failure_record
1847 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001848static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001849{
1850 u64 private;
1851 u64 private_failure;
1852 struct io_failure_record *failure;
1853 int ret;
1854
1855 private = 0;
1856 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1857 (u64)-1, 1, EXTENT_DIRTY)) {
1858 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1859 start, &private_failure);
1860 if (ret == 0) {
1861 failure = (struct io_failure_record *)(unsigned long)
1862 private_failure;
1863 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1864 failure->start, 0);
1865 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1866 failure->start,
1867 failure->start + failure->len - 1,
1868 EXTENT_DIRTY | EXTENT_LOCKED,
1869 GFP_NOFS);
1870 kfree(failure);
1871 }
1872 }
Chris Mason7e383262008-04-09 16:28:12 -04001873 return 0;
1874}
1875
Chris Masond352ac62008-09-29 15:18:18 -04001876/*
1877 * when reads are done, we need to check csums to verify the data is correct
1878 * if there's a match, we allow the bio to finish. If not, we go through
1879 * the io_failure_record routines to find good copies
1880 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001881static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001882 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001883{
Chris Mason35ebb932007-10-30 16:56:53 -04001884 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001885 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001886 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001887 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001888 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001889 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001890 struct btrfs_root *root = BTRFS_I(inode)->root;
1891 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001892
Chris Masond20f7042008-12-08 16:58:54 -05001893 if (PageChecked(page)) {
1894 ClearPageChecked(page);
1895 goto good;
1896 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001897
1898 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001899 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001900
Yan Zheng17d217f2008-12-12 10:03:38 -05001901 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001902 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001903 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1904 GFP_NOFS);
1905 return 0;
1906 }
1907
Yanc2e639f2008-02-04 08:57:25 -05001908 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001909 private = state->private;
1910 ret = 0;
1911 } else {
1912 ret = get_state_private(io_tree, start, &private);
1913 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001914 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001915 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001916 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001917
Chris Masonff79f812007-10-15 16:22:25 -04001918 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1919 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001920 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001921 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001922
Chris Mason9ab86c82009-01-07 09:48:51 -05001923 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001924good:
Chris Mason7e383262008-04-09 16:28:12 -04001925 /* if the io failure tree for this inode is non-empty,
1926 * check to see if we've recovered from a failed IO
1927 */
Chris Mason1259ab72008-05-12 13:39:03 -04001928 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001929 return 0;
1930
1931zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001932 if (printk_ratelimit()) {
1933 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1934 "private %llu\n", page->mapping->host->i_ino,
1935 (unsigned long long)start, csum,
1936 (unsigned long long)private);
1937 }
Chris Masondb945352007-10-15 16:15:53 -04001938 memset(kaddr + offset, 1, end - start + 1);
1939 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001940 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001941 if (private == 0)
1942 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001943 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001944}
Chris Masonb888db22007-08-27 16:49:44 -04001945
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001946struct delayed_iput {
1947 struct list_head list;
1948 struct inode *inode;
1949};
1950
1951void btrfs_add_delayed_iput(struct inode *inode)
1952{
1953 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1954 struct delayed_iput *delayed;
1955
1956 if (atomic_add_unless(&inode->i_count, -1, 1))
1957 return;
1958
1959 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
1960 delayed->inode = inode;
1961
1962 spin_lock(&fs_info->delayed_iput_lock);
1963 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
1964 spin_unlock(&fs_info->delayed_iput_lock);
1965}
1966
1967void btrfs_run_delayed_iputs(struct btrfs_root *root)
1968{
1969 LIST_HEAD(list);
1970 struct btrfs_fs_info *fs_info = root->fs_info;
1971 struct delayed_iput *delayed;
1972 int empty;
1973
1974 spin_lock(&fs_info->delayed_iput_lock);
1975 empty = list_empty(&fs_info->delayed_iputs);
1976 spin_unlock(&fs_info->delayed_iput_lock);
1977 if (empty)
1978 return;
1979
1980 down_read(&root->fs_info->cleanup_work_sem);
1981 spin_lock(&fs_info->delayed_iput_lock);
1982 list_splice_init(&fs_info->delayed_iputs, &list);
1983 spin_unlock(&fs_info->delayed_iput_lock);
1984
1985 while (!list_empty(&list)) {
1986 delayed = list_entry(list.next, struct delayed_iput, list);
1987 list_del(&delayed->list);
1988 iput(delayed->inode);
1989 kfree(delayed);
1990 }
1991 up_read(&root->fs_info->cleanup_work_sem);
1992}
1993
Josef Bacik7b128762008-07-24 12:17:14 -04001994/*
1995 * This creates an orphan entry for the given inode in case something goes
1996 * wrong in the middle of an unlink/truncate.
1997 */
1998int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1999{
2000 struct btrfs_root *root = BTRFS_I(inode)->root;
2001 int ret = 0;
2002
Yanbcc63ab2008-07-30 16:29:20 -04002003 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002004
2005 /* already on the orphan list, we're good */
2006 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002007 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002008 return 0;
2009 }
2010
2011 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2012
Yanbcc63ab2008-07-30 16:29:20 -04002013 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002014
2015 /*
2016 * insert an orphan item to track this unlinked/truncated file
2017 */
2018 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2019
2020 return ret;
2021}
2022
2023/*
2024 * We have done the truncate/delete so we can go ahead and remove the orphan
2025 * item for this particular inode.
2026 */
2027int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2028{
2029 struct btrfs_root *root = BTRFS_I(inode)->root;
2030 int ret = 0;
2031
Yanbcc63ab2008-07-30 16:29:20 -04002032 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002033
2034 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002035 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002036 return 0;
2037 }
2038
2039 list_del_init(&BTRFS_I(inode)->i_orphan);
2040 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04002041 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002042 return 0;
2043 }
2044
Yanbcc63ab2008-07-30 16:29:20 -04002045 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002046
2047 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2048
2049 return ret;
2050}
2051
2052/*
2053 * this cleans up any orphans that may be left on the list from the last use
2054 * of this root.
2055 */
2056void btrfs_orphan_cleanup(struct btrfs_root *root)
2057{
2058 struct btrfs_path *path;
2059 struct extent_buffer *leaf;
2060 struct btrfs_item *item;
2061 struct btrfs_key key, found_key;
2062 struct btrfs_trans_handle *trans;
2063 struct inode *inode;
2064 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2065
Yan, Zhengc71bf092009-11-12 09:34:40 +00002066 if (!xchg(&root->clean_orphans, 0))
Josef Bacik7b128762008-07-24 12:17:14 -04002067 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002068
2069 path = btrfs_alloc_path();
2070 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002071 path->reada = -1;
2072
2073 key.objectid = BTRFS_ORPHAN_OBJECTID;
2074 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2075 key.offset = (u64)-1;
2076
Josef Bacik7b128762008-07-24 12:17:14 -04002077 while (1) {
2078 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2079 if (ret < 0) {
2080 printk(KERN_ERR "Error searching slot for orphan: %d"
2081 "\n", ret);
2082 break;
2083 }
2084
2085 /*
2086 * if ret == 0 means we found what we were searching for, which
2087 * is weird, but possible, so only screw with path if we didnt
2088 * find the key and see if we have stuff that matches
2089 */
2090 if (ret > 0) {
2091 if (path->slots[0] == 0)
2092 break;
2093 path->slots[0]--;
2094 }
2095
2096 /* pull out the item */
2097 leaf = path->nodes[0];
2098 item = btrfs_item_nr(leaf, path->slots[0]);
2099 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2100
2101 /* make sure the item matches what we want */
2102 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2103 break;
2104 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2105 break;
2106
2107 /* release the path since we're done with it */
2108 btrfs_release_path(root, path);
2109
2110 /*
2111 * this is where we are basically btrfs_lookup, without the
2112 * crossing root thing. we store the inode number in the
2113 * offset of the orphan item.
2114 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002115 found_key.objectid = found_key.offset;
2116 found_key.type = BTRFS_INODE_ITEM_KEY;
2117 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002118 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002119 if (IS_ERR(inode))
Josef Bacik7b128762008-07-24 12:17:14 -04002120 break;
2121
Josef Bacik7b128762008-07-24 12:17:14 -04002122 /*
2123 * add this inode to the orphan list so btrfs_orphan_del does
2124 * the proper thing when we hit it
2125 */
Yanbcc63ab2008-07-30 16:29:20 -04002126 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002127 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04002128 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002129
2130 /*
2131 * if this is a bad inode, means we actually succeeded in
2132 * removing the inode, but not the orphan record, which means
2133 * we need to manually delete the orphan since iput will just
2134 * do a destroy_inode
2135 */
2136 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002137 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04002138 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002139 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002140 iput(inode);
2141 continue;
2142 }
2143
2144 /* if we have links, this was a truncate, lets do that */
2145 if (inode->i_nlink) {
2146 nr_truncate++;
2147 btrfs_truncate(inode);
2148 } else {
2149 nr_unlink++;
2150 }
2151
2152 /* this will do delete_inode and everything for us */
2153 iput(inode);
2154 }
2155
2156 if (nr_unlink)
2157 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2158 if (nr_truncate)
2159 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2160
2161 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002162}
2163
Chris Masond352ac62008-09-29 15:18:18 -04002164/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002165 * very simple check to peek ahead in the leaf looking for xattrs. If we
2166 * don't find any xattrs, we know there can't be any acls.
2167 *
2168 * slot is the slot the inode is in, objectid is the objectid of the inode
2169 */
2170static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2171 int slot, u64 objectid)
2172{
2173 u32 nritems = btrfs_header_nritems(leaf);
2174 struct btrfs_key found_key;
2175 int scanned = 0;
2176
2177 slot++;
2178 while (slot < nritems) {
2179 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2180
2181 /* we found a different objectid, there must not be acls */
2182 if (found_key.objectid != objectid)
2183 return 0;
2184
2185 /* we found an xattr, assume we've got an acl */
2186 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2187 return 1;
2188
2189 /*
2190 * we found a key greater than an xattr key, there can't
2191 * be any acls later on
2192 */
2193 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2194 return 0;
2195
2196 slot++;
2197 scanned++;
2198
2199 /*
2200 * it goes inode, inode backrefs, xattrs, extents,
2201 * so if there are a ton of hard links to an inode there can
2202 * be a lot of backrefs. Don't waste time searching too hard,
2203 * this is just an optimization
2204 */
2205 if (scanned >= 8)
2206 break;
2207 }
2208 /* we hit the end of the leaf before we found an xattr or
2209 * something larger than an xattr. We have to assume the inode
2210 * has acls
2211 */
2212 return 1;
2213}
2214
2215/*
Chris Masond352ac62008-09-29 15:18:18 -04002216 * read an inode from the btree into the in-memory inode
2217 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002218static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002219{
2220 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002221 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002222 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002223 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002224 struct btrfs_root *root = BTRFS_I(inode)->root;
2225 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002226 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002227 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002228 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002229 int ret;
2230
2231 path = btrfs_alloc_path();
2232 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002233 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002234
Chris Mason39279cc2007-06-12 06:35:45 -04002235 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002236 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002237 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002238
Chris Mason5f39d392007-10-15 16:14:19 -04002239 leaf = path->nodes[0];
2240 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2241 struct btrfs_inode_item);
2242
2243 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2244 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2245 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2246 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002247 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002248
2249 tspec = btrfs_inode_atime(inode_item);
2250 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2251 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2252
2253 tspec = btrfs_inode_mtime(inode_item);
2254 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2255 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2256
2257 tspec = btrfs_inode_ctime(inode_item);
2258 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2259 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2260
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002261 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002262 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002263 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002264 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002265 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002266 rdev = btrfs_inode_rdev(leaf, inode_item);
2267
Josef Bacikaec74772008-07-24 12:12:38 -04002268 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002269 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002270
Chris Mason5f39d392007-10-15 16:14:19 -04002271 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002272
Chris Mason46a53cc2009-04-27 11:47:50 -04002273 /*
2274 * try to precache a NULL acl entry for files that don't have
2275 * any xattrs or acls
2276 */
2277 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002278 if (!maybe_acls)
2279 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002280
Yan Zhengd2fb3432008-12-11 16:30:39 -05002281 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2282 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002283 btrfs_free_path(path);
2284 inode_item = NULL;
2285
Chris Mason39279cc2007-06-12 06:35:45 -04002286 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002287 case S_IFREG:
2288 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002289 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002290 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002291 inode->i_fop = &btrfs_file_operations;
2292 inode->i_op = &btrfs_file_inode_operations;
2293 break;
2294 case S_IFDIR:
2295 inode->i_fop = &btrfs_dir_file_operations;
2296 if (root == root->fs_info->tree_root)
2297 inode->i_op = &btrfs_dir_ro_inode_operations;
2298 else
2299 inode->i_op = &btrfs_dir_inode_operations;
2300 break;
2301 case S_IFLNK:
2302 inode->i_op = &btrfs_symlink_inode_operations;
2303 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002304 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002305 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002306 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002307 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002308 init_special_inode(inode, inode->i_mode, rdev);
2309 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002310 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002311
2312 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002313 return;
2314
2315make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002316 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002317 make_bad_inode(inode);
2318}
2319
Chris Masond352ac62008-09-29 15:18:18 -04002320/*
2321 * given a leaf and an inode, copy the inode fields into the leaf
2322 */
Chris Masone02119d2008-09-05 16:13:11 -04002323static void fill_inode_item(struct btrfs_trans_handle *trans,
2324 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002325 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002326 struct inode *inode)
2327{
Chris Mason5f39d392007-10-15 16:14:19 -04002328 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2329 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002330 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002331 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2332 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2333
2334 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2335 inode->i_atime.tv_sec);
2336 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2337 inode->i_atime.tv_nsec);
2338
2339 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2340 inode->i_mtime.tv_sec);
2341 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2342 inode->i_mtime.tv_nsec);
2343
2344 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2345 inode->i_ctime.tv_sec);
2346 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2347 inode->i_ctime.tv_nsec);
2348
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002349 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002350 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002351 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002352 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002353 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002354 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002355 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002356}
2357
Chris Masond352ac62008-09-29 15:18:18 -04002358/*
2359 * copy everything in the in-memory inode into the btree.
2360 */
Chris Masond3977122009-01-05 21:25:51 -05002361noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2362 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002363{
2364 struct btrfs_inode_item *inode_item;
2365 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002366 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002367 int ret;
2368
2369 path = btrfs_alloc_path();
2370 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002371 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002372 ret = btrfs_lookup_inode(trans, root, path,
2373 &BTRFS_I(inode)->location, 1);
2374 if (ret) {
2375 if (ret > 0)
2376 ret = -ENOENT;
2377 goto failed;
2378 }
2379
Chris Masonb4ce94d2009-02-04 09:25:08 -05002380 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002381 leaf = path->nodes[0];
2382 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002383 struct btrfs_inode_item);
2384
Chris Masone02119d2008-09-05 16:13:11 -04002385 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002386 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002387 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002388 ret = 0;
2389failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002390 btrfs_free_path(path);
2391 return ret;
2392}
2393
2394
Chris Masond352ac62008-09-29 15:18:18 -04002395/*
2396 * unlink helper that gets used here in inode.c and in the tree logging
2397 * recovery code. It remove a link in a directory with a given name, and
2398 * also drops the back refs in the inode to the directory
2399 */
Chris Masone02119d2008-09-05 16:13:11 -04002400int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2401 struct btrfs_root *root,
2402 struct inode *dir, struct inode *inode,
2403 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002404{
2405 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002406 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002407 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002408 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002409 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002410 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002411
2412 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002413 if (!path) {
2414 ret = -ENOMEM;
2415 goto err;
2416 }
2417
Chris Masonb9473432009-03-13 11:00:37 -04002418 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002419 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2420 name, name_len, -1);
2421 if (IS_ERR(di)) {
2422 ret = PTR_ERR(di);
2423 goto err;
2424 }
2425 if (!di) {
2426 ret = -ENOENT;
2427 goto err;
2428 }
Chris Mason5f39d392007-10-15 16:14:19 -04002429 leaf = path->nodes[0];
2430 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002431 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002432 if (ret)
2433 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002434 btrfs_release_path(root, path);
2435
Josef Bacikaec74772008-07-24 12:12:38 -04002436 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002437 inode->i_ino,
2438 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002439 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002440 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002441 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002442 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002443 goto err;
2444 }
2445
Chris Mason39279cc2007-06-12 06:35:45 -04002446 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002447 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002448 if (IS_ERR(di)) {
2449 ret = PTR_ERR(di);
2450 goto err;
2451 }
2452 if (!di) {
2453 ret = -ENOENT;
2454 goto err;
2455 }
2456 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002457 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002458
Chris Masone02119d2008-09-05 16:13:11 -04002459 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2460 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002461 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002462
2463 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2464 dir, index);
2465 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002466err:
2467 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002468 if (ret)
2469 goto out;
2470
2471 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2472 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2473 btrfs_update_inode(trans, root, dir);
2474 btrfs_drop_nlink(inode);
2475 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002476out:
Chris Mason39279cc2007-06-12 06:35:45 -04002477 return ret;
2478}
2479
2480static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2481{
2482 struct btrfs_root *root;
2483 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002484 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002485 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002486 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002487
2488 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002489
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002490 /*
2491 * 5 items for unlink inode
2492 * 1 for orphan
2493 */
2494 ret = btrfs_reserve_metadata_space(root, 6);
2495 if (ret)
2496 return ret;
2497
Chris Mason39279cc2007-06-12 06:35:45 -04002498 trans = btrfs_start_transaction(root, 1);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002499 if (IS_ERR(trans)) {
2500 btrfs_unreserve_metadata_space(root, 6);
2501 return PTR_ERR(trans);
2502 }
Chris Mason5f39d392007-10-15 16:14:19 -04002503
Chris Mason39279cc2007-06-12 06:35:45 -04002504 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002505
2506 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2507
Chris Masone02119d2008-09-05 16:13:11 -04002508 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2509 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002510
2511 if (inode->i_nlink == 0)
2512 ret = btrfs_orphan_add(trans, inode);
2513
Chris Masond3c2fdc2007-09-17 10:58:06 -04002514 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002515
Chris Mason89ce8a62008-06-25 16:01:31 -04002516 btrfs_end_transaction_throttle(trans, root);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002517 btrfs_unreserve_metadata_space(root, 6);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002518 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002519 return ret;
2520}
2521
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002522int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2523 struct btrfs_root *root,
2524 struct inode *dir, u64 objectid,
2525 const char *name, int name_len)
2526{
2527 struct btrfs_path *path;
2528 struct extent_buffer *leaf;
2529 struct btrfs_dir_item *di;
2530 struct btrfs_key key;
2531 u64 index;
2532 int ret;
2533
2534 path = btrfs_alloc_path();
2535 if (!path)
2536 return -ENOMEM;
2537
2538 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2539 name, name_len, -1);
2540 BUG_ON(!di || IS_ERR(di));
2541
2542 leaf = path->nodes[0];
2543 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2544 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2545 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2546 BUG_ON(ret);
2547 btrfs_release_path(root, path);
2548
2549 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2550 objectid, root->root_key.objectid,
2551 dir->i_ino, &index, name, name_len);
2552 if (ret < 0) {
2553 BUG_ON(ret != -ENOENT);
2554 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2555 name, name_len);
2556 BUG_ON(!di || IS_ERR(di));
2557
2558 leaf = path->nodes[0];
2559 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2560 btrfs_release_path(root, path);
2561 index = key.offset;
2562 }
2563
2564 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2565 index, name, name_len, -1);
2566 BUG_ON(!di || IS_ERR(di));
2567
2568 leaf = path->nodes[0];
2569 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2570 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2571 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2572 BUG_ON(ret);
2573 btrfs_release_path(root, path);
2574
2575 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2576 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2577 ret = btrfs_update_inode(trans, root, dir);
2578 BUG_ON(ret);
2579 dir->i_sb->s_dirt = 1;
2580
2581 btrfs_free_path(path);
2582 return 0;
2583}
2584
Chris Mason39279cc2007-06-12 06:35:45 -04002585static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2586{
2587 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002588 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002589 int ret;
2590 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002591 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002592 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002593
Chris Mason3394e162008-11-17 20:42:26 -05002594 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002595 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002596 return -ENOTEMPTY;
2597
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002598 ret = btrfs_reserve_metadata_space(root, 5);
2599 if (ret)
2600 return ret;
2601
Chris Mason39279cc2007-06-12 06:35:45 -04002602 trans = btrfs_start_transaction(root, 1);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002603 if (IS_ERR(trans)) {
2604 btrfs_unreserve_metadata_space(root, 5);
2605 return PTR_ERR(trans);
2606 }
2607
Chris Mason39279cc2007-06-12 06:35:45 -04002608 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002609
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002610 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2611 err = btrfs_unlink_subvol(trans, root, dir,
2612 BTRFS_I(inode)->location.objectid,
2613 dentry->d_name.name,
2614 dentry->d_name.len);
2615 goto out;
2616 }
2617
Josef Bacik7b128762008-07-24 12:17:14 -04002618 err = btrfs_orphan_add(trans, inode);
2619 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002620 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002621
Chris Mason39279cc2007-06-12 06:35:45 -04002622 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002623 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2624 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002625 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002626 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002627out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002628 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002629 ret = btrfs_end_transaction_throttle(trans, root);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002630 btrfs_unreserve_metadata_space(root, 5);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002631 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002632
Chris Mason39279cc2007-06-12 06:35:45 -04002633 if (ret && !err)
2634 err = ret;
2635 return err;
2636}
2637
Chris Masond20f7042008-12-08 16:58:54 -05002638#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002639/*
Chris Mason323ac952008-10-01 19:05:46 -04002640 * when truncating bytes in a file, it is possible to avoid reading
2641 * the leaves that contain only checksum items. This can be the
2642 * majority of the IO required to delete a large file, but it must
2643 * be done carefully.
2644 *
2645 * The keys in the level just above the leaves are checked to make sure
2646 * the lowest key in a given leaf is a csum key, and starts at an offset
2647 * after the new size.
2648 *
2649 * Then the key for the next leaf is checked to make sure it also has
2650 * a checksum item for the same file. If it does, we know our target leaf
2651 * contains only checksum items, and it can be safely freed without reading
2652 * it.
2653 *
2654 * This is just an optimization targeted at large files. It may do
2655 * nothing. It will return 0 unless things went badly.
2656 */
2657static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2658 struct btrfs_root *root,
2659 struct btrfs_path *path,
2660 struct inode *inode, u64 new_size)
2661{
2662 struct btrfs_key key;
2663 int ret;
2664 int nritems;
2665 struct btrfs_key found_key;
2666 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002667 struct btrfs_leaf_ref *ref;
2668 u64 leaf_gen;
2669 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002670
2671 path->lowest_level = 1;
2672 key.objectid = inode->i_ino;
2673 key.type = BTRFS_CSUM_ITEM_KEY;
2674 key.offset = new_size;
2675again:
2676 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2677 if (ret < 0)
2678 goto out;
2679
2680 if (path->nodes[1] == NULL) {
2681 ret = 0;
2682 goto out;
2683 }
2684 ret = 0;
2685 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2686 nritems = btrfs_header_nritems(path->nodes[1]);
2687
2688 if (!nritems)
2689 goto out;
2690
2691 if (path->slots[1] >= nritems)
2692 goto next_node;
2693
2694 /* did we find a key greater than anything we want to delete? */
2695 if (found_key.objectid > inode->i_ino ||
2696 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2697 goto out;
2698
2699 /* we check the next key in the node to make sure the leave contains
2700 * only checksum items. This comparison doesn't work if our
2701 * leaf is the last one in the node
2702 */
2703 if (path->slots[1] + 1 >= nritems) {
2704next_node:
2705 /* search forward from the last key in the node, this
2706 * will bring us into the next node in the tree
2707 */
2708 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2709
2710 /* unlikely, but we inc below, so check to be safe */
2711 if (found_key.offset == (u64)-1)
2712 goto out;
2713
2714 /* search_forward needs a path with locks held, do the
2715 * search again for the original key. It is possible
2716 * this will race with a balance and return a path that
2717 * we could modify, but this drop is just an optimization
2718 * and is allowed to miss some leaves.
2719 */
2720 btrfs_release_path(root, path);
2721 found_key.offset++;
2722
2723 /* setup a max key for search_forward */
2724 other_key.offset = (u64)-1;
2725 other_key.type = key.type;
2726 other_key.objectid = key.objectid;
2727
2728 path->keep_locks = 1;
2729 ret = btrfs_search_forward(root, &found_key, &other_key,
2730 path, 0, 0);
2731 path->keep_locks = 0;
2732 if (ret || found_key.objectid != key.objectid ||
2733 found_key.type != key.type) {
2734 ret = 0;
2735 goto out;
2736 }
2737
2738 key.offset = found_key.offset;
2739 btrfs_release_path(root, path);
2740 cond_resched();
2741 goto again;
2742 }
2743
2744 /* we know there's one more slot after us in the tree,
2745 * read that key so we can verify it is also a checksum item
2746 */
2747 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2748
2749 if (found_key.objectid < inode->i_ino)
2750 goto next_key;
2751
2752 if (found_key.type != key.type || found_key.offset < new_size)
2753 goto next_key;
2754
2755 /*
2756 * if the key for the next leaf isn't a csum key from this objectid,
2757 * we can't be sure there aren't good items inside this leaf.
2758 * Bail out
2759 */
2760 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2761 goto out;
2762
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002763 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2764 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002765 /*
2766 * it is safe to delete this leaf, it contains only
2767 * csum items from this inode at an offset >= new_size
2768 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002769 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002770 BUG_ON(ret);
2771
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002772 if (root->ref_cows && leaf_gen < trans->transid) {
2773 ref = btrfs_alloc_leaf_ref(root, 0);
2774 if (ref) {
2775 ref->root_gen = root->root_key.offset;
2776 ref->bytenr = leaf_start;
2777 ref->owner = 0;
2778 ref->generation = leaf_gen;
2779 ref->nritems = 0;
2780
Chris Masonbd56b302009-02-04 09:27:02 -05002781 btrfs_sort_leaf_ref(ref);
2782
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002783 ret = btrfs_add_leaf_ref(root, ref, 0);
2784 WARN_ON(ret);
2785 btrfs_free_leaf_ref(root, ref);
2786 } else {
2787 WARN_ON(1);
2788 }
2789 }
Chris Mason323ac952008-10-01 19:05:46 -04002790next_key:
2791 btrfs_release_path(root, path);
2792
2793 if (other_key.objectid == inode->i_ino &&
2794 other_key.type == key.type && other_key.offset > key.offset) {
2795 key.offset = other_key.offset;
2796 cond_resched();
2797 goto again;
2798 }
2799 ret = 0;
2800out:
2801 /* fixup any changes we've made to the path */
2802 path->lowest_level = 0;
2803 path->keep_locks = 0;
2804 btrfs_release_path(root, path);
2805 return ret;
2806}
2807
Chris Masond20f7042008-12-08 16:58:54 -05002808#endif
2809
Chris Mason323ac952008-10-01 19:05:46 -04002810/*
Chris Mason39279cc2007-06-12 06:35:45 -04002811 * this can truncate away extent items, csum items and directory items.
2812 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002813 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002814 *
2815 * csum items that cross the new i_size are truncated to the new size
2816 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002817 *
2818 * min_type is the minimum key type to truncate down to. If set to 0, this
2819 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002820 */
Yan, Zheng80825102009-11-12 09:35:36 +00002821int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2822 struct btrfs_root *root,
2823 struct inode *inode,
2824 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002825{
Chris Mason39279cc2007-06-12 06:35:45 -04002826 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002827 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002828 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00002829 struct btrfs_key key;
2830 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002831 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002832 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002833 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002834 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00002835 u64 mask = root->sectorsize - 1;
2836 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04002837 int found_extent;
2838 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002839 int pending_del_nr = 0;
2840 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002841 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002842 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00002843 int ret;
2844 int err = 0;
2845
2846 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002847
Chris Masone02119d2008-09-05 16:13:11 -04002848 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002849 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00002850
Chris Mason39279cc2007-06-12 06:35:45 -04002851 path = btrfs_alloc_path();
2852 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04002853 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04002854
Chris Mason39279cc2007-06-12 06:35:45 -04002855 key.objectid = inode->i_ino;
2856 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002857 key.type = (u8)-1;
2858
Chris Mason85e21ba2008-01-29 15:11:36 -05002859search_again:
Chris Masonb9473432009-03-13 11:00:37 -04002860 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05002861 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00002862 if (ret < 0) {
2863 err = ret;
2864 goto out;
2865 }
Chris Masond3977122009-01-05 21:25:51 -05002866
Chris Mason85e21ba2008-01-29 15:11:36 -05002867 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002868 /* there are no items in the tree for us to truncate, we're
2869 * done
2870 */
Yan, Zheng80825102009-11-12 09:35:36 +00002871 if (path->slots[0] == 0)
2872 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05002873 path->slots[0]--;
2874 }
2875
Chris Masond3977122009-01-05 21:25:51 -05002876 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002877 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002878 leaf = path->nodes[0];
2879 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2880 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002881 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002882
Chris Mason5f39d392007-10-15 16:14:19 -04002883 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002884 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002885
Chris Mason85e21ba2008-01-29 15:11:36 -05002886 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002887 break;
2888
Chris Mason5f39d392007-10-15 16:14:19 -04002889 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002890 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002891 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002892 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002893 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002894 encoding = btrfs_file_extent_compression(leaf, fi);
2895 encoding |= btrfs_file_extent_encryption(leaf, fi);
2896 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2897
Chris Mason179e29e2007-11-01 11:28:41 -04002898 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002899 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002900 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002901 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002902 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002903 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002904 }
Yan008630c2007-11-07 13:31:09 -05002905 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002906 }
Yan, Zheng80825102009-11-12 09:35:36 +00002907 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04002908 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00002909 } else {
2910 if (item_end < new_size)
2911 break;
2912 if (found_key.offset >= new_size)
2913 del_item = 1;
2914 else
2915 del_item = 0;
2916 }
Chris Mason39279cc2007-06-12 06:35:45 -04002917 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002918 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002919 if (found_type != BTRFS_EXTENT_DATA_KEY)
2920 goto delete;
2921
2922 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002923 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002924 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002925 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002926 u64 orig_num_bytes =
2927 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002928 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002929 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002930 extent_num_bytes = extent_num_bytes &
2931 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002932 btrfs_set_file_extent_num_bytes(leaf, fi,
2933 extent_num_bytes);
2934 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002935 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002936 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002937 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002938 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002939 } else {
Chris Masondb945352007-10-15 16:15:53 -04002940 extent_num_bytes =
2941 btrfs_file_extent_disk_num_bytes(leaf,
2942 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002943 extent_offset = found_key.offset -
2944 btrfs_file_extent_offset(leaf, fi);
2945
Chris Mason39279cc2007-06-12 06:35:45 -04002946 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002947 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002948 if (extent_start != 0) {
2949 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002950 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002951 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002952 }
2953 }
Chris Mason90692182008-02-08 13:49:28 -05002954 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002955 /*
2956 * we can't truncate inline items that have had
2957 * special encodings
2958 */
2959 if (!del_item &&
2960 btrfs_file_extent_compression(leaf, fi) == 0 &&
2961 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2962 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002963 u32 size = new_size - found_key.offset;
2964
2965 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002966 inode_sub_bytes(inode, item_end + 1 -
2967 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002968 }
2969 size =
2970 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002971 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002972 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002973 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002974 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002975 inode_sub_bytes(inode, item_end + 1 -
2976 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002977 }
Chris Mason39279cc2007-06-12 06:35:45 -04002978 }
Chris Mason179e29e2007-11-01 11:28:41 -04002979delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002980 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002981 if (!pending_del_nr) {
2982 /* no pending yet, add ourselves */
2983 pending_del_slot = path->slots[0];
2984 pending_del_nr = 1;
2985 } else if (pending_del_nr &&
2986 path->slots[0] + 1 == pending_del_slot) {
2987 /* hop on the pending chunk */
2988 pending_del_nr++;
2989 pending_del_slot = path->slots[0];
2990 } else {
Chris Masond3977122009-01-05 21:25:51 -05002991 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05002992 }
Chris Mason39279cc2007-06-12 06:35:45 -04002993 } else {
2994 break;
2995 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002996 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04002997 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002998 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002999 extent_num_bytes, 0,
3000 btrfs_header_owner(leaf),
3001 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003002 BUG_ON(ret);
3003 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003004
Yan, Zheng80825102009-11-12 09:35:36 +00003005 if (found_type == BTRFS_INODE_ITEM_KEY)
3006 break;
3007
3008 if (path->slots[0] == 0 ||
3009 path->slots[0] != pending_del_slot) {
3010 if (root->ref_cows) {
3011 err = -EAGAIN;
3012 goto out;
3013 }
3014 if (pending_del_nr) {
3015 ret = btrfs_del_items(trans, root, path,
3016 pending_del_slot,
3017 pending_del_nr);
3018 BUG_ON(ret);
3019 pending_del_nr = 0;
3020 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003021 btrfs_release_path(root, path);
3022 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003023 } else {
3024 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003025 }
Chris Mason39279cc2007-06-12 06:35:45 -04003026 }
Yan, Zheng80825102009-11-12 09:35:36 +00003027out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003028 if (pending_del_nr) {
3029 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3030 pending_del_nr);
3031 }
Chris Mason39279cc2007-06-12 06:35:45 -04003032 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003033 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003034}
3035
Chris Masona52d9a82007-08-27 16:49:44 -04003036/*
3037 * taken from block_truncate_page, but does cow as it zeros out
3038 * any bytes left in the last page in the file.
3039 */
3040static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3041{
3042 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003043 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003044 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3045 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003046 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003047 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003048 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003049 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3050 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3051 struct page *page;
3052 int ret = 0;
3053 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003054 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003055
3056 if ((offset & (blocksize - 1)) == 0)
3057 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003058 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
3059 if (ret)
3060 goto out;
3061
3062 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
3063 if (ret)
3064 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003065
3066 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003067again:
Chris Masona52d9a82007-08-27 16:49:44 -04003068 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003069 if (!page) {
3070 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3071 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04003072 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003073 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003074
3075 page_start = page_offset(page);
3076 page_end = page_start + PAGE_CACHE_SIZE - 1;
3077
Chris Masona52d9a82007-08-27 16:49:44 -04003078 if (!PageUptodate(page)) {
3079 ret = btrfs_readpage(NULL, page);
3080 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003081 if (page->mapping != mapping) {
3082 unlock_page(page);
3083 page_cache_release(page);
3084 goto again;
3085 }
Chris Masona52d9a82007-08-27 16:49:44 -04003086 if (!PageUptodate(page)) {
3087 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003088 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003089 }
3090 }
Chris Mason211c17f2008-05-15 09:13:45 -04003091 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003092
Josef Bacik2ac55d42010-02-03 19:33:23 +00003093 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3094 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003095 set_page_extent_mapped(page);
3096
3097 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3098 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003099 unlock_extent_cached(io_tree, page_start, page_end,
3100 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003101 unlock_page(page);
3102 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003103 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003104 btrfs_put_ordered_extent(ordered);
3105 goto again;
3106 }
3107
Josef Bacik2ac55d42010-02-03 19:33:23 +00003108 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003109 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003110 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003111
Josef Bacik2ac55d42010-02-03 19:33:23 +00003112 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3113 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003114 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003115 unlock_extent_cached(io_tree, page_start, page_end,
3116 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003117 goto out_unlock;
3118 }
3119
Chris Masone6dcd2d2008-07-17 12:53:50 -04003120 ret = 0;
3121 if (offset != PAGE_CACHE_SIZE) {
3122 kaddr = kmap(page);
3123 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3124 flush_dcache_page(page);
3125 kunmap(page);
3126 }
Chris Mason247e7432008-07-17 12:53:51 -04003127 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003128 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003129 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3130 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003131
Chris Mason89642222008-07-24 09:41:53 -04003132out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003133 if (ret)
3134 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3135 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003136 unlock_page(page);
3137 page_cache_release(page);
3138out:
3139 return ret;
3140}
3141
Yan Zheng9036c102008-10-30 14:19:41 -04003142int btrfs_cont_expand(struct inode *inode, loff_t size)
3143{
3144 struct btrfs_trans_handle *trans;
3145 struct btrfs_root *root = BTRFS_I(inode)->root;
3146 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3147 struct extent_map *em;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003148 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003149 u64 mask = root->sectorsize - 1;
3150 u64 hole_start = (inode->i_size + mask) & ~mask;
3151 u64 block_end = (size + mask) & ~mask;
3152 u64 last_byte;
3153 u64 cur_offset;
3154 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003155 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003156
3157 if (size <= hole_start)
3158 return 0;
3159
Yan Zheng9036c102008-10-30 14:19:41 -04003160 while (1) {
3161 struct btrfs_ordered_extent *ordered;
3162 btrfs_wait_ordered_range(inode, hole_start,
3163 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003164 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3165 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003166 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3167 if (!ordered)
3168 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003169 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3170 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003171 btrfs_put_ordered_extent(ordered);
3172 }
3173
Yan Zheng9036c102008-10-30 14:19:41 -04003174 cur_offset = hole_start;
3175 while (1) {
3176 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3177 block_end - cur_offset, 0);
3178 BUG_ON(IS_ERR(em) || !em);
3179 last_byte = min(extent_map_end(em), block_end);
3180 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003181 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003182 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003183 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003184
3185 err = btrfs_reserve_metadata_space(root, 2);
Chris Mason771ed682008-11-06 22:02:51 -05003186 if (err)
3187 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003188
Yan, Zheng80825102009-11-12 09:35:36 +00003189 trans = btrfs_start_transaction(root, 1);
3190 btrfs_set_trans_block_group(trans, inode);
3191
3192 err = btrfs_drop_extents(trans, inode, cur_offset,
3193 cur_offset + hole_size,
3194 &hint_byte, 1);
3195 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003196
Yan Zheng9036c102008-10-30 14:19:41 -04003197 err = btrfs_insert_file_extent(trans, root,
3198 inode->i_ino, cur_offset, 0,
3199 0, hole_size, 0, hole_size,
3200 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003201 BUG_ON(err);
3202
Yan Zheng9036c102008-10-30 14:19:41 -04003203 btrfs_drop_extent_cache(inode, hole_start,
3204 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003205
3206 btrfs_end_transaction(trans, root);
3207 btrfs_unreserve_metadata_space(root, 2);
Yan Zheng9036c102008-10-30 14:19:41 -04003208 }
3209 free_extent_map(em);
3210 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003211 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003212 break;
3213 }
3214
Josef Bacik2ac55d42010-02-03 19:33:23 +00003215 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3216 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003217 return err;
3218}
3219
Yan, Zheng80825102009-11-12 09:35:36 +00003220static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3221{
3222 struct btrfs_root *root = BTRFS_I(inode)->root;
3223 struct btrfs_trans_handle *trans;
3224 unsigned long nr;
3225 int ret;
3226
3227 if (attr->ia_size == inode->i_size)
3228 return 0;
3229
3230 if (attr->ia_size > inode->i_size) {
3231 unsigned long limit;
3232 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3233 if (attr->ia_size > inode->i_sb->s_maxbytes)
3234 return -EFBIG;
3235 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3236 send_sig(SIGXFSZ, current, 0);
3237 return -EFBIG;
3238 }
3239 }
3240
3241 ret = btrfs_reserve_metadata_space(root, 1);
3242 if (ret)
3243 return ret;
3244
3245 trans = btrfs_start_transaction(root, 1);
3246 btrfs_set_trans_block_group(trans, inode);
3247
3248 ret = btrfs_orphan_add(trans, inode);
3249 BUG_ON(ret);
3250
3251 nr = trans->blocks_used;
3252 btrfs_end_transaction(trans, root);
3253 btrfs_unreserve_metadata_space(root, 1);
3254 btrfs_btree_balance_dirty(root, nr);
3255
3256 if (attr->ia_size > inode->i_size) {
3257 ret = btrfs_cont_expand(inode, attr->ia_size);
3258 if (ret) {
3259 btrfs_truncate(inode);
3260 return ret;
3261 }
3262
3263 i_size_write(inode, attr->ia_size);
3264 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3265
3266 trans = btrfs_start_transaction(root, 1);
3267 btrfs_set_trans_block_group(trans, inode);
3268
3269 ret = btrfs_update_inode(trans, root, inode);
3270 BUG_ON(ret);
3271 if (inode->i_nlink > 0) {
3272 ret = btrfs_orphan_del(trans, inode);
3273 BUG_ON(ret);
3274 }
3275 nr = trans->blocks_used;
3276 btrfs_end_transaction(trans, root);
3277 btrfs_btree_balance_dirty(root, nr);
3278 return 0;
3279 }
3280
3281 /*
3282 * We're truncating a file that used to have good data down to
3283 * zero. Make sure it gets into the ordered flush list so that
3284 * any new writes get down to disk quickly.
3285 */
3286 if (attr->ia_size == 0)
3287 BTRFS_I(inode)->ordered_data_close = 1;
3288
3289 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3290 ret = vmtruncate(inode, attr->ia_size);
3291 BUG_ON(ret);
3292
3293 return 0;
3294}
3295
Chris Mason39279cc2007-06-12 06:35:45 -04003296static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3297{
3298 struct inode *inode = dentry->d_inode;
3299 int err;
3300
3301 err = inode_change_ok(inode, attr);
3302 if (err)
3303 return err;
3304
Chris Mason5a3f23d2009-03-31 13:27:11 -04003305 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003306 err = btrfs_setattr_size(inode, attr);
3307 if (err)
3308 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003309 }
Yan, Zheng80825102009-11-12 09:35:36 +00003310 attr->ia_valid &= ~ATTR_SIZE;
Yan Zheng9036c102008-10-30 14:19:41 -04003311
Yan, Zheng80825102009-11-12 09:35:36 +00003312 if (attr->ia_valid)
3313 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003314
3315 if (!err && ((attr->ia_valid & ATTR_MODE)))
3316 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003317 return err;
3318}
Chris Mason61295eb2008-01-14 16:24:38 -05003319
Chris Mason39279cc2007-06-12 06:35:45 -04003320void btrfs_delete_inode(struct inode *inode)
3321{
3322 struct btrfs_trans_handle *trans;
3323 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04003324 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003325 int ret;
3326
3327 truncate_inode_pages(&inode->i_data, 0);
3328 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003329 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003330 goto no_delete;
3331 }
Chris Mason4a096752008-07-21 10:29:44 -04003332 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003333
Yan, Zhengc71bf092009-11-12 09:34:40 +00003334 if (root->fs_info->log_root_recovering) {
3335 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3336 goto no_delete;
3337 }
3338
Yan, Zheng76dda932009-09-21 16:00:26 -04003339 if (inode->i_nlink > 0) {
3340 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3341 goto no_delete;
3342 }
3343
Chris Masondbe674a2008-07-17 12:54:05 -04003344 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003345
Yan, Zheng80825102009-11-12 09:35:36 +00003346 while (1) {
3347 trans = btrfs_start_transaction(root, 1);
3348 btrfs_set_trans_block_group(trans, inode);
3349 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3350
3351 if (ret != -EAGAIN)
3352 break;
3353
3354 nr = trans->blocks_used;
3355 btrfs_end_transaction(trans, root);
3356 trans = NULL;
3357 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003358 }
3359
Yan, Zheng80825102009-11-12 09:35:36 +00003360 if (ret == 0) {
3361 ret = btrfs_orphan_del(trans, inode);
3362 BUG_ON(ret);
3363 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003364
Chris Masond3c2fdc2007-09-17 10:58:06 -04003365 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003366 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003367 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003368no_delete:
3369 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003370 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003371}
3372
3373/*
3374 * this returns the key found in the dir entry in the location pointer.
3375 * If no dir entries were found, location->objectid is 0.
3376 */
3377static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3378 struct btrfs_key *location)
3379{
3380 const char *name = dentry->d_name.name;
3381 int namelen = dentry->d_name.len;
3382 struct btrfs_dir_item *di;
3383 struct btrfs_path *path;
3384 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003385 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003386
3387 path = btrfs_alloc_path();
3388 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003389
Chris Mason39279cc2007-06-12 06:35:45 -04003390 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3391 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003392 if (IS_ERR(di))
3393 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003394
3395 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003396 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003397
Chris Mason5f39d392007-10-15 16:14:19 -04003398 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003399out:
Chris Mason39279cc2007-06-12 06:35:45 -04003400 btrfs_free_path(path);
3401 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003402out_err:
3403 location->objectid = 0;
3404 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003405}
3406
3407/*
3408 * when we hit a tree root in a directory, the btrfs part of the inode
3409 * needs to be changed to reflect the root directory of the tree root. This
3410 * is kind of like crossing a mount point.
3411 */
3412static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003413 struct inode *dir,
3414 struct dentry *dentry,
3415 struct btrfs_key *location,
3416 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003417{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003418 struct btrfs_path *path;
3419 struct btrfs_root *new_root;
3420 struct btrfs_root_ref *ref;
3421 struct extent_buffer *leaf;
3422 int ret;
3423 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003424
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003425 path = btrfs_alloc_path();
3426 if (!path) {
3427 err = -ENOMEM;
3428 goto out;
3429 }
Chris Mason39279cc2007-06-12 06:35:45 -04003430
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003431 err = -ENOENT;
3432 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3433 BTRFS_I(dir)->root->root_key.objectid,
3434 location->objectid);
3435 if (ret) {
3436 if (ret < 0)
3437 err = ret;
3438 goto out;
3439 }
Chris Mason39279cc2007-06-12 06:35:45 -04003440
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003441 leaf = path->nodes[0];
3442 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3443 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3444 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3445 goto out;
3446
3447 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3448 (unsigned long)(ref + 1),
3449 dentry->d_name.len);
3450 if (ret)
3451 goto out;
3452
3453 btrfs_release_path(root->fs_info->tree_root, path);
3454
3455 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3456 if (IS_ERR(new_root)) {
3457 err = PTR_ERR(new_root);
3458 goto out;
3459 }
3460
3461 if (btrfs_root_refs(&new_root->root_item) == 0) {
3462 err = -ENOENT;
3463 goto out;
3464 }
3465
3466 *sub_root = new_root;
3467 location->objectid = btrfs_root_dirid(&new_root->root_item);
3468 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003469 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003470 err = 0;
3471out:
3472 btrfs_free_path(path);
3473 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003474}
3475
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003476static void inode_tree_add(struct inode *inode)
3477{
3478 struct btrfs_root *root = BTRFS_I(inode)->root;
3479 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003480 struct rb_node **p;
3481 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003482again:
3483 p = &root->inode_tree.rb_node;
3484 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003485
Yan, Zheng76dda932009-09-21 16:00:26 -04003486 if (hlist_unhashed(&inode->i_hash))
3487 return;
3488
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003489 spin_lock(&root->inode_lock);
3490 while (*p) {
3491 parent = *p;
3492 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3493
3494 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003495 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003496 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003497 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003498 else {
3499 WARN_ON(!(entry->vfs_inode.i_state &
3500 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003501 rb_erase(parent, &root->inode_tree);
3502 RB_CLEAR_NODE(parent);
3503 spin_unlock(&root->inode_lock);
3504 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003505 }
3506 }
3507 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3508 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3509 spin_unlock(&root->inode_lock);
3510}
3511
3512static void inode_tree_del(struct inode *inode)
3513{
3514 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003515 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003516
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003517 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003518 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003519 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003520 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003521 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003522 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003523 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003524
3525 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3526 synchronize_srcu(&root->fs_info->subvol_srcu);
3527 spin_lock(&root->inode_lock);
3528 empty = RB_EMPTY_ROOT(&root->inode_tree);
3529 spin_unlock(&root->inode_lock);
3530 if (empty)
3531 btrfs_add_dead_root(root);
3532 }
3533}
3534
3535int btrfs_invalidate_inodes(struct btrfs_root *root)
3536{
3537 struct rb_node *node;
3538 struct rb_node *prev;
3539 struct btrfs_inode *entry;
3540 struct inode *inode;
3541 u64 objectid = 0;
3542
3543 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3544
3545 spin_lock(&root->inode_lock);
3546again:
3547 node = root->inode_tree.rb_node;
3548 prev = NULL;
3549 while (node) {
3550 prev = node;
3551 entry = rb_entry(node, struct btrfs_inode, rb_node);
3552
3553 if (objectid < entry->vfs_inode.i_ino)
3554 node = node->rb_left;
3555 else if (objectid > entry->vfs_inode.i_ino)
3556 node = node->rb_right;
3557 else
3558 break;
3559 }
3560 if (!node) {
3561 while (prev) {
3562 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3563 if (objectid <= entry->vfs_inode.i_ino) {
3564 node = prev;
3565 break;
3566 }
3567 prev = rb_next(prev);
3568 }
3569 }
3570 while (node) {
3571 entry = rb_entry(node, struct btrfs_inode, rb_node);
3572 objectid = entry->vfs_inode.i_ino + 1;
3573 inode = igrab(&entry->vfs_inode);
3574 if (inode) {
3575 spin_unlock(&root->inode_lock);
3576 if (atomic_read(&inode->i_count) > 1)
3577 d_prune_aliases(inode);
3578 /*
3579 * btrfs_drop_inode will remove it from
3580 * the inode cache when its usage count
3581 * hits zero.
3582 */
3583 iput(inode);
3584 cond_resched();
3585 spin_lock(&root->inode_lock);
3586 goto again;
3587 }
3588
3589 if (cond_resched_lock(&root->inode_lock))
3590 goto again;
3591
3592 node = rb_next(node);
3593 }
3594 spin_unlock(&root->inode_lock);
3595 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003596}
3597
Chris Masone02119d2008-09-05 16:13:11 -04003598static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003599{
Chris Masone02119d2008-09-05 16:13:11 -04003600 struct btrfs_inode *bi = BTRFS_I(inode);
3601
Chris Masone02119d2008-09-05 16:13:11 -04003602 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05003603 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003604 bi->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04003605 bi->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003606 bi->logged_trans = 0;
3607 bi->delalloc_bytes = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003608 bi->reserved_bytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003609 bi->disk_i_size = 0;
3610 bi->flags = 0;
3611 bi->index_cnt = (u64)-1;
Chris Mason12fcfd22009-03-24 10:24:20 -04003612 bi->last_unlink_trans = 0;
Chris Mason27574952009-05-14 13:10:02 -04003613 bi->ordered_data_close = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05003614 bi->force_compress = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003615 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3616 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04003617 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003618 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3619 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003620 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04003621 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003622 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Chris Masonba1da2f2008-07-17 12:54:15 -04003623 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masone02119d2008-09-05 16:13:11 -04003624 mutex_init(&BTRFS_I(inode)->log_mutex);
3625}
3626
3627static int btrfs_init_locked_inode(struct inode *inode, void *p)
3628{
3629 struct btrfs_iget_args *args = p;
3630 inode->i_ino = args->ino;
3631 init_btrfs_i(inode);
3632 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003633 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003634 return 0;
3635}
3636
3637static int btrfs_find_actor(struct inode *inode, void *opaque)
3638{
3639 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003640 return args->ino == inode->i_ino &&
3641 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003642}
3643
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003644static struct inode *btrfs_iget_locked(struct super_block *s,
3645 u64 objectid,
3646 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003647{
3648 struct inode *inode;
3649 struct btrfs_iget_args args;
3650 args.ino = objectid;
3651 args.root = root;
3652
3653 inode = iget5_locked(s, objectid, btrfs_find_actor,
3654 btrfs_init_locked_inode,
3655 (void *)&args);
3656 return inode;
3657}
3658
Balaji Rao1a54ef82008-07-21 02:01:04 +05303659/* Get an inode object given its location and corresponding root.
3660 * Returns in *is_new if the inode was read from disk
3661 */
3662struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00003663 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303664{
3665 struct inode *inode;
3666
3667 inode = btrfs_iget_locked(s, location->objectid, root);
3668 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003669 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303670
3671 if (inode->i_state & I_NEW) {
3672 BTRFS_I(inode)->root = root;
3673 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3674 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003675
3676 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303677 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00003678 if (new)
3679 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05303680 }
3681
3682 return inode;
3683}
3684
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003685static struct inode *new_simple_dir(struct super_block *s,
3686 struct btrfs_key *key,
3687 struct btrfs_root *root)
3688{
3689 struct inode *inode = new_inode(s);
3690
3691 if (!inode)
3692 return ERR_PTR(-ENOMEM);
3693
3694 init_btrfs_i(inode);
3695
3696 BTRFS_I(inode)->root = root;
3697 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3698 BTRFS_I(inode)->dummy_inode = 1;
3699
3700 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3701 inode->i_op = &simple_dir_inode_operations;
3702 inode->i_fop = &simple_dir_operations;
3703 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3704 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3705
3706 return inode;
3707}
3708
Chris Mason3de45862008-11-17 21:02:50 -05003709struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003710{
Chris Masond3977122009-01-05 21:25:51 -05003711 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003712 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003713 struct btrfs_root *sub_root = root;
3714 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003715 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003716 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003717
Yan, Zheng76dda932009-09-21 16:00:26 -04003718 dentry->d_op = &btrfs_dentry_operations;
3719
Chris Mason39279cc2007-06-12 06:35:45 -04003720 if (dentry->d_name.len > BTRFS_NAME_LEN)
3721 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003722
Chris Mason39279cc2007-06-12 06:35:45 -04003723 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003724
Chris Mason39279cc2007-06-12 06:35:45 -04003725 if (ret < 0)
3726 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003727
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003728 if (location.objectid == 0)
3729 return NULL;
3730
3731 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00003732 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003733 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003734 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003735
3736 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3737
Yan, Zheng76dda932009-09-21 16:00:26 -04003738 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003739 ret = fixup_tree_root_location(root, dir, dentry,
3740 &location, &sub_root);
3741 if (ret < 0) {
3742 if (ret != -ENOENT)
3743 inode = ERR_PTR(ret);
3744 else
3745 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3746 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00003747 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003748 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003749 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3750
Yan, Zhengc71bf092009-11-12 09:34:40 +00003751 if (root != sub_root) {
3752 down_read(&root->fs_info->cleanup_work_sem);
3753 if (!(inode->i_sb->s_flags & MS_RDONLY))
3754 btrfs_orphan_cleanup(sub_root);
3755 up_read(&root->fs_info->cleanup_work_sem);
3756 }
3757
Chris Mason3de45862008-11-17 21:02:50 -05003758 return inode;
3759}
3760
Yan, Zheng76dda932009-09-21 16:00:26 -04003761static int btrfs_dentry_delete(struct dentry *dentry)
3762{
3763 struct btrfs_root *root;
3764
Yan, Zhengefefb142009-10-09 09:25:16 -04003765 if (!dentry->d_inode && !IS_ROOT(dentry))
3766 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04003767
Yan, Zhengefefb142009-10-09 09:25:16 -04003768 if (dentry->d_inode) {
3769 root = BTRFS_I(dentry->d_inode)->root;
3770 if (btrfs_root_refs(&root->root_item) == 0)
3771 return 1;
3772 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003773 return 0;
3774}
3775
Chris Mason3de45862008-11-17 21:02:50 -05003776static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3777 struct nameidata *nd)
3778{
3779 struct inode *inode;
3780
Chris Mason3de45862008-11-17 21:02:50 -05003781 inode = btrfs_lookup_dentry(dir, dentry);
3782 if (IS_ERR(inode))
3783 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003784
Chris Mason39279cc2007-06-12 06:35:45 -04003785 return d_splice_alias(inode, dentry);
3786}
3787
Chris Mason39279cc2007-06-12 06:35:45 -04003788static unsigned char btrfs_filetype_table[] = {
3789 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3790};
3791
David Woodhousecbdf5a22008-08-06 19:42:33 +01003792static int btrfs_real_readdir(struct file *filp, void *dirent,
3793 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003794{
Chris Mason6da6aba2007-12-18 16:15:09 -05003795 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003796 struct btrfs_root *root = BTRFS_I(inode)->root;
3797 struct btrfs_item *item;
3798 struct btrfs_dir_item *di;
3799 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003800 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003801 struct btrfs_path *path;
3802 int ret;
3803 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003804 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003805 int slot;
3806 int advance;
3807 unsigned char d_type;
3808 int over = 0;
3809 u32 di_cur;
3810 u32 di_total;
3811 u32 di_len;
3812 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003813 char tmp_name[32];
3814 char *name_ptr;
3815 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003816
3817 /* FIXME, use a real flag for deciding about the key type */
3818 if (root->fs_info->tree_root == root)
3819 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003820
Chris Mason39544012007-12-12 14:38:19 -05003821 /* special case for "." */
3822 if (filp->f_pos == 0) {
3823 over = filldir(dirent, ".", 1,
3824 1, inode->i_ino,
3825 DT_DIR);
3826 if (over)
3827 return 0;
3828 filp->f_pos = 1;
3829 }
Chris Mason39544012007-12-12 14:38:19 -05003830 /* special case for .., just use the back ref */
3831 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003832 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003833 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003834 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003835 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003836 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003837 filp->f_pos = 2;
3838 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003839 path = btrfs_alloc_path();
3840 path->reada = 2;
3841
Chris Mason39279cc2007-06-12 06:35:45 -04003842 btrfs_set_key_type(&key, key_type);
3843 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003844 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003845
Chris Mason39279cc2007-06-12 06:35:45 -04003846 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3847 if (ret < 0)
3848 goto err;
3849 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003850
3851 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003852 leaf = path->nodes[0];
3853 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003854 slot = path->slots[0];
3855 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003856 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003857 ret = btrfs_next_leaf(root, path);
3858 if (ret)
3859 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003860 leaf = path->nodes[0];
3861 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003862 slot = path->slots[0];
3863 } else {
3864 slot++;
3865 path->slots[0]++;
3866 }
3867 }
Chris Mason3de45862008-11-17 21:02:50 -05003868
Chris Mason39279cc2007-06-12 06:35:45 -04003869 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003870 item = btrfs_item_nr(leaf, slot);
3871 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3872
3873 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003874 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003875 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003876 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003877 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003878 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003879
3880 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003881
Chris Mason39279cc2007-06-12 06:35:45 -04003882 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3883 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003884 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003885
3886 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003887 struct btrfs_key location;
3888
3889 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003890 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003891 name_ptr = tmp_name;
3892 } else {
3893 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003894 if (!name_ptr) {
3895 ret = -ENOMEM;
3896 goto err;
3897 }
Chris Mason5f39d392007-10-15 16:14:19 -04003898 }
3899 read_extent_buffer(leaf, name_ptr,
3900 (unsigned long)(di + 1), name_len);
3901
3902 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3903 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003904
3905 /* is this a reference to our own snapshot? If so
3906 * skip it
3907 */
3908 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3909 location.objectid == root->root_key.objectid) {
3910 over = 0;
3911 goto skip;
3912 }
Chris Mason5f39d392007-10-15 16:14:19 -04003913 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003914 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003915 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003916
Chris Mason3de45862008-11-17 21:02:50 -05003917skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003918 if (name_ptr != tmp_name)
3919 kfree(name_ptr);
3920
Chris Mason39279cc2007-06-12 06:35:45 -04003921 if (over)
3922 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003923 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003924 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003925 di_cur += di_len;
3926 di = (struct btrfs_dir_item *)((char *)di + di_len);
3927 }
3928 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003929
3930 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003931 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00003932 /*
3933 * 32-bit glibc will use getdents64, but then strtol -
3934 * so the last number we can serve is this.
3935 */
3936 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05003937 else
3938 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003939nopos:
3940 ret = 0;
3941err:
Chris Mason39279cc2007-06-12 06:35:45 -04003942 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003943 return ret;
3944}
3945
3946int btrfs_write_inode(struct inode *inode, int wait)
3947{
3948 struct btrfs_root *root = BTRFS_I(inode)->root;
3949 struct btrfs_trans_handle *trans;
3950 int ret = 0;
3951
Yan Zhengc146afa2008-11-12 14:34:12 -05003952 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003953 return 0;
3954
Chris Mason39279cc2007-06-12 06:35:45 -04003955 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003956 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003957 btrfs_set_trans_block_group(trans, inode);
3958 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003959 }
3960 return ret;
3961}
3962
3963/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003964 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003965 * inode changes. But, it is most likely to find the inode in cache.
3966 * FIXME, needs more benchmarking...there are no reasons other than performance
3967 * to keep or drop this code.
3968 */
3969void btrfs_dirty_inode(struct inode *inode)
3970{
3971 struct btrfs_root *root = BTRFS_I(inode)->root;
3972 struct btrfs_trans_handle *trans;
3973
Chris Masonf9295742008-07-17 12:54:14 -04003974 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003975 btrfs_set_trans_block_group(trans, inode);
3976 btrfs_update_inode(trans, root, inode);
3977 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003978}
3979
Chris Masond352ac62008-09-29 15:18:18 -04003980/*
3981 * find the highest existing sequence number in a directory
3982 * and then set the in-memory index_cnt variable to reflect
3983 * free sequence numbers
3984 */
Josef Bacikaec74772008-07-24 12:12:38 -04003985static int btrfs_set_inode_index_count(struct inode *inode)
3986{
3987 struct btrfs_root *root = BTRFS_I(inode)->root;
3988 struct btrfs_key key, found_key;
3989 struct btrfs_path *path;
3990 struct extent_buffer *leaf;
3991 int ret;
3992
3993 key.objectid = inode->i_ino;
3994 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3995 key.offset = (u64)-1;
3996
3997 path = btrfs_alloc_path();
3998 if (!path)
3999 return -ENOMEM;
4000
4001 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4002 if (ret < 0)
4003 goto out;
4004 /* FIXME: we should be able to handle this */
4005 if (ret == 0)
4006 goto out;
4007 ret = 0;
4008
4009 /*
4010 * MAGIC NUMBER EXPLANATION:
4011 * since we search a directory based on f_pos we have to start at 2
4012 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4013 * else has to start at 2
4014 */
4015 if (path->slots[0] == 0) {
4016 BTRFS_I(inode)->index_cnt = 2;
4017 goto out;
4018 }
4019
4020 path->slots[0]--;
4021
4022 leaf = path->nodes[0];
4023 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4024
4025 if (found_key.objectid != inode->i_ino ||
4026 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4027 BTRFS_I(inode)->index_cnt = 2;
4028 goto out;
4029 }
4030
4031 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4032out:
4033 btrfs_free_path(path);
4034 return ret;
4035}
4036
Chris Masond352ac62008-09-29 15:18:18 -04004037/*
4038 * helper to find a free sequence number in a given directory. This current
4039 * code is very simple, later versions will do smarter things in the btree
4040 */
Chris Mason3de45862008-11-17 21:02:50 -05004041int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004042{
4043 int ret = 0;
4044
4045 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4046 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004047 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004048 return ret;
4049 }
4050
Chris Mason00e4e6b2008-08-05 11:18:09 -04004051 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004052 BTRFS_I(dir)->index_cnt++;
4053
4054 return ret;
4055}
4056
Chris Mason39279cc2007-06-12 06:35:45 -04004057static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4058 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004059 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004060 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004061 u64 ref_objectid, u64 objectid,
4062 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004063{
4064 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004065 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004066 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004067 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004068 struct btrfs_inode_ref *ref;
4069 struct btrfs_key key[2];
4070 u32 sizes[2];
4071 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004072 int ret;
4073 int owner;
4074
Chris Mason5f39d392007-10-15 16:14:19 -04004075 path = btrfs_alloc_path();
4076 BUG_ON(!path);
4077
Chris Mason39279cc2007-06-12 06:35:45 -04004078 inode = new_inode(root->fs_info->sb);
4079 if (!inode)
4080 return ERR_PTR(-ENOMEM);
4081
Josef Bacikaec74772008-07-24 12:12:38 -04004082 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004083 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004084 if (ret) {
4085 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004086 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004087 }
Josef Bacikaec74772008-07-24 12:12:38 -04004088 }
4089 /*
4090 * index_cnt is ignored for everything but a dir,
4091 * btrfs_get_inode_index_count has an explanation for the magic
4092 * number
4093 */
Chris Masone02119d2008-09-05 16:13:11 -04004094 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004095 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004096 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004097 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05004098 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004099
Chris Mason39279cc2007-06-12 06:35:45 -04004100 if (mode & S_IFDIR)
4101 owner = 0;
4102 else
4103 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004104 BTRFS_I(inode)->block_group =
4105 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004106
4107 key[0].objectid = objectid;
4108 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4109 key[0].offset = 0;
4110
4111 key[1].objectid = objectid;
4112 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4113 key[1].offset = ref_objectid;
4114
4115 sizes[0] = sizeof(struct btrfs_inode_item);
4116 sizes[1] = name_len + sizeof(*ref);
4117
Chris Masonb9473432009-03-13 11:00:37 -04004118 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004119 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4120 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004121 goto fail;
4122
Chris Mason79683f22008-11-19 22:00:53 -05004123 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05004124
Chris Mason42f15d72009-02-06 11:35:57 -05004125 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05004126 inode->i_gid = dir->i_gid;
4127 if (S_ISDIR(mode))
4128 mode |= S_ISGID;
4129 } else
4130 inode->i_gid = current_fsgid();
4131
Chris Mason39279cc2007-06-12 06:35:45 -04004132 inode->i_mode = mode;
4133 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004134 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004135 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004136 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4137 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004138 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004139
4140 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4141 struct btrfs_inode_ref);
4142 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004143 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004144 ptr = (unsigned long)(ref + 1);
4145 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4146
Chris Mason5f39d392007-10-15 16:14:19 -04004147 btrfs_mark_buffer_dirty(path->nodes[0]);
4148 btrfs_free_path(path);
4149
Chris Mason39279cc2007-06-12 06:35:45 -04004150 location = &BTRFS_I(inode)->location;
4151 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004152 location->offset = 0;
4153 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4154
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004155 btrfs_inherit_iflags(inode, dir);
4156
Chris Mason94272162009-07-02 12:26:06 -04004157 if ((mode & S_IFREG)) {
4158 if (btrfs_test_opt(root, NODATASUM))
4159 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4160 if (btrfs_test_opt(root, NODATACOW))
4161 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4162 }
4163
Chris Mason39279cc2007-06-12 06:35:45 -04004164 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004165 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004166 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004167fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004168 if (dir)
4169 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004170 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004171 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004172 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004173}
4174
4175static inline u8 btrfs_inode_type(struct inode *inode)
4176{
4177 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4178}
4179
Chris Masond352ac62008-09-29 15:18:18 -04004180/*
4181 * utility function to add 'inode' into 'parent_inode' with
4182 * a give name and a given sequence number.
4183 * if 'add_backref' is true, also insert a backref from the
4184 * inode to the parent directory.
4185 */
Chris Masone02119d2008-09-05 16:13:11 -04004186int btrfs_add_link(struct btrfs_trans_handle *trans,
4187 struct inode *parent_inode, struct inode *inode,
4188 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004189{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004190 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004191 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004192 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004193
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4195 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4196 } else {
4197 key.objectid = inode->i_ino;
4198 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4199 key.offset = 0;
4200 }
Chris Mason39279cc2007-06-12 06:35:45 -04004201
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004202 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4203 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4204 key.objectid, root->root_key.objectid,
4205 parent_inode->i_ino,
4206 index, name, name_len);
4207 } else if (add_backref) {
4208 ret = btrfs_insert_inode_ref(trans, root,
4209 name, name_len, inode->i_ino,
4210 parent_inode->i_ino, index);
4211 }
4212
Chris Mason39279cc2007-06-12 06:35:45 -04004213 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004214 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4215 parent_inode->i_ino, &key,
4216 btrfs_inode_type(inode), index);
4217 BUG_ON(ret);
4218
Chris Masondbe674a2008-07-17 12:54:05 -04004219 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004220 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004221 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004222 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004223 }
4224 return ret;
4225}
4226
4227static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004228 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004229 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004230{
Chris Masone02119d2008-09-05 16:13:11 -04004231 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4232 inode, dentry->d_name.name,
4233 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004234 if (!err) {
4235 d_instantiate(dentry, inode);
4236 return 0;
4237 }
4238 if (err > 0)
4239 err = -EEXIST;
4240 return err;
4241}
4242
Josef Bacik618e21d2007-07-11 10:18:17 -04004243static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4244 int mode, dev_t rdev)
4245{
4246 struct btrfs_trans_handle *trans;
4247 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004248 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004249 int err;
4250 int drop_inode = 0;
4251 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004252 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004253 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004254
4255 if (!new_valid_dev(rdev))
4256 return -EINVAL;
4257
Josef Bacik9ed74f22009-09-11 16:12:44 -04004258 /*
4259 * 2 for inode item and ref
4260 * 2 for dir items
4261 * 1 for xattr if selinux is on
4262 */
4263 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004264 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004265 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004266
Josef Bacik618e21d2007-07-11 10:18:17 -04004267 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004268 if (!trans)
4269 goto fail;
Josef Bacik618e21d2007-07-11 10:18:17 -04004270 btrfs_set_trans_block_group(trans, dir);
4271
4272 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4273 if (err) {
4274 err = -ENOSPC;
4275 goto out_unlock;
4276 }
4277
Josef Bacikaec74772008-07-24 12:12:38 -04004278 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004279 dentry->d_name.len,
4280 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004281 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004282 err = PTR_ERR(inode);
4283 if (IS_ERR(inode))
4284 goto out_unlock;
4285
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004286 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004287 if (err) {
4288 drop_inode = 1;
4289 goto out_unlock;
4290 }
4291
Josef Bacik618e21d2007-07-11 10:18:17 -04004292 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004293 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004294 if (err)
4295 drop_inode = 1;
4296 else {
4297 inode->i_op = &btrfs_special_inode_operations;
4298 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004299 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004300 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004301 btrfs_update_inode_block_group(trans, inode);
4302 btrfs_update_inode_block_group(trans, dir);
4303out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004304 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004305 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004306fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004307 btrfs_unreserve_metadata_space(root, 5);
Josef Bacik618e21d2007-07-11 10:18:17 -04004308 if (drop_inode) {
4309 inode_dec_link_count(inode);
4310 iput(inode);
4311 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004312 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004313 return err;
4314}
4315
Chris Mason39279cc2007-06-12 06:35:45 -04004316static int btrfs_create(struct inode *dir, struct dentry *dentry,
4317 int mode, struct nameidata *nd)
4318{
4319 struct btrfs_trans_handle *trans;
4320 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004321 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004322 int err;
4323 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05004324 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004325 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004326 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004327
Josef Bacik9ed74f22009-09-11 16:12:44 -04004328 /*
4329 * 2 for inode item and ref
4330 * 2 for dir items
4331 * 1 for xattr if selinux is on
4332 */
4333 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004334 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004335 return err;
4336
Chris Mason39279cc2007-06-12 06:35:45 -04004337 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004338 if (!trans)
4339 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04004340 btrfs_set_trans_block_group(trans, dir);
4341
4342 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4343 if (err) {
4344 err = -ENOSPC;
4345 goto out_unlock;
4346 }
4347
Josef Bacikaec74772008-07-24 12:12:38 -04004348 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004349 dentry->d_name.len,
4350 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004351 objectid, BTRFS_I(dir)->block_group, mode,
4352 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004353 err = PTR_ERR(inode);
4354 if (IS_ERR(inode))
4355 goto out_unlock;
4356
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004357 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004358 if (err) {
4359 drop_inode = 1;
4360 goto out_unlock;
4361 }
4362
Chris Mason39279cc2007-06-12 06:35:45 -04004363 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004364 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004365 if (err)
4366 drop_inode = 1;
4367 else {
4368 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004369 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004370 inode->i_fop = &btrfs_file_operations;
4371 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004372 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004373 }
Chris Mason39279cc2007-06-12 06:35:45 -04004374 btrfs_update_inode_block_group(trans, inode);
4375 btrfs_update_inode_block_group(trans, dir);
4376out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004377 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004378 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004379fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004380 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004381 if (drop_inode) {
4382 inode_dec_link_count(inode);
4383 iput(inode);
4384 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004385 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004386 return err;
4387}
4388
4389static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4390 struct dentry *dentry)
4391{
4392 struct btrfs_trans_handle *trans;
4393 struct btrfs_root *root = BTRFS_I(dir)->root;
4394 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004395 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004396 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004397 int err;
4398 int drop_inode = 0;
4399
4400 if (inode->i_nlink == 0)
4401 return -ENOENT;
4402
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004403 /* do not allow sys_link's with other subvols of the same device */
4404 if (root->objectid != BTRFS_I(inode)->root->objectid)
4405 return -EPERM;
4406
Josef Bacik9ed74f22009-09-11 16:12:44 -04004407 /*
4408 * 1 item for inode ref
4409 * 2 items for dir items
4410 */
4411 err = btrfs_reserve_metadata_space(root, 3);
Chris Mason1832a6d2007-12-21 16:27:21 -05004412 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004413 return err;
4414
4415 btrfs_inc_nlink(inode);
4416
Chris Mason3de45862008-11-17 21:02:50 -05004417 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004418 if (err)
4419 goto fail;
4420
Chris Mason39279cc2007-06-12 06:35:45 -04004421 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004422
Chris Mason39279cc2007-06-12 06:35:45 -04004423 btrfs_set_trans_block_group(trans, dir);
4424 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004425
Chris Mason00e4e6b2008-08-05 11:18:09 -04004426 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004427
Yan, Zhenga5719522009-09-24 09:17:31 -04004428 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004429 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004430 } else {
4431 btrfs_update_inode_block_group(trans, dir);
4432 err = btrfs_update_inode(trans, root, inode);
4433 BUG_ON(err);
4434 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4435 }
Chris Mason39279cc2007-06-12 06:35:45 -04004436
Chris Masond3c2fdc2007-09-17 10:58:06 -04004437 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004438 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004439fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004440 btrfs_unreserve_metadata_space(root, 3);
Chris Mason39279cc2007-06-12 06:35:45 -04004441 if (drop_inode) {
4442 inode_dec_link_count(inode);
4443 iput(inode);
4444 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004445 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004446 return err;
4447}
4448
Chris Mason39279cc2007-06-12 06:35:45 -04004449static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4450{
Chris Masonb9d86662008-05-02 16:13:49 -04004451 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004452 struct btrfs_trans_handle *trans;
4453 struct btrfs_root *root = BTRFS_I(dir)->root;
4454 int err = 0;
4455 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004456 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004457 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04004458 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004459
Josef Bacik9ed74f22009-09-11 16:12:44 -04004460 /*
4461 * 2 items for inode and ref
4462 * 2 items for dir items
4463 * 1 for xattr if selinux is on
4464 */
4465 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004466 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004467 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004468
Chris Mason39279cc2007-06-12 06:35:45 -04004469 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004470 if (!trans) {
4471 err = -ENOMEM;
Chris Mason39279cc2007-06-12 06:35:45 -04004472 goto out_unlock;
4473 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004474 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004475
4476 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4477 if (err) {
4478 err = -ENOSPC;
Miao Xie0be2e982010-02-11 08:06:58 +00004479 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04004480 }
4481
Josef Bacikaec74772008-07-24 12:12:38 -04004482 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004483 dentry->d_name.len,
4484 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004485 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4486 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004487 if (IS_ERR(inode)) {
4488 err = PTR_ERR(inode);
4489 goto out_fail;
4490 }
Chris Mason5f39d392007-10-15 16:14:19 -04004491
Chris Mason39279cc2007-06-12 06:35:45 -04004492 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004493
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004494 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004495 if (err)
4496 goto out_fail;
4497
Chris Mason39279cc2007-06-12 06:35:45 -04004498 inode->i_op = &btrfs_dir_inode_operations;
4499 inode->i_fop = &btrfs_dir_file_operations;
4500 btrfs_set_trans_block_group(trans, inode);
4501
Chris Masondbe674a2008-07-17 12:54:05 -04004502 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004503 err = btrfs_update_inode(trans, root, inode);
4504 if (err)
4505 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004506
Chris Masone02119d2008-09-05 16:13:11 -04004507 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4508 inode, dentry->d_name.name,
4509 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004510 if (err)
4511 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004512
Chris Mason39279cc2007-06-12 06:35:45 -04004513 d_instantiate(dentry, inode);
4514 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004515 btrfs_update_inode_block_group(trans, inode);
4516 btrfs_update_inode_block_group(trans, dir);
4517
4518out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004519 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004520 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04004521
Chris Mason39279cc2007-06-12 06:35:45 -04004522out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004523 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004524 if (drop_on_err)
4525 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04004526 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004527 return err;
4528}
4529
Chris Masond352ac62008-09-29 15:18:18 -04004530/* helper for btfs_get_extent. Given an existing extent in the tree,
4531 * and an extent that you want to insert, deal with overlap and insert
4532 * the new extent into the tree.
4533 */
Chris Mason3b951512008-04-17 11:29:12 -04004534static int merge_extent_mapping(struct extent_map_tree *em_tree,
4535 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004536 struct extent_map *em,
4537 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004538{
4539 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004540
Chris Masone6dcd2d2008-07-17 12:53:50 -04004541 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4542 start_diff = map_start - em->start;
4543 em->start = map_start;
4544 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004545 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4546 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004547 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004548 em->block_len -= start_diff;
4549 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004550 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004551}
4552
Chris Masonc8b97812008-10-29 14:49:59 -04004553static noinline int uncompress_inline(struct btrfs_path *path,
4554 struct inode *inode, struct page *page,
4555 size_t pg_offset, u64 extent_offset,
4556 struct btrfs_file_extent_item *item)
4557{
4558 int ret;
4559 struct extent_buffer *leaf = path->nodes[0];
4560 char *tmp;
4561 size_t max_size;
4562 unsigned long inline_size;
4563 unsigned long ptr;
4564
4565 WARN_ON(pg_offset != 0);
4566 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4567 inline_size = btrfs_file_extent_inline_item_len(leaf,
4568 btrfs_item_nr(leaf, path->slots[0]));
4569 tmp = kmalloc(inline_size, GFP_NOFS);
4570 ptr = btrfs_file_extent_inline_start(item);
4571
4572 read_extent_buffer(leaf, tmp, ptr, inline_size);
4573
Chris Mason5b050f02008-11-11 09:34:41 -05004574 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004575 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4576 inline_size, max_size);
4577 if (ret) {
4578 char *kaddr = kmap_atomic(page, KM_USER0);
4579 unsigned long copy_size = min_t(u64,
4580 PAGE_CACHE_SIZE - pg_offset,
4581 max_size - extent_offset);
4582 memset(kaddr + pg_offset, 0, copy_size);
4583 kunmap_atomic(kaddr, KM_USER0);
4584 }
4585 kfree(tmp);
4586 return 0;
4587}
4588
Chris Masond352ac62008-09-29 15:18:18 -04004589/*
4590 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004591 * the ugly parts come from merging extents from the disk with the in-ram
4592 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004593 * where the in-ram extents might be locked pending data=ordered completion.
4594 *
4595 * This also copies inline extents directly into the page.
4596 */
Chris Masond3977122009-01-05 21:25:51 -05004597
Chris Masona52d9a82007-08-27 16:49:44 -04004598struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004599 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004600 int create)
4601{
4602 int ret;
4603 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004604 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004605 u64 extent_start = 0;
4606 u64 extent_end = 0;
4607 u64 objectid = inode->i_ino;
4608 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004609 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004610 struct btrfs_root *root = BTRFS_I(inode)->root;
4611 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004612 struct extent_buffer *leaf;
4613 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004614 struct extent_map *em = NULL;
4615 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004616 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004617 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004618 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004619
Chris Masona52d9a82007-08-27 16:49:44 -04004620again:
Chris Mason890871b2009-09-02 16:24:52 -04004621 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004622 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004623 if (em)
4624 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004625 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004626
Chris Masona52d9a82007-08-27 16:49:44 -04004627 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004628 if (em->start > start || em->start + em->len <= start)
4629 free_extent_map(em);
4630 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004631 free_extent_map(em);
4632 else
4633 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004634 }
Chris Masond1310b22008-01-24 16:13:08 -05004635 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004636 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004637 err = -ENOMEM;
4638 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004639 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004640 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004641 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004642 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004643 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004644 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004645
4646 if (!path) {
4647 path = btrfs_alloc_path();
4648 BUG_ON(!path);
4649 }
4650
Chris Mason179e29e2007-11-01 11:28:41 -04004651 ret = btrfs_lookup_file_extent(trans, root, path,
4652 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004653 if (ret < 0) {
4654 err = ret;
4655 goto out;
4656 }
4657
4658 if (ret != 0) {
4659 if (path->slots[0] == 0)
4660 goto not_found;
4661 path->slots[0]--;
4662 }
4663
Chris Mason5f39d392007-10-15 16:14:19 -04004664 leaf = path->nodes[0];
4665 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004666 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004667 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004668 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4669 found_type = btrfs_key_type(&found_key);
4670 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004671 found_type != BTRFS_EXTENT_DATA_KEY) {
4672 goto not_found;
4673 }
4674
Chris Mason5f39d392007-10-15 16:14:19 -04004675 found_type = btrfs_file_extent_type(leaf, item);
4676 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004677 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004678 if (found_type == BTRFS_FILE_EXTENT_REG ||
4679 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004680 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004681 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004682 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4683 size_t size;
4684 size = btrfs_file_extent_inline_len(leaf, item);
4685 extent_end = (extent_start + size + root->sectorsize - 1) &
4686 ~((u64)root->sectorsize - 1);
4687 }
4688
4689 if (start >= extent_end) {
4690 path->slots[0]++;
4691 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4692 ret = btrfs_next_leaf(root, path);
4693 if (ret < 0) {
4694 err = ret;
4695 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004696 }
Yan Zheng9036c102008-10-30 14:19:41 -04004697 if (ret > 0)
4698 goto not_found;
4699 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004700 }
Yan Zheng9036c102008-10-30 14:19:41 -04004701 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4702 if (found_key.objectid != objectid ||
4703 found_key.type != BTRFS_EXTENT_DATA_KEY)
4704 goto not_found;
4705 if (start + len <= found_key.offset)
4706 goto not_found;
4707 em->start = start;
4708 em->len = found_key.offset - start;
4709 goto not_found_em;
4710 }
4711
Yan Zhengd899e052008-10-30 14:25:28 -04004712 if (found_type == BTRFS_FILE_EXTENT_REG ||
4713 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004714 em->start = extent_start;
4715 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004716 em->orig_start = extent_start -
4717 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004718 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4719 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004720 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004721 goto insert;
4722 }
Chris Masonc8b97812008-10-29 14:49:59 -04004723 if (compressed) {
4724 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4725 em->block_start = bytenr;
4726 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4727 item);
4728 } else {
4729 bytenr += btrfs_file_extent_offset(leaf, item);
4730 em->block_start = bytenr;
4731 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004732 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4733 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004734 }
Chris Masona52d9a82007-08-27 16:49:44 -04004735 goto insert;
4736 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004737 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004738 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004739 size_t size;
4740 size_t extent_offset;
4741 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004742
Chris Masona52d9a82007-08-27 16:49:44 -04004743 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004744 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004745 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004746 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004747 goto out;
4748 }
4749
Yan Zheng9036c102008-10-30 14:19:41 -04004750 size = btrfs_file_extent_inline_len(leaf, item);
4751 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004752 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004753 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004754 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004755 em->len = (copy_size + root->sectorsize - 1) &
4756 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004757 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004758 if (compressed)
4759 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004760 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004761 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004762 if (btrfs_file_extent_compression(leaf, item) ==
4763 BTRFS_COMPRESS_ZLIB) {
4764 ret = uncompress_inline(path, inode, page,
4765 pg_offset,
4766 extent_offset, item);
4767 BUG_ON(ret);
4768 } else {
4769 map = kmap(page);
4770 read_extent_buffer(leaf, map + pg_offset, ptr,
4771 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04004772 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4773 memset(map + pg_offset + copy_size, 0,
4774 PAGE_CACHE_SIZE - pg_offset -
4775 copy_size);
4776 }
Chris Masonc8b97812008-10-29 14:49:59 -04004777 kunmap(page);
4778 }
Chris Mason179e29e2007-11-01 11:28:41 -04004779 flush_dcache_page(page);
4780 } else if (create && PageUptodate(page)) {
4781 if (!trans) {
4782 kunmap(page);
4783 free_extent_map(em);
4784 em = NULL;
4785 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004786 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004787 goto again;
4788 }
Chris Masonc8b97812008-10-29 14:49:59 -04004789 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004790 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004791 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004792 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004793 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004794 }
Chris Masond1310b22008-01-24 16:13:08 -05004795 set_extent_uptodate(io_tree, em->start,
4796 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004797 goto insert;
4798 } else {
Chris Masond3977122009-01-05 21:25:51 -05004799 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004800 WARN_ON(1);
4801 }
4802not_found:
4803 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004804 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004805not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004806 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004807 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004808insert:
4809 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004810 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004811 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4812 "[%llu %llu]\n", (unsigned long long)em->start,
4813 (unsigned long long)em->len,
4814 (unsigned long long)start,
4815 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004816 err = -EIO;
4817 goto out;
4818 }
Chris Masond1310b22008-01-24 16:13:08 -05004819
4820 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04004821 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004822 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004823 /* it is possible that someone inserted the extent into the tree
4824 * while we had the lock dropped. It is also possible that
4825 * an overlapping map exists in the tree
4826 */
Chris Masona52d9a82007-08-27 16:49:44 -04004827 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004828 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004829
4830 ret = 0;
4831
Chris Mason3b951512008-04-17 11:29:12 -04004832 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004833 if (existing && (existing->start > start ||
4834 existing->start + existing->len <= start)) {
4835 free_extent_map(existing);
4836 existing = NULL;
4837 }
Chris Mason3b951512008-04-17 11:29:12 -04004838 if (!existing) {
4839 existing = lookup_extent_mapping(em_tree, em->start,
4840 em->len);
4841 if (existing) {
4842 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004843 em, start,
4844 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004845 free_extent_map(existing);
4846 if (err) {
4847 free_extent_map(em);
4848 em = NULL;
4849 }
4850 } else {
4851 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004852 free_extent_map(em);
4853 em = NULL;
4854 }
4855 } else {
4856 free_extent_map(em);
4857 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004858 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004859 }
Chris Masona52d9a82007-08-27 16:49:44 -04004860 }
Chris Mason890871b2009-09-02 16:24:52 -04004861 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004862out:
Chris Masonf4219502008-07-22 11:18:09 -04004863 if (path)
4864 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004865 if (trans) {
4866 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004867 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004868 err = ret;
4869 }
Chris Masona52d9a82007-08-27 16:49:44 -04004870 if (err) {
4871 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04004872 return ERR_PTR(err);
4873 }
4874 return em;
4875}
4876
Chris Mason16432982008-04-10 10:23:21 -04004877static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4878 const struct iovec *iov, loff_t offset,
4879 unsigned long nr_segs)
4880{
Chris Masone1c4b742008-04-22 13:26:46 -04004881 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004882}
4883
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004884static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4885 __u64 start, __u64 len)
4886{
4887 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4888}
4889
Chris Mason9ebefb182007-06-15 13:50:00 -04004890int btrfs_readpage(struct file *file, struct page *page)
4891{
Chris Masond1310b22008-01-24 16:13:08 -05004892 struct extent_io_tree *tree;
4893 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004894 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004895}
Chris Mason1832a6d2007-12-21 16:27:21 -05004896
Chris Mason39279cc2007-06-12 06:35:45 -04004897static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4898{
Chris Masond1310b22008-01-24 16:13:08 -05004899 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004900
4901
4902 if (current->flags & PF_MEMALLOC) {
4903 redirty_page_for_writepage(wbc, page);
4904 unlock_page(page);
4905 return 0;
4906 }
Chris Masond1310b22008-01-24 16:13:08 -05004907 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004908 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4909}
Chris Mason39279cc2007-06-12 06:35:45 -04004910
Chris Masonf4219502008-07-22 11:18:09 -04004911int btrfs_writepages(struct address_space *mapping,
4912 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004913{
Chris Masond1310b22008-01-24 16:13:08 -05004914 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004915
Chris Masond1310b22008-01-24 16:13:08 -05004916 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004917 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4918}
4919
Chris Mason3ab2fb52007-11-08 10:59:22 -05004920static int
4921btrfs_readpages(struct file *file, struct address_space *mapping,
4922 struct list_head *pages, unsigned nr_pages)
4923{
Chris Masond1310b22008-01-24 16:13:08 -05004924 struct extent_io_tree *tree;
4925 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004926 return extent_readpages(tree, mapping, pages, nr_pages,
4927 btrfs_get_extent);
4928}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004929static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004930{
Chris Masond1310b22008-01-24 16:13:08 -05004931 struct extent_io_tree *tree;
4932 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004933 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004934
Chris Masond1310b22008-01-24 16:13:08 -05004935 tree = &BTRFS_I(page->mapping->host)->io_tree;
4936 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004937 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004938 if (ret == 1) {
4939 ClearPagePrivate(page);
4940 set_page_private(page, 0);
4941 page_cache_release(page);
4942 }
4943 return ret;
4944}
Chris Mason39279cc2007-06-12 06:35:45 -04004945
Chris Masone6dcd2d2008-07-17 12:53:50 -04004946static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4947{
Chris Mason98509cf2008-09-11 15:51:43 -04004948 if (PageWriteback(page) || PageDirty(page))
4949 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05004950 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004951}
4952
Chris Masona52d9a82007-08-27 16:49:44 -04004953static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4954{
Chris Masond1310b22008-01-24 16:13:08 -05004955 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004956 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004957 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004958 u64 page_start = page_offset(page);
4959 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004960
Chris Mason8b62b722009-09-02 16:53:46 -04004961
4962 /*
4963 * we have the page locked, so new writeback can't start,
4964 * and the dirty bit won't be cleared while we are here.
4965 *
4966 * Wait for IO on this page so that we can safely clear
4967 * the PagePrivate2 bit and do ordered accounting
4968 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004969 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04004970
Chris Masond1310b22008-01-24 16:13:08 -05004971 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004972 if (offset) {
4973 btrfs_releasepage(page, GFP_NOFS);
4974 return;
4975 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00004976 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
4977 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004978 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4979 page_offset(page));
4980 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004981 /*
4982 * IO on this page will never be started, so we need
4983 * to account for any ordered extents now
4984 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004985 clear_extent_bit(tree, page_start, page_end,
4986 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04004987 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004988 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04004989 /*
4990 * whoever cleared the private bit is responsible
4991 * for the finish_ordered_io
4992 */
4993 if (TestClearPagePrivate2(page)) {
4994 btrfs_finish_ordered_io(page->mapping->host,
4995 page_start, page_end);
4996 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004997 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004998 cached_state = NULL;
4999 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
5000 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005001 }
5002 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005003 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00005004 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005005 __btrfs_releasepage(page, GFP_NOFS);
5006
Chris Mason4a096752008-07-21 10:29:44 -04005007 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005008 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005009 ClearPagePrivate(page);
5010 set_page_private(page, 0);
5011 page_cache_release(page);
5012 }
Chris Mason39279cc2007-06-12 06:35:45 -04005013}
5014
Chris Mason9ebefb182007-06-15 13:50:00 -04005015/*
5016 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
5017 * called from a page fault handler when a page is first dirtied. Hence we must
5018 * be careful to check for EOF conditions here. We set the page up correctly
5019 * for a written page which means we get ENOSPC checking when writing into
5020 * holes and correct delalloc and unwritten extent mapping on filesystems that
5021 * support these features.
5022 *
5023 * We are not allowed to take the i_mutex here so we have to play games to
5024 * protect against truncate races as the page could now be beyond EOF. Because
5025 * vmtruncate() writes the inode size before removing pages, once we have the
5026 * page lock we can determine safely if the page is beyond EOF. If it is not
5027 * beyond EOF, then the page is guaranteed safe against truncation until we
5028 * unlock the page.
5029 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07005030int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04005031{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005032 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05005033 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05005034 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005035 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5036 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005037 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005038 char *kaddr;
5039 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04005040 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05005041 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04005042 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005043 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04005044
Josef Bacik6a632092009-02-20 11:00:09 -05005045 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07005046 if (ret) {
5047 if (ret == -ENOMEM)
5048 ret = VM_FAULT_OOM;
5049 else /* -ENOSPC, -EIO, etc */
5050 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05005051 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07005052 }
Chris Mason1832a6d2007-12-21 16:27:21 -05005053
Josef Bacik9ed74f22009-09-11 16:12:44 -04005054 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
5055 if (ret) {
5056 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5057 ret = VM_FAULT_SIGBUS;
5058 goto out;
5059 }
5060
Nick Piggin56a76f82009-03-31 15:23:23 -07005061 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005062again:
Chris Mason9ebefb182007-06-15 13:50:00 -04005063 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04005064 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005065 page_start = page_offset(page);
5066 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005067
Chris Mason9ebefb182007-06-15 13:50:00 -04005068 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04005069 (page_start >= size)) {
Josef Bacik6a632092009-02-20 11:00:09 -05005070 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason9ebefb182007-06-15 13:50:00 -04005071 /* page got truncated out from underneath us */
5072 goto out_unlock;
5073 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005074 wait_on_page_writeback(page);
5075
Josef Bacik2ac55d42010-02-03 19:33:23 +00005076 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
5077 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005078 set_page_extent_mapped(page);
5079
Chris Masoneb84ae02008-07-17 13:53:27 -04005080 /*
5081 * we can't set the delalloc bits if there are pending ordered
5082 * extents. Drop our locks and wait for them to finish
5083 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005084 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5085 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00005086 unlock_extent_cached(io_tree, page_start, page_end,
5087 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005088 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005089 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005090 btrfs_put_ordered_extent(ordered);
5091 goto again;
5092 }
5093
Josef Bacikfbf19082009-10-01 17:10:23 -04005094 /*
5095 * XXX - page_mkwrite gets called every time the page is dirtied, even
5096 * if it was already dirty, so for space accounting reasons we need to
5097 * clear any delalloc bits for the range we are fixing to save. There
5098 * is probably a better way to do this, but for now keep consistent with
5099 * prepare_pages in the normal write path.
5100 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00005101 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005102 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00005103 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04005104
Josef Bacik2ac55d42010-02-03 19:33:23 +00005105 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
5106 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005107 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00005108 unlock_extent_cached(io_tree, page_start, page_end,
5109 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005110 ret = VM_FAULT_SIGBUS;
Josef Bacikfbf19082009-10-01 17:10:23 -04005111 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005112 goto out_unlock;
5113 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005114 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04005115
5116 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04005117 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005118 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04005119 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04005120 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04005121
Chris Masone6dcd2d2008-07-17 12:53:50 -04005122 if (zero_start != PAGE_CACHE_SIZE) {
5123 kaddr = kmap(page);
5124 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5125 flush_dcache_page(page);
5126 kunmap(page);
5127 }
Chris Mason247e7432008-07-17 12:53:51 -04005128 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005129 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04005130 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005131
Chris Mason257c62e2009-10-13 13:21:08 -04005132 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5133 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5134
Josef Bacik2ac55d42010-02-03 19:33:23 +00005135 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04005136
5137out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005138 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason50a9b212009-09-11 12:33:12 -04005139 if (!ret)
5140 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04005141 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05005142out:
Chris Mason9ebefb182007-06-15 13:50:00 -04005143 return ret;
5144}
5145
Chris Mason39279cc2007-06-12 06:35:45 -04005146static void btrfs_truncate(struct inode *inode)
5147{
5148 struct btrfs_root *root = BTRFS_I(inode)->root;
5149 int ret;
5150 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04005151 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04005152 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005153
Yan, Zheng80825102009-11-12 09:35:36 +00005154 if (!S_ISREG(inode->i_mode)) {
5155 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04005156 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005157 }
Chris Mason39279cc2007-06-12 06:35:45 -04005158
Josef Bacik5d5e1032009-10-13 16:46:49 -04005159 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5160 if (ret)
5161 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005162
Chris Mason4a096752008-07-21 10:29:44 -04005163 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00005164 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005165
Chris Mason39279cc2007-06-12 06:35:45 -04005166 trans = btrfs_start_transaction(root, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00005167 btrfs_set_trans_block_group(trans, inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005168
5169 /*
5170 * setattr is responsible for setting the ordered_data_close flag,
5171 * but that is only tested during the last file release. That
5172 * could happen well after the next commit, leaving a great big
5173 * window where new writes may get lost if someone chooses to write
5174 * to this file after truncating to zero
5175 *
5176 * The inode doesn't have any dirty data here, and so if we commit
5177 * this is a noop. If someone immediately starts writing to the inode
5178 * it is very likely we'll catch some of their writes in this
5179 * transaction, and the commit will find this file on the ordered
5180 * data list with good things to send down.
5181 *
5182 * This is a best effort solution, there is still a window where
5183 * using truncate to replace the contents of the file will
5184 * end up with a zero length file after a crash.
5185 */
5186 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5187 btrfs_add_ordered_operation(trans, root, inode);
5188
Yan, Zheng80825102009-11-12 09:35:36 +00005189 while (1) {
5190 ret = btrfs_truncate_inode_items(trans, root, inode,
5191 inode->i_size,
5192 BTRFS_EXTENT_DATA_KEY);
5193 if (ret != -EAGAIN)
5194 break;
Chris Mason39279cc2007-06-12 06:35:45 -04005195
Yan, Zheng80825102009-11-12 09:35:36 +00005196 ret = btrfs_update_inode(trans, root, inode);
5197 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005198
Yan, Zheng80825102009-11-12 09:35:36 +00005199 nr = trans->blocks_used;
5200 btrfs_end_transaction(trans, root);
5201 btrfs_btree_balance_dirty(root, nr);
5202
5203 trans = btrfs_start_transaction(root, 1);
5204 btrfs_set_trans_block_group(trans, inode);
5205 }
5206
5207 if (ret == 0 && inode->i_nlink > 0) {
5208 ret = btrfs_orphan_del(trans, inode);
5209 BUG_ON(ret);
5210 }
5211
5212 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04005213 BUG_ON(ret);
5214
Josef Bacik7b128762008-07-24 12:17:14 -04005215 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04005216 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005217 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04005218 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005219}
5220
Sven Wegener3b963622008-06-09 21:57:42 -04005221/*
Chris Masond352ac62008-09-29 15:18:18 -04005222 * create a new subvolume directory/inode (helper for the ioctl).
5223 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05005224int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04005225 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005226 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04005227{
Chris Mason39279cc2007-06-12 06:35:45 -04005228 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005229 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005230 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005231
Josef Bacikaec74772008-07-24 12:12:38 -04005232 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005233 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04005234 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005235 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005236 inode->i_op = &btrfs_dir_inode_operations;
5237 inode->i_fop = &btrfs_dir_file_operations;
5238
Chris Mason39279cc2007-06-12 06:35:45 -04005239 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04005240 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04005241
Yan, Zheng76dda932009-09-21 16:00:26 -04005242 err = btrfs_update_inode(trans, new_root, inode);
5243 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005244
Yan, Zheng76dda932009-09-21 16:00:26 -04005245 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005246 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005247}
5248
Chris Masond352ac62008-09-29 15:18:18 -04005249/* helper function for file defrag and space balancing. This
5250 * forces readahead on a given range of bytes in an inode
5251 */
Chris Masonedbd8d42007-12-21 16:27:24 -05005252unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04005253 struct file_ra_state *ra, struct file *file,
5254 pgoff_t offset, pgoff_t last_index)
5255{
Chris Mason8e7bf942008-04-28 09:02:36 -04005256 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04005257
Chris Mason86479a02007-09-10 19:58:16 -04005258 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5259 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04005260}
5261
Chris Mason39279cc2007-06-12 06:35:45 -04005262struct inode *btrfs_alloc_inode(struct super_block *sb)
5263{
5264 struct btrfs_inode *ei;
5265
5266 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5267 if (!ei)
5268 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04005269 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04005270 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005271 ei->logged_trans = 0;
Josef Bacik32c00af2009-10-08 13:34:05 -04005272 ei->outstanding_extents = 0;
5273 ei->reserved_extents = 0;
Josef Bacika6dbd422009-11-11 15:53:34 -05005274 ei->root = NULL;
Josef Bacik32c00af2009-10-08 13:34:05 -04005275 spin_lock_init(&ei->accounting_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005276 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04005277 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005278 INIT_LIST_HEAD(&ei->ordered_operations);
Chris Mason39279cc2007-06-12 06:35:45 -04005279 return &ei->vfs_inode;
5280}
5281
5282void btrfs_destroy_inode(struct inode *inode)
5283{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005284 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005285 struct btrfs_root *root = BTRFS_I(inode)->root;
5286
Chris Mason39279cc2007-06-12 06:35:45 -04005287 WARN_ON(!list_empty(&inode->i_dentry));
5288 WARN_ON(inode->i_data.nrpages);
5289
Chris Mason5a3f23d2009-03-31 13:27:11 -04005290 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05005291 * This can happen where we create an inode, but somebody else also
5292 * created the same inode and we need to destroy the one we already
5293 * created.
5294 */
5295 if (!root)
5296 goto free;
5297
5298 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005299 * Make sure we're properly removed from the ordered operation
5300 * lists.
5301 */
5302 smp_mb();
5303 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5304 spin_lock(&root->fs_info->ordered_extent_lock);
5305 list_del_init(&BTRFS_I(inode)->ordered_operations);
5306 spin_unlock(&root->fs_info->ordered_extent_lock);
5307 }
5308
5309 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005310 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00005311 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
5312 inode->i_ino);
5313 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04005314 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04005315 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005316
Chris Masond3977122009-01-05 21:25:51 -05005317 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005318 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5319 if (!ordered)
5320 break;
5321 else {
Chris Masond3977122009-01-05 21:25:51 -05005322 printk(KERN_ERR "btrfs found ordered "
5323 "extent %llu %llu on inode cleanup\n",
5324 (unsigned long long)ordered->file_offset,
5325 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005326 btrfs_remove_ordered_extent(inode, ordered);
5327 btrfs_put_ordered_extent(ordered);
5328 btrfs_put_ordered_extent(ordered);
5329 }
5330 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005331 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005332 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05005333free:
Chris Mason39279cc2007-06-12 06:35:45 -04005334 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5335}
5336
Yan, Zheng76dda932009-09-21 16:00:26 -04005337void btrfs_drop_inode(struct inode *inode)
5338{
5339 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005340 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5341 generic_delete_inode(inode);
5342 else
5343 generic_drop_inode(inode);
5344}
5345
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005346static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04005347{
5348 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5349
5350 inode_init_once(&ei->vfs_inode);
5351}
5352
5353void btrfs_destroy_cachep(void)
5354{
5355 if (btrfs_inode_cachep)
5356 kmem_cache_destroy(btrfs_inode_cachep);
5357 if (btrfs_trans_handle_cachep)
5358 kmem_cache_destroy(btrfs_trans_handle_cachep);
5359 if (btrfs_transaction_cachep)
5360 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04005361 if (btrfs_path_cachep)
5362 kmem_cache_destroy(btrfs_path_cachep);
5363}
5364
5365int btrfs_init_cachep(void)
5366{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005367 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5368 sizeof(struct btrfs_inode), 0,
5369 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04005370 if (!btrfs_inode_cachep)
5371 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005372
5373 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5374 sizeof(struct btrfs_trans_handle), 0,
5375 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005376 if (!btrfs_trans_handle_cachep)
5377 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005378
5379 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5380 sizeof(struct btrfs_transaction), 0,
5381 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005382 if (!btrfs_transaction_cachep)
5383 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005384
5385 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5386 sizeof(struct btrfs_path), 0,
5387 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005388 if (!btrfs_path_cachep)
5389 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005390
Chris Mason39279cc2007-06-12 06:35:45 -04005391 return 0;
5392fail:
5393 btrfs_destroy_cachep();
5394 return -ENOMEM;
5395}
5396
5397static int btrfs_getattr(struct vfsmount *mnt,
5398 struct dentry *dentry, struct kstat *stat)
5399{
5400 struct inode *inode = dentry->d_inode;
5401 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05005402 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05005403 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005404 stat->blocks = (inode_get_bytes(inode) +
5405 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04005406 return 0;
5407}
5408
Chris Masond3977122009-01-05 21:25:51 -05005409static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5410 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005411{
5412 struct btrfs_trans_handle *trans;
5413 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005414 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005415 struct inode *new_inode = new_dentry->d_inode;
5416 struct inode *old_inode = old_dentry->d_inode;
5417 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005418 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005419 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005420 int ret;
5421
Yan, Zhengf679a842009-09-24 09:17:31 -04005422 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5423 return -EPERM;
5424
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005425 /* we only allow rename subvolume link between subvolumes */
5426 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05005427 return -EXDEV;
5428
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005429 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5430 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5431 return -ENOTEMPTY;
5432
Chris Mason39279cc2007-06-12 06:35:45 -04005433 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005434 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04005435 return -ENOTEMPTY;
Chris Mason0660b5a2008-11-17 20:37:39 -05005436
Josef Bacik9ed74f22009-09-11 16:12:44 -04005437 /*
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005438 * We want to reserve the absolute worst case amount of items. So if
5439 * both inodes are subvols and we need to unlink them then that would
5440 * require 4 item modifications, but if they are both normal inodes it
5441 * would require 5 item modifications, so we'll assume their normal
5442 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5443 * should cover the worst case number of items we'll modify.
Josef Bacik9ed74f22009-09-11 16:12:44 -04005444 */
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005445 ret = btrfs_reserve_metadata_space(root, 11);
Chris Mason1832a6d2007-12-21 16:27:21 -05005446 if (ret)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005447 return ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05005448
Chris Mason5a3f23d2009-03-31 13:27:11 -04005449 /*
5450 * we're using rename to replace one file with another.
5451 * and the replacement file is large. Start IO on it now so
5452 * we don't add too much work to the end of the transaction
5453 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04005454 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04005455 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5456 filemap_flush(old_inode->i_mapping);
5457
Yan, Zheng76dda932009-09-21 16:00:26 -04005458 /* close the racy window with snapshot create/destroy ioctl */
5459 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5460 down_read(&root->fs_info->subvol_sem);
Chris Mason5f39d392007-10-15 16:14:19 -04005461
Chris Mason39279cc2007-06-12 06:35:45 -04005462 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga5719522009-09-24 09:17:31 -04005463 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04005464
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005465 if (dest != root)
5466 btrfs_record_root_in_trans(trans, dest);
5467
Yan, Zhenga5719522009-09-24 09:17:31 -04005468 ret = btrfs_set_inode_index(new_dir, &index);
5469 if (ret)
5470 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005471
Yan, Zhenga5719522009-09-24 09:17:31 -04005472 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005473 /* force full log commit if subvolume involved. */
5474 root->fs_info->last_trans_log_full_commit = trans->transid;
5475 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04005476 ret = btrfs_insert_inode_ref(trans, dest,
5477 new_dentry->d_name.name,
5478 new_dentry->d_name.len,
5479 old_inode->i_ino,
5480 new_dir->i_ino, index);
5481 if (ret)
5482 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005483 /*
5484 * this is an ugly little race, but the rename is required
5485 * to make sure that if we crash, the inode is either at the
5486 * old name or the new one. pinning the log transaction lets
5487 * us make sure we don't allow a log commit to come in after
5488 * we unlink the name but before we add the new name back in.
5489 */
5490 btrfs_pin_log_trans(root);
5491 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005492 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005493 * make sure the inode gets flushed if it is replacing
5494 * something.
5495 */
5496 if (new_inode && new_inode->i_size &&
5497 old_inode && S_ISREG(old_inode->i_mode)) {
5498 btrfs_add_ordered_operation(trans, root, old_inode);
5499 }
5500
Chris Mason39279cc2007-06-12 06:35:45 -04005501 old_dir->i_ctime = old_dir->i_mtime = ctime;
5502 new_dir->i_ctime = new_dir->i_mtime = ctime;
5503 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04005504
Chris Mason12fcfd22009-03-24 10:24:20 -04005505 if (old_dentry->d_parent != new_dentry->d_parent)
5506 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5507
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005508 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5509 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5510 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5511 old_dentry->d_name.name,
5512 old_dentry->d_name.len);
5513 } else {
5514 btrfs_inc_nlink(old_dentry->d_inode);
5515 ret = btrfs_unlink_inode(trans, root, old_dir,
5516 old_dentry->d_inode,
5517 old_dentry->d_name.name,
5518 old_dentry->d_name.len);
5519 }
5520 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005521
5522 if (new_inode) {
5523 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 if (unlikely(new_inode->i_ino ==
5525 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5526 root_objectid = BTRFS_I(new_inode)->location.objectid;
5527 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5528 root_objectid,
5529 new_dentry->d_name.name,
5530 new_dentry->d_name.len);
5531 BUG_ON(new_inode->i_nlink == 0);
5532 } else {
5533 ret = btrfs_unlink_inode(trans, dest, new_dir,
5534 new_dentry->d_inode,
5535 new_dentry->d_name.name,
5536 new_dentry->d_name.len);
5537 }
5538 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005539 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04005540 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005542 }
Chris Mason39279cc2007-06-12 06:35:45 -04005543 }
Josef Bacikaec74772008-07-24 12:12:38 -04005544
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005545 ret = btrfs_add_link(trans, new_dir, old_inode,
5546 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04005547 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005548 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005549
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005550 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5551 btrfs_log_new_name(trans, old_inode, old_dir,
5552 new_dentry->d_parent);
5553 btrfs_end_log_trans(root);
5554 }
Chris Mason39279cc2007-06-12 06:35:45 -04005555out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04005556 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005557
Yan, Zheng76dda932009-09-21 16:00:26 -04005558 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5559 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005560
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005561 btrfs_unreserve_metadata_space(root, 11);
Chris Mason39279cc2007-06-12 06:35:45 -04005562 return ret;
5563}
5564
Chris Masond352ac62008-09-29 15:18:18 -04005565/*
5566 * some fairly slow code that needs optimization. This walks the list
5567 * of all the inodes with pending delalloc and forces them to disk.
5568 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005569int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04005570{
5571 struct list_head *head = &root->fs_info->delalloc_inodes;
5572 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005573 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04005574
Yan Zhengc146afa2008-11-12 14:34:12 -05005575 if (root->fs_info->sb->s_flags & MS_RDONLY)
5576 return -EROFS;
5577
Chris Mason75eff682008-12-15 15:54:40 -05005578 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05005579 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04005580 binode = list_entry(head->next, struct btrfs_inode,
5581 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005582 inode = igrab(&binode->vfs_inode);
5583 if (!inode)
5584 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05005585 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005586 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005587 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005588 if (delay_iput)
5589 btrfs_add_delayed_iput(inode);
5590 else
5591 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005592 }
5593 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05005594 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04005595 }
Chris Mason75eff682008-12-15 15:54:40 -05005596 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04005597
5598 /* the filemap_flush will queue IO into the worker threads, but
5599 * we have to make sure the IO is actually started and that
5600 * ordered extents get created before we return
5601 */
5602 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05005603 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05005604 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005605 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05005606 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5607 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04005608 }
5609 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04005610 return 0;
5611}
5612
Chris Mason39279cc2007-06-12 06:35:45 -04005613static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5614 const char *symname)
5615{
5616 struct btrfs_trans_handle *trans;
5617 struct btrfs_root *root = BTRFS_I(dir)->root;
5618 struct btrfs_path *path;
5619 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05005620 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005621 int err;
5622 int drop_inode = 0;
5623 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005624 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04005625 int name_len;
5626 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04005627 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005628 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04005629 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05005630 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005631
5632 name_len = strlen(symname) + 1;
5633 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5634 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05005635
Josef Bacik9ed74f22009-09-11 16:12:44 -04005636 /*
5637 * 2 items for inode item and ref
5638 * 2 items for dir items
5639 * 1 item for xattr if selinux is on
5640 */
5641 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05005642 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04005643 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05005644
Chris Mason39279cc2007-06-12 06:35:45 -04005645 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005646 if (!trans)
5647 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04005648 btrfs_set_trans_block_group(trans, dir);
5649
5650 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5651 if (err) {
5652 err = -ENOSPC;
5653 goto out_unlock;
5654 }
5655
Josef Bacikaec74772008-07-24 12:12:38 -04005656 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05005657 dentry->d_name.len,
5658 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04005659 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5660 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005661 err = PTR_ERR(inode);
5662 if (IS_ERR(inode))
5663 goto out_unlock;
5664
Yan, Zhengf34f57a2009-11-12 09:35:27 +00005665 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04005666 if (err) {
5667 drop_inode = 1;
5668 goto out_unlock;
5669 }
5670
Chris Mason39279cc2007-06-12 06:35:45 -04005671 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005672 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005673 if (err)
5674 drop_inode = 1;
5675 else {
5676 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005677 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04005678 inode->i_fop = &btrfs_file_operations;
5679 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05005680 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04005681 }
Chris Mason39279cc2007-06-12 06:35:45 -04005682 btrfs_update_inode_block_group(trans, inode);
5683 btrfs_update_inode_block_group(trans, dir);
5684 if (drop_inode)
5685 goto out_unlock;
5686
5687 path = btrfs_alloc_path();
5688 BUG_ON(!path);
5689 key.objectid = inode->i_ino;
5690 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005691 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5692 datasize = btrfs_file_extent_calc_inline_size(name_len);
5693 err = btrfs_insert_empty_item(trans, root, path, &key,
5694 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005695 if (err) {
5696 drop_inode = 1;
5697 goto out_unlock;
5698 }
Chris Mason5f39d392007-10-15 16:14:19 -04005699 leaf = path->nodes[0];
5700 ei = btrfs_item_ptr(leaf, path->slots[0],
5701 struct btrfs_file_extent_item);
5702 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5703 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04005704 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04005705 btrfs_set_file_extent_encryption(leaf, ei, 0);
5706 btrfs_set_file_extent_compression(leaf, ei, 0);
5707 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5708 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5709
Chris Mason39279cc2007-06-12 06:35:45 -04005710 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04005711 write_extent_buffer(leaf, symname, ptr, name_len);
5712 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04005713 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04005714
Chris Mason39279cc2007-06-12 06:35:45 -04005715 inode->i_op = &btrfs_symlink_inode_operations;
5716 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04005717 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04005718 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04005719 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005720 err = btrfs_update_inode(trans, root, inode);
5721 if (err)
5722 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005723
5724out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04005725 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04005726 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005727out_fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005728 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04005729 if (drop_inode) {
5730 inode_dec_link_count(inode);
5731 iput(inode);
5732 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04005733 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005734 return err;
5735}
Chris Mason16432982008-04-10 10:23:21 -04005736
Yan, Zheng5a303d52009-11-12 09:34:52 +00005737static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005738 u64 alloc_hint, int mode, loff_t actual_len)
Yan Zhengd899e052008-10-30 14:25:28 -04005739{
Yan, Zheng5a303d52009-11-12 09:34:52 +00005740 struct btrfs_trans_handle *trans;
Yan Zhengd899e052008-10-30 14:25:28 -04005741 struct btrfs_root *root = BTRFS_I(inode)->root;
5742 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04005743 u64 cur_offset = start;
5744 u64 num_bytes = end - start;
5745 int ret = 0;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005746 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04005747
Yan Zhengd899e052008-10-30 14:25:28 -04005748 while (num_bytes > 0) {
Chris Mason3a1abec2009-12-17 15:47:17 -05005749 trans = btrfs_start_transaction(root, 1);
5750
Josef Bacik287a0ab2010-03-19 18:07:23 +00005751 ret = btrfs_reserve_extent(trans, root, num_bytes,
Yan Zhengd899e052008-10-30 14:25:28 -04005752 root->sectorsize, 0, alloc_hint,
5753 (u64)-1, &ins, 1);
5754 if (ret) {
5755 WARN_ON(1);
Chris Mason3a1abec2009-12-17 15:47:17 -05005756 goto stop_trans;
Yan Zhengd899e052008-10-30 14:25:28 -04005757 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00005758
5759 ret = btrfs_reserve_metadata_space(root, 3);
5760 if (ret) {
5761 btrfs_free_reserved_extent(root, ins.objectid,
5762 ins.offset);
Chris Mason3a1abec2009-12-17 15:47:17 -05005763 goto stop_trans;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005764 }
5765
Yan Zhengd899e052008-10-30 14:25:28 -04005766 ret = insert_reserved_file_extent(trans, inode,
5767 cur_offset, ins.objectid,
5768 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00005769 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04005770 BTRFS_FILE_EXTENT_PREALLOC);
5771 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04005772 btrfs_drop_extent_cache(inode, cur_offset,
5773 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005774
Yan Zhengd899e052008-10-30 14:25:28 -04005775 num_bytes -= ins.offset;
5776 cur_offset += ins.offset;
5777 alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005778
Yan Zhengd899e052008-10-30 14:25:28 -04005779 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005780 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04005781 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Aneesh Kumar K.V23b5c502010-02-04 11:33:03 -05005782 (actual_len > inode->i_size) &&
5783 (cur_offset > inode->i_size)) {
5784
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005785 if (cur_offset > actual_len)
5786 i_size = actual_len;
5787 else
5788 i_size = cur_offset;
5789 i_size_write(inode, i_size);
5790 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005791 }
5792
Yan Zhengd899e052008-10-30 14:25:28 -04005793 ret = btrfs_update_inode(trans, root, inode);
5794 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04005795
Yan, Zheng5a303d52009-11-12 09:34:52 +00005796 btrfs_end_transaction(trans, root);
5797 btrfs_unreserve_metadata_space(root, 3);
5798 }
Yan Zhengd899e052008-10-30 14:25:28 -04005799 return ret;
Chris Mason3a1abec2009-12-17 15:47:17 -05005800
5801stop_trans:
5802 btrfs_end_transaction(trans, root);
5803 return ret;
5804
Yan Zhengd899e052008-10-30 14:25:28 -04005805}
5806
5807static long btrfs_fallocate(struct inode *inode, int mode,
5808 loff_t offset, loff_t len)
5809{
Josef Bacik2ac55d42010-02-03 19:33:23 +00005810 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04005811 u64 cur_offset;
5812 u64 last_byte;
5813 u64 alloc_start;
5814 u64 alloc_end;
5815 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04005816 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04005817 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5818 struct extent_map *em;
5819 int ret;
5820
5821 alloc_start = offset & ~mask;
5822 alloc_end = (offset + len + mask) & ~mask;
5823
Chris Mason546888d2009-04-21 11:53:38 -04005824 /*
5825 * wait for ordered IO before we have any locks. We'll loop again
5826 * below with the locks held.
5827 */
5828 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5829
Yan Zhengd899e052008-10-30 14:25:28 -04005830 mutex_lock(&inode->i_mutex);
5831 if (alloc_start > inode->i_size) {
5832 ret = btrfs_cont_expand(inode, alloc_start);
5833 if (ret)
5834 goto out;
5835 }
5836
Yan, Zheng5a303d52009-11-12 09:34:52 +00005837 ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode,
Josef Bacika970b0a2009-06-27 21:07:34 -04005838 alloc_end - alloc_start);
5839 if (ret)
5840 goto out;
5841
Chris Masone980b502009-04-24 14:39:24 -04005842 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04005843 while (1) {
5844 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04005845
Chris Mason546888d2009-04-21 11:53:38 -04005846 /* the extent lock is ordered inside the running
5847 * transaction
5848 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00005849 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
5850 locked_end, 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04005851 ordered = btrfs_lookup_first_ordered_extent(inode,
5852 alloc_end - 1);
5853 if (ordered &&
5854 ordered->file_offset + ordered->len > alloc_start &&
5855 ordered->file_offset < alloc_end) {
5856 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005857 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
5858 alloc_start, locked_end,
5859 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005860 /*
5861 * we can't wait on the range with the transaction
5862 * running or with the extent lock held
5863 */
Yan Zhengd899e052008-10-30 14:25:28 -04005864 btrfs_wait_ordered_range(inode, alloc_start,
5865 alloc_end - alloc_start);
5866 } else {
5867 if (ordered)
5868 btrfs_put_ordered_extent(ordered);
5869 break;
5870 }
5871 }
5872
5873 cur_offset = alloc_start;
5874 while (1) {
5875 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5876 alloc_end - cur_offset, 0);
5877 BUG_ON(IS_ERR(em) || !em);
5878 last_byte = min(extent_map_end(em), alloc_end);
5879 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005880 if (em->block_start == EXTENT_MAP_HOLE ||
5881 (cur_offset >= inode->i_size &&
5882 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5883 ret = prealloc_file_range(inode,
5884 cur_offset, last_byte,
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005885 alloc_hint, mode, offset+len);
Yan Zhengd899e052008-10-30 14:25:28 -04005886 if (ret < 0) {
5887 free_extent_map(em);
5888 break;
5889 }
5890 }
5891 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5892 alloc_hint = em->block_start;
5893 free_extent_map(em);
5894
5895 cur_offset = last_byte;
5896 if (cur_offset >= alloc_end) {
5897 ret = 0;
5898 break;
5899 }
5900 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00005901 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5902 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005903
Yan, Zheng5a303d52009-11-12 09:34:52 +00005904 btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode,
5905 alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04005906out:
5907 mutex_unlock(&inode->i_mutex);
5908 return ret;
5909}
5910
Chris Masone6dcd2d2008-07-17 12:53:50 -04005911static int btrfs_set_page_dirty(struct page *page)
5912{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005913 return __set_page_dirty_nobuffers(page);
5914}
5915
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005916static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05005917{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005918 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05005919 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04005920 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05005921}
Chris Mason39279cc2007-06-12 06:35:45 -04005922
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005923static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05005924 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005925 .lookup = btrfs_lookup,
5926 .create = btrfs_create,
5927 .unlink = btrfs_unlink,
5928 .link = btrfs_link,
5929 .mkdir = btrfs_mkdir,
5930 .rmdir = btrfs_rmdir,
5931 .rename = btrfs_rename,
5932 .symlink = btrfs_symlink,
5933 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005934 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005935 .setxattr = btrfs_setxattr,
5936 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005937 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005938 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005939 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005940};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005941static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005942 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05005943 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005944};
Yan, Zheng76dda932009-09-21 16:00:26 -04005945
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005946static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005947 .llseek = generic_file_llseek,
5948 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01005949 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005950 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005951#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005952 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005953#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04005954 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04005955 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04005956};
5957
Chris Masond1310b22008-01-24 16:13:08 -05005958static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04005959 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05005960 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04005961 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005962 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005963 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04005964 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04005965 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05005966 .set_bit_hook = btrfs_set_bit_hook,
5967 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005968 .merge_extent_hook = btrfs_merge_extent_hook,
5969 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005970};
5971
Chris Mason35054392009-01-21 13:11:13 -05005972/*
5973 * btrfs doesn't support the bmap operation because swapfiles
5974 * use bmap to make a mapping of extents in the file. They assume
5975 * these extents won't change over the life of the file and they
5976 * use the bmap result to do IO directly to the drive.
5977 *
5978 * the btrfs bmap call would return logical addresses that aren't
5979 * suitable for IO and they also will change frequently as COW
5980 * operations happen. So, swapfile + btrfs == corruption.
5981 *
5982 * For now we're avoiding this by dropping bmap.
5983 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07005984static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04005985 .readpage = btrfs_readpage,
5986 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04005987 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05005988 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005989 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04005990 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005991 .invalidatepage = btrfs_invalidatepage,
5992 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005993 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02005994 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04005995};
5996
Alexey Dobriyan7f094102009-09-21 17:01:10 -07005997static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04005998 .readpage = btrfs_readpage,
5999 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04006000 .invalidatepage = btrfs_invalidatepage,
6001 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04006002};
6003
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006004static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006005 .truncate = btrfs_truncate,
6006 .getattr = btrfs_getattr,
6007 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006008 .setxattr = btrfs_setxattr,
6009 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05006010 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006011 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05006012 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04006013 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006014 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04006015};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006016static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04006017 .getattr = btrfs_getattr,
6018 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05006019 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006020 .setxattr = btrfs_setxattr,
6021 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04006022 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006023 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04006024};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006025static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006026 .readlink = generic_readlink,
6027 .follow_link = page_follow_link_light,
6028 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05006029 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05006030 .setxattr = btrfs_setxattr,
6031 .getxattr = btrfs_getxattr,
6032 .listxattr = btrfs_listxattr,
6033 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04006034};
Yan, Zheng76dda932009-09-21 16:00:26 -04006035
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04006036const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04006037 .d_delete = btrfs_dentry_delete,
6038};