blob: 3657925c2461e1f4d26ac1809df366db6b72b5a7 [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 Bacikf5a84ee32009-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 Bacikf5a84ee32009-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,
Chris Masond3977122009-01-05 21:25:51 -0500576 async_extent->start +
577 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500578
579 /* allocate blocks */
Josef Bacikf5a84ee32009-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 Bacikf5a84ee32009-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 Bacikf5a84ee32009-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
Chris Masonc8b97812008-10-29 14:49:59 -0400799 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
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{
1230 struct btrfs_root *root = BTRFS_I(inode)->root;
1231 u64 size;
1232
1233 if (!(orig->state & EXTENT_DELALLOC))
1234 return 0;
1235
1236 size = orig->end - orig->start + 1;
1237 if (size > root->fs_info->max_extent) {
1238 u64 num_extents;
1239 u64 new_size;
1240
1241 new_size = orig->end - split + 1;
1242 num_extents = div64_u64(size + root->fs_info->max_extent - 1,
1243 root->fs_info->max_extent);
1244
1245 /*
Josef Bacik32c00af2009-10-08 13:34:05 -04001246 * if we break a large extent up then leave oustanding_extents
1247 * be, since we've already accounted for the large extent.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001248 */
1249 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1250 root->fs_info->max_extent) < num_extents)
1251 return 0;
1252 }
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
1261/*
1262 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1263 * extents so we can keep track of new extents that are just merged onto old
1264 * extents, such as when we are doing sequential writes, so we can properly
1265 * account for the metadata space we'll need.
1266 */
1267static int btrfs_merge_extent_hook(struct inode *inode,
1268 struct extent_state *new,
1269 struct extent_state *other)
1270{
1271 struct btrfs_root *root = BTRFS_I(inode)->root;
1272 u64 new_size, old_size;
1273 u64 num_extents;
1274
1275 /* not delalloc, ignore it */
1276 if (!(other->state & EXTENT_DELALLOC))
1277 return 0;
1278
1279 old_size = other->end - other->start + 1;
1280 if (new->start < other->start)
1281 new_size = other->end - new->start + 1;
1282 else
1283 new_size = new->end - other->start + 1;
1284
1285 /* we're not bigger than the max, unreserve the space and go */
1286 if (new_size <= root->fs_info->max_extent) {
Josef Bacik32c00af2009-10-08 13:34:05 -04001287 spin_lock(&BTRFS_I(inode)->accounting_lock);
1288 BTRFS_I(inode)->outstanding_extents--;
1289 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001290 return 0;
1291 }
1292
1293 /*
1294 * If we grew by another max_extent, just return, we want to keep that
1295 * reserved amount.
1296 */
1297 num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
1298 root->fs_info->max_extent);
1299 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1300 root->fs_info->max_extent) > num_extents)
1301 return 0;
1302
Josef Bacik32c00af2009-10-08 13:34:05 -04001303 spin_lock(&BTRFS_I(inode)->accounting_lock);
1304 BTRFS_I(inode)->outstanding_extents--;
1305 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001306
1307 return 0;
1308}
1309
Chris Masond352ac62008-09-29 15:18:18 -04001310/*
1311 * extent_io.c set_bit_hook, used to track delayed allocation
1312 * bytes in this file, and to maintain the list of inodes that
1313 * have pending delalloc work to be done.
1314 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001315static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001316 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001317{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001318
Chris Mason75eff682008-12-15 15:54:40 -05001319 /*
1320 * set_bit and clear bit hooks normally require _irqsave/restore
1321 * but in this case, we are only testeing for the DELALLOC
1322 * bit, which is only set or cleared with irqs on
1323 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001324 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001325 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001326
Josef Bacik32c00af2009-10-08 13:34:05 -04001327 spin_lock(&BTRFS_I(inode)->accounting_lock);
1328 BTRFS_I(inode)->outstanding_extents++;
1329 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik6a632092009-02-20 11:00:09 -05001330 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
Chris Mason75eff682008-12-15 15:54:40 -05001331 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001332 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001333 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001334 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1335 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1336 &root->fs_info->delalloc_inodes);
1337 }
Chris Mason75eff682008-12-15 15:54:40 -05001338 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001339 }
1340 return 0;
1341}
1342
Chris Masond352ac62008-09-29 15:18:18 -04001343/*
1344 * extent_io.c clear_bit_hook, see set_bit_hook for why
1345 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001346static int btrfs_clear_bit_hook(struct inode *inode,
1347 struct extent_state *state, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001348{
Chris Mason75eff682008-12-15 15:54:40 -05001349 /*
1350 * set_bit and clear bit hooks normally require _irqsave/restore
1351 * but in this case, we are only testeing for the DELALLOC
1352 * bit, which is only set or cleared with irqs on
1353 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001354 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001355 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001356
Josef Bacik32c00af2009-10-08 13:34:05 -04001357 if (bits & EXTENT_DO_ACCOUNTING) {
1358 spin_lock(&BTRFS_I(inode)->accounting_lock);
1359 BTRFS_I(inode)->outstanding_extents--;
1360 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1361 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1362 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04001363
Chris Mason75eff682008-12-15 15:54:40 -05001364 spin_lock(&root->fs_info->delalloc_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001365 if (state->end - state->start + 1 >
1366 root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001367 printk(KERN_INFO "btrfs warning: delalloc account "
1368 "%llu %llu\n",
Josef Bacik9ed74f22009-09-11 16:12:44 -04001369 (unsigned long long)
1370 state->end - state->start + 1,
Chris Masond3977122009-01-05 21:25:51 -05001371 (unsigned long long)
1372 root->fs_info->delalloc_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001373 btrfs_delalloc_free_space(root, inode, (u64)-1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001374 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001375 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001376 } else {
Josef Bacik6a632092009-02-20 11:00:09 -05001377 btrfs_delalloc_free_space(root, inode,
Josef Bacik9ed74f22009-09-11 16:12:44 -04001378 state->end -
1379 state->start + 1);
1380 root->fs_info->delalloc_bytes -= state->end -
1381 state->start + 1;
1382 BTRFS_I(inode)->delalloc_bytes -= state->end -
1383 state->start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001384 }
Chris Masonea8c2812008-08-04 23:17:27 -04001385 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1386 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1387 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1388 }
Chris Mason75eff682008-12-15 15:54:40 -05001389 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001390 }
1391 return 0;
1392}
1393
Chris Masond352ac62008-09-29 15:18:18 -04001394/*
1395 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1396 * we don't create bios that span stripes or chunks
1397 */
Chris Mason239b14b2008-03-24 15:02:07 -04001398int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001399 size_t size, struct bio *bio,
1400 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001401{
1402 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1403 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001404 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001405 u64 length = 0;
1406 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001407 int ret;
1408
Chris Mason771ed682008-11-06 22:02:51 -05001409 if (bio_flags & EXTENT_BIO_COMPRESSED)
1410 return 0;
1411
Chris Masonf2d8d742008-04-21 10:03:05 -04001412 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001413 map_tree = &root->fs_info->mapping_tree;
1414 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001415 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001416 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001417
Chris Masond3977122009-01-05 21:25:51 -05001418 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001419 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001420 return 0;
1421}
1422
Chris Masond352ac62008-09-29 15:18:18 -04001423/*
1424 * in order to insert checksums into the metadata in large chunks,
1425 * we wait until bio submission time. All the pages in the bio are
1426 * checksummed and sums are attached onto the ordered extent record.
1427 *
1428 * At IO completion time the cums attached on the ordered extent record
1429 * are inserted into the btree
1430 */
Chris Masond3977122009-01-05 21:25:51 -05001431static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1432 struct bio *bio, int mirror_num,
1433 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001434{
Chris Mason065631f2008-02-20 12:07:25 -05001435 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001436 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001437
Chris Masond20f7042008-12-08 16:58:54 -05001438 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001439 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001440 return 0;
1441}
Chris Masone0156402008-04-16 11:15:20 -04001442
Chris Mason4a69a412008-11-06 22:03:00 -05001443/*
1444 * in order to insert checksums into the metadata in large chunks,
1445 * we wait until bio submission time. All the pages in the bio are
1446 * checksummed and sums are attached onto the ordered extent record.
1447 *
1448 * At IO completion time the cums attached on the ordered extent record
1449 * are inserted into the btree
1450 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001451static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001452 int mirror_num, unsigned long bio_flags)
1453{
1454 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001455 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001456}
1457
Chris Masond352ac62008-09-29 15:18:18 -04001458/*
Chris Masoncad321a2008-12-17 14:51:42 -05001459 * extent_io.c submission hook. This does the right thing for csum calculation
1460 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001461 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001462static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001463 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001464{
1465 struct btrfs_root *root = BTRFS_I(inode)->root;
1466 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001467 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001468
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001469 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001470
Chris Masone6dcd2d2008-07-17 12:53:50 -04001471 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1472 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001473
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001474 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001475 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001476 return btrfs_submit_compressed_read(inode, bio,
1477 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001478 } else if (!skip_sum)
1479 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001480 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001481 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001482 /* csum items have already been cloned */
1483 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1484 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001485 /* we're doing a write, do the async checksumming */
1486 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001487 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001488 bio_flags, __btrfs_submit_bio_start,
1489 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001490 }
1491
Chris Mason0b86a832008-03-24 15:01:56 -04001492mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001493 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001494}
Chris Mason6885f302008-02-20 16:11:05 -05001495
Chris Masond352ac62008-09-29 15:18:18 -04001496/*
1497 * given a list of ordered sums record them in the inode. This happens
1498 * at IO completion time based on sums calculated at bio submission time.
1499 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001500static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001501 struct inode *inode, u64 file_offset,
1502 struct list_head *list)
1503{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001504 struct btrfs_ordered_sum *sum;
1505
1506 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001507
1508 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001509 btrfs_csum_file_blocks(trans,
1510 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001511 }
1512 return 0;
1513}
1514
Chris Masonea8c2812008-08-04 23:17:27 -04001515int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1516{
Chris Masond3977122009-01-05 21:25:51 -05001517 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001518 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001519 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1520 GFP_NOFS);
1521}
1522
Chris Masond352ac62008-09-29 15:18:18 -04001523/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001524struct btrfs_writepage_fixup {
1525 struct page *page;
1526 struct btrfs_work work;
1527};
1528
Christoph Hellwigb2950862008-12-02 09:54:17 -05001529static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001530{
1531 struct btrfs_writepage_fixup *fixup;
1532 struct btrfs_ordered_extent *ordered;
1533 struct page *page;
1534 struct inode *inode;
1535 u64 page_start;
1536 u64 page_end;
1537
1538 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1539 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001540again:
Chris Mason247e7432008-07-17 12:53:51 -04001541 lock_page(page);
1542 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1543 ClearPageChecked(page);
1544 goto out_page;
1545 }
1546
1547 inode = page->mapping->host;
1548 page_start = page_offset(page);
1549 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1550
1551 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001552
1553 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001554 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001555 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001556
1557 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1558 if (ordered) {
1559 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1560 page_end, GFP_NOFS);
1561 unlock_page(page);
1562 btrfs_start_ordered_extent(inode, ordered, 1);
1563 goto again;
1564 }
Chris Mason247e7432008-07-17 12:53:51 -04001565
Chris Masonea8c2812008-08-04 23:17:27 -04001566 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001567 ClearPageChecked(page);
1568out:
1569 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1570out_page:
1571 unlock_page(page);
1572 page_cache_release(page);
1573}
1574
1575/*
1576 * There are a few paths in the higher layers of the kernel that directly
1577 * set the page dirty bit without asking the filesystem if it is a
1578 * good idea. This causes problems because we want to make sure COW
1579 * properly happens and the data=ordered rules are followed.
1580 *
Chris Masonc8b97812008-10-29 14:49:59 -04001581 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001582 * hasn't been properly setup for IO. We kick off an async process
1583 * to fix it up. The async helper will wait for ordered extents, set
1584 * the delalloc bit and make it safe to write the page.
1585 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001586static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001587{
1588 struct inode *inode = page->mapping->host;
1589 struct btrfs_writepage_fixup *fixup;
1590 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001591
Chris Mason8b62b722009-09-02 16:53:46 -04001592 /* this page is properly in the ordered list */
1593 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001594 return 0;
1595
1596 if (PageChecked(page))
1597 return -EAGAIN;
1598
1599 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1600 if (!fixup)
1601 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001602
Chris Mason247e7432008-07-17 12:53:51 -04001603 SetPageChecked(page);
1604 page_cache_get(page);
1605 fixup->work.func = btrfs_writepage_fixup_worker;
1606 fixup->page = page;
1607 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1608 return -EAGAIN;
1609}
1610
Yan Zhengd899e052008-10-30 14:25:28 -04001611static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1612 struct inode *inode, u64 file_pos,
1613 u64 disk_bytenr, u64 disk_num_bytes,
1614 u64 num_bytes, u64 ram_bytes,
1615 u8 compression, u8 encryption,
1616 u16 other_encoding, int extent_type)
1617{
1618 struct btrfs_root *root = BTRFS_I(inode)->root;
1619 struct btrfs_file_extent_item *fi;
1620 struct btrfs_path *path;
1621 struct extent_buffer *leaf;
1622 struct btrfs_key ins;
1623 u64 hint;
1624 int ret;
1625
1626 path = btrfs_alloc_path();
1627 BUG_ON(!path);
1628
Chris Masonb9473432009-03-13 11:00:37 -04001629 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001630
1631 /*
1632 * we may be replacing one extent in the tree with another.
1633 * The new extent is pinned in the extent map, and we don't want
1634 * to drop it from the cache until it is completely in the btree.
1635 *
1636 * So, tell btrfs_drop_extents to leave this extent in the cache.
1637 * the caller is expected to unpin it and allow it to be merged
1638 * with the others.
1639 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001640 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1641 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001642 BUG_ON(ret);
1643
1644 ins.objectid = inode->i_ino;
1645 ins.offset = file_pos;
1646 ins.type = BTRFS_EXTENT_DATA_KEY;
1647 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1648 BUG_ON(ret);
1649 leaf = path->nodes[0];
1650 fi = btrfs_item_ptr(leaf, path->slots[0],
1651 struct btrfs_file_extent_item);
1652 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1653 btrfs_set_file_extent_type(leaf, fi, extent_type);
1654 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1655 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1656 btrfs_set_file_extent_offset(leaf, fi, 0);
1657 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1658 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1659 btrfs_set_file_extent_compression(leaf, fi, compression);
1660 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1661 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001662
1663 btrfs_unlock_up_safe(path, 1);
1664 btrfs_set_lock_blocking(leaf);
1665
Yan Zhengd899e052008-10-30 14:25:28 -04001666 btrfs_mark_buffer_dirty(leaf);
1667
1668 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001669
1670 ins.objectid = disk_bytenr;
1671 ins.offset = disk_num_bytes;
1672 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001673 ret = btrfs_alloc_reserved_file_extent(trans, root,
1674 root->root_key.objectid,
1675 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001676 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001677 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001678
Yan Zhengd899e052008-10-30 14:25:28 -04001679 return 0;
1680}
1681
Chris Mason5d13a982009-03-13 11:41:46 -04001682/*
1683 * helper function for btrfs_finish_ordered_io, this
1684 * just reads in some of the csum leaves to prime them into ram
1685 * before we start the transaction. It limits the amount of btree
1686 * reads required while inside the transaction.
1687 */
Chris Masond352ac62008-09-29 15:18:18 -04001688/* as ordered data IO finishes, this gets called so we can finish
1689 * an ordered extent if the range of bytes in the file it covers are
1690 * fully written.
1691 */
Chris Mason211f90e2008-07-18 11:56:15 -04001692static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001693{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001694 struct btrfs_root *root = BTRFS_I(inode)->root;
1695 struct btrfs_trans_handle *trans;
Chris Mason5d13a982009-03-13 11:41:46 -04001696 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001697 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan Zhengd899e052008-10-30 14:25:28 -04001698 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001699 int ret;
1700
1701 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001702 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001703 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001704
Josef Bacikefd049f2010-02-02 20:50:10 +00001705 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001706 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001707
Yan, Zhengc2167752009-11-12 09:34:21 +00001708 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1709 BUG_ON(!list_empty(&ordered_extent->list));
1710 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1711 if (!ret) {
1712 trans = btrfs_join_transaction(root, 1);
1713 ret = btrfs_update_inode(trans, root, inode);
1714 BUG_ON(ret);
1715 btrfs_end_transaction(trans, root);
1716 }
1717 goto out;
1718 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001719
1720 lock_extent(io_tree, ordered_extent->file_offset,
1721 ordered_extent->file_offset + ordered_extent->len - 1,
1722 GFP_NOFS);
1723
Yan, Zhengc2167752009-11-12 09:34:21 +00001724 trans = btrfs_join_transaction(root, 1);
1725
Chris Masonc8b97812008-10-29 14:49:59 -04001726 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001727 compressed = 1;
1728 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1729 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001730 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001731 ordered_extent->file_offset,
1732 ordered_extent->file_offset +
1733 ordered_extent->len);
1734 BUG_ON(ret);
1735 } else {
1736 ret = insert_reserved_file_extent(trans, inode,
1737 ordered_extent->file_offset,
1738 ordered_extent->start,
1739 ordered_extent->disk_len,
1740 ordered_extent->len,
1741 ordered_extent->len,
1742 compressed, 0, 0,
1743 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001744 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1745 ordered_extent->file_offset,
1746 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001747 BUG_ON(ret);
1748 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001749 unlock_extent(io_tree, ordered_extent->file_offset,
1750 ordered_extent->file_offset + ordered_extent->len - 1,
1751 GFP_NOFS);
1752 add_pending_csums(trans, inode, ordered_extent->file_offset,
1753 &ordered_extent->list);
1754
Yan, Zhengc2167752009-11-12 09:34:21 +00001755 /* this also removes the ordered extent from the tree */
1756 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1757 ret = btrfs_update_inode(trans, root, inode);
1758 BUG_ON(ret);
1759 btrfs_end_transaction(trans, root);
1760out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001761 /* once for us */
1762 btrfs_put_ordered_extent(ordered_extent);
1763 /* once for the tree */
1764 btrfs_put_ordered_extent(ordered_extent);
1765
Chris Masone6dcd2d2008-07-17 12:53:50 -04001766 return 0;
1767}
1768
Christoph Hellwigb2950862008-12-02 09:54:17 -05001769static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001770 struct extent_state *state, int uptodate)
1771{
Chris Mason8b62b722009-09-02 16:53:46 -04001772 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001773 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1774}
1775
Chris Masond352ac62008-09-29 15:18:18 -04001776/*
1777 * When IO fails, either with EIO or csum verification fails, we
1778 * try other mirrors that might have a good copy of the data. This
1779 * io_failure_record is used to record state as we go through all the
1780 * mirrors. If another mirror has good data, the page is set up to date
1781 * and things continue. If a good mirror can't be found, the original
1782 * bio end_io callback is called to indicate things have failed.
1783 */
Chris Mason7e383262008-04-09 16:28:12 -04001784struct io_failure_record {
1785 struct page *page;
1786 u64 start;
1787 u64 len;
1788 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001789 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001790 int last_mirror;
1791};
1792
Christoph Hellwigb2950862008-12-02 09:54:17 -05001793static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001794 struct page *page, u64 start, u64 end,
1795 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001796{
1797 struct io_failure_record *failrec = NULL;
1798 u64 private;
1799 struct extent_map *em;
1800 struct inode *inode = page->mapping->host;
1801 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001802 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001803 struct bio *bio;
1804 int num_copies;
1805 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001806 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001807 u64 logical;
1808
1809 ret = get_state_private(failure_tree, start, &private);
1810 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001811 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1812 if (!failrec)
1813 return -ENOMEM;
1814 failrec->start = start;
1815 failrec->len = end - start + 1;
1816 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001817 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001818
Chris Mason890871b2009-09-02 16:24:52 -04001819 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001820 em = lookup_extent_mapping(em_tree, start, failrec->len);
1821 if (em->start > start || em->start + em->len < start) {
1822 free_extent_map(em);
1823 em = NULL;
1824 }
Chris Mason890871b2009-09-02 16:24:52 -04001825 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001826
1827 if (!em || IS_ERR(em)) {
1828 kfree(failrec);
1829 return -EIO;
1830 }
1831 logical = start - em->start;
1832 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001833 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1834 logical = em->block_start;
1835 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1836 }
Chris Mason7e383262008-04-09 16:28:12 -04001837 failrec->logical = logical;
1838 free_extent_map(em);
1839 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1840 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001841 set_state_private(failure_tree, start,
1842 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001843 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001844 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001845 }
1846 num_copies = btrfs_num_copies(
1847 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1848 failrec->logical, failrec->len);
1849 failrec->last_mirror++;
1850 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001851 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001852 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1853 failrec->start,
1854 EXTENT_LOCKED);
1855 if (state && state->start != failrec->start)
1856 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001857 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001858 }
1859 if (!state || failrec->last_mirror > num_copies) {
1860 set_state_private(failure_tree, failrec->start, 0);
1861 clear_extent_bits(failure_tree, failrec->start,
1862 failrec->start + failrec->len - 1,
1863 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1864 kfree(failrec);
1865 return -EIO;
1866 }
1867 bio = bio_alloc(GFP_NOFS, 1);
1868 bio->bi_private = state;
1869 bio->bi_end_io = failed_bio->bi_end_io;
1870 bio->bi_sector = failrec->logical >> 9;
1871 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001872 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001873
Chris Mason7e383262008-04-09 16:28:12 -04001874 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001875 if (failed_bio->bi_rw & (1 << BIO_RW))
1876 rw = WRITE;
1877 else
1878 rw = READ;
1879
1880 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001881 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001882 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001883 return 0;
1884}
1885
Chris Masond352ac62008-09-29 15:18:18 -04001886/*
1887 * each time an IO finishes, we do a fast check in the IO failure tree
1888 * to see if we need to process or clean up an io_failure_record
1889 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001890static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001891{
1892 u64 private;
1893 u64 private_failure;
1894 struct io_failure_record *failure;
1895 int ret;
1896
1897 private = 0;
1898 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1899 (u64)-1, 1, EXTENT_DIRTY)) {
1900 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1901 start, &private_failure);
1902 if (ret == 0) {
1903 failure = (struct io_failure_record *)(unsigned long)
1904 private_failure;
1905 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1906 failure->start, 0);
1907 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1908 failure->start,
1909 failure->start + failure->len - 1,
1910 EXTENT_DIRTY | EXTENT_LOCKED,
1911 GFP_NOFS);
1912 kfree(failure);
1913 }
1914 }
Chris Mason7e383262008-04-09 16:28:12 -04001915 return 0;
1916}
1917
Chris Masond352ac62008-09-29 15:18:18 -04001918/*
1919 * when reads are done, we need to check csums to verify the data is correct
1920 * if there's a match, we allow the bio to finish. If not, we go through
1921 * the io_failure_record routines to find good copies
1922 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001923static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001924 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001925{
Chris Mason35ebb932007-10-30 16:56:53 -04001926 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001927 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001928 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001929 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001930 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001931 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001932 struct btrfs_root *root = BTRFS_I(inode)->root;
1933 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001934
Chris Masond20f7042008-12-08 16:58:54 -05001935 if (PageChecked(page)) {
1936 ClearPageChecked(page);
1937 goto good;
1938 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001939
1940 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001941 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001942
Yan Zheng17d217f2008-12-12 10:03:38 -05001943 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001944 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001945 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1946 GFP_NOFS);
1947 return 0;
1948 }
1949
Yanc2e639f2008-02-04 08:57:25 -05001950 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001951 private = state->private;
1952 ret = 0;
1953 } else {
1954 ret = get_state_private(io_tree, start, &private);
1955 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001956 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001957 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001958 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001959
Chris Masonff79f812007-10-15 16:22:25 -04001960 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1961 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001962 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001963 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001964
Chris Mason9ab86c82009-01-07 09:48:51 -05001965 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001966good:
Chris Mason7e383262008-04-09 16:28:12 -04001967 /* if the io failure tree for this inode is non-empty,
1968 * check to see if we've recovered from a failed IO
1969 */
Chris Mason1259ab72008-05-12 13:39:03 -04001970 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001971 return 0;
1972
1973zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001974 if (printk_ratelimit()) {
1975 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1976 "private %llu\n", page->mapping->host->i_ino,
1977 (unsigned long long)start, csum,
1978 (unsigned long long)private);
1979 }
Chris Masondb945352007-10-15 16:15:53 -04001980 memset(kaddr + offset, 1, end - start + 1);
1981 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001982 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001983 if (private == 0)
1984 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001985 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001986}
Chris Masonb888db22007-08-27 16:49:44 -04001987
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001988struct delayed_iput {
1989 struct list_head list;
1990 struct inode *inode;
1991};
1992
1993void btrfs_add_delayed_iput(struct inode *inode)
1994{
1995 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1996 struct delayed_iput *delayed;
1997
1998 if (atomic_add_unless(&inode->i_count, -1, 1))
1999 return;
2000
2001 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2002 delayed->inode = inode;
2003
2004 spin_lock(&fs_info->delayed_iput_lock);
2005 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2006 spin_unlock(&fs_info->delayed_iput_lock);
2007}
2008
2009void btrfs_run_delayed_iputs(struct btrfs_root *root)
2010{
2011 LIST_HEAD(list);
2012 struct btrfs_fs_info *fs_info = root->fs_info;
2013 struct delayed_iput *delayed;
2014 int empty;
2015
2016 spin_lock(&fs_info->delayed_iput_lock);
2017 empty = list_empty(&fs_info->delayed_iputs);
2018 spin_unlock(&fs_info->delayed_iput_lock);
2019 if (empty)
2020 return;
2021
2022 down_read(&root->fs_info->cleanup_work_sem);
2023 spin_lock(&fs_info->delayed_iput_lock);
2024 list_splice_init(&fs_info->delayed_iputs, &list);
2025 spin_unlock(&fs_info->delayed_iput_lock);
2026
2027 while (!list_empty(&list)) {
2028 delayed = list_entry(list.next, struct delayed_iput, list);
2029 list_del(&delayed->list);
2030 iput(delayed->inode);
2031 kfree(delayed);
2032 }
2033 up_read(&root->fs_info->cleanup_work_sem);
2034}
2035
Josef Bacik7b128762008-07-24 12:17:14 -04002036/*
2037 * This creates an orphan entry for the given inode in case something goes
2038 * wrong in the middle of an unlink/truncate.
2039 */
2040int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2041{
2042 struct btrfs_root *root = BTRFS_I(inode)->root;
2043 int ret = 0;
2044
Yanbcc63ab2008-07-30 16:29:20 -04002045 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002046
2047 /* already on the orphan list, we're good */
2048 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002049 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002050 return 0;
2051 }
2052
2053 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2054
Yanbcc63ab2008-07-30 16:29:20 -04002055 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002056
2057 /*
2058 * insert an orphan item to track this unlinked/truncated file
2059 */
2060 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2061
2062 return ret;
2063}
2064
2065/*
2066 * We have done the truncate/delete so we can go ahead and remove the orphan
2067 * item for this particular inode.
2068 */
2069int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2070{
2071 struct btrfs_root *root = BTRFS_I(inode)->root;
2072 int ret = 0;
2073
Yanbcc63ab2008-07-30 16:29:20 -04002074 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002075
2076 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002077 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002078 return 0;
2079 }
2080
2081 list_del_init(&BTRFS_I(inode)->i_orphan);
2082 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04002083 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002084 return 0;
2085 }
2086
Yanbcc63ab2008-07-30 16:29:20 -04002087 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002088
2089 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2090
2091 return ret;
2092}
2093
2094/*
2095 * this cleans up any orphans that may be left on the list from the last use
2096 * of this root.
2097 */
2098void btrfs_orphan_cleanup(struct btrfs_root *root)
2099{
2100 struct btrfs_path *path;
2101 struct extent_buffer *leaf;
2102 struct btrfs_item *item;
2103 struct btrfs_key key, found_key;
2104 struct btrfs_trans_handle *trans;
2105 struct inode *inode;
2106 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2107
Yan, Zhengc71bf092009-11-12 09:34:40 +00002108 if (!xchg(&root->clean_orphans, 0))
Josef Bacik7b128762008-07-24 12:17:14 -04002109 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002110
2111 path = btrfs_alloc_path();
2112 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002113 path->reada = -1;
2114
2115 key.objectid = BTRFS_ORPHAN_OBJECTID;
2116 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2117 key.offset = (u64)-1;
2118
Josef Bacik7b128762008-07-24 12:17:14 -04002119 while (1) {
2120 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2121 if (ret < 0) {
2122 printk(KERN_ERR "Error searching slot for orphan: %d"
2123 "\n", ret);
2124 break;
2125 }
2126
2127 /*
2128 * if ret == 0 means we found what we were searching for, which
2129 * is weird, but possible, so only screw with path if we didnt
2130 * find the key and see if we have stuff that matches
2131 */
2132 if (ret > 0) {
2133 if (path->slots[0] == 0)
2134 break;
2135 path->slots[0]--;
2136 }
2137
2138 /* pull out the item */
2139 leaf = path->nodes[0];
2140 item = btrfs_item_nr(leaf, path->slots[0]);
2141 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2142
2143 /* make sure the item matches what we want */
2144 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2145 break;
2146 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2147 break;
2148
2149 /* release the path since we're done with it */
2150 btrfs_release_path(root, path);
2151
2152 /*
2153 * this is where we are basically btrfs_lookup, without the
2154 * crossing root thing. we store the inode number in the
2155 * offset of the orphan item.
2156 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002157 found_key.objectid = found_key.offset;
2158 found_key.type = BTRFS_INODE_ITEM_KEY;
2159 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002160 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002161 if (IS_ERR(inode))
Josef Bacik7b128762008-07-24 12:17:14 -04002162 break;
2163
Josef Bacik7b128762008-07-24 12:17:14 -04002164 /*
2165 * add this inode to the orphan list so btrfs_orphan_del does
2166 * the proper thing when we hit it
2167 */
Yanbcc63ab2008-07-30 16:29:20 -04002168 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002169 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04002170 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002171
2172 /*
2173 * if this is a bad inode, means we actually succeeded in
2174 * removing the inode, but not the orphan record, which means
2175 * we need to manually delete the orphan since iput will just
2176 * do a destroy_inode
2177 */
2178 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002179 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04002180 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002181 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002182 iput(inode);
2183 continue;
2184 }
2185
2186 /* if we have links, this was a truncate, lets do that */
2187 if (inode->i_nlink) {
2188 nr_truncate++;
2189 btrfs_truncate(inode);
2190 } else {
2191 nr_unlink++;
2192 }
2193
2194 /* this will do delete_inode and everything for us */
2195 iput(inode);
2196 }
2197
2198 if (nr_unlink)
2199 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2200 if (nr_truncate)
2201 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2202
2203 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002204}
2205
Chris Masond352ac62008-09-29 15:18:18 -04002206/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002207 * very simple check to peek ahead in the leaf looking for xattrs. If we
2208 * don't find any xattrs, we know there can't be any acls.
2209 *
2210 * slot is the slot the inode is in, objectid is the objectid of the inode
2211 */
2212static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2213 int slot, u64 objectid)
2214{
2215 u32 nritems = btrfs_header_nritems(leaf);
2216 struct btrfs_key found_key;
2217 int scanned = 0;
2218
2219 slot++;
2220 while (slot < nritems) {
2221 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2222
2223 /* we found a different objectid, there must not be acls */
2224 if (found_key.objectid != objectid)
2225 return 0;
2226
2227 /* we found an xattr, assume we've got an acl */
2228 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2229 return 1;
2230
2231 /*
2232 * we found a key greater than an xattr key, there can't
2233 * be any acls later on
2234 */
2235 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2236 return 0;
2237
2238 slot++;
2239 scanned++;
2240
2241 /*
2242 * it goes inode, inode backrefs, xattrs, extents,
2243 * so if there are a ton of hard links to an inode there can
2244 * be a lot of backrefs. Don't waste time searching too hard,
2245 * this is just an optimization
2246 */
2247 if (scanned >= 8)
2248 break;
2249 }
2250 /* we hit the end of the leaf before we found an xattr or
2251 * something larger than an xattr. We have to assume the inode
2252 * has acls
2253 */
2254 return 1;
2255}
2256
2257/*
Chris Masond352ac62008-09-29 15:18:18 -04002258 * read an inode from the btree into the in-memory inode
2259 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002260static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002261{
2262 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002263 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002264 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002265 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002266 struct btrfs_root *root = BTRFS_I(inode)->root;
2267 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002268 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002269 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002270 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002271 int ret;
2272
2273 path = btrfs_alloc_path();
2274 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002275 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002276
Chris Mason39279cc2007-06-12 06:35:45 -04002277 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002278 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002279 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002280
Chris Mason5f39d392007-10-15 16:14:19 -04002281 leaf = path->nodes[0];
2282 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2283 struct btrfs_inode_item);
2284
2285 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2286 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2287 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2288 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002289 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002290
2291 tspec = btrfs_inode_atime(inode_item);
2292 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2293 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2294
2295 tspec = btrfs_inode_mtime(inode_item);
2296 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2297 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2298
2299 tspec = btrfs_inode_ctime(inode_item);
2300 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2301 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2302
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002303 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002304 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002305 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002306 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002307 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002308 rdev = btrfs_inode_rdev(leaf, inode_item);
2309
Josef Bacikaec74772008-07-24 12:12:38 -04002310 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002311 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002312
Chris Mason5f39d392007-10-15 16:14:19 -04002313 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002314
Chris Mason46a53cc2009-04-27 11:47:50 -04002315 /*
2316 * try to precache a NULL acl entry for files that don't have
2317 * any xattrs or acls
2318 */
2319 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002320 if (!maybe_acls)
2321 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002322
Yan Zhengd2fb3432008-12-11 16:30:39 -05002323 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2324 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002325 btrfs_free_path(path);
2326 inode_item = NULL;
2327
Chris Mason39279cc2007-06-12 06:35:45 -04002328 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002329 case S_IFREG:
2330 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002331 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002332 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002333 inode->i_fop = &btrfs_file_operations;
2334 inode->i_op = &btrfs_file_inode_operations;
2335 break;
2336 case S_IFDIR:
2337 inode->i_fop = &btrfs_dir_file_operations;
2338 if (root == root->fs_info->tree_root)
2339 inode->i_op = &btrfs_dir_ro_inode_operations;
2340 else
2341 inode->i_op = &btrfs_dir_inode_operations;
2342 break;
2343 case S_IFLNK:
2344 inode->i_op = &btrfs_symlink_inode_operations;
2345 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002346 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002347 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002348 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002349 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002350 init_special_inode(inode, inode->i_mode, rdev);
2351 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002352 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002353
2354 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002355 return;
2356
2357make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002358 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002359 make_bad_inode(inode);
2360}
2361
Chris Masond352ac62008-09-29 15:18:18 -04002362/*
2363 * given a leaf and an inode, copy the inode fields into the leaf
2364 */
Chris Masone02119d2008-09-05 16:13:11 -04002365static void fill_inode_item(struct btrfs_trans_handle *trans,
2366 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002367 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002368 struct inode *inode)
2369{
Chris Mason5f39d392007-10-15 16:14:19 -04002370 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2371 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002372 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002373 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2374 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2375
2376 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2377 inode->i_atime.tv_sec);
2378 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2379 inode->i_atime.tv_nsec);
2380
2381 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2382 inode->i_mtime.tv_sec);
2383 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2384 inode->i_mtime.tv_nsec);
2385
2386 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2387 inode->i_ctime.tv_sec);
2388 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2389 inode->i_ctime.tv_nsec);
2390
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002391 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002392 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002393 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002394 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002395 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002396 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002397 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002398}
2399
Chris Masond352ac62008-09-29 15:18:18 -04002400/*
2401 * copy everything in the in-memory inode into the btree.
2402 */
Chris Masond3977122009-01-05 21:25:51 -05002403noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2404 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002405{
2406 struct btrfs_inode_item *inode_item;
2407 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002408 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002409 int ret;
2410
2411 path = btrfs_alloc_path();
2412 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002413 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002414 ret = btrfs_lookup_inode(trans, root, path,
2415 &BTRFS_I(inode)->location, 1);
2416 if (ret) {
2417 if (ret > 0)
2418 ret = -ENOENT;
2419 goto failed;
2420 }
2421
Chris Masonb4ce94d2009-02-04 09:25:08 -05002422 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002423 leaf = path->nodes[0];
2424 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002425 struct btrfs_inode_item);
2426
Chris Masone02119d2008-09-05 16:13:11 -04002427 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002428 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002429 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002430 ret = 0;
2431failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002432 btrfs_free_path(path);
2433 return ret;
2434}
2435
2436
Chris Masond352ac62008-09-29 15:18:18 -04002437/*
2438 * unlink helper that gets used here in inode.c and in the tree logging
2439 * recovery code. It remove a link in a directory with a given name, and
2440 * also drops the back refs in the inode to the directory
2441 */
Chris Masone02119d2008-09-05 16:13:11 -04002442int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2443 struct btrfs_root *root,
2444 struct inode *dir, struct inode *inode,
2445 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002446{
2447 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002448 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002449 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002450 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002451 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002452 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002453
2454 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002455 if (!path) {
2456 ret = -ENOMEM;
2457 goto err;
2458 }
2459
Chris Masonb9473432009-03-13 11:00:37 -04002460 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002461 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2462 name, name_len, -1);
2463 if (IS_ERR(di)) {
2464 ret = PTR_ERR(di);
2465 goto err;
2466 }
2467 if (!di) {
2468 ret = -ENOENT;
2469 goto err;
2470 }
Chris Mason5f39d392007-10-15 16:14:19 -04002471 leaf = path->nodes[0];
2472 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002473 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002474 if (ret)
2475 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002476 btrfs_release_path(root, path);
2477
Josef Bacikaec74772008-07-24 12:12:38 -04002478 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002479 inode->i_ino,
2480 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002481 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002482 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002483 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002484 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002485 goto err;
2486 }
2487
Chris Mason39279cc2007-06-12 06:35:45 -04002488 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002489 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002490 if (IS_ERR(di)) {
2491 ret = PTR_ERR(di);
2492 goto err;
2493 }
2494 if (!di) {
2495 ret = -ENOENT;
2496 goto err;
2497 }
2498 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002499 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002500
Chris Masone02119d2008-09-05 16:13:11 -04002501 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2502 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002503 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002504
2505 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2506 dir, index);
2507 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002508err:
2509 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002510 if (ret)
2511 goto out;
2512
2513 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2514 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2515 btrfs_update_inode(trans, root, dir);
2516 btrfs_drop_nlink(inode);
2517 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002518out:
Chris Mason39279cc2007-06-12 06:35:45 -04002519 return ret;
2520}
2521
2522static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2523{
2524 struct btrfs_root *root;
2525 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002526 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002527 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002528 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002529
2530 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002531
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002532 /*
2533 * 5 items for unlink inode
2534 * 1 for orphan
2535 */
2536 ret = btrfs_reserve_metadata_space(root, 6);
2537 if (ret)
2538 return ret;
2539
Chris Mason39279cc2007-06-12 06:35:45 -04002540 trans = btrfs_start_transaction(root, 1);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002541 if (IS_ERR(trans)) {
2542 btrfs_unreserve_metadata_space(root, 6);
2543 return PTR_ERR(trans);
2544 }
Chris Mason5f39d392007-10-15 16:14:19 -04002545
Chris Mason39279cc2007-06-12 06:35:45 -04002546 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002547
2548 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2549
Chris Masone02119d2008-09-05 16:13:11 -04002550 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2551 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002552
2553 if (inode->i_nlink == 0)
2554 ret = btrfs_orphan_add(trans, inode);
2555
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002556 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002557
Chris Mason89ce8a62008-06-25 16:01:31 -04002558 btrfs_end_transaction_throttle(trans, root);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002559 btrfs_unreserve_metadata_space(root, 6);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002560 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002561 return ret;
2562}
2563
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002564int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2565 struct btrfs_root *root,
2566 struct inode *dir, u64 objectid,
2567 const char *name, int name_len)
2568{
2569 struct btrfs_path *path;
2570 struct extent_buffer *leaf;
2571 struct btrfs_dir_item *di;
2572 struct btrfs_key key;
2573 u64 index;
2574 int ret;
2575
2576 path = btrfs_alloc_path();
2577 if (!path)
2578 return -ENOMEM;
2579
2580 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2581 name, name_len, -1);
2582 BUG_ON(!di || IS_ERR(di));
2583
2584 leaf = path->nodes[0];
2585 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2586 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2587 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2588 BUG_ON(ret);
2589 btrfs_release_path(root, path);
2590
2591 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2592 objectid, root->root_key.objectid,
2593 dir->i_ino, &index, name, name_len);
2594 if (ret < 0) {
2595 BUG_ON(ret != -ENOENT);
2596 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2597 name, name_len);
2598 BUG_ON(!di || IS_ERR(di));
2599
2600 leaf = path->nodes[0];
2601 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2602 btrfs_release_path(root, path);
2603 index = key.offset;
2604 }
2605
2606 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2607 index, name, name_len, -1);
2608 BUG_ON(!di || IS_ERR(di));
2609
2610 leaf = path->nodes[0];
2611 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2612 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2613 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2614 BUG_ON(ret);
2615 btrfs_release_path(root, path);
2616
2617 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2618 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2619 ret = btrfs_update_inode(trans, root, dir);
2620 BUG_ON(ret);
2621 dir->i_sb->s_dirt = 1;
2622
2623 btrfs_free_path(path);
2624 return 0;
2625}
2626
Chris Mason39279cc2007-06-12 06:35:45 -04002627static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2628{
2629 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002630 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002631 int ret;
2632 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002633 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002634 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002635
Chris Mason3394e162008-11-17 20:42:26 -05002636 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002637 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002638 return -ENOTEMPTY;
2639
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002640 ret = btrfs_reserve_metadata_space(root, 5);
2641 if (ret)
2642 return ret;
2643
Chris Mason39279cc2007-06-12 06:35:45 -04002644 trans = btrfs_start_transaction(root, 1);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002645 if (IS_ERR(trans)) {
2646 btrfs_unreserve_metadata_space(root, 5);
2647 return PTR_ERR(trans);
2648 }
2649
Chris Mason39279cc2007-06-12 06:35:45 -04002650 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002651
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002652 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2653 err = btrfs_unlink_subvol(trans, root, dir,
2654 BTRFS_I(inode)->location.objectid,
2655 dentry->d_name.name,
2656 dentry->d_name.len);
2657 goto out;
2658 }
2659
Josef Bacik7b128762008-07-24 12:17:14 -04002660 err = btrfs_orphan_add(trans, inode);
2661 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002662 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002663
Chris Mason39279cc2007-06-12 06:35:45 -04002664 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002665 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2666 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002667 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002668 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002669out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002670 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002671 ret = btrfs_end_transaction_throttle(trans, root);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002672 btrfs_unreserve_metadata_space(root, 5);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002673 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002674
Chris Mason39279cc2007-06-12 06:35:45 -04002675 if (ret && !err)
2676 err = ret;
2677 return err;
2678}
2679
Chris Masond20f7042008-12-08 16:58:54 -05002680#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002681/*
Chris Mason323ac952008-10-01 19:05:46 -04002682 * when truncating bytes in a file, it is possible to avoid reading
2683 * the leaves that contain only checksum items. This can be the
2684 * majority of the IO required to delete a large file, but it must
2685 * be done carefully.
2686 *
2687 * The keys in the level just above the leaves are checked to make sure
2688 * the lowest key in a given leaf is a csum key, and starts at an offset
2689 * after the new size.
2690 *
2691 * Then the key for the next leaf is checked to make sure it also has
2692 * a checksum item for the same file. If it does, we know our target leaf
2693 * contains only checksum items, and it can be safely freed without reading
2694 * it.
2695 *
2696 * This is just an optimization targeted at large files. It may do
2697 * nothing. It will return 0 unless things went badly.
2698 */
2699static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2700 struct btrfs_root *root,
2701 struct btrfs_path *path,
2702 struct inode *inode, u64 new_size)
2703{
2704 struct btrfs_key key;
2705 int ret;
2706 int nritems;
2707 struct btrfs_key found_key;
2708 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002709 struct btrfs_leaf_ref *ref;
2710 u64 leaf_gen;
2711 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002712
2713 path->lowest_level = 1;
2714 key.objectid = inode->i_ino;
2715 key.type = BTRFS_CSUM_ITEM_KEY;
2716 key.offset = new_size;
2717again:
2718 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2719 if (ret < 0)
2720 goto out;
2721
2722 if (path->nodes[1] == NULL) {
2723 ret = 0;
2724 goto out;
2725 }
2726 ret = 0;
2727 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2728 nritems = btrfs_header_nritems(path->nodes[1]);
2729
2730 if (!nritems)
2731 goto out;
2732
2733 if (path->slots[1] >= nritems)
2734 goto next_node;
2735
2736 /* did we find a key greater than anything we want to delete? */
2737 if (found_key.objectid > inode->i_ino ||
2738 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2739 goto out;
2740
2741 /* we check the next key in the node to make sure the leave contains
2742 * only checksum items. This comparison doesn't work if our
2743 * leaf is the last one in the node
2744 */
2745 if (path->slots[1] + 1 >= nritems) {
2746next_node:
2747 /* search forward from the last key in the node, this
2748 * will bring us into the next node in the tree
2749 */
2750 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2751
2752 /* unlikely, but we inc below, so check to be safe */
2753 if (found_key.offset == (u64)-1)
2754 goto out;
2755
2756 /* search_forward needs a path with locks held, do the
2757 * search again for the original key. It is possible
2758 * this will race with a balance and return a path that
2759 * we could modify, but this drop is just an optimization
2760 * and is allowed to miss some leaves.
2761 */
2762 btrfs_release_path(root, path);
2763 found_key.offset++;
2764
2765 /* setup a max key for search_forward */
2766 other_key.offset = (u64)-1;
2767 other_key.type = key.type;
2768 other_key.objectid = key.objectid;
2769
2770 path->keep_locks = 1;
2771 ret = btrfs_search_forward(root, &found_key, &other_key,
2772 path, 0, 0);
2773 path->keep_locks = 0;
2774 if (ret || found_key.objectid != key.objectid ||
2775 found_key.type != key.type) {
2776 ret = 0;
2777 goto out;
2778 }
2779
2780 key.offset = found_key.offset;
2781 btrfs_release_path(root, path);
2782 cond_resched();
2783 goto again;
2784 }
2785
2786 /* we know there's one more slot after us in the tree,
2787 * read that key so we can verify it is also a checksum item
2788 */
2789 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2790
2791 if (found_key.objectid < inode->i_ino)
2792 goto next_key;
2793
2794 if (found_key.type != key.type || found_key.offset < new_size)
2795 goto next_key;
2796
2797 /*
2798 * if the key for the next leaf isn't a csum key from this objectid,
2799 * we can't be sure there aren't good items inside this leaf.
2800 * Bail out
2801 */
2802 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2803 goto out;
2804
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002805 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2806 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002807 /*
2808 * it is safe to delete this leaf, it contains only
2809 * csum items from this inode at an offset >= new_size
2810 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002811 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002812 BUG_ON(ret);
2813
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002814 if (root->ref_cows && leaf_gen < trans->transid) {
2815 ref = btrfs_alloc_leaf_ref(root, 0);
2816 if (ref) {
2817 ref->root_gen = root->root_key.offset;
2818 ref->bytenr = leaf_start;
2819 ref->owner = 0;
2820 ref->generation = leaf_gen;
2821 ref->nritems = 0;
2822
Chris Masonbd56b302009-02-04 09:27:02 -05002823 btrfs_sort_leaf_ref(ref);
2824
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002825 ret = btrfs_add_leaf_ref(root, ref, 0);
2826 WARN_ON(ret);
2827 btrfs_free_leaf_ref(root, ref);
2828 } else {
2829 WARN_ON(1);
2830 }
2831 }
Chris Mason323ac952008-10-01 19:05:46 -04002832next_key:
2833 btrfs_release_path(root, path);
2834
2835 if (other_key.objectid == inode->i_ino &&
2836 other_key.type == key.type && other_key.offset > key.offset) {
2837 key.offset = other_key.offset;
2838 cond_resched();
2839 goto again;
2840 }
2841 ret = 0;
2842out:
2843 /* fixup any changes we've made to the path */
2844 path->lowest_level = 0;
2845 path->keep_locks = 0;
2846 btrfs_release_path(root, path);
2847 return ret;
2848}
2849
Chris Masond20f7042008-12-08 16:58:54 -05002850#endif
2851
Chris Mason323ac952008-10-01 19:05:46 -04002852/*
Chris Mason39279cc2007-06-12 06:35:45 -04002853 * this can truncate away extent items, csum items and directory items.
2854 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002855 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002856 *
2857 * csum items that cross the new i_size are truncated to the new size
2858 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002859 *
2860 * min_type is the minimum key type to truncate down to. If set to 0, this
2861 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002862 */
Yan, Zheng80825102009-11-12 09:35:36 +00002863int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2864 struct btrfs_root *root,
2865 struct inode *inode,
2866 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002867{
Chris Mason39279cc2007-06-12 06:35:45 -04002868 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002869 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002870 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00002871 struct btrfs_key key;
2872 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002873 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002874 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002875 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002876 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00002877 u64 mask = root->sectorsize - 1;
2878 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04002879 int found_extent;
2880 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002881 int pending_del_nr = 0;
2882 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002883 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002884 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00002885 int ret;
2886 int err = 0;
2887
2888 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002889
Chris Masone02119d2008-09-05 16:13:11 -04002890 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002891 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00002892
Chris Mason39279cc2007-06-12 06:35:45 -04002893 path = btrfs_alloc_path();
2894 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04002895 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04002896
Chris Mason39279cc2007-06-12 06:35:45 -04002897 key.objectid = inode->i_ino;
2898 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002899 key.type = (u8)-1;
2900
Chris Mason85e21ba2008-01-29 15:11:36 -05002901search_again:
Chris Masonb9473432009-03-13 11:00:37 -04002902 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05002903 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00002904 if (ret < 0) {
2905 err = ret;
2906 goto out;
2907 }
Chris Masond3977122009-01-05 21:25:51 -05002908
Chris Mason85e21ba2008-01-29 15:11:36 -05002909 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002910 /* there are no items in the tree for us to truncate, we're
2911 * done
2912 */
Yan, Zheng80825102009-11-12 09:35:36 +00002913 if (path->slots[0] == 0)
2914 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05002915 path->slots[0]--;
2916 }
2917
Chris Masond3977122009-01-05 21:25:51 -05002918 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002919 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002920 leaf = path->nodes[0];
2921 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2922 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002923 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002924
Chris Mason5f39d392007-10-15 16:14:19 -04002925 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002926 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002927
Chris Mason85e21ba2008-01-29 15:11:36 -05002928 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002929 break;
2930
Chris Mason5f39d392007-10-15 16:14:19 -04002931 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002932 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002933 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002934 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002935 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002936 encoding = btrfs_file_extent_compression(leaf, fi);
2937 encoding |= btrfs_file_extent_encryption(leaf, fi);
2938 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2939
Chris Mason179e29e2007-11-01 11:28:41 -04002940 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002941 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002942 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002943 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002944 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002945 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002946 }
Yan008630c2007-11-07 13:31:09 -05002947 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002948 }
Yan, Zheng80825102009-11-12 09:35:36 +00002949 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04002950 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00002951 } else {
2952 if (item_end < new_size)
2953 break;
2954 if (found_key.offset >= new_size)
2955 del_item = 1;
2956 else
2957 del_item = 0;
2958 }
Chris Mason39279cc2007-06-12 06:35:45 -04002959 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002960 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002961 if (found_type != BTRFS_EXTENT_DATA_KEY)
2962 goto delete;
2963
2964 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002965 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002966 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002967 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002968 u64 orig_num_bytes =
2969 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002970 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002971 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002972 extent_num_bytes = extent_num_bytes &
2973 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002974 btrfs_set_file_extent_num_bytes(leaf, fi,
2975 extent_num_bytes);
2976 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002977 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002978 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002979 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002980 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002981 } else {
Chris Masondb945352007-10-15 16:15:53 -04002982 extent_num_bytes =
2983 btrfs_file_extent_disk_num_bytes(leaf,
2984 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002985 extent_offset = found_key.offset -
2986 btrfs_file_extent_offset(leaf, fi);
2987
Chris Mason39279cc2007-06-12 06:35:45 -04002988 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002989 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002990 if (extent_start != 0) {
2991 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002992 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002993 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002994 }
2995 }
Chris Mason90692182008-02-08 13:49:28 -05002996 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002997 /*
2998 * we can't truncate inline items that have had
2999 * special encodings
3000 */
3001 if (!del_item &&
3002 btrfs_file_extent_compression(leaf, fi) == 0 &&
3003 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3004 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003005 u32 size = new_size - found_key.offset;
3006
3007 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003008 inode_sub_bytes(inode, item_end + 1 -
3009 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003010 }
3011 size =
3012 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003013 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003014 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003015 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003016 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003017 inode_sub_bytes(inode, item_end + 1 -
3018 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003019 }
Chris Mason39279cc2007-06-12 06:35:45 -04003020 }
Chris Mason179e29e2007-11-01 11:28:41 -04003021delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003022 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003023 if (!pending_del_nr) {
3024 /* no pending yet, add ourselves */
3025 pending_del_slot = path->slots[0];
3026 pending_del_nr = 1;
3027 } else if (pending_del_nr &&
3028 path->slots[0] + 1 == pending_del_slot) {
3029 /* hop on the pending chunk */
3030 pending_del_nr++;
3031 pending_del_slot = path->slots[0];
3032 } else {
Chris Masond3977122009-01-05 21:25:51 -05003033 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003034 }
Chris Mason39279cc2007-06-12 06:35:45 -04003035 } else {
3036 break;
3037 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003038 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04003039 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003040 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003041 extent_num_bytes, 0,
3042 btrfs_header_owner(leaf),
3043 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003044 BUG_ON(ret);
3045 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003046
Yan, Zheng80825102009-11-12 09:35:36 +00003047 if (found_type == BTRFS_INODE_ITEM_KEY)
3048 break;
3049
3050 if (path->slots[0] == 0 ||
3051 path->slots[0] != pending_del_slot) {
3052 if (root->ref_cows) {
3053 err = -EAGAIN;
3054 goto out;
3055 }
3056 if (pending_del_nr) {
3057 ret = btrfs_del_items(trans, root, path,
3058 pending_del_slot,
3059 pending_del_nr);
3060 BUG_ON(ret);
3061 pending_del_nr = 0;
3062 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003063 btrfs_release_path(root, path);
3064 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003065 } else {
3066 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003067 }
Chris Mason39279cc2007-06-12 06:35:45 -04003068 }
Yan, Zheng80825102009-11-12 09:35:36 +00003069out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003070 if (pending_del_nr) {
3071 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3072 pending_del_nr);
3073 }
Chris Mason39279cc2007-06-12 06:35:45 -04003074 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003075 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003076}
3077
Chris Masona52d9a82007-08-27 16:49:44 -04003078/*
3079 * taken from block_truncate_page, but does cow as it zeros out
3080 * any bytes left in the last page in the file.
3081 */
3082static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3083{
3084 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003085 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003086 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3087 struct btrfs_ordered_extent *ordered;
3088 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003089 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003090 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3091 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3092 struct page *page;
3093 int ret = 0;
3094 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003095 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003096
3097 if ((offset & (blocksize - 1)) == 0)
3098 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003099 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
3100 if (ret)
3101 goto out;
3102
3103 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
3104 if (ret)
3105 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003106
3107 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003108again:
Chris Masona52d9a82007-08-27 16:49:44 -04003109 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003110 if (!page) {
3111 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3112 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04003113 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003114 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003115
3116 page_start = page_offset(page);
3117 page_end = page_start + PAGE_CACHE_SIZE - 1;
3118
Chris Masona52d9a82007-08-27 16:49:44 -04003119 if (!PageUptodate(page)) {
3120 ret = btrfs_readpage(NULL, page);
3121 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003122 if (page->mapping != mapping) {
3123 unlock_page(page);
3124 page_cache_release(page);
3125 goto again;
3126 }
Chris Masona52d9a82007-08-27 16:49:44 -04003127 if (!PageUptodate(page)) {
3128 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003129 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003130 }
3131 }
Chris Mason211c17f2008-05-15 09:13:45 -04003132 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003133
3134 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3135 set_page_extent_mapped(page);
3136
3137 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3138 if (ordered) {
3139 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3140 unlock_page(page);
3141 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003142 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003143 btrfs_put_ordered_extent(ordered);
3144 goto again;
3145 }
3146
Josef Bacik5d5e1032009-10-13 16:46:49 -04003147 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
3148 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3149 GFP_NOFS);
3150
Josef Bacik9ed74f22009-09-11 16:12:44 -04003151 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3152 if (ret) {
3153 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3154 goto out_unlock;
3155 }
3156
Chris Masone6dcd2d2008-07-17 12:53:50 -04003157 ret = 0;
3158 if (offset != PAGE_CACHE_SIZE) {
3159 kaddr = kmap(page);
3160 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3161 flush_dcache_page(page);
3162 kunmap(page);
3163 }
Chris Mason247e7432008-07-17 12:53:51 -04003164 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003165 set_page_dirty(page);
3166 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003167
Chris Mason89642222008-07-24 09:41:53 -04003168out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003169 if (ret)
3170 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3171 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003172 unlock_page(page);
3173 page_cache_release(page);
3174out:
3175 return ret;
3176}
3177
Yan Zheng9036c102008-10-30 14:19:41 -04003178int btrfs_cont_expand(struct inode *inode, loff_t size)
3179{
3180 struct btrfs_trans_handle *trans;
3181 struct btrfs_root *root = BTRFS_I(inode)->root;
3182 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3183 struct extent_map *em;
3184 u64 mask = root->sectorsize - 1;
3185 u64 hole_start = (inode->i_size + mask) & ~mask;
3186 u64 block_end = (size + mask) & ~mask;
3187 u64 last_byte;
3188 u64 cur_offset;
3189 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003190 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003191
3192 if (size <= hole_start)
3193 return 0;
3194
Yan Zheng9036c102008-10-30 14:19:41 -04003195 while (1) {
3196 struct btrfs_ordered_extent *ordered;
3197 btrfs_wait_ordered_range(inode, hole_start,
3198 block_end - hole_start);
3199 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3200 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3201 if (!ordered)
3202 break;
3203 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3204 btrfs_put_ordered_extent(ordered);
3205 }
3206
Yan Zheng9036c102008-10-30 14:19:41 -04003207 cur_offset = hole_start;
3208 while (1) {
3209 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3210 block_end - cur_offset, 0);
3211 BUG_ON(IS_ERR(em) || !em);
3212 last_byte = min(extent_map_end(em), block_end);
3213 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003214 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003215 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003216 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003217
3218 err = btrfs_reserve_metadata_space(root, 2);
Chris Mason771ed682008-11-06 22:02:51 -05003219 if (err)
3220 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003221
Yan, Zheng80825102009-11-12 09:35:36 +00003222 trans = btrfs_start_transaction(root, 1);
3223 btrfs_set_trans_block_group(trans, inode);
3224
3225 err = btrfs_drop_extents(trans, inode, cur_offset,
3226 cur_offset + hole_size,
3227 &hint_byte, 1);
3228 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003229
Yan Zheng9036c102008-10-30 14:19:41 -04003230 err = btrfs_insert_file_extent(trans, root,
3231 inode->i_ino, cur_offset, 0,
3232 0, hole_size, 0, hole_size,
3233 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003234 BUG_ON(err);
3235
Yan Zheng9036c102008-10-30 14:19:41 -04003236 btrfs_drop_extent_cache(inode, hole_start,
3237 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003238
3239 btrfs_end_transaction(trans, root);
3240 btrfs_unreserve_metadata_space(root, 2);
Yan Zheng9036c102008-10-30 14:19:41 -04003241 }
3242 free_extent_map(em);
3243 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003244 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003245 break;
3246 }
3247
Yan Zheng9036c102008-10-30 14:19:41 -04003248 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3249 return err;
3250}
3251
Yan, Zheng80825102009-11-12 09:35:36 +00003252static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3253{
3254 struct btrfs_root *root = BTRFS_I(inode)->root;
3255 struct btrfs_trans_handle *trans;
3256 unsigned long nr;
3257 int ret;
3258
3259 if (attr->ia_size == inode->i_size)
3260 return 0;
3261
3262 if (attr->ia_size > inode->i_size) {
3263 unsigned long limit;
3264 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3265 if (attr->ia_size > inode->i_sb->s_maxbytes)
3266 return -EFBIG;
3267 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3268 send_sig(SIGXFSZ, current, 0);
3269 return -EFBIG;
3270 }
3271 }
3272
3273 ret = btrfs_reserve_metadata_space(root, 1);
3274 if (ret)
3275 return ret;
3276
3277 trans = btrfs_start_transaction(root, 1);
3278 btrfs_set_trans_block_group(trans, inode);
3279
3280 ret = btrfs_orphan_add(trans, inode);
3281 BUG_ON(ret);
3282
3283 nr = trans->blocks_used;
3284 btrfs_end_transaction(trans, root);
3285 btrfs_unreserve_metadata_space(root, 1);
3286 btrfs_btree_balance_dirty(root, nr);
3287
3288 if (attr->ia_size > inode->i_size) {
3289 ret = btrfs_cont_expand(inode, attr->ia_size);
3290 if (ret) {
3291 btrfs_truncate(inode);
3292 return ret;
3293 }
3294
3295 i_size_write(inode, attr->ia_size);
3296 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3297
3298 trans = btrfs_start_transaction(root, 1);
3299 btrfs_set_trans_block_group(trans, inode);
3300
3301 ret = btrfs_update_inode(trans, root, inode);
3302 BUG_ON(ret);
3303 if (inode->i_nlink > 0) {
3304 ret = btrfs_orphan_del(trans, inode);
3305 BUG_ON(ret);
3306 }
3307 nr = trans->blocks_used;
3308 btrfs_end_transaction(trans, root);
3309 btrfs_btree_balance_dirty(root, nr);
3310 return 0;
3311 }
3312
3313 /*
3314 * We're truncating a file that used to have good data down to
3315 * zero. Make sure it gets into the ordered flush list so that
3316 * any new writes get down to disk quickly.
3317 */
3318 if (attr->ia_size == 0)
3319 BTRFS_I(inode)->ordered_data_close = 1;
3320
3321 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3322 ret = vmtruncate(inode, attr->ia_size);
3323 BUG_ON(ret);
3324
3325 return 0;
3326}
3327
Chris Mason39279cc2007-06-12 06:35:45 -04003328static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3329{
3330 struct inode *inode = dentry->d_inode;
3331 int err;
3332
3333 err = inode_change_ok(inode, attr);
3334 if (err)
3335 return err;
3336
Chris Mason5a3f23d2009-03-31 13:27:11 -04003337 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003338 err = btrfs_setattr_size(inode, attr);
3339 if (err)
3340 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003341 }
Yan, Zheng80825102009-11-12 09:35:36 +00003342 attr->ia_valid &= ~ATTR_SIZE;
Yan Zheng9036c102008-10-30 14:19:41 -04003343
Yan, Zheng80825102009-11-12 09:35:36 +00003344 if (attr->ia_valid)
3345 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003346
3347 if (!err && ((attr->ia_valid & ATTR_MODE)))
3348 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003349 return err;
3350}
Chris Mason61295eb2008-01-14 16:24:38 -05003351
Chris Mason39279cc2007-06-12 06:35:45 -04003352void btrfs_delete_inode(struct inode *inode)
3353{
3354 struct btrfs_trans_handle *trans;
3355 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003356 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003357 int ret;
3358
3359 truncate_inode_pages(&inode->i_data, 0);
3360 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003361 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003362 goto no_delete;
3363 }
Chris Mason4a096752008-07-21 10:29:44 -04003364 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003365
Yan, Zhengc71bf092009-11-12 09:34:40 +00003366 if (root->fs_info->log_root_recovering) {
3367 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3368 goto no_delete;
3369 }
3370
Yan, Zheng76dda932009-09-21 16:00:26 -04003371 if (inode->i_nlink > 0) {
3372 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3373 goto no_delete;
3374 }
3375
Chris Masondbe674a2008-07-17 12:54:05 -04003376 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003377
Yan, Zheng80825102009-11-12 09:35:36 +00003378 while (1) {
3379 trans = btrfs_start_transaction(root, 1);
3380 btrfs_set_trans_block_group(trans, inode);
3381 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3382
3383 if (ret != -EAGAIN)
3384 break;
3385
3386 nr = trans->blocks_used;
3387 btrfs_end_transaction(trans, root);
3388 trans = NULL;
3389 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003390 }
3391
Yan, Zheng80825102009-11-12 09:35:36 +00003392 if (ret == 0) {
3393 ret = btrfs_orphan_del(trans, inode);
3394 BUG_ON(ret);
3395 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003396
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003397 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003398 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003399 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003400no_delete:
3401 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003402 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003403}
3404
3405/*
3406 * this returns the key found in the dir entry in the location pointer.
3407 * If no dir entries were found, location->objectid is 0.
3408 */
3409static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3410 struct btrfs_key *location)
3411{
3412 const char *name = dentry->d_name.name;
3413 int namelen = dentry->d_name.len;
3414 struct btrfs_dir_item *di;
3415 struct btrfs_path *path;
3416 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003417 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003418
3419 path = btrfs_alloc_path();
3420 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003421
Chris Mason39279cc2007-06-12 06:35:45 -04003422 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3423 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003424 if (IS_ERR(di))
3425 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003426
3427 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003428 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003429
Chris Mason5f39d392007-10-15 16:14:19 -04003430 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003431out:
Chris Mason39279cc2007-06-12 06:35:45 -04003432 btrfs_free_path(path);
3433 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003434out_err:
3435 location->objectid = 0;
3436 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003437}
3438
3439/*
3440 * when we hit a tree root in a directory, the btrfs part of the inode
3441 * needs to be changed to reflect the root directory of the tree root. This
3442 * is kind of like crossing a mount point.
3443 */
3444static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003445 struct inode *dir,
3446 struct dentry *dentry,
3447 struct btrfs_key *location,
3448 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003449{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003450 struct btrfs_path *path;
3451 struct btrfs_root *new_root;
3452 struct btrfs_root_ref *ref;
3453 struct extent_buffer *leaf;
3454 int ret;
3455 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003456
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003457 path = btrfs_alloc_path();
3458 if (!path) {
3459 err = -ENOMEM;
3460 goto out;
3461 }
Chris Mason39279cc2007-06-12 06:35:45 -04003462
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003463 err = -ENOENT;
3464 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3465 BTRFS_I(dir)->root->root_key.objectid,
3466 location->objectid);
3467 if (ret) {
3468 if (ret < 0)
3469 err = ret;
3470 goto out;
3471 }
Chris Mason39279cc2007-06-12 06:35:45 -04003472
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003473 leaf = path->nodes[0];
3474 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3475 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3476 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3477 goto out;
3478
3479 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3480 (unsigned long)(ref + 1),
3481 dentry->d_name.len);
3482 if (ret)
3483 goto out;
3484
3485 btrfs_release_path(root->fs_info->tree_root, path);
3486
3487 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3488 if (IS_ERR(new_root)) {
3489 err = PTR_ERR(new_root);
3490 goto out;
3491 }
3492
3493 if (btrfs_root_refs(&new_root->root_item) == 0) {
3494 err = -ENOENT;
3495 goto out;
3496 }
3497
3498 *sub_root = new_root;
3499 location->objectid = btrfs_root_dirid(&new_root->root_item);
3500 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003501 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003502 err = 0;
3503out:
3504 btrfs_free_path(path);
3505 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003506}
3507
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003508static void inode_tree_add(struct inode *inode)
3509{
3510 struct btrfs_root *root = BTRFS_I(inode)->root;
3511 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003512 struct rb_node **p;
3513 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003514again:
3515 p = &root->inode_tree.rb_node;
3516 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003517
Yan, Zheng76dda932009-09-21 16:00:26 -04003518 if (hlist_unhashed(&inode->i_hash))
3519 return;
3520
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003521 spin_lock(&root->inode_lock);
3522 while (*p) {
3523 parent = *p;
3524 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3525
3526 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003527 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003528 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003529 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003530 else {
3531 WARN_ON(!(entry->vfs_inode.i_state &
3532 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003533 rb_erase(parent, &root->inode_tree);
3534 RB_CLEAR_NODE(parent);
3535 spin_unlock(&root->inode_lock);
3536 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003537 }
3538 }
3539 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3540 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3541 spin_unlock(&root->inode_lock);
3542}
3543
3544static void inode_tree_del(struct inode *inode)
3545{
3546 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003547 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003548
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003549 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003550 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003551 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003552 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003553 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003554 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003555 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003556
3557 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3558 synchronize_srcu(&root->fs_info->subvol_srcu);
3559 spin_lock(&root->inode_lock);
3560 empty = RB_EMPTY_ROOT(&root->inode_tree);
3561 spin_unlock(&root->inode_lock);
3562 if (empty)
3563 btrfs_add_dead_root(root);
3564 }
3565}
3566
3567int btrfs_invalidate_inodes(struct btrfs_root *root)
3568{
3569 struct rb_node *node;
3570 struct rb_node *prev;
3571 struct btrfs_inode *entry;
3572 struct inode *inode;
3573 u64 objectid = 0;
3574
3575 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3576
3577 spin_lock(&root->inode_lock);
3578again:
3579 node = root->inode_tree.rb_node;
3580 prev = NULL;
3581 while (node) {
3582 prev = node;
3583 entry = rb_entry(node, struct btrfs_inode, rb_node);
3584
3585 if (objectid < entry->vfs_inode.i_ino)
3586 node = node->rb_left;
3587 else if (objectid > entry->vfs_inode.i_ino)
3588 node = node->rb_right;
3589 else
3590 break;
3591 }
3592 if (!node) {
3593 while (prev) {
3594 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3595 if (objectid <= entry->vfs_inode.i_ino) {
3596 node = prev;
3597 break;
3598 }
3599 prev = rb_next(prev);
3600 }
3601 }
3602 while (node) {
3603 entry = rb_entry(node, struct btrfs_inode, rb_node);
3604 objectid = entry->vfs_inode.i_ino + 1;
3605 inode = igrab(&entry->vfs_inode);
3606 if (inode) {
3607 spin_unlock(&root->inode_lock);
3608 if (atomic_read(&inode->i_count) > 1)
3609 d_prune_aliases(inode);
3610 /*
3611 * btrfs_drop_inode will remove it from
3612 * the inode cache when its usage count
3613 * hits zero.
3614 */
3615 iput(inode);
3616 cond_resched();
3617 spin_lock(&root->inode_lock);
3618 goto again;
3619 }
3620
3621 if (cond_resched_lock(&root->inode_lock))
3622 goto again;
3623
3624 node = rb_next(node);
3625 }
3626 spin_unlock(&root->inode_lock);
3627 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003628}
3629
Chris Masone02119d2008-09-05 16:13:11 -04003630static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003631{
Chris Masone02119d2008-09-05 16:13:11 -04003632 struct btrfs_inode *bi = BTRFS_I(inode);
3633
Chris Masone02119d2008-09-05 16:13:11 -04003634 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05003635 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003636 bi->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04003637 bi->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003638 bi->logged_trans = 0;
3639 bi->delalloc_bytes = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003640 bi->reserved_bytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003641 bi->disk_i_size = 0;
3642 bi->flags = 0;
3643 bi->index_cnt = (u64)-1;
Chris Mason12fcfd22009-03-24 10:24:20 -04003644 bi->last_unlink_trans = 0;
Chris Mason27574952009-05-14 13:10:02 -04003645 bi->ordered_data_close = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05003646 bi->force_compress = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003647 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3648 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04003649 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003650 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3651 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003652 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04003653 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003654 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Chris Masonba1da2f2008-07-17 12:54:15 -04003655 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masone02119d2008-09-05 16:13:11 -04003656 mutex_init(&BTRFS_I(inode)->log_mutex);
3657}
3658
3659static int btrfs_init_locked_inode(struct inode *inode, void *p)
3660{
3661 struct btrfs_iget_args *args = p;
3662 inode->i_ino = args->ino;
3663 init_btrfs_i(inode);
3664 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003665 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003666 return 0;
3667}
3668
3669static int btrfs_find_actor(struct inode *inode, void *opaque)
3670{
3671 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003672 return args->ino == inode->i_ino &&
3673 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003674}
3675
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003676static struct inode *btrfs_iget_locked(struct super_block *s,
3677 u64 objectid,
3678 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003679{
3680 struct inode *inode;
3681 struct btrfs_iget_args args;
3682 args.ino = objectid;
3683 args.root = root;
3684
3685 inode = iget5_locked(s, objectid, btrfs_find_actor,
3686 btrfs_init_locked_inode,
3687 (void *)&args);
3688 return inode;
3689}
3690
Balaji Rao1a54ef82008-07-21 02:01:04 +05303691/* Get an inode object given its location and corresponding root.
3692 * Returns in *is_new if the inode was read from disk
3693 */
3694struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00003695 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303696{
3697 struct inode *inode;
3698
3699 inode = btrfs_iget_locked(s, location->objectid, root);
3700 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003701 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303702
3703 if (inode->i_state & I_NEW) {
3704 BTRFS_I(inode)->root = root;
3705 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3706 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003707
3708 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303709 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00003710 if (new)
3711 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05303712 }
3713
3714 return inode;
3715}
3716
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003717static struct inode *new_simple_dir(struct super_block *s,
3718 struct btrfs_key *key,
3719 struct btrfs_root *root)
3720{
3721 struct inode *inode = new_inode(s);
3722
3723 if (!inode)
3724 return ERR_PTR(-ENOMEM);
3725
3726 init_btrfs_i(inode);
3727
3728 BTRFS_I(inode)->root = root;
3729 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3730 BTRFS_I(inode)->dummy_inode = 1;
3731
3732 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3733 inode->i_op = &simple_dir_inode_operations;
3734 inode->i_fop = &simple_dir_operations;
3735 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3736 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3737
3738 return inode;
3739}
3740
Chris Mason3de45862008-11-17 21:02:50 -05003741struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003742{
Chris Masond3977122009-01-05 21:25:51 -05003743 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003744 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003745 struct btrfs_root *sub_root = root;
3746 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003747 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003748 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003749
Yan, Zheng76dda932009-09-21 16:00:26 -04003750 dentry->d_op = &btrfs_dentry_operations;
3751
Chris Mason39279cc2007-06-12 06:35:45 -04003752 if (dentry->d_name.len > BTRFS_NAME_LEN)
3753 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003754
Chris Mason39279cc2007-06-12 06:35:45 -04003755 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003756
Chris Mason39279cc2007-06-12 06:35:45 -04003757 if (ret < 0)
3758 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003759
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003760 if (location.objectid == 0)
3761 return NULL;
3762
3763 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00003764 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003765 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003766 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003767
3768 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3769
Yan, Zheng76dda932009-09-21 16:00:26 -04003770 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003771 ret = fixup_tree_root_location(root, dir, dentry,
3772 &location, &sub_root);
3773 if (ret < 0) {
3774 if (ret != -ENOENT)
3775 inode = ERR_PTR(ret);
3776 else
3777 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3778 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00003779 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003780 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003781 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3782
Yan, Zhengc71bf092009-11-12 09:34:40 +00003783 if (root != sub_root) {
3784 down_read(&root->fs_info->cleanup_work_sem);
3785 if (!(inode->i_sb->s_flags & MS_RDONLY))
3786 btrfs_orphan_cleanup(sub_root);
3787 up_read(&root->fs_info->cleanup_work_sem);
3788 }
3789
Chris Mason3de45862008-11-17 21:02:50 -05003790 return inode;
3791}
3792
Yan, Zheng76dda932009-09-21 16:00:26 -04003793static int btrfs_dentry_delete(struct dentry *dentry)
3794{
3795 struct btrfs_root *root;
3796
Yan, Zhengefefb142009-10-09 09:25:16 -04003797 if (!dentry->d_inode && !IS_ROOT(dentry))
3798 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04003799
Yan, Zhengefefb142009-10-09 09:25:16 -04003800 if (dentry->d_inode) {
3801 root = BTRFS_I(dentry->d_inode)->root;
3802 if (btrfs_root_refs(&root->root_item) == 0)
3803 return 1;
3804 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003805 return 0;
3806}
3807
Chris Mason3de45862008-11-17 21:02:50 -05003808static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3809 struct nameidata *nd)
3810{
3811 struct inode *inode;
3812
Chris Mason3de45862008-11-17 21:02:50 -05003813 inode = btrfs_lookup_dentry(dir, dentry);
3814 if (IS_ERR(inode))
3815 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003816
Chris Mason39279cc2007-06-12 06:35:45 -04003817 return d_splice_alias(inode, dentry);
3818}
3819
Chris Mason39279cc2007-06-12 06:35:45 -04003820static unsigned char btrfs_filetype_table[] = {
3821 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3822};
3823
David Woodhousecbdf5a22008-08-06 19:42:33 +01003824static int btrfs_real_readdir(struct file *filp, void *dirent,
3825 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003826{
Chris Mason6da6aba2007-12-18 16:15:09 -05003827 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003828 struct btrfs_root *root = BTRFS_I(inode)->root;
3829 struct btrfs_item *item;
3830 struct btrfs_dir_item *di;
3831 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003832 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003833 struct btrfs_path *path;
3834 int ret;
3835 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003836 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003837 int slot;
3838 int advance;
3839 unsigned char d_type;
3840 int over = 0;
3841 u32 di_cur;
3842 u32 di_total;
3843 u32 di_len;
3844 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003845 char tmp_name[32];
3846 char *name_ptr;
3847 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003848
3849 /* FIXME, use a real flag for deciding about the key type */
3850 if (root->fs_info->tree_root == root)
3851 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003852
Chris Mason39544012007-12-12 14:38:19 -05003853 /* special case for "." */
3854 if (filp->f_pos == 0) {
3855 over = filldir(dirent, ".", 1,
3856 1, inode->i_ino,
3857 DT_DIR);
3858 if (over)
3859 return 0;
3860 filp->f_pos = 1;
3861 }
Chris Mason39544012007-12-12 14:38:19 -05003862 /* special case for .., just use the back ref */
3863 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003864 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003865 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003866 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003867 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003868 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003869 filp->f_pos = 2;
3870 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003871 path = btrfs_alloc_path();
3872 path->reada = 2;
3873
Chris Mason39279cc2007-06-12 06:35:45 -04003874 btrfs_set_key_type(&key, key_type);
3875 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003876 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003877
Chris Mason39279cc2007-06-12 06:35:45 -04003878 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3879 if (ret < 0)
3880 goto err;
3881 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003882
3883 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003884 leaf = path->nodes[0];
3885 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003886 slot = path->slots[0];
3887 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003888 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003889 ret = btrfs_next_leaf(root, path);
3890 if (ret)
3891 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003892 leaf = path->nodes[0];
3893 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003894 slot = path->slots[0];
3895 } else {
3896 slot++;
3897 path->slots[0]++;
3898 }
3899 }
Chris Mason3de45862008-11-17 21:02:50 -05003900
Chris Mason39279cc2007-06-12 06:35:45 -04003901 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003902 item = btrfs_item_nr(leaf, slot);
3903 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3904
3905 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003906 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003907 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003908 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003909 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003910 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003911
3912 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003913
Chris Mason39279cc2007-06-12 06:35:45 -04003914 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3915 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003916 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003917
3918 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003919 struct btrfs_key location;
3920
3921 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003922 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003923 name_ptr = tmp_name;
3924 } else {
3925 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003926 if (!name_ptr) {
3927 ret = -ENOMEM;
3928 goto err;
3929 }
Chris Mason5f39d392007-10-15 16:14:19 -04003930 }
3931 read_extent_buffer(leaf, name_ptr,
3932 (unsigned long)(di + 1), name_len);
3933
3934 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3935 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003936
3937 /* is this a reference to our own snapshot? If so
3938 * skip it
3939 */
3940 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3941 location.objectid == root->root_key.objectid) {
3942 over = 0;
3943 goto skip;
3944 }
Chris Mason5f39d392007-10-15 16:14:19 -04003945 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003946 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003947 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003948
Chris Mason3de45862008-11-17 21:02:50 -05003949skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003950 if (name_ptr != tmp_name)
3951 kfree(name_ptr);
3952
Chris Mason39279cc2007-06-12 06:35:45 -04003953 if (over)
3954 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003955 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003956 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003957 di_cur += di_len;
3958 di = (struct btrfs_dir_item *)((char *)di + di_len);
3959 }
3960 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003961
3962 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003963 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00003964 /*
3965 * 32-bit glibc will use getdents64, but then strtol -
3966 * so the last number we can serve is this.
3967 */
3968 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05003969 else
3970 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003971nopos:
3972 ret = 0;
3973err:
Chris Mason39279cc2007-06-12 06:35:45 -04003974 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003975 return ret;
3976}
3977
3978int btrfs_write_inode(struct inode *inode, int wait)
3979{
3980 struct btrfs_root *root = BTRFS_I(inode)->root;
3981 struct btrfs_trans_handle *trans;
3982 int ret = 0;
3983
Yan Zhengc146afa2008-11-12 14:34:12 -05003984 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003985 return 0;
3986
Chris Mason39279cc2007-06-12 06:35:45 -04003987 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003988 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003989 btrfs_set_trans_block_group(trans, inode);
3990 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003991 }
3992 return ret;
3993}
3994
3995/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003996 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003997 * inode changes. But, it is most likely to find the inode in cache.
3998 * FIXME, needs more benchmarking...there are no reasons other than performance
3999 * to keep or drop this code.
4000 */
4001void btrfs_dirty_inode(struct inode *inode)
4002{
4003 struct btrfs_root *root = BTRFS_I(inode)->root;
4004 struct btrfs_trans_handle *trans;
4005
Chris Masonf9295742008-07-17 12:54:14 -04004006 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004007 btrfs_set_trans_block_group(trans, inode);
4008 btrfs_update_inode(trans, root, inode);
4009 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004010}
4011
Chris Masond352ac62008-09-29 15:18:18 -04004012/*
4013 * find the highest existing sequence number in a directory
4014 * and then set the in-memory index_cnt variable to reflect
4015 * free sequence numbers
4016 */
Josef Bacikaec74772008-07-24 12:12:38 -04004017static int btrfs_set_inode_index_count(struct inode *inode)
4018{
4019 struct btrfs_root *root = BTRFS_I(inode)->root;
4020 struct btrfs_key key, found_key;
4021 struct btrfs_path *path;
4022 struct extent_buffer *leaf;
4023 int ret;
4024
4025 key.objectid = inode->i_ino;
4026 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4027 key.offset = (u64)-1;
4028
4029 path = btrfs_alloc_path();
4030 if (!path)
4031 return -ENOMEM;
4032
4033 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4034 if (ret < 0)
4035 goto out;
4036 /* FIXME: we should be able to handle this */
4037 if (ret == 0)
4038 goto out;
4039 ret = 0;
4040
4041 /*
4042 * MAGIC NUMBER EXPLANATION:
4043 * since we search a directory based on f_pos we have to start at 2
4044 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4045 * else has to start at 2
4046 */
4047 if (path->slots[0] == 0) {
4048 BTRFS_I(inode)->index_cnt = 2;
4049 goto out;
4050 }
4051
4052 path->slots[0]--;
4053
4054 leaf = path->nodes[0];
4055 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4056
4057 if (found_key.objectid != inode->i_ino ||
4058 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4059 BTRFS_I(inode)->index_cnt = 2;
4060 goto out;
4061 }
4062
4063 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4064out:
4065 btrfs_free_path(path);
4066 return ret;
4067}
4068
Chris Masond352ac62008-09-29 15:18:18 -04004069/*
4070 * helper to find a free sequence number in a given directory. This current
4071 * code is very simple, later versions will do smarter things in the btree
4072 */
Chris Mason3de45862008-11-17 21:02:50 -05004073int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004074{
4075 int ret = 0;
4076
4077 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4078 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004079 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004080 return ret;
4081 }
4082
Chris Mason00e4e6b2008-08-05 11:18:09 -04004083 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004084 BTRFS_I(dir)->index_cnt++;
4085
4086 return ret;
4087}
4088
Chris Mason39279cc2007-06-12 06:35:45 -04004089static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4090 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004091 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004092 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004093 u64 ref_objectid, u64 objectid,
4094 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004095{
4096 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004097 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004098 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004099 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004100 struct btrfs_inode_ref *ref;
4101 struct btrfs_key key[2];
4102 u32 sizes[2];
4103 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004104 int ret;
4105 int owner;
4106
Chris Mason5f39d392007-10-15 16:14:19 -04004107 path = btrfs_alloc_path();
4108 BUG_ON(!path);
4109
Chris Mason39279cc2007-06-12 06:35:45 -04004110 inode = new_inode(root->fs_info->sb);
4111 if (!inode)
4112 return ERR_PTR(-ENOMEM);
4113
Josef Bacikaec74772008-07-24 12:12:38 -04004114 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004115 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004116 if (ret) {
4117 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004118 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004119 }
Josef Bacikaec74772008-07-24 12:12:38 -04004120 }
4121 /*
4122 * index_cnt is ignored for everything but a dir,
4123 * btrfs_get_inode_index_count has an explanation for the magic
4124 * number
4125 */
Chris Masone02119d2008-09-05 16:13:11 -04004126 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004127 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004128 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004129 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05004130 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004131
Chris Mason39279cc2007-06-12 06:35:45 -04004132 if (mode & S_IFDIR)
4133 owner = 0;
4134 else
4135 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004136 BTRFS_I(inode)->block_group =
4137 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004138
4139 key[0].objectid = objectid;
4140 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4141 key[0].offset = 0;
4142
4143 key[1].objectid = objectid;
4144 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4145 key[1].offset = ref_objectid;
4146
4147 sizes[0] = sizeof(struct btrfs_inode_item);
4148 sizes[1] = name_len + sizeof(*ref);
4149
Chris Masonb9473432009-03-13 11:00:37 -04004150 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004151 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4152 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004153 goto fail;
4154
Chris Mason79683f22008-11-19 22:00:53 -05004155 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05004156
Chris Mason42f15d72009-02-06 11:35:57 -05004157 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05004158 inode->i_gid = dir->i_gid;
4159 if (S_ISDIR(mode))
4160 mode |= S_ISGID;
4161 } else
4162 inode->i_gid = current_fsgid();
4163
Chris Mason39279cc2007-06-12 06:35:45 -04004164 inode->i_mode = mode;
4165 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004166 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004167 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004168 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4169 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004170 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004171
4172 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4173 struct btrfs_inode_ref);
4174 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004175 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004176 ptr = (unsigned long)(ref + 1);
4177 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4178
Chris Mason5f39d392007-10-15 16:14:19 -04004179 btrfs_mark_buffer_dirty(path->nodes[0]);
4180 btrfs_free_path(path);
4181
Chris Mason39279cc2007-06-12 06:35:45 -04004182 location = &BTRFS_I(inode)->location;
4183 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004184 location->offset = 0;
4185 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4186
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004187 btrfs_inherit_iflags(inode, dir);
4188
Chris Mason94272162009-07-02 12:26:06 -04004189 if ((mode & S_IFREG)) {
4190 if (btrfs_test_opt(root, NODATASUM))
4191 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4192 if (btrfs_test_opt(root, NODATACOW))
4193 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4194 }
4195
Chris Mason39279cc2007-06-12 06:35:45 -04004196 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004197 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004198 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004199fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004200 if (dir)
4201 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004202 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004203 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004204 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004205}
4206
4207static inline u8 btrfs_inode_type(struct inode *inode)
4208{
4209 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4210}
4211
Chris Masond352ac62008-09-29 15:18:18 -04004212/*
4213 * utility function to add 'inode' into 'parent_inode' with
4214 * a give name and a given sequence number.
4215 * if 'add_backref' is true, also insert a backref from the
4216 * inode to the parent directory.
4217 */
Chris Masone02119d2008-09-05 16:13:11 -04004218int btrfs_add_link(struct btrfs_trans_handle *trans,
4219 struct inode *parent_inode, struct inode *inode,
4220 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004221{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004222 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004223 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004224 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004225
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004226 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4227 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4228 } else {
4229 key.objectid = inode->i_ino;
4230 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4231 key.offset = 0;
4232 }
Chris Mason39279cc2007-06-12 06:35:45 -04004233
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004234 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4235 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4236 key.objectid, root->root_key.objectid,
4237 parent_inode->i_ino,
4238 index, name, name_len);
4239 } else if (add_backref) {
4240 ret = btrfs_insert_inode_ref(trans, root,
4241 name, name_len, inode->i_ino,
4242 parent_inode->i_ino, index);
4243 }
4244
Chris Mason39279cc2007-06-12 06:35:45 -04004245 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4247 parent_inode->i_ino, &key,
4248 btrfs_inode_type(inode), index);
4249 BUG_ON(ret);
4250
Chris Masondbe674a2008-07-17 12:54:05 -04004251 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004252 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004253 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004254 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004255 }
4256 return ret;
4257}
4258
4259static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004260 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004261 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004262{
Chris Masone02119d2008-09-05 16:13:11 -04004263 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4264 inode, dentry->d_name.name,
4265 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004266 if (!err) {
4267 d_instantiate(dentry, inode);
4268 return 0;
4269 }
4270 if (err > 0)
4271 err = -EEXIST;
4272 return err;
4273}
4274
Josef Bacik618e21d2007-07-11 10:18:17 -04004275static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4276 int mode, dev_t rdev)
4277{
4278 struct btrfs_trans_handle *trans;
4279 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004280 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004281 int err;
4282 int drop_inode = 0;
4283 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004284 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004285 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004286
4287 if (!new_valid_dev(rdev))
4288 return -EINVAL;
4289
Josef Bacik9ed74f22009-09-11 16:12:44 -04004290 /*
4291 * 2 for inode item and ref
4292 * 2 for dir items
4293 * 1 for xattr if selinux is on
4294 */
4295 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004296 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004297 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004298
Josef Bacik618e21d2007-07-11 10:18:17 -04004299 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004300 if (!trans)
4301 goto fail;
Josef Bacik618e21d2007-07-11 10:18:17 -04004302 btrfs_set_trans_block_group(trans, dir);
4303
4304 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4305 if (err) {
4306 err = -ENOSPC;
4307 goto out_unlock;
4308 }
4309
Josef Bacikaec74772008-07-24 12:12:38 -04004310 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004311 dentry->d_name.len,
4312 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004313 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004314 err = PTR_ERR(inode);
4315 if (IS_ERR(inode))
4316 goto out_unlock;
4317
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004318 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004319 if (err) {
4320 drop_inode = 1;
4321 goto out_unlock;
4322 }
4323
Josef Bacik618e21d2007-07-11 10:18:17 -04004324 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004325 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004326 if (err)
4327 drop_inode = 1;
4328 else {
4329 inode->i_op = &btrfs_special_inode_operations;
4330 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004331 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004332 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004333 btrfs_update_inode_block_group(trans, inode);
4334 btrfs_update_inode_block_group(trans, dir);
4335out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004336 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004337 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004338fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004339 btrfs_unreserve_metadata_space(root, 5);
Josef Bacik618e21d2007-07-11 10:18:17 -04004340 if (drop_inode) {
4341 inode_dec_link_count(inode);
4342 iput(inode);
4343 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004344 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004345 return err;
4346}
4347
Chris Mason39279cc2007-06-12 06:35:45 -04004348static int btrfs_create(struct inode *dir, struct dentry *dentry,
4349 int mode, struct nameidata *nd)
4350{
4351 struct btrfs_trans_handle *trans;
4352 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004353 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004354 int err;
4355 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05004356 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004357 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004358 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004359
Josef Bacik9ed74f22009-09-11 16:12:44 -04004360 /*
4361 * 2 for inode item and ref
4362 * 2 for dir items
4363 * 1 for xattr if selinux is on
4364 */
4365 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004366 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004367 return err;
4368
Chris Mason39279cc2007-06-12 06:35:45 -04004369 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004370 if (!trans)
4371 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04004372 btrfs_set_trans_block_group(trans, dir);
4373
4374 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4375 if (err) {
4376 err = -ENOSPC;
4377 goto out_unlock;
4378 }
4379
Josef Bacikaec74772008-07-24 12:12:38 -04004380 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004381 dentry->d_name.len,
4382 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004383 objectid, BTRFS_I(dir)->block_group, mode,
4384 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004385 err = PTR_ERR(inode);
4386 if (IS_ERR(inode))
4387 goto out_unlock;
4388
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004389 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004390 if (err) {
4391 drop_inode = 1;
4392 goto out_unlock;
4393 }
4394
Chris Mason39279cc2007-06-12 06:35:45 -04004395 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004396 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004397 if (err)
4398 drop_inode = 1;
4399 else {
4400 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004401 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004402 inode->i_fop = &btrfs_file_operations;
4403 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004404 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004405 }
Chris Mason39279cc2007-06-12 06:35:45 -04004406 btrfs_update_inode_block_group(trans, inode);
4407 btrfs_update_inode_block_group(trans, dir);
4408out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004409 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004410 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004411fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004412 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004413 if (drop_inode) {
4414 inode_dec_link_count(inode);
4415 iput(inode);
4416 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004417 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004418 return err;
4419}
4420
4421static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4422 struct dentry *dentry)
4423{
4424 struct btrfs_trans_handle *trans;
4425 struct btrfs_root *root = BTRFS_I(dir)->root;
4426 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004427 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004428 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004429 int err;
4430 int drop_inode = 0;
4431
4432 if (inode->i_nlink == 0)
4433 return -ENOENT;
4434
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004435 /* do not allow sys_link's with other subvols of the same device */
4436 if (root->objectid != BTRFS_I(inode)->root->objectid)
4437 return -EPERM;
4438
Josef Bacik9ed74f22009-09-11 16:12:44 -04004439 /*
4440 * 1 item for inode ref
4441 * 2 items for dir items
4442 */
4443 err = btrfs_reserve_metadata_space(root, 3);
Chris Mason1832a6d2007-12-21 16:27:21 -05004444 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004445 return err;
4446
4447 btrfs_inc_nlink(inode);
4448
Chris Mason3de45862008-11-17 21:02:50 -05004449 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004450 if (err)
4451 goto fail;
4452
Chris Mason39279cc2007-06-12 06:35:45 -04004453 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004454
Chris Mason39279cc2007-06-12 06:35:45 -04004455 btrfs_set_trans_block_group(trans, dir);
4456 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004457
Chris Mason00e4e6b2008-08-05 11:18:09 -04004458 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004459
Yan, Zhenga5719522009-09-24 09:17:31 -04004460 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004461 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004462 } else {
4463 btrfs_update_inode_block_group(trans, dir);
4464 err = btrfs_update_inode(trans, root, inode);
4465 BUG_ON(err);
4466 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4467 }
Chris Mason39279cc2007-06-12 06:35:45 -04004468
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004469 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004470 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004471fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004472 btrfs_unreserve_metadata_space(root, 3);
Chris Mason39279cc2007-06-12 06:35:45 -04004473 if (drop_inode) {
4474 inode_dec_link_count(inode);
4475 iput(inode);
4476 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004477 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004478 return err;
4479}
4480
Chris Mason39279cc2007-06-12 06:35:45 -04004481static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4482{
Chris Masonb9d86662008-05-02 16:13:49 -04004483 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004484 struct btrfs_trans_handle *trans;
4485 struct btrfs_root *root = BTRFS_I(dir)->root;
4486 int err = 0;
4487 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004488 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004489 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004490 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004491
Josef Bacik9ed74f22009-09-11 16:12:44 -04004492 /*
4493 * 2 items for inode and ref
4494 * 2 items for dir items
4495 * 1 for xattr if selinux is on
4496 */
4497 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004498 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004499 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004500
Chris Mason39279cc2007-06-12 06:35:45 -04004501 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004502 if (!trans) {
4503 err = -ENOMEM;
Chris Mason39279cc2007-06-12 06:35:45 -04004504 goto out_unlock;
4505 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004506 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004507
4508 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4509 if (err) {
4510 err = -ENOSPC;
4511 goto out_unlock;
4512 }
4513
Josef Bacikaec74772008-07-24 12:12:38 -04004514 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004515 dentry->d_name.len,
4516 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004517 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4518 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004519 if (IS_ERR(inode)) {
4520 err = PTR_ERR(inode);
4521 goto out_fail;
4522 }
Chris Mason5f39d392007-10-15 16:14:19 -04004523
Chris Mason39279cc2007-06-12 06:35:45 -04004524 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004525
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004526 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004527 if (err)
4528 goto out_fail;
4529
Chris Mason39279cc2007-06-12 06:35:45 -04004530 inode->i_op = &btrfs_dir_inode_operations;
4531 inode->i_fop = &btrfs_dir_file_operations;
4532 btrfs_set_trans_block_group(trans, inode);
4533
Chris Masondbe674a2008-07-17 12:54:05 -04004534 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004535 err = btrfs_update_inode(trans, root, inode);
4536 if (err)
4537 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004538
Chris Masone02119d2008-09-05 16:13:11 -04004539 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4540 inode, dentry->d_name.name,
4541 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004542 if (err)
4543 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004544
Chris Mason39279cc2007-06-12 06:35:45 -04004545 d_instantiate(dentry, inode);
4546 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004547 btrfs_update_inode_block_group(trans, inode);
4548 btrfs_update_inode_block_group(trans, dir);
4549
4550out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004551 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004552 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04004553
Chris Mason39279cc2007-06-12 06:35:45 -04004554out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004555 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004556 if (drop_on_err)
4557 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004558 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004559 return err;
4560}
4561
Chris Masond352ac62008-09-29 15:18:18 -04004562/* helper for btfs_get_extent. Given an existing extent in the tree,
4563 * and an extent that you want to insert, deal with overlap and insert
4564 * the new extent into the tree.
4565 */
Chris Mason3b951512008-04-17 11:29:12 -04004566static int merge_extent_mapping(struct extent_map_tree *em_tree,
4567 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004568 struct extent_map *em,
4569 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004570{
4571 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004572
Chris Masone6dcd2d2008-07-17 12:53:50 -04004573 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4574 start_diff = map_start - em->start;
4575 em->start = map_start;
4576 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004577 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4578 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004579 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004580 em->block_len -= start_diff;
4581 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004582 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004583}
4584
Chris Masonc8b97812008-10-29 14:49:59 -04004585static noinline int uncompress_inline(struct btrfs_path *path,
4586 struct inode *inode, struct page *page,
4587 size_t pg_offset, u64 extent_offset,
4588 struct btrfs_file_extent_item *item)
4589{
4590 int ret;
4591 struct extent_buffer *leaf = path->nodes[0];
4592 char *tmp;
4593 size_t max_size;
4594 unsigned long inline_size;
4595 unsigned long ptr;
4596
4597 WARN_ON(pg_offset != 0);
4598 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4599 inline_size = btrfs_file_extent_inline_item_len(leaf,
4600 btrfs_item_nr(leaf, path->slots[0]));
4601 tmp = kmalloc(inline_size, GFP_NOFS);
4602 ptr = btrfs_file_extent_inline_start(item);
4603
4604 read_extent_buffer(leaf, tmp, ptr, inline_size);
4605
Chris Mason5b050f02008-11-11 09:34:41 -05004606 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004607 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4608 inline_size, max_size);
4609 if (ret) {
4610 char *kaddr = kmap_atomic(page, KM_USER0);
4611 unsigned long copy_size = min_t(u64,
4612 PAGE_CACHE_SIZE - pg_offset,
4613 max_size - extent_offset);
4614 memset(kaddr + pg_offset, 0, copy_size);
4615 kunmap_atomic(kaddr, KM_USER0);
4616 }
4617 kfree(tmp);
4618 return 0;
4619}
4620
Chris Masond352ac62008-09-29 15:18:18 -04004621/*
4622 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004623 * the ugly parts come from merging extents from the disk with the in-ram
4624 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004625 * where the in-ram extents might be locked pending data=ordered completion.
4626 *
4627 * This also copies inline extents directly into the page.
4628 */
Chris Masond3977122009-01-05 21:25:51 -05004629
Chris Masona52d9a82007-08-27 16:49:44 -04004630struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004631 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004632 int create)
4633{
4634 int ret;
4635 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004636 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004637 u64 extent_start = 0;
4638 u64 extent_end = 0;
4639 u64 objectid = inode->i_ino;
4640 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004641 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004642 struct btrfs_root *root = BTRFS_I(inode)->root;
4643 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004644 struct extent_buffer *leaf;
4645 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004646 struct extent_map *em = NULL;
4647 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004648 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004649 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004650 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004651
Chris Masona52d9a82007-08-27 16:49:44 -04004652again:
Chris Mason890871b2009-09-02 16:24:52 -04004653 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004654 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004655 if (em)
4656 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004657 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004658
Chris Masona52d9a82007-08-27 16:49:44 -04004659 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004660 if (em->start > start || em->start + em->len <= start)
4661 free_extent_map(em);
4662 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004663 free_extent_map(em);
4664 else
4665 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004666 }
Chris Masond1310b22008-01-24 16:13:08 -05004667 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004668 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004669 err = -ENOMEM;
4670 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004671 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004672 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004673 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004674 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004675 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004676 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004677
4678 if (!path) {
4679 path = btrfs_alloc_path();
4680 BUG_ON(!path);
4681 }
4682
Chris Mason179e29e2007-11-01 11:28:41 -04004683 ret = btrfs_lookup_file_extent(trans, root, path,
4684 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004685 if (ret < 0) {
4686 err = ret;
4687 goto out;
4688 }
4689
4690 if (ret != 0) {
4691 if (path->slots[0] == 0)
4692 goto not_found;
4693 path->slots[0]--;
4694 }
4695
Chris Mason5f39d392007-10-15 16:14:19 -04004696 leaf = path->nodes[0];
4697 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004698 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004699 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004700 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4701 found_type = btrfs_key_type(&found_key);
4702 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004703 found_type != BTRFS_EXTENT_DATA_KEY) {
4704 goto not_found;
4705 }
4706
Chris Mason5f39d392007-10-15 16:14:19 -04004707 found_type = btrfs_file_extent_type(leaf, item);
4708 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004709 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004710 if (found_type == BTRFS_FILE_EXTENT_REG ||
4711 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004712 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004713 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004714 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4715 size_t size;
4716 size = btrfs_file_extent_inline_len(leaf, item);
4717 extent_end = (extent_start + size + root->sectorsize - 1) &
4718 ~((u64)root->sectorsize - 1);
4719 }
4720
4721 if (start >= extent_end) {
4722 path->slots[0]++;
4723 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4724 ret = btrfs_next_leaf(root, path);
4725 if (ret < 0) {
4726 err = ret;
4727 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004728 }
Yan Zheng9036c102008-10-30 14:19:41 -04004729 if (ret > 0)
4730 goto not_found;
4731 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004732 }
Yan Zheng9036c102008-10-30 14:19:41 -04004733 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4734 if (found_key.objectid != objectid ||
4735 found_key.type != BTRFS_EXTENT_DATA_KEY)
4736 goto not_found;
4737 if (start + len <= found_key.offset)
4738 goto not_found;
4739 em->start = start;
4740 em->len = found_key.offset - start;
4741 goto not_found_em;
4742 }
4743
Yan Zhengd899e052008-10-30 14:25:28 -04004744 if (found_type == BTRFS_FILE_EXTENT_REG ||
4745 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004746 em->start = extent_start;
4747 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004748 em->orig_start = extent_start -
4749 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004750 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4751 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004752 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004753 goto insert;
4754 }
Chris Masonc8b97812008-10-29 14:49:59 -04004755 if (compressed) {
4756 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4757 em->block_start = bytenr;
4758 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4759 item);
4760 } else {
4761 bytenr += btrfs_file_extent_offset(leaf, item);
4762 em->block_start = bytenr;
4763 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004764 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4765 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004766 }
Chris Masona52d9a82007-08-27 16:49:44 -04004767 goto insert;
4768 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004769 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004770 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004771 size_t size;
4772 size_t extent_offset;
4773 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004774
Chris Masona52d9a82007-08-27 16:49:44 -04004775 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004776 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004777 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004778 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004779 goto out;
4780 }
4781
Yan Zheng9036c102008-10-30 14:19:41 -04004782 size = btrfs_file_extent_inline_len(leaf, item);
4783 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004784 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004785 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004786 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004787 em->len = (copy_size + root->sectorsize - 1) &
4788 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004789 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004790 if (compressed)
4791 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004792 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004793 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004794 if (btrfs_file_extent_compression(leaf, item) ==
4795 BTRFS_COMPRESS_ZLIB) {
4796 ret = uncompress_inline(path, inode, page,
4797 pg_offset,
4798 extent_offset, item);
4799 BUG_ON(ret);
4800 } else {
4801 map = kmap(page);
4802 read_extent_buffer(leaf, map + pg_offset, ptr,
4803 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04004804 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4805 memset(map + pg_offset + copy_size, 0,
4806 PAGE_CACHE_SIZE - pg_offset -
4807 copy_size);
4808 }
Chris Masonc8b97812008-10-29 14:49:59 -04004809 kunmap(page);
4810 }
Chris Mason179e29e2007-11-01 11:28:41 -04004811 flush_dcache_page(page);
4812 } else if (create && PageUptodate(page)) {
4813 if (!trans) {
4814 kunmap(page);
4815 free_extent_map(em);
4816 em = NULL;
4817 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004818 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004819 goto again;
4820 }
Chris Masonc8b97812008-10-29 14:49:59 -04004821 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004822 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004823 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004824 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004825 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004826 }
Chris Masond1310b22008-01-24 16:13:08 -05004827 set_extent_uptodate(io_tree, em->start,
4828 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004829 goto insert;
4830 } else {
Chris Masond3977122009-01-05 21:25:51 -05004831 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004832 WARN_ON(1);
4833 }
4834not_found:
4835 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004836 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004837not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004838 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004839 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004840insert:
4841 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004842 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004843 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4844 "[%llu %llu]\n", (unsigned long long)em->start,
4845 (unsigned long long)em->len,
4846 (unsigned long long)start,
4847 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004848 err = -EIO;
4849 goto out;
4850 }
Chris Masond1310b22008-01-24 16:13:08 -05004851
4852 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04004853 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004854 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004855 /* it is possible that someone inserted the extent into the tree
4856 * while we had the lock dropped. It is also possible that
4857 * an overlapping map exists in the tree
4858 */
Chris Masona52d9a82007-08-27 16:49:44 -04004859 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004860 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004861
4862 ret = 0;
4863
Chris Mason3b951512008-04-17 11:29:12 -04004864 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004865 if (existing && (existing->start > start ||
4866 existing->start + existing->len <= start)) {
4867 free_extent_map(existing);
4868 existing = NULL;
4869 }
Chris Mason3b951512008-04-17 11:29:12 -04004870 if (!existing) {
4871 existing = lookup_extent_mapping(em_tree, em->start,
4872 em->len);
4873 if (existing) {
4874 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004875 em, start,
4876 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004877 free_extent_map(existing);
4878 if (err) {
4879 free_extent_map(em);
4880 em = NULL;
4881 }
4882 } else {
4883 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004884 free_extent_map(em);
4885 em = NULL;
4886 }
4887 } else {
4888 free_extent_map(em);
4889 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004890 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004891 }
Chris Masona52d9a82007-08-27 16:49:44 -04004892 }
Chris Mason890871b2009-09-02 16:24:52 -04004893 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004894out:
Chris Masonf4219502008-07-22 11:18:09 -04004895 if (path)
4896 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004897 if (trans) {
4898 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004899 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004900 err = ret;
4901 }
Chris Masona52d9a82007-08-27 16:49:44 -04004902 if (err) {
4903 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04004904 return ERR_PTR(err);
4905 }
4906 return em;
4907}
4908
Chris Mason16432982008-04-10 10:23:21 -04004909static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4910 const struct iovec *iov, loff_t offset,
4911 unsigned long nr_segs)
4912{
Chris Masone1c4b742008-04-22 13:26:46 -04004913 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004914}
4915
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004916static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4917 __u64 start, __u64 len)
4918{
4919 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4920}
4921
Chris Mason9ebefb182007-06-15 13:50:00 -04004922int btrfs_readpage(struct file *file, struct page *page)
4923{
Chris Masond1310b22008-01-24 16:13:08 -05004924 struct extent_io_tree *tree;
4925 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004926 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004927}
Chris Mason1832a6d2007-12-21 16:27:21 -05004928
Chris Mason39279cc2007-06-12 06:35:45 -04004929static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4930{
Chris Masond1310b22008-01-24 16:13:08 -05004931 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004932
4933
4934 if (current->flags & PF_MEMALLOC) {
4935 redirty_page_for_writepage(wbc, page);
4936 unlock_page(page);
4937 return 0;
4938 }
Chris Masond1310b22008-01-24 16:13:08 -05004939 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004940 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4941}
Chris Mason39279cc2007-06-12 06:35:45 -04004942
Chris Masonf4219502008-07-22 11:18:09 -04004943int btrfs_writepages(struct address_space *mapping,
4944 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004945{
Chris Masond1310b22008-01-24 16:13:08 -05004946 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004947
Chris Masond1310b22008-01-24 16:13:08 -05004948 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004949 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4950}
4951
Chris Mason3ab2fb52007-11-08 10:59:22 -05004952static int
4953btrfs_readpages(struct file *file, struct address_space *mapping,
4954 struct list_head *pages, unsigned nr_pages)
4955{
Chris Masond1310b22008-01-24 16:13:08 -05004956 struct extent_io_tree *tree;
4957 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004958 return extent_readpages(tree, mapping, pages, nr_pages,
4959 btrfs_get_extent);
4960}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004961static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004962{
Chris Masond1310b22008-01-24 16:13:08 -05004963 struct extent_io_tree *tree;
4964 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004965 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004966
Chris Masond1310b22008-01-24 16:13:08 -05004967 tree = &BTRFS_I(page->mapping->host)->io_tree;
4968 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004969 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004970 if (ret == 1) {
4971 ClearPagePrivate(page);
4972 set_page_private(page, 0);
4973 page_cache_release(page);
4974 }
4975 return ret;
4976}
Chris Mason39279cc2007-06-12 06:35:45 -04004977
Chris Masone6dcd2d2008-07-17 12:53:50 -04004978static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4979{
Chris Mason98509cf2008-09-11 15:51:43 -04004980 if (PageWriteback(page) || PageDirty(page))
4981 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05004982 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004983}
4984
Chris Masona52d9a82007-08-27 16:49:44 -04004985static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4986{
Chris Masond1310b22008-01-24 16:13:08 -05004987 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004988 struct btrfs_ordered_extent *ordered;
4989 u64 page_start = page_offset(page);
4990 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004991
Chris Mason8b62b722009-09-02 16:53:46 -04004992
4993 /*
4994 * we have the page locked, so new writeback can't start,
4995 * and the dirty bit won't be cleared while we are here.
4996 *
4997 * Wait for IO on this page so that we can safely clear
4998 * the PagePrivate2 bit and do ordered accounting
4999 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005000 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04005001
Chris Masond1310b22008-01-24 16:13:08 -05005002 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005003 if (offset) {
5004 btrfs_releasepage(page, GFP_NOFS);
5005 return;
5006 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005007 lock_extent(tree, page_start, page_end, GFP_NOFS);
5008 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
5009 page_offset(page));
5010 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04005011 /*
5012 * IO on this page will never be started, so we need
5013 * to account for any ordered extents now
5014 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005015 clear_extent_bit(tree, page_start, page_end,
5016 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04005017 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
5018 NULL, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04005019 /*
5020 * whoever cleared the private bit is responsible
5021 * for the finish_ordered_io
5022 */
5023 if (TestClearPagePrivate2(page)) {
5024 btrfs_finish_ordered_io(page->mapping->host,
5025 page_start, page_end);
5026 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005027 btrfs_put_ordered_extent(ordered);
5028 lock_extent(tree, page_start, page_end, GFP_NOFS);
5029 }
5030 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005031 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
5032 EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005033 __btrfs_releasepage(page, GFP_NOFS);
5034
Chris Mason4a096752008-07-21 10:29:44 -04005035 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005036 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005037 ClearPagePrivate(page);
5038 set_page_private(page, 0);
5039 page_cache_release(page);
5040 }
Chris Mason39279cc2007-06-12 06:35:45 -04005041}
5042
Chris Mason9ebefb182007-06-15 13:50:00 -04005043/*
5044 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
5045 * called from a page fault handler when a page is first dirtied. Hence we must
5046 * be careful to check for EOF conditions here. We set the page up correctly
5047 * for a written page which means we get ENOSPC checking when writing into
5048 * holes and correct delalloc and unwritten extent mapping on filesystems that
5049 * support these features.
5050 *
5051 * We are not allowed to take the i_mutex here so we have to play games to
5052 * protect against truncate races as the page could now be beyond EOF. Because
5053 * vmtruncate() writes the inode size before removing pages, once we have the
5054 * page lock we can determine safely if the page is beyond EOF. If it is not
5055 * beyond EOF, then the page is guaranteed safe against truncation until we
5056 * unlock the page.
5057 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07005058int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04005059{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005060 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05005061 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05005062 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005063 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5064 struct btrfs_ordered_extent *ordered;
5065 char *kaddr;
5066 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04005067 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05005068 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04005069 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005070 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04005071
Josef Bacik6a632092009-02-20 11:00:09 -05005072 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07005073 if (ret) {
5074 if (ret == -ENOMEM)
5075 ret = VM_FAULT_OOM;
5076 else /* -ENOSPC, -EIO, etc */
5077 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05005078 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07005079 }
Chris Mason1832a6d2007-12-21 16:27:21 -05005080
Josef Bacik9ed74f22009-09-11 16:12:44 -04005081 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
5082 if (ret) {
5083 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5084 ret = VM_FAULT_SIGBUS;
5085 goto out;
5086 }
5087
Nick Piggin56a76f82009-03-31 15:23:23 -07005088 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005089again:
Chris Mason9ebefb182007-06-15 13:50:00 -04005090 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04005091 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005092 page_start = page_offset(page);
5093 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005094
Chris Mason9ebefb182007-06-15 13:50:00 -04005095 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04005096 (page_start >= size)) {
Josef Bacik6a632092009-02-20 11:00:09 -05005097 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason9ebefb182007-06-15 13:50:00 -04005098 /* page got truncated out from underneath us */
5099 goto out_unlock;
5100 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005101 wait_on_page_writeback(page);
5102
5103 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
5104 set_page_extent_mapped(page);
5105
Chris Masoneb84ae02008-07-17 13:53:27 -04005106 /*
5107 * we can't set the delalloc bits if there are pending ordered
5108 * extents. Drop our locks and wait for them to finish
5109 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005110 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5111 if (ordered) {
5112 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5113 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005114 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005115 btrfs_put_ordered_extent(ordered);
5116 goto again;
5117 }
5118
Josef Bacikfbf19082009-10-01 17:10:23 -04005119 /*
5120 * XXX - page_mkwrite gets called every time the page is dirtied, even
5121 * if it was already dirty, so for space accounting reasons we need to
5122 * clear any delalloc bits for the range we are fixing to save. There
5123 * is probably a better way to do this, but for now keep consistent with
5124 * prepare_pages in the normal write path.
5125 */
5126 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005127 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
5128 GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04005129
Josef Bacik9ed74f22009-09-11 16:12:44 -04005130 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
5131 if (ret) {
5132 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5133 ret = VM_FAULT_SIGBUS;
Josef Bacikfbf19082009-10-01 17:10:23 -04005134 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005135 goto out_unlock;
5136 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005137 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04005138
5139 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04005140 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005141 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04005142 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04005143 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04005144
Chris Masone6dcd2d2008-07-17 12:53:50 -04005145 if (zero_start != PAGE_CACHE_SIZE) {
5146 kaddr = kmap(page);
5147 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5148 flush_dcache_page(page);
5149 kunmap(page);
5150 }
Chris Mason247e7432008-07-17 12:53:51 -04005151 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005152 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04005153 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005154
Chris Mason257c62e2009-10-13 13:21:08 -04005155 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5156 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5157
Chris Masone6dcd2d2008-07-17 12:53:50 -04005158 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04005159
5160out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005161 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason50a9b212009-09-11 12:33:12 -04005162 if (!ret)
5163 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04005164 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05005165out:
Chris Mason9ebefb182007-06-15 13:50:00 -04005166 return ret;
5167}
5168
Chris Mason39279cc2007-06-12 06:35:45 -04005169static void btrfs_truncate(struct inode *inode)
5170{
5171 struct btrfs_root *root = BTRFS_I(inode)->root;
5172 int ret;
5173 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005174 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04005175 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005176
Yan, Zheng80825102009-11-12 09:35:36 +00005177 if (!S_ISREG(inode->i_mode)) {
5178 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04005179 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005180 }
Chris Mason39279cc2007-06-12 06:35:45 -04005181
Josef Bacik5d5e1032009-10-13 16:46:49 -04005182 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5183 if (ret)
5184 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005185
Chris Mason4a096752008-07-21 10:29:44 -04005186 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00005187 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005188
Chris Mason39279cc2007-06-12 06:35:45 -04005189 trans = btrfs_start_transaction(root, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00005190 btrfs_set_trans_block_group(trans, inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005191
5192 /*
5193 * setattr is responsible for setting the ordered_data_close flag,
5194 * but that is only tested during the last file release. That
5195 * could happen well after the next commit, leaving a great big
5196 * window where new writes may get lost if someone chooses to write
5197 * to this file after truncating to zero
5198 *
5199 * The inode doesn't have any dirty data here, and so if we commit
5200 * this is a noop. If someone immediately starts writing to the inode
5201 * it is very likely we'll catch some of their writes in this
5202 * transaction, and the commit will find this file on the ordered
5203 * data list with good things to send down.
5204 *
5205 * This is a best effort solution, there is still a window where
5206 * using truncate to replace the contents of the file will
5207 * end up with a zero length file after a crash.
5208 */
5209 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5210 btrfs_add_ordered_operation(trans, root, inode);
5211
Yan, Zheng80825102009-11-12 09:35:36 +00005212 while (1) {
5213 ret = btrfs_truncate_inode_items(trans, root, inode,
5214 inode->i_size,
5215 BTRFS_EXTENT_DATA_KEY);
5216 if (ret != -EAGAIN)
5217 break;
Chris Mason39279cc2007-06-12 06:35:45 -04005218
Yan, Zheng80825102009-11-12 09:35:36 +00005219 ret = btrfs_update_inode(trans, root, inode);
5220 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005221
Yan, Zheng80825102009-11-12 09:35:36 +00005222 nr = trans->blocks_used;
5223 btrfs_end_transaction(trans, root);
5224 btrfs_btree_balance_dirty(root, nr);
5225
5226 trans = btrfs_start_transaction(root, 1);
5227 btrfs_set_trans_block_group(trans, inode);
5228 }
5229
5230 if (ret == 0 && inode->i_nlink > 0) {
5231 ret = btrfs_orphan_del(trans, inode);
5232 BUG_ON(ret);
5233 }
5234
5235 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04005236 BUG_ON(ret);
5237
Josef Bacik7b128762008-07-24 12:17:14 -04005238 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04005239 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005240 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005241 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005242}
5243
Sven Wegener3b963622008-06-09 21:57:42 -04005244/*
Chris Masond352ac62008-09-29 15:18:18 -04005245 * create a new subvolume directory/inode (helper for the ioctl).
5246 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05005247int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04005248 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005249 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04005250{
Chris Mason39279cc2007-06-12 06:35:45 -04005251 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005252 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005253 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005254
Josef Bacikaec74772008-07-24 12:12:38 -04005255 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005256 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04005257 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005258 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005259 inode->i_op = &btrfs_dir_inode_operations;
5260 inode->i_fop = &btrfs_dir_file_operations;
5261
Chris Mason39279cc2007-06-12 06:35:45 -04005262 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04005263 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04005264
Yan, Zheng76dda932009-09-21 16:00:26 -04005265 err = btrfs_update_inode(trans, new_root, inode);
5266 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005267
Yan, Zheng76dda932009-09-21 16:00:26 -04005268 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005269 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005270}
5271
Chris Masond352ac62008-09-29 15:18:18 -04005272/* helper function for file defrag and space balancing. This
5273 * forces readahead on a given range of bytes in an inode
5274 */
Chris Masonedbd8d42007-12-21 16:27:24 -05005275unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04005276 struct file_ra_state *ra, struct file *file,
5277 pgoff_t offset, pgoff_t last_index)
5278{
Chris Mason8e7bf942008-04-28 09:02:36 -04005279 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04005280
Chris Mason86479a02007-09-10 19:58:16 -04005281 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5282 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04005283}
5284
Chris Mason39279cc2007-06-12 06:35:45 -04005285struct inode *btrfs_alloc_inode(struct super_block *sb)
5286{
5287 struct btrfs_inode *ei;
5288
5289 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5290 if (!ei)
5291 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04005292 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04005293 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005294 ei->logged_trans = 0;
Josef Bacik32c00af2009-10-08 13:34:05 -04005295 ei->outstanding_extents = 0;
5296 ei->reserved_extents = 0;
Josef Bacika6dbd422009-11-11 15:53:34 -05005297 ei->root = NULL;
Josef Bacik32c00af2009-10-08 13:34:05 -04005298 spin_lock_init(&ei->accounting_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005299 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04005300 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005301 INIT_LIST_HEAD(&ei->ordered_operations);
Chris Mason39279cc2007-06-12 06:35:45 -04005302 return &ei->vfs_inode;
5303}
5304
5305void btrfs_destroy_inode(struct inode *inode)
5306{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005307 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005308 struct btrfs_root *root = BTRFS_I(inode)->root;
5309
Chris Mason39279cc2007-06-12 06:35:45 -04005310 WARN_ON(!list_empty(&inode->i_dentry));
5311 WARN_ON(inode->i_data.nrpages);
5312
Chris Mason5a3f23d2009-03-31 13:27:11 -04005313 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05005314 * This can happen where we create an inode, but somebody else also
5315 * created the same inode and we need to destroy the one we already
5316 * created.
5317 */
5318 if (!root)
5319 goto free;
5320
5321 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005322 * Make sure we're properly removed from the ordered operation
5323 * lists.
5324 */
5325 smp_mb();
5326 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5327 spin_lock(&root->fs_info->ordered_extent_lock);
5328 list_del_init(&BTRFS_I(inode)->ordered_operations);
5329 spin_unlock(&root->fs_info->ordered_extent_lock);
5330 }
5331
5332 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005333 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00005334 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
5335 inode->i_ino);
5336 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04005337 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04005338 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005339
Chris Masond3977122009-01-05 21:25:51 -05005340 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005341 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5342 if (!ordered)
5343 break;
5344 else {
Chris Masond3977122009-01-05 21:25:51 -05005345 printk(KERN_ERR "btrfs found ordered "
5346 "extent %llu %llu on inode cleanup\n",
5347 (unsigned long long)ordered->file_offset,
5348 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005349 btrfs_remove_ordered_extent(inode, ordered);
5350 btrfs_put_ordered_extent(ordered);
5351 btrfs_put_ordered_extent(ordered);
5352 }
5353 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005354 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005355 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05005356free:
Chris Mason39279cc2007-06-12 06:35:45 -04005357 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5358}
5359
Yan, Zheng76dda932009-09-21 16:00:26 -04005360void btrfs_drop_inode(struct inode *inode)
5361{
5362 struct btrfs_root *root = BTRFS_I(inode)->root;
5363
5364 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5365 generic_delete_inode(inode);
5366 else
5367 generic_drop_inode(inode);
5368}
5369
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005370static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04005371{
5372 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5373
5374 inode_init_once(&ei->vfs_inode);
5375}
5376
5377void btrfs_destroy_cachep(void)
5378{
5379 if (btrfs_inode_cachep)
5380 kmem_cache_destroy(btrfs_inode_cachep);
5381 if (btrfs_trans_handle_cachep)
5382 kmem_cache_destroy(btrfs_trans_handle_cachep);
5383 if (btrfs_transaction_cachep)
5384 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04005385 if (btrfs_path_cachep)
5386 kmem_cache_destroy(btrfs_path_cachep);
5387}
5388
5389int btrfs_init_cachep(void)
5390{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005391 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5392 sizeof(struct btrfs_inode), 0,
5393 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04005394 if (!btrfs_inode_cachep)
5395 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005396
5397 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5398 sizeof(struct btrfs_trans_handle), 0,
5399 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005400 if (!btrfs_trans_handle_cachep)
5401 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005402
5403 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5404 sizeof(struct btrfs_transaction), 0,
5405 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005406 if (!btrfs_transaction_cachep)
5407 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005408
5409 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5410 sizeof(struct btrfs_path), 0,
5411 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005412 if (!btrfs_path_cachep)
5413 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005414
Chris Mason39279cc2007-06-12 06:35:45 -04005415 return 0;
5416fail:
5417 btrfs_destroy_cachep();
5418 return -ENOMEM;
5419}
5420
5421static int btrfs_getattr(struct vfsmount *mnt,
5422 struct dentry *dentry, struct kstat *stat)
5423{
5424 struct inode *inode = dentry->d_inode;
5425 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05005426 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05005427 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005428 stat->blocks = (inode_get_bytes(inode) +
5429 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04005430 return 0;
5431}
5432
Chris Masond3977122009-01-05 21:25:51 -05005433static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5434 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005435{
5436 struct btrfs_trans_handle *trans;
5437 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005438 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005439 struct inode *new_inode = new_dentry->d_inode;
5440 struct inode *old_inode = old_dentry->d_inode;
5441 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005442 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005443 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005444 int ret;
5445
Yan, Zhengf679a842009-09-24 09:17:31 -04005446 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5447 return -EPERM;
5448
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005449 /* we only allow rename subvolume link between subvolumes */
5450 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05005451 return -EXDEV;
5452
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005453 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5454 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5455 return -ENOTEMPTY;
5456
Chris Mason39279cc2007-06-12 06:35:45 -04005457 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005458 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04005459 return -ENOTEMPTY;
Chris Mason0660b5a2008-11-17 20:37:39 -05005460
Josef Bacik9ed74f22009-09-11 16:12:44 -04005461 /*
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005462 * We want to reserve the absolute worst case amount of items. So if
5463 * both inodes are subvols and we need to unlink them then that would
5464 * require 4 item modifications, but if they are both normal inodes it
5465 * would require 5 item modifications, so we'll assume their normal
5466 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5467 * should cover the worst case number of items we'll modify.
Josef Bacik9ed74f22009-09-11 16:12:44 -04005468 */
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005469 ret = btrfs_reserve_metadata_space(root, 11);
Chris Mason1832a6d2007-12-21 16:27:21 -05005470 if (ret)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005471 return ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05005472
Chris Mason5a3f23d2009-03-31 13:27:11 -04005473 /*
5474 * we're using rename to replace one file with another.
5475 * and the replacement file is large. Start IO on it now so
5476 * we don't add too much work to the end of the transaction
5477 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04005478 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04005479 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5480 filemap_flush(old_inode->i_mapping);
5481
Yan, Zheng76dda932009-09-21 16:00:26 -04005482 /* close the racy window with snapshot create/destroy ioctl */
5483 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5484 down_read(&root->fs_info->subvol_sem);
Chris Mason5f39d392007-10-15 16:14:19 -04005485
Chris Mason39279cc2007-06-12 06:35:45 -04005486 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga5719522009-09-24 09:17:31 -04005487 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04005488
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005489 if (dest != root)
5490 btrfs_record_root_in_trans(trans, dest);
5491
Yan, Zhenga5719522009-09-24 09:17:31 -04005492 ret = btrfs_set_inode_index(new_dir, &index);
5493 if (ret)
5494 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005495
Yan, Zhenga5719522009-09-24 09:17:31 -04005496 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005497 /* force full log commit if subvolume involved. */
5498 root->fs_info->last_trans_log_full_commit = trans->transid;
5499 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04005500 ret = btrfs_insert_inode_ref(trans, dest,
5501 new_dentry->d_name.name,
5502 new_dentry->d_name.len,
5503 old_inode->i_ino,
5504 new_dir->i_ino, index);
5505 if (ret)
5506 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005507 /*
5508 * this is an ugly little race, but the rename is required
5509 * to make sure that if we crash, the inode is either at the
5510 * old name or the new one. pinning the log transaction lets
5511 * us make sure we don't allow a log commit to come in after
5512 * we unlink the name but before we add the new name back in.
5513 */
5514 btrfs_pin_log_trans(root);
5515 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005516 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005517 * make sure the inode gets flushed if it is replacing
5518 * something.
5519 */
5520 if (new_inode && new_inode->i_size &&
5521 old_inode && S_ISREG(old_inode->i_mode)) {
5522 btrfs_add_ordered_operation(trans, root, old_inode);
5523 }
5524
Chris Mason39279cc2007-06-12 06:35:45 -04005525 old_dir->i_ctime = old_dir->i_mtime = ctime;
5526 new_dir->i_ctime = new_dir->i_mtime = ctime;
5527 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04005528
Chris Mason12fcfd22009-03-24 10:24:20 -04005529 if (old_dentry->d_parent != new_dentry->d_parent)
5530 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5531
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005532 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5533 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5534 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5535 old_dentry->d_name.name,
5536 old_dentry->d_name.len);
5537 } else {
5538 btrfs_inc_nlink(old_dentry->d_inode);
5539 ret = btrfs_unlink_inode(trans, root, old_dir,
5540 old_dentry->d_inode,
5541 old_dentry->d_name.name,
5542 old_dentry->d_name.len);
5543 }
5544 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005545
5546 if (new_inode) {
5547 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005548 if (unlikely(new_inode->i_ino ==
5549 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5550 root_objectid = BTRFS_I(new_inode)->location.objectid;
5551 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5552 root_objectid,
5553 new_dentry->d_name.name,
5554 new_dentry->d_name.len);
5555 BUG_ON(new_inode->i_nlink == 0);
5556 } else {
5557 ret = btrfs_unlink_inode(trans, dest, new_dir,
5558 new_dentry->d_inode,
5559 new_dentry->d_name.name,
5560 new_dentry->d_name.len);
5561 }
5562 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005563 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04005564 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005565 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005566 }
Chris Mason39279cc2007-06-12 06:35:45 -04005567 }
Josef Bacikaec74772008-07-24 12:12:38 -04005568
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005569 ret = btrfs_add_link(trans, new_dir, old_inode,
5570 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04005571 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005572 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005573
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005574 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5575 btrfs_log_new_name(trans, old_inode, old_dir,
5576 new_dentry->d_parent);
5577 btrfs_end_log_trans(root);
5578 }
Chris Mason39279cc2007-06-12 06:35:45 -04005579out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04005580 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005581
Yan, Zheng76dda932009-09-21 16:00:26 -04005582 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5583 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005584
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005585 btrfs_unreserve_metadata_space(root, 11);
Chris Mason39279cc2007-06-12 06:35:45 -04005586 return ret;
5587}
5588
Chris Masond352ac62008-09-29 15:18:18 -04005589/*
5590 * some fairly slow code that needs optimization. This walks the list
5591 * of all the inodes with pending delalloc and forces them to disk.
5592 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005593int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04005594{
5595 struct list_head *head = &root->fs_info->delalloc_inodes;
5596 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005597 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04005598
Yan Zhengc146afa2008-11-12 14:34:12 -05005599 if (root->fs_info->sb->s_flags & MS_RDONLY)
5600 return -EROFS;
5601
Chris Mason75eff682008-12-15 15:54:40 -05005602 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05005603 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04005604 binode = list_entry(head->next, struct btrfs_inode,
5605 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005606 inode = igrab(&binode->vfs_inode);
5607 if (!inode)
5608 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05005609 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005610 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005611 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005612 if (delay_iput)
5613 btrfs_add_delayed_iput(inode);
5614 else
5615 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005616 }
5617 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05005618 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04005619 }
Chris Mason75eff682008-12-15 15:54:40 -05005620 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04005621
5622 /* the filemap_flush will queue IO into the worker threads, but
5623 * we have to make sure the IO is actually started and that
5624 * ordered extents get created before we return
5625 */
5626 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05005627 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05005628 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005629 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05005630 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5631 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04005632 }
5633 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04005634 return 0;
5635}
5636
Chris Mason39279cc2007-06-12 06:35:45 -04005637static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5638 const char *symname)
5639{
5640 struct btrfs_trans_handle *trans;
5641 struct btrfs_root *root = BTRFS_I(dir)->root;
5642 struct btrfs_path *path;
5643 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05005644 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005645 int err;
5646 int drop_inode = 0;
5647 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005648 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04005649 int name_len;
5650 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04005651 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005652 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04005653 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05005654 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005655
5656 name_len = strlen(symname) + 1;
5657 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5658 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05005659
Josef Bacik9ed74f22009-09-11 16:12:44 -04005660 /*
5661 * 2 items for inode item and ref
5662 * 2 items for dir items
5663 * 1 item for xattr if selinux is on
5664 */
5665 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05005666 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04005667 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05005668
Chris Mason39279cc2007-06-12 06:35:45 -04005669 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005670 if (!trans)
5671 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04005672 btrfs_set_trans_block_group(trans, dir);
5673
5674 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5675 if (err) {
5676 err = -ENOSPC;
5677 goto out_unlock;
5678 }
5679
Josef Bacikaec74772008-07-24 12:12:38 -04005680 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05005681 dentry->d_name.len,
5682 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04005683 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5684 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005685 err = PTR_ERR(inode);
5686 if (IS_ERR(inode))
5687 goto out_unlock;
5688
Yan, Zhengf34f57a2009-11-12 09:35:27 +00005689 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04005690 if (err) {
5691 drop_inode = 1;
5692 goto out_unlock;
5693 }
5694
Chris Mason39279cc2007-06-12 06:35:45 -04005695 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005696 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005697 if (err)
5698 drop_inode = 1;
5699 else {
5700 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005701 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04005702 inode->i_fop = &btrfs_file_operations;
5703 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05005704 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04005705 }
Chris Mason39279cc2007-06-12 06:35:45 -04005706 btrfs_update_inode_block_group(trans, inode);
5707 btrfs_update_inode_block_group(trans, dir);
5708 if (drop_inode)
5709 goto out_unlock;
5710
5711 path = btrfs_alloc_path();
5712 BUG_ON(!path);
5713 key.objectid = inode->i_ino;
5714 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005715 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5716 datasize = btrfs_file_extent_calc_inline_size(name_len);
5717 err = btrfs_insert_empty_item(trans, root, path, &key,
5718 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005719 if (err) {
5720 drop_inode = 1;
5721 goto out_unlock;
5722 }
Chris Mason5f39d392007-10-15 16:14:19 -04005723 leaf = path->nodes[0];
5724 ei = btrfs_item_ptr(leaf, path->slots[0],
5725 struct btrfs_file_extent_item);
5726 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5727 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04005728 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04005729 btrfs_set_file_extent_encryption(leaf, ei, 0);
5730 btrfs_set_file_extent_compression(leaf, ei, 0);
5731 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5732 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5733
Chris Mason39279cc2007-06-12 06:35:45 -04005734 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04005735 write_extent_buffer(leaf, symname, ptr, name_len);
5736 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04005737 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04005738
Chris Mason39279cc2007-06-12 06:35:45 -04005739 inode->i_op = &btrfs_symlink_inode_operations;
5740 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04005741 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04005742 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04005743 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005744 err = btrfs_update_inode(trans, root, inode);
5745 if (err)
5746 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005747
5748out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005749 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04005750 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005751out_fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005752 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04005753 if (drop_inode) {
5754 inode_dec_link_count(inode);
5755 iput(inode);
5756 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005757 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005758 return err;
5759}
Chris Mason16432982008-04-10 10:23:21 -04005760
Yan, Zheng5a303d52009-11-12 09:34:52 +00005761static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005762 u64 alloc_hint, int mode, loff_t actual_len)
Yan Zhengd899e052008-10-30 14:25:28 -04005763{
Yan, Zheng5a303d52009-11-12 09:34:52 +00005764 struct btrfs_trans_handle *trans;
Yan Zhengd899e052008-10-30 14:25:28 -04005765 struct btrfs_root *root = BTRFS_I(inode)->root;
5766 struct btrfs_key ins;
5767 u64 alloc_size;
5768 u64 cur_offset = start;
5769 u64 num_bytes = end - start;
5770 int ret = 0;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005771 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04005772
Yan Zhengd899e052008-10-30 14:25:28 -04005773 while (num_bytes > 0) {
5774 alloc_size = min(num_bytes, root->fs_info->max_extent);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005775
Chris Mason3a1abec2009-12-17 15:47:17 -05005776 trans = btrfs_start_transaction(root, 1);
5777
Yan Zhengd899e052008-10-30 14:25:28 -04005778 ret = btrfs_reserve_extent(trans, root, alloc_size,
5779 root->sectorsize, 0, alloc_hint,
5780 (u64)-1, &ins, 1);
5781 if (ret) {
5782 WARN_ON(1);
Chris Mason3a1abec2009-12-17 15:47:17 -05005783 goto stop_trans;
Yan Zhengd899e052008-10-30 14:25:28 -04005784 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00005785
5786 ret = btrfs_reserve_metadata_space(root, 3);
5787 if (ret) {
5788 btrfs_free_reserved_extent(root, ins.objectid,
5789 ins.offset);
Chris Mason3a1abec2009-12-17 15:47:17 -05005790 goto stop_trans;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005791 }
5792
Yan Zhengd899e052008-10-30 14:25:28 -04005793 ret = insert_reserved_file_extent(trans, inode,
5794 cur_offset, ins.objectid,
5795 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00005796 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04005797 BTRFS_FILE_EXTENT_PREALLOC);
5798 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04005799 btrfs_drop_extent_cache(inode, cur_offset,
5800 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005801
Yan Zhengd899e052008-10-30 14:25:28 -04005802 num_bytes -= ins.offset;
5803 cur_offset += ins.offset;
5804 alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005805
Yan Zhengd899e052008-10-30 14:25:28 -04005806 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005807 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04005808 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Aneesh Kumar K.V23b5c502010-02-04 11:33:03 -05005809 (actual_len > inode->i_size) &&
5810 (cur_offset > inode->i_size)) {
5811
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005812 if (cur_offset > actual_len)
5813 i_size = actual_len;
5814 else
5815 i_size = cur_offset;
5816 i_size_write(inode, i_size);
5817 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005818 }
5819
Yan Zhengd899e052008-10-30 14:25:28 -04005820 ret = btrfs_update_inode(trans, root, inode);
5821 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04005822
Yan, Zheng5a303d52009-11-12 09:34:52 +00005823 btrfs_end_transaction(trans, root);
5824 btrfs_unreserve_metadata_space(root, 3);
5825 }
Yan Zhengd899e052008-10-30 14:25:28 -04005826 return ret;
Chris Mason3a1abec2009-12-17 15:47:17 -05005827
5828stop_trans:
5829 btrfs_end_transaction(trans, root);
5830 return ret;
5831
Yan Zhengd899e052008-10-30 14:25:28 -04005832}
5833
5834static long btrfs_fallocate(struct inode *inode, int mode,
5835 loff_t offset, loff_t len)
5836{
5837 u64 cur_offset;
5838 u64 last_byte;
5839 u64 alloc_start;
5840 u64 alloc_end;
5841 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04005842 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04005843 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5844 struct extent_map *em;
5845 int ret;
5846
5847 alloc_start = offset & ~mask;
5848 alloc_end = (offset + len + mask) & ~mask;
5849
Chris Mason546888d2009-04-21 11:53:38 -04005850 /*
5851 * wait for ordered IO before we have any locks. We'll loop again
5852 * below with the locks held.
5853 */
5854 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5855
Yan Zhengd899e052008-10-30 14:25:28 -04005856 mutex_lock(&inode->i_mutex);
5857 if (alloc_start > inode->i_size) {
5858 ret = btrfs_cont_expand(inode, alloc_start);
5859 if (ret)
5860 goto out;
5861 }
5862
Yan, Zheng5a303d52009-11-12 09:34:52 +00005863 ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode,
Josef Bacika970b0a2009-06-27 21:07:34 -04005864 alloc_end - alloc_start);
5865 if (ret)
5866 goto out;
5867
Chris Masone980b502009-04-24 14:39:24 -04005868 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04005869 while (1) {
5870 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04005871
Chris Mason546888d2009-04-21 11:53:38 -04005872 /* the extent lock is ordered inside the running
5873 * transaction
5874 */
Chris Masone980b502009-04-24 14:39:24 -04005875 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5876 GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04005877 ordered = btrfs_lookup_first_ordered_extent(inode,
5878 alloc_end - 1);
5879 if (ordered &&
5880 ordered->file_offset + ordered->len > alloc_start &&
5881 ordered->file_offset < alloc_end) {
5882 btrfs_put_ordered_extent(ordered);
5883 unlock_extent(&BTRFS_I(inode)->io_tree,
Chris Masone980b502009-04-24 14:39:24 -04005884 alloc_start, locked_end, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005885 /*
5886 * we can't wait on the range with the transaction
5887 * running or with the extent lock held
5888 */
Yan Zhengd899e052008-10-30 14:25:28 -04005889 btrfs_wait_ordered_range(inode, alloc_start,
5890 alloc_end - alloc_start);
5891 } else {
5892 if (ordered)
5893 btrfs_put_ordered_extent(ordered);
5894 break;
5895 }
5896 }
5897
5898 cur_offset = alloc_start;
5899 while (1) {
5900 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5901 alloc_end - cur_offset, 0);
5902 BUG_ON(IS_ERR(em) || !em);
5903 last_byte = min(extent_map_end(em), alloc_end);
5904 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005905 if (em->block_start == EXTENT_MAP_HOLE ||
5906 (cur_offset >= inode->i_size &&
5907 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5908 ret = prealloc_file_range(inode,
5909 cur_offset, last_byte,
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005910 alloc_hint, mode, offset+len);
Yan Zhengd899e052008-10-30 14:25:28 -04005911 if (ret < 0) {
5912 free_extent_map(em);
5913 break;
5914 }
5915 }
5916 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5917 alloc_hint = em->block_start;
5918 free_extent_map(em);
5919
5920 cur_offset = last_byte;
5921 if (cur_offset >= alloc_end) {
5922 ret = 0;
5923 break;
5924 }
5925 }
Chris Masone980b502009-04-24 14:39:24 -04005926 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04005927 GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005928
Yan, Zheng5a303d52009-11-12 09:34:52 +00005929 btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode,
5930 alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04005931out:
5932 mutex_unlock(&inode->i_mutex);
5933 return ret;
5934}
5935
Chris Masone6dcd2d2008-07-17 12:53:50 -04005936static int btrfs_set_page_dirty(struct page *page)
5937{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005938 return __set_page_dirty_nobuffers(page);
5939}
5940
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005941static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05005942{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005943 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05005944 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04005945 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05005946}
Chris Mason39279cc2007-06-12 06:35:45 -04005947
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005948static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05005949 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005950 .lookup = btrfs_lookup,
5951 .create = btrfs_create,
5952 .unlink = btrfs_unlink,
5953 .link = btrfs_link,
5954 .mkdir = btrfs_mkdir,
5955 .rmdir = btrfs_rmdir,
5956 .rename = btrfs_rename,
5957 .symlink = btrfs_symlink,
5958 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005959 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005960 .setxattr = btrfs_setxattr,
5961 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005962 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005963 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005964 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005965};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005966static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005967 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05005968 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005969};
Yan, Zheng76dda932009-09-21 16:00:26 -04005970
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005971static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005972 .llseek = generic_file_llseek,
5973 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01005974 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005975 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005976#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005977 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005978#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04005979 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04005980 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04005981};
5982
Chris Masond1310b22008-01-24 16:13:08 -05005983static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04005984 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05005985 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04005986 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005987 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005988 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04005989 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04005990 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05005991 .set_bit_hook = btrfs_set_bit_hook,
5992 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005993 .merge_extent_hook = btrfs_merge_extent_hook,
5994 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005995};
5996
Chris Mason35054392009-01-21 13:11:13 -05005997/*
5998 * btrfs doesn't support the bmap operation because swapfiles
5999 * use bmap to make a mapping of extents in the file. They assume
6000 * these extents won't change over the life of the file and they
6001 * use the bmap result to do IO directly to the drive.
6002 *
6003 * the btrfs bmap call would return logical addresses that aren't
6004 * suitable for IO and they also will change frequently as COW
6005 * operations happen. So, swapfile + btrfs == corruption.
6006 *
6007 * For now we're avoiding this by dropping bmap.
6008 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006009static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006010 .readpage = btrfs_readpage,
6011 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04006012 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05006013 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04006014 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04006015 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04006016 .invalidatepage = btrfs_invalidatepage,
6017 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006018 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02006019 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04006020};
6021
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006022static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006023 .readpage = btrfs_readpage,
6024 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04006025 .invalidatepage = btrfs_invalidatepage,
6026 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04006027};
6028
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006029static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006030 .truncate = btrfs_truncate,
6031 .getattr = btrfs_getattr,
6032 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006033 .setxattr = btrfs_setxattr,
6034 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05006035 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006036 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05006037 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04006038 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006039 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04006040};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006041static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04006042 .getattr = btrfs_getattr,
6043 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05006044 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006045 .setxattr = btrfs_setxattr,
6046 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04006047 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006048 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04006049};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006050static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006051 .readlink = generic_readlink,
6052 .follow_link = page_follow_link_light,
6053 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05006054 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05006055 .setxattr = btrfs_setxattr,
6056 .getxattr = btrfs_getxattr,
6057 .listxattr = btrfs_listxattr,
6058 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04006059};
Yan, Zheng76dda932009-09-21 16:00:26 -04006060
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04006061const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04006062 .d_delete = btrfs_dentry_delete,
6063};