blob: 4deb280f8969b78e373257d394106895d0ea82a6 [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 Masonc8b97812008-10-29 14:49:59 -0400382 btrfs_test_opt(root, COMPRESS)) {
383 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400384 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400385
Chris Masonc8b97812008-10-29 14:49:59 -0400386 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
387 total_compressed, pages,
388 nr_pages, &nr_pages_ret,
389 &total_in,
390 &total_compressed,
391 max_compressed);
392
393 if (!ret) {
394 unsigned long offset = total_compressed &
395 (PAGE_CACHE_SIZE - 1);
396 struct page *page = pages[nr_pages_ret - 1];
397 char *kaddr;
398
399 /* zero the tail end of the last page, we might be
400 * sending it down to disk
401 */
402 if (offset) {
403 kaddr = kmap_atomic(page, KM_USER0);
404 memset(kaddr + offset, 0,
405 PAGE_CACHE_SIZE - offset);
406 kunmap_atomic(kaddr, KM_USER0);
407 }
408 will_compress = 1;
409 }
410 }
411 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500412 trans = btrfs_join_transaction(root, 1);
413 BUG_ON(!trans);
414 btrfs_set_trans_block_group(trans, inode);
415
Chris Masonc8b97812008-10-29 14:49:59 -0400416 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500417 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400418 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500419 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400420 */
421 ret = cow_file_range_inline(trans, root, inode,
422 start, end, 0, NULL);
423 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500424 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400425 ret = cow_file_range_inline(trans, root, inode,
426 start, end,
427 total_compressed, pages);
428 }
429 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500430 /*
431 * inline extent creation worked, we don't need
432 * to create any more async work items. Unlock
433 * and free up our temp pages.
434 */
Chris Masonc8b97812008-10-29 14:49:59 -0400435 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400436 &BTRFS_I(inode)->io_tree,
437 start, end, NULL,
438 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400439 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400440 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400441 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000442
443 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400444 goto free_pages_out;
445 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000446 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400447 }
448
449 if (will_compress) {
450 /*
451 * we aren't doing an inline extent round the compressed size
452 * up to a block size boundary so the allocator does sane
453 * things
454 */
455 total_compressed = (total_compressed + blocksize - 1) &
456 ~(blocksize - 1);
457
458 /*
459 * one last check to make sure the compression is really a
460 * win, compare the page count read with the blocks on disk
461 */
462 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
463 ~(PAGE_CACHE_SIZE - 1);
464 if (total_compressed >= total_in) {
465 will_compress = 0;
466 } else {
467 disk_num_bytes = total_compressed;
468 num_bytes = total_in;
469 }
470 }
471 if (!will_compress && pages) {
472 /*
473 * the compression code ran but failed to make things smaller,
474 * free any pages it allocated and our page pointer array
475 */
476 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400477 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400478 page_cache_release(pages[i]);
479 }
480 kfree(pages);
481 pages = NULL;
482 total_compressed = 0;
483 nr_pages_ret = 0;
484
485 /* flag the file so we don't compress in the future */
Chris Masona555f812010-01-28 16:18:15 -0500486 if (!btrfs_test_opt(root, FORCE_COMPRESS))
487 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Masonc8b97812008-10-29 14:49:59 -0400488 }
Chris Mason771ed682008-11-06 22:02:51 -0500489 if (will_compress) {
490 *num_added += 1;
491
492 /* the async work queues will take care of doing actual
493 * allocation on disk for these compressed pages,
494 * and will submit them to the elevator.
495 */
496 add_async_extent(async_cow, start, num_bytes,
497 total_compressed, pages, nr_pages_ret);
498
Chris Mason42dc7ba2008-12-15 11:44:56 -0500499 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500500 start += num_bytes;
501 pages = NULL;
502 cond_resched();
503 goto again;
504 }
505 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500506cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500507 /*
508 * No compression, but we still need to write the pages in
509 * the file we've been given so far. redirty the locked
510 * page if it corresponds to our extent and set things up
511 * for the async work queue to run cow_file_range to do
512 * the normal delalloc dance
513 */
514 if (page_offset(locked_page) >= start &&
515 page_offset(locked_page) <= end) {
516 __set_page_dirty_nobuffers(locked_page);
517 /* unlocked later on in the async handlers */
518 }
519 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
520 *num_added += 1;
521 }
522
523out:
524 return 0;
525
526free_pages_out:
527 for (i = 0; i < nr_pages_ret; i++) {
528 WARN_ON(pages[i]->mapping);
529 page_cache_release(pages[i]);
530 }
Chris Masond3977122009-01-05 21:25:51 -0500531 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500532
533 goto out;
534}
535
536/*
537 * phase two of compressed writeback. This is the ordered portion
538 * of the code, which only gets called in the order the work was
539 * queued. We walk all the async extents created by compress_file_range
540 * and send them down to the disk.
541 */
542static noinline int submit_compressed_extents(struct inode *inode,
543 struct async_cow *async_cow)
544{
545 struct async_extent *async_extent;
546 u64 alloc_hint = 0;
547 struct btrfs_trans_handle *trans;
548 struct btrfs_key ins;
549 struct extent_map *em;
550 struct btrfs_root *root = BTRFS_I(inode)->root;
551 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
552 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500553 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500554
555 if (list_empty(&async_cow->extents))
556 return 0;
557
Chris Mason771ed682008-11-06 22:02:51 -0500558
Chris Masond3977122009-01-05 21:25:51 -0500559 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500560 async_extent = list_entry(async_cow->extents.next,
561 struct async_extent, list);
562 list_del(&async_extent->list);
563
564 io_tree = &BTRFS_I(inode)->io_tree;
565
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500566retry:
Chris Mason771ed682008-11-06 22:02:51 -0500567 /* did the compression code fall back to uncompressed IO? */
568 if (!async_extent->pages) {
569 int page_started = 0;
570 unsigned long nr_written = 0;
571
572 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500573 async_extent->start +
574 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500575
576 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500577 ret = cow_file_range(inode, async_cow->locked_page,
578 async_extent->start,
579 async_extent->start +
580 async_extent->ram_size - 1,
581 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500582
583 /*
584 * if page_started, cow_file_range inserted an
585 * inline extent and took care of all the unlocking
586 * and IO for us. Otherwise, we need to submit
587 * all those pages down to the drive.
588 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500589 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500590 extent_write_locked_range(io_tree,
591 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500592 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500593 async_extent->ram_size - 1,
594 btrfs_get_extent,
595 WB_SYNC_ALL);
596 kfree(async_extent);
597 cond_resched();
598 continue;
599 }
600
601 lock_extent(io_tree, async_extent->start,
602 async_extent->start + async_extent->ram_size - 1,
603 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500604
Yan, Zhengc2167752009-11-12 09:34:21 +0000605 trans = btrfs_join_transaction(root, 1);
Chris Mason771ed682008-11-06 22:02:51 -0500606 ret = btrfs_reserve_extent(trans, root,
607 async_extent->compressed_size,
608 async_extent->compressed_size,
609 0, alloc_hint,
610 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000611 btrfs_end_transaction(trans, root);
612
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500613 if (ret) {
614 int i;
615 for (i = 0; i < async_extent->nr_pages; i++) {
616 WARN_ON(async_extent->pages[i]->mapping);
617 page_cache_release(async_extent->pages[i]);
618 }
619 kfree(async_extent->pages);
620 async_extent->nr_pages = 0;
621 async_extent->pages = NULL;
622 unlock_extent(io_tree, async_extent->start,
623 async_extent->start +
624 async_extent->ram_size - 1, GFP_NOFS);
625 goto retry;
626 }
627
Yan, Zhengc2167752009-11-12 09:34:21 +0000628 /*
629 * here we're doing allocation and writeback of the
630 * compressed pages
631 */
632 btrfs_drop_extent_cache(inode, async_extent->start,
633 async_extent->start +
634 async_extent->ram_size - 1, 0);
635
Chris Mason771ed682008-11-06 22:02:51 -0500636 em = alloc_extent_map(GFP_NOFS);
637 em->start = async_extent->start;
638 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500639 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500640
641 em->block_start = ins.objectid;
642 em->block_len = ins.offset;
643 em->bdev = root->fs_info->fs_devices->latest_bdev;
644 set_bit(EXTENT_FLAG_PINNED, &em->flags);
645 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
646
Chris Masond3977122009-01-05 21:25:51 -0500647 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400648 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500649 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400650 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500651 if (ret != -EEXIST) {
652 free_extent_map(em);
653 break;
654 }
655 btrfs_drop_extent_cache(inode, async_extent->start,
656 async_extent->start +
657 async_extent->ram_size - 1, 0);
658 }
659
660 ret = btrfs_add_ordered_extent(inode, async_extent->start,
661 ins.objectid,
662 async_extent->ram_size,
663 ins.offset,
664 BTRFS_ORDERED_COMPRESSED);
665 BUG_ON(ret);
666
Chris Mason771ed682008-11-06 22:02:51 -0500667 /*
668 * clear dirty, set writeback and unlock the pages.
669 */
670 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400671 &BTRFS_I(inode)->io_tree,
672 async_extent->start,
673 async_extent->start +
674 async_extent->ram_size - 1,
675 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
676 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400677 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400678 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500679
680 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500681 async_extent->start,
682 async_extent->ram_size,
683 ins.objectid,
684 ins.offset, async_extent->pages,
685 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500686
687 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500688 alloc_hint = ins.objectid + ins.offset;
689 kfree(async_extent);
690 cond_resched();
691 }
692
Chris Mason771ed682008-11-06 22:02:51 -0500693 return 0;
694}
695
696/*
697 * when extent_io.c finds a delayed allocation range in the file,
698 * the call backs end up in this code. The basic idea is to
699 * allocate extents on disk for the range, and create ordered data structs
700 * in ram to track those extents.
701 *
702 * locked_page is the page that writepage had locked already. We use
703 * it to make sure we don't do extra locks or unlocks.
704 *
705 * *page_started is set to one if we unlock locked_page and do everything
706 * required to start IO on it. It may be clean and already done with
707 * IO when we return.
708 */
709static noinline int cow_file_range(struct inode *inode,
710 struct page *locked_page,
711 u64 start, u64 end, int *page_started,
712 unsigned long *nr_written,
713 int unlock)
714{
715 struct btrfs_root *root = BTRFS_I(inode)->root;
716 struct btrfs_trans_handle *trans;
717 u64 alloc_hint = 0;
718 u64 num_bytes;
719 unsigned long ram_size;
720 u64 disk_num_bytes;
721 u64 cur_alloc_size;
722 u64 blocksize = root->sectorsize;
723 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500724 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500725 struct btrfs_key ins;
726 struct extent_map *em;
727 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
728 int ret = 0;
729
730 trans = btrfs_join_transaction(root, 1);
731 BUG_ON(!trans);
732 btrfs_set_trans_block_group(trans, inode);
733
Chris Mason42dc7ba2008-12-15 11:44:56 -0500734 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500735
736 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
737 num_bytes = max(blocksize, num_bytes);
738 disk_num_bytes = num_bytes;
739 ret = 0;
740
741 if (start == 0) {
742 /* lets try to make an inline extent */
743 ret = cow_file_range_inline(trans, root, inode,
744 start, end, 0, NULL);
745 if (ret == 0) {
746 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400747 &BTRFS_I(inode)->io_tree,
748 start, end, NULL,
749 EXTENT_CLEAR_UNLOCK_PAGE |
750 EXTENT_CLEAR_UNLOCK |
751 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400752 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400753 EXTENT_CLEAR_DIRTY |
754 EXTENT_SET_WRITEBACK |
755 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000756
Chris Mason771ed682008-11-06 22:02:51 -0500757 *nr_written = *nr_written +
758 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
759 *page_started = 1;
760 ret = 0;
761 goto out;
762 }
763 }
Chris Masonc8b97812008-10-29 14:49:59 -0400764
765 BUG_ON(disk_num_bytes >
766 btrfs_super_total_bytes(&root->fs_info->super_copy));
767
Chris Masonb917b7c2009-09-18 16:07:03 -0400768
769 read_lock(&BTRFS_I(inode)->extent_tree.lock);
770 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
771 start, num_bytes);
772 if (em) {
Josef Bacik6346c932009-11-10 21:23:47 -0500773 /*
774 * if block start isn't an actual block number then find the
775 * first block in this inode and use that as a hint. If that
776 * block is also bogus then just don't worry about it.
777 */
778 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
779 free_extent_map(em);
780 em = search_extent_mapping(em_tree, 0, 0);
781 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
782 alloc_hint = em->block_start;
783 if (em)
784 free_extent_map(em);
785 } else {
786 alloc_hint = em->block_start;
787 free_extent_map(em);
788 }
Chris Masonb917b7c2009-09-18 16:07:03 -0400789 }
790 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400791 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400792
Chris Masond3977122009-01-05 21:25:51 -0500793 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400794 unsigned long op;
795
Chris Masonc8b97812008-10-29 14:49:59 -0400796 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400797 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500798 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400799 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500800 BUG_ON(ret);
801
Chris Masone6dcd2d2008-07-17 12:53:50 -0400802 em = alloc_extent_map(GFP_NOFS);
803 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500804 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500805 ram_size = ins.offset;
806 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400807
Chris Masone6dcd2d2008-07-17 12:53:50 -0400808 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400809 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400810 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400811 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400812
Chris Masond3977122009-01-05 21:25:51 -0500813 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400814 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400815 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400816 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400817 if (ret != -EEXIST) {
818 free_extent_map(em);
819 break;
820 }
821 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400822 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400823 }
824
Chris Mason98d20f62008-04-14 09:46:10 -0400825 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400826 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500827 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400828 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400829
Yan Zheng17d217f2008-12-12 10:03:38 -0500830 if (root->root_key.objectid ==
831 BTRFS_DATA_RELOC_TREE_OBJECTID) {
832 ret = btrfs_reloc_clone_csums(inode, start,
833 cur_alloc_size);
834 BUG_ON(ret);
835 }
836
Chris Masond3977122009-01-05 21:25:51 -0500837 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400838 break;
Chris Masond3977122009-01-05 21:25:51 -0500839
Chris Masonc8b97812008-10-29 14:49:59 -0400840 /* we're not doing compressed IO, don't unlock the first
841 * page (which the caller expects to stay locked), don't
842 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400843 *
844 * Do set the Private2 bit so we know this page was properly
845 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400846 */
Chris Masona791e352009-10-08 11:27:10 -0400847 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
848 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
849 EXTENT_SET_PRIVATE2;
850
Chris Masonc8b97812008-10-29 14:49:59 -0400851 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
852 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400853 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400854 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500855 num_bytes -= cur_alloc_size;
856 alloc_hint = ins.objectid + ins.offset;
857 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400858 }
Chris Masonb888db22007-08-27 16:49:44 -0400859out:
Chris Mason771ed682008-11-06 22:02:51 -0500860 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400861 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400862
Chris Masonbe20aa92007-12-17 20:14:01 -0500863 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500864}
Chris Masonc8b97812008-10-29 14:49:59 -0400865
Chris Mason771ed682008-11-06 22:02:51 -0500866/*
867 * work queue call back to started compression on a file and pages
868 */
869static noinline void async_cow_start(struct btrfs_work *work)
870{
871 struct async_cow *async_cow;
872 int num_added = 0;
873 async_cow = container_of(work, struct async_cow, work);
874
875 compress_file_range(async_cow->inode, async_cow->locked_page,
876 async_cow->start, async_cow->end, async_cow,
877 &num_added);
878 if (num_added == 0)
879 async_cow->inode = NULL;
880}
881
882/*
883 * work queue call back to submit previously compressed pages
884 */
885static noinline void async_cow_submit(struct btrfs_work *work)
886{
887 struct async_cow *async_cow;
888 struct btrfs_root *root;
889 unsigned long nr_pages;
890
891 async_cow = container_of(work, struct async_cow, work);
892
893 root = async_cow->root;
894 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
895 PAGE_CACHE_SHIFT;
896
897 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
898
899 if (atomic_read(&root->fs_info->async_delalloc_pages) <
900 5 * 1042 * 1024 &&
901 waitqueue_active(&root->fs_info->async_submit_wait))
902 wake_up(&root->fs_info->async_submit_wait);
903
Chris Masond3977122009-01-05 21:25:51 -0500904 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500905 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500906}
Chris Masonc8b97812008-10-29 14:49:59 -0400907
Chris Mason771ed682008-11-06 22:02:51 -0500908static noinline void async_cow_free(struct btrfs_work *work)
909{
910 struct async_cow *async_cow;
911 async_cow = container_of(work, struct async_cow, work);
912 kfree(async_cow);
913}
914
915static int cow_file_range_async(struct inode *inode, struct page *locked_page,
916 u64 start, u64 end, int *page_started,
917 unsigned long *nr_written)
918{
919 struct async_cow *async_cow;
920 struct btrfs_root *root = BTRFS_I(inode)->root;
921 unsigned long nr_pages;
922 u64 cur_end;
923 int limit = 10 * 1024 * 1042;
924
Chris Masona3429ab2009-10-08 12:30:20 -0400925 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
926 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500927 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500928 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
929 async_cow->inode = inode;
930 async_cow->root = root;
931 async_cow->locked_page = locked_page;
932 async_cow->start = start;
933
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200934 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500935 cur_end = end;
936 else
937 cur_end = min(end, start + 512 * 1024 - 1);
938
939 async_cow->end = cur_end;
940 INIT_LIST_HEAD(&async_cow->extents);
941
942 async_cow->work.func = async_cow_start;
943 async_cow->work.ordered_func = async_cow_submit;
944 async_cow->work.ordered_free = async_cow_free;
945 async_cow->work.flags = 0;
946
Chris Mason771ed682008-11-06 22:02:51 -0500947 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
948 PAGE_CACHE_SHIFT;
949 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
950
951 btrfs_queue_worker(&root->fs_info->delalloc_workers,
952 &async_cow->work);
953
954 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
955 wait_event(root->fs_info->async_submit_wait,
956 (atomic_read(&root->fs_info->async_delalloc_pages) <
957 limit));
958 }
959
Chris Masond3977122009-01-05 21:25:51 -0500960 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500961 atomic_read(&root->fs_info->async_delalloc_pages)) {
962 wait_event(root->fs_info->async_submit_wait,
963 (atomic_read(&root->fs_info->async_delalloc_pages) ==
964 0));
965 }
966
967 *nr_written += nr_pages;
968 start = cur_end + 1;
969 }
970 *page_started = 1;
971 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500972}
973
Chris Masond3977122009-01-05 21:25:51 -0500974static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500975 u64 bytenr, u64 num_bytes)
976{
977 int ret;
978 struct btrfs_ordered_sum *sums;
979 LIST_HEAD(list);
980
Yan Zheng07d400a2009-01-06 11:42:00 -0500981 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
982 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500983 if (ret == 0 && list_empty(&list))
984 return 0;
985
986 while (!list_empty(&list)) {
987 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
988 list_del(&sums->list);
989 kfree(sums);
990 }
991 return 1;
992}
993
Chris Masond352ac62008-09-29 15:18:18 -0400994/*
995 * when nowcow writeback call back. This checks for snapshots or COW copies
996 * of the extents that exist in the file, and COWs the file as required.
997 *
998 * If no cow copies or snapshots exist, we write directly to the existing
999 * blocks on disk
1000 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001001static noinline int run_delalloc_nocow(struct inode *inode,
1002 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001003 u64 start, u64 end, int *page_started, int force,
1004 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001005{
Chris Masonbe20aa92007-12-17 20:14:01 -05001006 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001007 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001008 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001009 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001010 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001011 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001012 u64 cow_start;
1013 u64 cur_offset;
1014 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001015 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001016 u64 disk_bytenr;
1017 u64 num_bytes;
1018 int extent_type;
1019 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001020 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001021 int nocow;
1022 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -05001023
1024 path = btrfs_alloc_path();
1025 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001026 trans = btrfs_join_transaction(root, 1);
1027 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -05001028
Yan Zheng80ff3852008-10-30 14:20:02 -04001029 cow_start = (u64)-1;
1030 cur_offset = start;
1031 while (1) {
1032 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1033 cur_offset, 0);
1034 BUG_ON(ret < 0);
1035 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1036 leaf = path->nodes[0];
1037 btrfs_item_key_to_cpu(leaf, &found_key,
1038 path->slots[0] - 1);
1039 if (found_key.objectid == inode->i_ino &&
1040 found_key.type == BTRFS_EXTENT_DATA_KEY)
1041 path->slots[0]--;
1042 }
1043 check_prev = 0;
1044next_slot:
1045 leaf = path->nodes[0];
1046 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1047 ret = btrfs_next_leaf(root, path);
1048 if (ret < 0)
1049 BUG_ON(1);
1050 if (ret > 0)
1051 break;
1052 leaf = path->nodes[0];
1053 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001054
Yan Zheng80ff3852008-10-30 14:20:02 -04001055 nocow = 0;
1056 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001057 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001058 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001059
Yan Zheng80ff3852008-10-30 14:20:02 -04001060 if (found_key.objectid > inode->i_ino ||
1061 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1062 found_key.offset > end)
1063 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001064
Yan Zheng80ff3852008-10-30 14:20:02 -04001065 if (found_key.offset > cur_offset) {
1066 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001067 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001068 goto out_check;
1069 }
Chris Masonc31f8832008-01-08 15:46:31 -05001070
Yan Zheng80ff3852008-10-30 14:20:02 -04001071 fi = btrfs_item_ptr(leaf, path->slots[0],
1072 struct btrfs_file_extent_item);
1073 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001074
Yan Zhengd899e052008-10-30 14:25:28 -04001075 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1076 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001077 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001078 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001079 extent_end = found_key.offset +
1080 btrfs_file_extent_num_bytes(leaf, fi);
1081 if (extent_end <= start) {
1082 path->slots[0]++;
1083 goto next_slot;
1084 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001085 if (disk_bytenr == 0)
1086 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001087 if (btrfs_file_extent_compression(leaf, fi) ||
1088 btrfs_file_extent_encryption(leaf, fi) ||
1089 btrfs_file_extent_other_encoding(leaf, fi))
1090 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001091 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1092 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001093 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001094 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001095 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001096 found_key.offset -
1097 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001098 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001099 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001100 disk_bytenr += cur_offset - found_key.offset;
1101 num_bytes = min(end + 1, extent_end) - cur_offset;
1102 /*
1103 * force cow if csum exists in the range.
1104 * this ensure that csum for a given extent are
1105 * either valid or do not exist.
1106 */
1107 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1108 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001109 nocow = 1;
1110 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1111 extent_end = found_key.offset +
1112 btrfs_file_extent_inline_len(leaf, fi);
1113 extent_end = ALIGN(extent_end, root->sectorsize);
1114 } else {
1115 BUG_ON(1);
1116 }
1117out_check:
1118 if (extent_end <= start) {
1119 path->slots[0]++;
1120 goto next_slot;
1121 }
1122 if (!nocow) {
1123 if (cow_start == (u64)-1)
1124 cow_start = cur_offset;
1125 cur_offset = extent_end;
1126 if (cur_offset > end)
1127 break;
1128 path->slots[0]++;
1129 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001130 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001131
Yan Zheng7ea394f2008-08-05 13:05:02 -04001132 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001133 if (cow_start != (u64)-1) {
1134 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001135 found_key.offset - 1, page_started,
1136 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001137 BUG_ON(ret);
1138 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001139 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001140
Yan Zhengd899e052008-10-30 14:25:28 -04001141 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1142 struct extent_map *em;
1143 struct extent_map_tree *em_tree;
1144 em_tree = &BTRFS_I(inode)->extent_tree;
1145 em = alloc_extent_map(GFP_NOFS);
1146 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001147 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001148 em->len = num_bytes;
1149 em->block_len = num_bytes;
1150 em->block_start = disk_bytenr;
1151 em->bdev = root->fs_info->fs_devices->latest_bdev;
1152 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1153 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001154 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001155 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001156 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001157 if (ret != -EEXIST) {
1158 free_extent_map(em);
1159 break;
1160 }
1161 btrfs_drop_extent_cache(inode, em->start,
1162 em->start + em->len - 1, 0);
1163 }
1164 type = BTRFS_ORDERED_PREALLOC;
1165 } else {
1166 type = BTRFS_ORDERED_NOCOW;
1167 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001168
1169 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001170 num_bytes, num_bytes, type);
1171 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001172
Yan Zhengd899e052008-10-30 14:25:28 -04001173 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001174 cur_offset, cur_offset + num_bytes - 1,
1175 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1176 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1177 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001178 cur_offset = extent_end;
1179 if (cur_offset > end)
1180 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001181 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001182 btrfs_release_path(root, path);
1183
1184 if (cur_offset <= end && cow_start == (u64)-1)
1185 cow_start = cur_offset;
1186 if (cow_start != (u64)-1) {
1187 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001188 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001189 BUG_ON(ret);
1190 }
1191
1192 ret = btrfs_end_transaction(trans, root);
1193 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001194 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001195 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001196}
1197
Chris Masond352ac62008-09-29 15:18:18 -04001198/*
1199 * extent_io.c call back to do delayed allocation processing
1200 */
Chris Masonc8b97812008-10-29 14:49:59 -04001201static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001202 u64 start, u64 end, int *page_started,
1203 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001204{
Chris Masonbe20aa92007-12-17 20:14:01 -05001205 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001206 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001207
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001208 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001209 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001210 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001211 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001212 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001213 page_started, 0, nr_written);
Chris Mason7f366cf2009-03-12 20:12:45 -04001214 else if (!btrfs_test_opt(root, COMPRESS))
1215 ret = cow_file_range(inode, locked_page, start, end,
1216 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001217 else
Chris Mason771ed682008-11-06 22:02:51 -05001218 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001219 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001220 return ret;
1221}
1222
Josef Bacik9ed74f22009-09-11 16:12:44 -04001223static int btrfs_split_extent_hook(struct inode *inode,
1224 struct extent_state *orig, u64 split)
1225{
1226 struct btrfs_root *root = BTRFS_I(inode)->root;
1227 u64 size;
1228
1229 if (!(orig->state & EXTENT_DELALLOC))
1230 return 0;
1231
1232 size = orig->end - orig->start + 1;
1233 if (size > root->fs_info->max_extent) {
1234 u64 num_extents;
1235 u64 new_size;
1236
1237 new_size = orig->end - split + 1;
1238 num_extents = div64_u64(size + root->fs_info->max_extent - 1,
1239 root->fs_info->max_extent);
1240
1241 /*
Josef Bacik32c00af2009-10-08 13:34:05 -04001242 * if we break a large extent up then leave oustanding_extents
1243 * be, since we've already accounted for the large extent.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001244 */
1245 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1246 root->fs_info->max_extent) < num_extents)
1247 return 0;
1248 }
1249
Josef Bacik32c00af2009-10-08 13:34:05 -04001250 spin_lock(&BTRFS_I(inode)->accounting_lock);
1251 BTRFS_I(inode)->outstanding_extents++;
1252 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001253
1254 return 0;
1255}
1256
1257/*
1258 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1259 * extents so we can keep track of new extents that are just merged onto old
1260 * extents, such as when we are doing sequential writes, so we can properly
1261 * account for the metadata space we'll need.
1262 */
1263static int btrfs_merge_extent_hook(struct inode *inode,
1264 struct extent_state *new,
1265 struct extent_state *other)
1266{
1267 struct btrfs_root *root = BTRFS_I(inode)->root;
1268 u64 new_size, old_size;
1269 u64 num_extents;
1270
1271 /* not delalloc, ignore it */
1272 if (!(other->state & EXTENT_DELALLOC))
1273 return 0;
1274
1275 old_size = other->end - other->start + 1;
1276 if (new->start < other->start)
1277 new_size = other->end - new->start + 1;
1278 else
1279 new_size = new->end - other->start + 1;
1280
1281 /* we're not bigger than the max, unreserve the space and go */
1282 if (new_size <= root->fs_info->max_extent) {
Josef Bacik32c00af2009-10-08 13:34:05 -04001283 spin_lock(&BTRFS_I(inode)->accounting_lock);
1284 BTRFS_I(inode)->outstanding_extents--;
1285 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001286 return 0;
1287 }
1288
1289 /*
1290 * If we grew by another max_extent, just return, we want to keep that
1291 * reserved amount.
1292 */
1293 num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
1294 root->fs_info->max_extent);
1295 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1296 root->fs_info->max_extent) > num_extents)
1297 return 0;
1298
Josef Bacik32c00af2009-10-08 13:34:05 -04001299 spin_lock(&BTRFS_I(inode)->accounting_lock);
1300 BTRFS_I(inode)->outstanding_extents--;
1301 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001302
1303 return 0;
1304}
1305
Chris Masond352ac62008-09-29 15:18:18 -04001306/*
1307 * extent_io.c set_bit_hook, used to track delayed allocation
1308 * bytes in this file, and to maintain the list of inodes that
1309 * have pending delalloc work to be done.
1310 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001311static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001312 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001313{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001314
Chris Mason75eff682008-12-15 15:54:40 -05001315 /*
1316 * set_bit and clear bit hooks normally require _irqsave/restore
1317 * but in this case, we are only testeing for the DELALLOC
1318 * bit, which is only set or cleared with irqs on
1319 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001320 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001321 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001322
Josef Bacik32c00af2009-10-08 13:34:05 -04001323 spin_lock(&BTRFS_I(inode)->accounting_lock);
1324 BTRFS_I(inode)->outstanding_extents++;
1325 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik6a632092009-02-20 11:00:09 -05001326 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
Chris Mason75eff682008-12-15 15:54:40 -05001327 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001328 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001329 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001330 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1331 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1332 &root->fs_info->delalloc_inodes);
1333 }
Chris Mason75eff682008-12-15 15:54:40 -05001334 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001335 }
1336 return 0;
1337}
1338
Chris Masond352ac62008-09-29 15:18:18 -04001339/*
1340 * extent_io.c clear_bit_hook, see set_bit_hook for why
1341 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001342static int btrfs_clear_bit_hook(struct inode *inode,
1343 struct extent_state *state, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001344{
Chris Mason75eff682008-12-15 15:54:40 -05001345 /*
1346 * set_bit and clear bit hooks normally require _irqsave/restore
1347 * but in this case, we are only testeing for the DELALLOC
1348 * bit, which is only set or cleared with irqs on
1349 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001350 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001351 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001352
Josef Bacik32c00af2009-10-08 13:34:05 -04001353 if (bits & EXTENT_DO_ACCOUNTING) {
1354 spin_lock(&BTRFS_I(inode)->accounting_lock);
1355 BTRFS_I(inode)->outstanding_extents--;
1356 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1357 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1358 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04001359
Chris Mason75eff682008-12-15 15:54:40 -05001360 spin_lock(&root->fs_info->delalloc_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001361 if (state->end - state->start + 1 >
1362 root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001363 printk(KERN_INFO "btrfs warning: delalloc account "
1364 "%llu %llu\n",
Josef Bacik9ed74f22009-09-11 16:12:44 -04001365 (unsigned long long)
1366 state->end - state->start + 1,
Chris Masond3977122009-01-05 21:25:51 -05001367 (unsigned long long)
1368 root->fs_info->delalloc_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001369 btrfs_delalloc_free_space(root, inode, (u64)-1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001370 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001371 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001372 } else {
Josef Bacik6a632092009-02-20 11:00:09 -05001373 btrfs_delalloc_free_space(root, inode,
Josef Bacik9ed74f22009-09-11 16:12:44 -04001374 state->end -
1375 state->start + 1);
1376 root->fs_info->delalloc_bytes -= state->end -
1377 state->start + 1;
1378 BTRFS_I(inode)->delalloc_bytes -= state->end -
1379 state->start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001380 }
Chris Masonea8c2812008-08-04 23:17:27 -04001381 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1382 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1383 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1384 }
Chris Mason75eff682008-12-15 15:54:40 -05001385 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001386 }
1387 return 0;
1388}
1389
Chris Masond352ac62008-09-29 15:18:18 -04001390/*
1391 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1392 * we don't create bios that span stripes or chunks
1393 */
Chris Mason239b14b2008-03-24 15:02:07 -04001394int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001395 size_t size, struct bio *bio,
1396 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001397{
1398 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1399 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001400 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001401 u64 length = 0;
1402 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001403 int ret;
1404
Chris Mason771ed682008-11-06 22:02:51 -05001405 if (bio_flags & EXTENT_BIO_COMPRESSED)
1406 return 0;
1407
Chris Masonf2d8d742008-04-21 10:03:05 -04001408 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001409 map_tree = &root->fs_info->mapping_tree;
1410 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001411 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001412 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001413
Chris Masond3977122009-01-05 21:25:51 -05001414 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001415 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001416 return 0;
1417}
1418
Chris Masond352ac62008-09-29 15:18:18 -04001419/*
1420 * in order to insert checksums into the metadata in large chunks,
1421 * we wait until bio submission time. All the pages in the bio are
1422 * checksummed and sums are attached onto the ordered extent record.
1423 *
1424 * At IO completion time the cums attached on the ordered extent record
1425 * are inserted into the btree
1426 */
Chris Masond3977122009-01-05 21:25:51 -05001427static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1428 struct bio *bio, int mirror_num,
1429 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001430{
Chris Mason065631f2008-02-20 12:07:25 -05001431 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001432 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001433
Chris Masond20f7042008-12-08 16:58:54 -05001434 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001435 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001436 return 0;
1437}
Chris Masone0156402008-04-16 11:15:20 -04001438
Chris Mason4a69a412008-11-06 22:03:00 -05001439/*
1440 * in order to insert checksums into the metadata in large chunks,
1441 * we wait until bio submission time. All the pages in the bio are
1442 * checksummed and sums are attached onto the ordered extent record.
1443 *
1444 * At IO completion time the cums attached on the ordered extent record
1445 * are inserted into the btree
1446 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001447static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001448 int mirror_num, unsigned long bio_flags)
1449{
1450 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001451 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001452}
1453
Chris Masond352ac62008-09-29 15:18:18 -04001454/*
Chris Masoncad321a2008-12-17 14:51:42 -05001455 * extent_io.c submission hook. This does the right thing for csum calculation
1456 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001457 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001458static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001459 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001460{
1461 struct btrfs_root *root = BTRFS_I(inode)->root;
1462 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001463 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001464
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001465 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001466
Chris Masone6dcd2d2008-07-17 12:53:50 -04001467 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1468 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001469
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001470 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001471 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001472 return btrfs_submit_compressed_read(inode, bio,
1473 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001474 } else if (!skip_sum)
1475 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001476 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001477 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001478 /* csum items have already been cloned */
1479 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1480 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001481 /* we're doing a write, do the async checksumming */
1482 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001483 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001484 bio_flags, __btrfs_submit_bio_start,
1485 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001486 }
1487
Chris Mason0b86a832008-03-24 15:01:56 -04001488mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001489 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001490}
Chris Mason6885f302008-02-20 16:11:05 -05001491
Chris Masond352ac62008-09-29 15:18:18 -04001492/*
1493 * given a list of ordered sums record them in the inode. This happens
1494 * at IO completion time based on sums calculated at bio submission time.
1495 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001496static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001497 struct inode *inode, u64 file_offset,
1498 struct list_head *list)
1499{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001500 struct btrfs_ordered_sum *sum;
1501
1502 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001503
1504 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001505 btrfs_csum_file_blocks(trans,
1506 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001507 }
1508 return 0;
1509}
1510
Chris Masonea8c2812008-08-04 23:17:27 -04001511int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1512{
Chris Masond3977122009-01-05 21:25:51 -05001513 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001514 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001515 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1516 GFP_NOFS);
1517}
1518
Chris Masond352ac62008-09-29 15:18:18 -04001519/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001520struct btrfs_writepage_fixup {
1521 struct page *page;
1522 struct btrfs_work work;
1523};
1524
Christoph Hellwigb2950862008-12-02 09:54:17 -05001525static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001526{
1527 struct btrfs_writepage_fixup *fixup;
1528 struct btrfs_ordered_extent *ordered;
1529 struct page *page;
1530 struct inode *inode;
1531 u64 page_start;
1532 u64 page_end;
1533
1534 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1535 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001536again:
Chris Mason247e7432008-07-17 12:53:51 -04001537 lock_page(page);
1538 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1539 ClearPageChecked(page);
1540 goto out_page;
1541 }
1542
1543 inode = page->mapping->host;
1544 page_start = page_offset(page);
1545 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1546
1547 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001548
1549 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001550 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001551 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001552
1553 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1554 if (ordered) {
1555 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1556 page_end, GFP_NOFS);
1557 unlock_page(page);
1558 btrfs_start_ordered_extent(inode, ordered, 1);
1559 goto again;
1560 }
Chris Mason247e7432008-07-17 12:53:51 -04001561
Chris Masonea8c2812008-08-04 23:17:27 -04001562 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001563 ClearPageChecked(page);
1564out:
1565 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1566out_page:
1567 unlock_page(page);
1568 page_cache_release(page);
1569}
1570
1571/*
1572 * There are a few paths in the higher layers of the kernel that directly
1573 * set the page dirty bit without asking the filesystem if it is a
1574 * good idea. This causes problems because we want to make sure COW
1575 * properly happens and the data=ordered rules are followed.
1576 *
Chris Masonc8b97812008-10-29 14:49:59 -04001577 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001578 * hasn't been properly setup for IO. We kick off an async process
1579 * to fix it up. The async helper will wait for ordered extents, set
1580 * the delalloc bit and make it safe to write the page.
1581 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001582static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001583{
1584 struct inode *inode = page->mapping->host;
1585 struct btrfs_writepage_fixup *fixup;
1586 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001587
Chris Mason8b62b722009-09-02 16:53:46 -04001588 /* this page is properly in the ordered list */
1589 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001590 return 0;
1591
1592 if (PageChecked(page))
1593 return -EAGAIN;
1594
1595 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1596 if (!fixup)
1597 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001598
Chris Mason247e7432008-07-17 12:53:51 -04001599 SetPageChecked(page);
1600 page_cache_get(page);
1601 fixup->work.func = btrfs_writepage_fixup_worker;
1602 fixup->page = page;
1603 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1604 return -EAGAIN;
1605}
1606
Yan Zhengd899e052008-10-30 14:25:28 -04001607static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1608 struct inode *inode, u64 file_pos,
1609 u64 disk_bytenr, u64 disk_num_bytes,
1610 u64 num_bytes, u64 ram_bytes,
1611 u8 compression, u8 encryption,
1612 u16 other_encoding, int extent_type)
1613{
1614 struct btrfs_root *root = BTRFS_I(inode)->root;
1615 struct btrfs_file_extent_item *fi;
1616 struct btrfs_path *path;
1617 struct extent_buffer *leaf;
1618 struct btrfs_key ins;
1619 u64 hint;
1620 int ret;
1621
1622 path = btrfs_alloc_path();
1623 BUG_ON(!path);
1624
Chris Masonb9473432009-03-13 11:00:37 -04001625 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001626
1627 /*
1628 * we may be replacing one extent in the tree with another.
1629 * The new extent is pinned in the extent map, and we don't want
1630 * to drop it from the cache until it is completely in the btree.
1631 *
1632 * So, tell btrfs_drop_extents to leave this extent in the cache.
1633 * the caller is expected to unpin it and allow it to be merged
1634 * with the others.
1635 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001636 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1637 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001638 BUG_ON(ret);
1639
1640 ins.objectid = inode->i_ino;
1641 ins.offset = file_pos;
1642 ins.type = BTRFS_EXTENT_DATA_KEY;
1643 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1644 BUG_ON(ret);
1645 leaf = path->nodes[0];
1646 fi = btrfs_item_ptr(leaf, path->slots[0],
1647 struct btrfs_file_extent_item);
1648 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1649 btrfs_set_file_extent_type(leaf, fi, extent_type);
1650 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1651 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1652 btrfs_set_file_extent_offset(leaf, fi, 0);
1653 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1654 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1655 btrfs_set_file_extent_compression(leaf, fi, compression);
1656 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1657 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001658
1659 btrfs_unlock_up_safe(path, 1);
1660 btrfs_set_lock_blocking(leaf);
1661
Yan Zhengd899e052008-10-30 14:25:28 -04001662 btrfs_mark_buffer_dirty(leaf);
1663
1664 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001665
1666 ins.objectid = disk_bytenr;
1667 ins.offset = disk_num_bytes;
1668 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001669 ret = btrfs_alloc_reserved_file_extent(trans, root,
1670 root->root_key.objectid,
1671 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001672 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001673 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001674
Yan Zhengd899e052008-10-30 14:25:28 -04001675 return 0;
1676}
1677
Chris Mason5d13a982009-03-13 11:41:46 -04001678/*
1679 * helper function for btrfs_finish_ordered_io, this
1680 * just reads in some of the csum leaves to prime them into ram
1681 * before we start the transaction. It limits the amount of btree
1682 * reads required while inside the transaction.
1683 */
Chris Masond352ac62008-09-29 15:18:18 -04001684/* as ordered data IO finishes, this gets called so we can finish
1685 * an ordered extent if the range of bytes in the file it covers are
1686 * fully written.
1687 */
Chris Mason211f90e2008-07-18 11:56:15 -04001688static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001689{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001690 struct btrfs_root *root = BTRFS_I(inode)->root;
1691 struct btrfs_trans_handle *trans;
Chris Mason5d13a982009-03-13 11:41:46 -04001692 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001693 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan Zhengd899e052008-10-30 14:25:28 -04001694 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001695 int ret;
1696
1697 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001698 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001699 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001700
Josef Bacikefd049f2010-02-02 20:50:10 +00001701 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001702 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001703
Yan, Zhengc2167752009-11-12 09:34:21 +00001704 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1705 BUG_ON(!list_empty(&ordered_extent->list));
1706 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1707 if (!ret) {
1708 trans = btrfs_join_transaction(root, 1);
1709 ret = btrfs_update_inode(trans, root, inode);
1710 BUG_ON(ret);
1711 btrfs_end_transaction(trans, root);
1712 }
1713 goto out;
1714 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001715
1716 lock_extent(io_tree, ordered_extent->file_offset,
1717 ordered_extent->file_offset + ordered_extent->len - 1,
1718 GFP_NOFS);
1719
Yan, Zhengc2167752009-11-12 09:34:21 +00001720 trans = btrfs_join_transaction(root, 1);
1721
Chris Masonc8b97812008-10-29 14:49:59 -04001722 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001723 compressed = 1;
1724 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1725 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001726 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001727 ordered_extent->file_offset,
1728 ordered_extent->file_offset +
1729 ordered_extent->len);
1730 BUG_ON(ret);
1731 } else {
1732 ret = insert_reserved_file_extent(trans, inode,
1733 ordered_extent->file_offset,
1734 ordered_extent->start,
1735 ordered_extent->disk_len,
1736 ordered_extent->len,
1737 ordered_extent->len,
1738 compressed, 0, 0,
1739 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001740 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1741 ordered_extent->file_offset,
1742 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001743 BUG_ON(ret);
1744 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001745 unlock_extent(io_tree, ordered_extent->file_offset,
1746 ordered_extent->file_offset + ordered_extent->len - 1,
1747 GFP_NOFS);
1748 add_pending_csums(trans, inode, ordered_extent->file_offset,
1749 &ordered_extent->list);
1750
Yan, Zhengc2167752009-11-12 09:34:21 +00001751 /* this also removes the ordered extent from the tree */
1752 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1753 ret = btrfs_update_inode(trans, root, inode);
1754 BUG_ON(ret);
1755 btrfs_end_transaction(trans, root);
1756out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001757 /* once for us */
1758 btrfs_put_ordered_extent(ordered_extent);
1759 /* once for the tree */
1760 btrfs_put_ordered_extent(ordered_extent);
1761
Chris Masone6dcd2d2008-07-17 12:53:50 -04001762 return 0;
1763}
1764
Christoph Hellwigb2950862008-12-02 09:54:17 -05001765static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001766 struct extent_state *state, int uptodate)
1767{
Chris Mason8b62b722009-09-02 16:53:46 -04001768 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001769 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1770}
1771
Chris Masond352ac62008-09-29 15:18:18 -04001772/*
1773 * When IO fails, either with EIO or csum verification fails, we
1774 * try other mirrors that might have a good copy of the data. This
1775 * io_failure_record is used to record state as we go through all the
1776 * mirrors. If another mirror has good data, the page is set up to date
1777 * and things continue. If a good mirror can't be found, the original
1778 * bio end_io callback is called to indicate things have failed.
1779 */
Chris Mason7e383262008-04-09 16:28:12 -04001780struct io_failure_record {
1781 struct page *page;
1782 u64 start;
1783 u64 len;
1784 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001785 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001786 int last_mirror;
1787};
1788
Christoph Hellwigb2950862008-12-02 09:54:17 -05001789static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001790 struct page *page, u64 start, u64 end,
1791 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001792{
1793 struct io_failure_record *failrec = NULL;
1794 u64 private;
1795 struct extent_map *em;
1796 struct inode *inode = page->mapping->host;
1797 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001798 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001799 struct bio *bio;
1800 int num_copies;
1801 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001802 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001803 u64 logical;
1804
1805 ret = get_state_private(failure_tree, start, &private);
1806 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001807 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1808 if (!failrec)
1809 return -ENOMEM;
1810 failrec->start = start;
1811 failrec->len = end - start + 1;
1812 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001813 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001814
Chris Mason890871b2009-09-02 16:24:52 -04001815 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001816 em = lookup_extent_mapping(em_tree, start, failrec->len);
1817 if (em->start > start || em->start + em->len < start) {
1818 free_extent_map(em);
1819 em = NULL;
1820 }
Chris Mason890871b2009-09-02 16:24:52 -04001821 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001822
1823 if (!em || IS_ERR(em)) {
1824 kfree(failrec);
1825 return -EIO;
1826 }
1827 logical = start - em->start;
1828 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001829 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1830 logical = em->block_start;
1831 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1832 }
Chris Mason7e383262008-04-09 16:28:12 -04001833 failrec->logical = logical;
1834 free_extent_map(em);
1835 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1836 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001837 set_state_private(failure_tree, start,
1838 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001839 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001840 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001841 }
1842 num_copies = btrfs_num_copies(
1843 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1844 failrec->logical, failrec->len);
1845 failrec->last_mirror++;
1846 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001847 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001848 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1849 failrec->start,
1850 EXTENT_LOCKED);
1851 if (state && state->start != failrec->start)
1852 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001853 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001854 }
1855 if (!state || failrec->last_mirror > num_copies) {
1856 set_state_private(failure_tree, failrec->start, 0);
1857 clear_extent_bits(failure_tree, failrec->start,
1858 failrec->start + failrec->len - 1,
1859 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1860 kfree(failrec);
1861 return -EIO;
1862 }
1863 bio = bio_alloc(GFP_NOFS, 1);
1864 bio->bi_private = state;
1865 bio->bi_end_io = failed_bio->bi_end_io;
1866 bio->bi_sector = failrec->logical >> 9;
1867 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001868 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001869
Chris Mason7e383262008-04-09 16:28:12 -04001870 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001871 if (failed_bio->bi_rw & (1 << BIO_RW))
1872 rw = WRITE;
1873 else
1874 rw = READ;
1875
1876 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001877 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001878 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001879 return 0;
1880}
1881
Chris Masond352ac62008-09-29 15:18:18 -04001882/*
1883 * each time an IO finishes, we do a fast check in the IO failure tree
1884 * to see if we need to process or clean up an io_failure_record
1885 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001886static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001887{
1888 u64 private;
1889 u64 private_failure;
1890 struct io_failure_record *failure;
1891 int ret;
1892
1893 private = 0;
1894 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1895 (u64)-1, 1, EXTENT_DIRTY)) {
1896 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1897 start, &private_failure);
1898 if (ret == 0) {
1899 failure = (struct io_failure_record *)(unsigned long)
1900 private_failure;
1901 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1902 failure->start, 0);
1903 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1904 failure->start,
1905 failure->start + failure->len - 1,
1906 EXTENT_DIRTY | EXTENT_LOCKED,
1907 GFP_NOFS);
1908 kfree(failure);
1909 }
1910 }
Chris Mason7e383262008-04-09 16:28:12 -04001911 return 0;
1912}
1913
Chris Masond352ac62008-09-29 15:18:18 -04001914/*
1915 * when reads are done, we need to check csums to verify the data is correct
1916 * if there's a match, we allow the bio to finish. If not, we go through
1917 * the io_failure_record routines to find good copies
1918 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001919static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001920 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001921{
Chris Mason35ebb932007-10-30 16:56:53 -04001922 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001923 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001924 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001925 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001926 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001927 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001928 struct btrfs_root *root = BTRFS_I(inode)->root;
1929 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001930
Chris Masond20f7042008-12-08 16:58:54 -05001931 if (PageChecked(page)) {
1932 ClearPageChecked(page);
1933 goto good;
1934 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001935
1936 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001937 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001938
Yan Zheng17d217f2008-12-12 10:03:38 -05001939 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001940 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001941 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1942 GFP_NOFS);
1943 return 0;
1944 }
1945
Yanc2e639f2008-02-04 08:57:25 -05001946 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001947 private = state->private;
1948 ret = 0;
1949 } else {
1950 ret = get_state_private(io_tree, start, &private);
1951 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001952 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001953 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001954 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001955
Chris Masonff79f812007-10-15 16:22:25 -04001956 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1957 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001958 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001959 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001960
Chris Mason9ab86c82009-01-07 09:48:51 -05001961 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001962good:
Chris Mason7e383262008-04-09 16:28:12 -04001963 /* if the io failure tree for this inode is non-empty,
1964 * check to see if we've recovered from a failed IO
1965 */
Chris Mason1259ab72008-05-12 13:39:03 -04001966 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001967 return 0;
1968
1969zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001970 if (printk_ratelimit()) {
1971 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1972 "private %llu\n", page->mapping->host->i_ino,
1973 (unsigned long long)start, csum,
1974 (unsigned long long)private);
1975 }
Chris Masondb945352007-10-15 16:15:53 -04001976 memset(kaddr + offset, 1, end - start + 1);
1977 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001978 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001979 if (private == 0)
1980 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001981 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001982}
Chris Masonb888db22007-08-27 16:49:44 -04001983
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001984struct delayed_iput {
1985 struct list_head list;
1986 struct inode *inode;
1987};
1988
1989void btrfs_add_delayed_iput(struct inode *inode)
1990{
1991 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1992 struct delayed_iput *delayed;
1993
1994 if (atomic_add_unless(&inode->i_count, -1, 1))
1995 return;
1996
1997 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
1998 delayed->inode = inode;
1999
2000 spin_lock(&fs_info->delayed_iput_lock);
2001 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2002 spin_unlock(&fs_info->delayed_iput_lock);
2003}
2004
2005void btrfs_run_delayed_iputs(struct btrfs_root *root)
2006{
2007 LIST_HEAD(list);
2008 struct btrfs_fs_info *fs_info = root->fs_info;
2009 struct delayed_iput *delayed;
2010 int empty;
2011
2012 spin_lock(&fs_info->delayed_iput_lock);
2013 empty = list_empty(&fs_info->delayed_iputs);
2014 spin_unlock(&fs_info->delayed_iput_lock);
2015 if (empty)
2016 return;
2017
2018 down_read(&root->fs_info->cleanup_work_sem);
2019 spin_lock(&fs_info->delayed_iput_lock);
2020 list_splice_init(&fs_info->delayed_iputs, &list);
2021 spin_unlock(&fs_info->delayed_iput_lock);
2022
2023 while (!list_empty(&list)) {
2024 delayed = list_entry(list.next, struct delayed_iput, list);
2025 list_del(&delayed->list);
2026 iput(delayed->inode);
2027 kfree(delayed);
2028 }
2029 up_read(&root->fs_info->cleanup_work_sem);
2030}
2031
Josef Bacik7b128762008-07-24 12:17:14 -04002032/*
2033 * This creates an orphan entry for the given inode in case something goes
2034 * wrong in the middle of an unlink/truncate.
2035 */
2036int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2037{
2038 struct btrfs_root *root = BTRFS_I(inode)->root;
2039 int ret = 0;
2040
Yanbcc63ab2008-07-30 16:29:20 -04002041 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002042
2043 /* already on the orphan list, we're good */
2044 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002045 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002046 return 0;
2047 }
2048
2049 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2050
Yanbcc63ab2008-07-30 16:29:20 -04002051 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002052
2053 /*
2054 * insert an orphan item to track this unlinked/truncated file
2055 */
2056 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2057
2058 return ret;
2059}
2060
2061/*
2062 * We have done the truncate/delete so we can go ahead and remove the orphan
2063 * item for this particular inode.
2064 */
2065int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2066{
2067 struct btrfs_root *root = BTRFS_I(inode)->root;
2068 int ret = 0;
2069
Yanbcc63ab2008-07-30 16:29:20 -04002070 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002071
2072 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002073 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002074 return 0;
2075 }
2076
2077 list_del_init(&BTRFS_I(inode)->i_orphan);
2078 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04002079 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002080 return 0;
2081 }
2082
Yanbcc63ab2008-07-30 16:29:20 -04002083 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002084
2085 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2086
2087 return ret;
2088}
2089
2090/*
2091 * this cleans up any orphans that may be left on the list from the last use
2092 * of this root.
2093 */
2094void btrfs_orphan_cleanup(struct btrfs_root *root)
2095{
2096 struct btrfs_path *path;
2097 struct extent_buffer *leaf;
2098 struct btrfs_item *item;
2099 struct btrfs_key key, found_key;
2100 struct btrfs_trans_handle *trans;
2101 struct inode *inode;
2102 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2103
Yan, Zhengc71bf092009-11-12 09:34:40 +00002104 if (!xchg(&root->clean_orphans, 0))
Josef Bacik7b128762008-07-24 12:17:14 -04002105 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002106
2107 path = btrfs_alloc_path();
2108 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002109 path->reada = -1;
2110
2111 key.objectid = BTRFS_ORPHAN_OBJECTID;
2112 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2113 key.offset = (u64)-1;
2114
Josef Bacik7b128762008-07-24 12:17:14 -04002115 while (1) {
2116 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2117 if (ret < 0) {
2118 printk(KERN_ERR "Error searching slot for orphan: %d"
2119 "\n", ret);
2120 break;
2121 }
2122
2123 /*
2124 * if ret == 0 means we found what we were searching for, which
2125 * is weird, but possible, so only screw with path if we didnt
2126 * find the key and see if we have stuff that matches
2127 */
2128 if (ret > 0) {
2129 if (path->slots[0] == 0)
2130 break;
2131 path->slots[0]--;
2132 }
2133
2134 /* pull out the item */
2135 leaf = path->nodes[0];
2136 item = btrfs_item_nr(leaf, path->slots[0]);
2137 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2138
2139 /* make sure the item matches what we want */
2140 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2141 break;
2142 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2143 break;
2144
2145 /* release the path since we're done with it */
2146 btrfs_release_path(root, path);
2147
2148 /*
2149 * this is where we are basically btrfs_lookup, without the
2150 * crossing root thing. we store the inode number in the
2151 * offset of the orphan item.
2152 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002153 found_key.objectid = found_key.offset;
2154 found_key.type = BTRFS_INODE_ITEM_KEY;
2155 found_key.offset = 0;
2156 inode = btrfs_iget(root->fs_info->sb, &found_key, root);
2157 if (IS_ERR(inode))
Josef Bacik7b128762008-07-24 12:17:14 -04002158 break;
2159
Josef Bacik7b128762008-07-24 12:17:14 -04002160 /*
2161 * add this inode to the orphan list so btrfs_orphan_del does
2162 * the proper thing when we hit it
2163 */
Yanbcc63ab2008-07-30 16:29:20 -04002164 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002165 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04002166 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002167
2168 /*
2169 * if this is a bad inode, means we actually succeeded in
2170 * removing the inode, but not the orphan record, which means
2171 * we need to manually delete the orphan since iput will just
2172 * do a destroy_inode
2173 */
2174 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002175 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04002176 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002177 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002178 iput(inode);
2179 continue;
2180 }
2181
2182 /* if we have links, this was a truncate, lets do that */
2183 if (inode->i_nlink) {
2184 nr_truncate++;
2185 btrfs_truncate(inode);
2186 } else {
2187 nr_unlink++;
2188 }
2189
2190 /* this will do delete_inode and everything for us */
2191 iput(inode);
2192 }
2193
2194 if (nr_unlink)
2195 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2196 if (nr_truncate)
2197 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2198
2199 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002200}
2201
Chris Masond352ac62008-09-29 15:18:18 -04002202/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002203 * very simple check to peek ahead in the leaf looking for xattrs. If we
2204 * don't find any xattrs, we know there can't be any acls.
2205 *
2206 * slot is the slot the inode is in, objectid is the objectid of the inode
2207 */
2208static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2209 int slot, u64 objectid)
2210{
2211 u32 nritems = btrfs_header_nritems(leaf);
2212 struct btrfs_key found_key;
2213 int scanned = 0;
2214
2215 slot++;
2216 while (slot < nritems) {
2217 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2218
2219 /* we found a different objectid, there must not be acls */
2220 if (found_key.objectid != objectid)
2221 return 0;
2222
2223 /* we found an xattr, assume we've got an acl */
2224 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2225 return 1;
2226
2227 /*
2228 * we found a key greater than an xattr key, there can't
2229 * be any acls later on
2230 */
2231 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2232 return 0;
2233
2234 slot++;
2235 scanned++;
2236
2237 /*
2238 * it goes inode, inode backrefs, xattrs, extents,
2239 * so if there are a ton of hard links to an inode there can
2240 * be a lot of backrefs. Don't waste time searching too hard,
2241 * this is just an optimization
2242 */
2243 if (scanned >= 8)
2244 break;
2245 }
2246 /* we hit the end of the leaf before we found an xattr or
2247 * something larger than an xattr. We have to assume the inode
2248 * has acls
2249 */
2250 return 1;
2251}
2252
2253/*
Chris Masond352ac62008-09-29 15:18:18 -04002254 * read an inode from the btree into the in-memory inode
2255 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002256static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002257{
2258 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002259 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002260 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002261 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002262 struct btrfs_root *root = BTRFS_I(inode)->root;
2263 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002264 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002265 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002266 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002267 int ret;
2268
2269 path = btrfs_alloc_path();
2270 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002271 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002272
Chris Mason39279cc2007-06-12 06:35:45 -04002273 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002274 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002275 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002276
Chris Mason5f39d392007-10-15 16:14:19 -04002277 leaf = path->nodes[0];
2278 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2279 struct btrfs_inode_item);
2280
2281 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2282 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2283 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2284 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002285 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002286
2287 tspec = btrfs_inode_atime(inode_item);
2288 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2289 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2290
2291 tspec = btrfs_inode_mtime(inode_item);
2292 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2293 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2294
2295 tspec = btrfs_inode_ctime(inode_item);
2296 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2297 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2298
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002299 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002300 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002301 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002302 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002303 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002304 rdev = btrfs_inode_rdev(leaf, inode_item);
2305
Josef Bacikaec74772008-07-24 12:12:38 -04002306 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002307 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002308
Chris Mason5f39d392007-10-15 16:14:19 -04002309 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002310
Chris Mason46a53cc2009-04-27 11:47:50 -04002311 /*
2312 * try to precache a NULL acl entry for files that don't have
2313 * any xattrs or acls
2314 */
2315 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002316 if (!maybe_acls)
2317 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002318
Yan Zhengd2fb3432008-12-11 16:30:39 -05002319 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2320 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002321 btrfs_free_path(path);
2322 inode_item = NULL;
2323
Chris Mason39279cc2007-06-12 06:35:45 -04002324 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002325 case S_IFREG:
2326 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002327 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002328 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002329 inode->i_fop = &btrfs_file_operations;
2330 inode->i_op = &btrfs_file_inode_operations;
2331 break;
2332 case S_IFDIR:
2333 inode->i_fop = &btrfs_dir_file_operations;
2334 if (root == root->fs_info->tree_root)
2335 inode->i_op = &btrfs_dir_ro_inode_operations;
2336 else
2337 inode->i_op = &btrfs_dir_inode_operations;
2338 break;
2339 case S_IFLNK:
2340 inode->i_op = &btrfs_symlink_inode_operations;
2341 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002342 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002343 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002344 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002345 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002346 init_special_inode(inode, inode->i_mode, rdev);
2347 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002348 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002349
2350 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002351 return;
2352
2353make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002354 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002355 make_bad_inode(inode);
2356}
2357
Chris Masond352ac62008-09-29 15:18:18 -04002358/*
2359 * given a leaf and an inode, copy the inode fields into the leaf
2360 */
Chris Masone02119d2008-09-05 16:13:11 -04002361static void fill_inode_item(struct btrfs_trans_handle *trans,
2362 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002363 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002364 struct inode *inode)
2365{
Chris Mason5f39d392007-10-15 16:14:19 -04002366 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2367 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002368 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002369 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2370 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2371
2372 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2373 inode->i_atime.tv_sec);
2374 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2375 inode->i_atime.tv_nsec);
2376
2377 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2378 inode->i_mtime.tv_sec);
2379 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2380 inode->i_mtime.tv_nsec);
2381
2382 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2383 inode->i_ctime.tv_sec);
2384 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2385 inode->i_ctime.tv_nsec);
2386
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002387 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002388 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002389 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002390 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002391 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002392 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002393 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002394}
2395
Chris Masond352ac62008-09-29 15:18:18 -04002396/*
2397 * copy everything in the in-memory inode into the btree.
2398 */
Chris Masond3977122009-01-05 21:25:51 -05002399noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2400 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002401{
2402 struct btrfs_inode_item *inode_item;
2403 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002404 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002405 int ret;
2406
2407 path = btrfs_alloc_path();
2408 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002409 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002410 ret = btrfs_lookup_inode(trans, root, path,
2411 &BTRFS_I(inode)->location, 1);
2412 if (ret) {
2413 if (ret > 0)
2414 ret = -ENOENT;
2415 goto failed;
2416 }
2417
Chris Masonb4ce94d2009-02-04 09:25:08 -05002418 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002419 leaf = path->nodes[0];
2420 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002421 struct btrfs_inode_item);
2422
Chris Masone02119d2008-09-05 16:13:11 -04002423 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002424 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002425 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002426 ret = 0;
2427failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002428 btrfs_free_path(path);
2429 return ret;
2430}
2431
2432
Chris Masond352ac62008-09-29 15:18:18 -04002433/*
2434 * unlink helper that gets used here in inode.c and in the tree logging
2435 * recovery code. It remove a link in a directory with a given name, and
2436 * also drops the back refs in the inode to the directory
2437 */
Chris Masone02119d2008-09-05 16:13:11 -04002438int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2439 struct btrfs_root *root,
2440 struct inode *dir, struct inode *inode,
2441 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002442{
2443 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002444 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002445 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002446 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002447 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002448 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002449
2450 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002451 if (!path) {
2452 ret = -ENOMEM;
2453 goto err;
2454 }
2455
Chris Masonb9473432009-03-13 11:00:37 -04002456 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002457 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2458 name, name_len, -1);
2459 if (IS_ERR(di)) {
2460 ret = PTR_ERR(di);
2461 goto err;
2462 }
2463 if (!di) {
2464 ret = -ENOENT;
2465 goto err;
2466 }
Chris Mason5f39d392007-10-15 16:14:19 -04002467 leaf = path->nodes[0];
2468 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002469 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002470 if (ret)
2471 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002472 btrfs_release_path(root, path);
2473
Josef Bacikaec74772008-07-24 12:12:38 -04002474 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002475 inode->i_ino,
2476 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002477 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002478 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002479 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002480 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002481 goto err;
2482 }
2483
Chris Mason39279cc2007-06-12 06:35:45 -04002484 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002485 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002486 if (IS_ERR(di)) {
2487 ret = PTR_ERR(di);
2488 goto err;
2489 }
2490 if (!di) {
2491 ret = -ENOENT;
2492 goto err;
2493 }
2494 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002495 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002496
Chris Masone02119d2008-09-05 16:13:11 -04002497 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2498 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002499 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002500
2501 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2502 dir, index);
2503 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002504err:
2505 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002506 if (ret)
2507 goto out;
2508
2509 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2510 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2511 btrfs_update_inode(trans, root, dir);
2512 btrfs_drop_nlink(inode);
2513 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002514out:
Chris Mason39279cc2007-06-12 06:35:45 -04002515 return ret;
2516}
2517
2518static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2519{
2520 struct btrfs_root *root;
2521 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002522 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002523 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002524 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002525
2526 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002527
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002528 /*
2529 * 5 items for unlink inode
2530 * 1 for orphan
2531 */
2532 ret = btrfs_reserve_metadata_space(root, 6);
2533 if (ret)
2534 return ret;
2535
Chris Mason39279cc2007-06-12 06:35:45 -04002536 trans = btrfs_start_transaction(root, 1);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002537 if (IS_ERR(trans)) {
2538 btrfs_unreserve_metadata_space(root, 6);
2539 return PTR_ERR(trans);
2540 }
Chris Mason5f39d392007-10-15 16:14:19 -04002541
Chris Mason39279cc2007-06-12 06:35:45 -04002542 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002543
2544 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2545
Chris Masone02119d2008-09-05 16:13:11 -04002546 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2547 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002548
2549 if (inode->i_nlink == 0)
2550 ret = btrfs_orphan_add(trans, inode);
2551
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002552 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002553
Chris Mason89ce8a62008-06-25 16:01:31 -04002554 btrfs_end_transaction_throttle(trans, root);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002555 btrfs_unreserve_metadata_space(root, 6);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002556 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002557 return ret;
2558}
2559
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002560int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2561 struct btrfs_root *root,
2562 struct inode *dir, u64 objectid,
2563 const char *name, int name_len)
2564{
2565 struct btrfs_path *path;
2566 struct extent_buffer *leaf;
2567 struct btrfs_dir_item *di;
2568 struct btrfs_key key;
2569 u64 index;
2570 int ret;
2571
2572 path = btrfs_alloc_path();
2573 if (!path)
2574 return -ENOMEM;
2575
2576 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2577 name, name_len, -1);
2578 BUG_ON(!di || IS_ERR(di));
2579
2580 leaf = path->nodes[0];
2581 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2582 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2583 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2584 BUG_ON(ret);
2585 btrfs_release_path(root, path);
2586
2587 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2588 objectid, root->root_key.objectid,
2589 dir->i_ino, &index, name, name_len);
2590 if (ret < 0) {
2591 BUG_ON(ret != -ENOENT);
2592 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2593 name, name_len);
2594 BUG_ON(!di || IS_ERR(di));
2595
2596 leaf = path->nodes[0];
2597 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2598 btrfs_release_path(root, path);
2599 index = key.offset;
2600 }
2601
2602 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2603 index, name, name_len, -1);
2604 BUG_ON(!di || IS_ERR(di));
2605
2606 leaf = path->nodes[0];
2607 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2608 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2609 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2610 BUG_ON(ret);
2611 btrfs_release_path(root, path);
2612
2613 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2614 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2615 ret = btrfs_update_inode(trans, root, dir);
2616 BUG_ON(ret);
2617 dir->i_sb->s_dirt = 1;
2618
2619 btrfs_free_path(path);
2620 return 0;
2621}
2622
Chris Mason39279cc2007-06-12 06:35:45 -04002623static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2624{
2625 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002626 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002627 int ret;
2628 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002629 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002630 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002631
Chris Mason3394e162008-11-17 20:42:26 -05002632 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002633 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002634 return -ENOTEMPTY;
2635
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002636 ret = btrfs_reserve_metadata_space(root, 5);
2637 if (ret)
2638 return ret;
2639
Chris Mason39279cc2007-06-12 06:35:45 -04002640 trans = btrfs_start_transaction(root, 1);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002641 if (IS_ERR(trans)) {
2642 btrfs_unreserve_metadata_space(root, 5);
2643 return PTR_ERR(trans);
2644 }
2645
Chris Mason39279cc2007-06-12 06:35:45 -04002646 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002647
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002648 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2649 err = btrfs_unlink_subvol(trans, root, dir,
2650 BTRFS_I(inode)->location.objectid,
2651 dentry->d_name.name,
2652 dentry->d_name.len);
2653 goto out;
2654 }
2655
Josef Bacik7b128762008-07-24 12:17:14 -04002656 err = btrfs_orphan_add(trans, inode);
2657 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002658 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002659
Chris Mason39279cc2007-06-12 06:35:45 -04002660 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002661 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2662 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002663 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002664 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002665out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002666 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002667 ret = btrfs_end_transaction_throttle(trans, root);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002668 btrfs_unreserve_metadata_space(root, 5);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002669 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002670
Chris Mason39279cc2007-06-12 06:35:45 -04002671 if (ret && !err)
2672 err = ret;
2673 return err;
2674}
2675
Chris Masond20f7042008-12-08 16:58:54 -05002676#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002677/*
Chris Mason323ac952008-10-01 19:05:46 -04002678 * when truncating bytes in a file, it is possible to avoid reading
2679 * the leaves that contain only checksum items. This can be the
2680 * majority of the IO required to delete a large file, but it must
2681 * be done carefully.
2682 *
2683 * The keys in the level just above the leaves are checked to make sure
2684 * the lowest key in a given leaf is a csum key, and starts at an offset
2685 * after the new size.
2686 *
2687 * Then the key for the next leaf is checked to make sure it also has
2688 * a checksum item for the same file. If it does, we know our target leaf
2689 * contains only checksum items, and it can be safely freed without reading
2690 * it.
2691 *
2692 * This is just an optimization targeted at large files. It may do
2693 * nothing. It will return 0 unless things went badly.
2694 */
2695static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2696 struct btrfs_root *root,
2697 struct btrfs_path *path,
2698 struct inode *inode, u64 new_size)
2699{
2700 struct btrfs_key key;
2701 int ret;
2702 int nritems;
2703 struct btrfs_key found_key;
2704 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002705 struct btrfs_leaf_ref *ref;
2706 u64 leaf_gen;
2707 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002708
2709 path->lowest_level = 1;
2710 key.objectid = inode->i_ino;
2711 key.type = BTRFS_CSUM_ITEM_KEY;
2712 key.offset = new_size;
2713again:
2714 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2715 if (ret < 0)
2716 goto out;
2717
2718 if (path->nodes[1] == NULL) {
2719 ret = 0;
2720 goto out;
2721 }
2722 ret = 0;
2723 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2724 nritems = btrfs_header_nritems(path->nodes[1]);
2725
2726 if (!nritems)
2727 goto out;
2728
2729 if (path->slots[1] >= nritems)
2730 goto next_node;
2731
2732 /* did we find a key greater than anything we want to delete? */
2733 if (found_key.objectid > inode->i_ino ||
2734 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2735 goto out;
2736
2737 /* we check the next key in the node to make sure the leave contains
2738 * only checksum items. This comparison doesn't work if our
2739 * leaf is the last one in the node
2740 */
2741 if (path->slots[1] + 1 >= nritems) {
2742next_node:
2743 /* search forward from the last key in the node, this
2744 * will bring us into the next node in the tree
2745 */
2746 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2747
2748 /* unlikely, but we inc below, so check to be safe */
2749 if (found_key.offset == (u64)-1)
2750 goto out;
2751
2752 /* search_forward needs a path with locks held, do the
2753 * search again for the original key. It is possible
2754 * this will race with a balance and return a path that
2755 * we could modify, but this drop is just an optimization
2756 * and is allowed to miss some leaves.
2757 */
2758 btrfs_release_path(root, path);
2759 found_key.offset++;
2760
2761 /* setup a max key for search_forward */
2762 other_key.offset = (u64)-1;
2763 other_key.type = key.type;
2764 other_key.objectid = key.objectid;
2765
2766 path->keep_locks = 1;
2767 ret = btrfs_search_forward(root, &found_key, &other_key,
2768 path, 0, 0);
2769 path->keep_locks = 0;
2770 if (ret || found_key.objectid != key.objectid ||
2771 found_key.type != key.type) {
2772 ret = 0;
2773 goto out;
2774 }
2775
2776 key.offset = found_key.offset;
2777 btrfs_release_path(root, path);
2778 cond_resched();
2779 goto again;
2780 }
2781
2782 /* we know there's one more slot after us in the tree,
2783 * read that key so we can verify it is also a checksum item
2784 */
2785 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2786
2787 if (found_key.objectid < inode->i_ino)
2788 goto next_key;
2789
2790 if (found_key.type != key.type || found_key.offset < new_size)
2791 goto next_key;
2792
2793 /*
2794 * if the key for the next leaf isn't a csum key from this objectid,
2795 * we can't be sure there aren't good items inside this leaf.
2796 * Bail out
2797 */
2798 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2799 goto out;
2800
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002801 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2802 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002803 /*
2804 * it is safe to delete this leaf, it contains only
2805 * csum items from this inode at an offset >= new_size
2806 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002807 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002808 BUG_ON(ret);
2809
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002810 if (root->ref_cows && leaf_gen < trans->transid) {
2811 ref = btrfs_alloc_leaf_ref(root, 0);
2812 if (ref) {
2813 ref->root_gen = root->root_key.offset;
2814 ref->bytenr = leaf_start;
2815 ref->owner = 0;
2816 ref->generation = leaf_gen;
2817 ref->nritems = 0;
2818
Chris Masonbd56b302009-02-04 09:27:02 -05002819 btrfs_sort_leaf_ref(ref);
2820
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002821 ret = btrfs_add_leaf_ref(root, ref, 0);
2822 WARN_ON(ret);
2823 btrfs_free_leaf_ref(root, ref);
2824 } else {
2825 WARN_ON(1);
2826 }
2827 }
Chris Mason323ac952008-10-01 19:05:46 -04002828next_key:
2829 btrfs_release_path(root, path);
2830
2831 if (other_key.objectid == inode->i_ino &&
2832 other_key.type == key.type && other_key.offset > key.offset) {
2833 key.offset = other_key.offset;
2834 cond_resched();
2835 goto again;
2836 }
2837 ret = 0;
2838out:
2839 /* fixup any changes we've made to the path */
2840 path->lowest_level = 0;
2841 path->keep_locks = 0;
2842 btrfs_release_path(root, path);
2843 return ret;
2844}
2845
Chris Masond20f7042008-12-08 16:58:54 -05002846#endif
2847
Chris Mason323ac952008-10-01 19:05:46 -04002848/*
Chris Mason39279cc2007-06-12 06:35:45 -04002849 * this can truncate away extent items, csum items and directory items.
2850 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002851 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002852 *
2853 * csum items that cross the new i_size are truncated to the new size
2854 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002855 *
2856 * min_type is the minimum key type to truncate down to. If set to 0, this
2857 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002858 */
Yan, Zheng80825102009-11-12 09:35:36 +00002859int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2860 struct btrfs_root *root,
2861 struct inode *inode,
2862 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002863{
Chris Mason39279cc2007-06-12 06:35:45 -04002864 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002865 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002866 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00002867 struct btrfs_key key;
2868 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002869 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002870 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002871 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002872 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00002873 u64 mask = root->sectorsize - 1;
2874 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04002875 int found_extent;
2876 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002877 int pending_del_nr = 0;
2878 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002879 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002880 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00002881 int ret;
2882 int err = 0;
2883
2884 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002885
Chris Masone02119d2008-09-05 16:13:11 -04002886 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002887 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00002888
Chris Mason39279cc2007-06-12 06:35:45 -04002889 path = btrfs_alloc_path();
2890 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04002891 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04002892
Chris Mason39279cc2007-06-12 06:35:45 -04002893 key.objectid = inode->i_ino;
2894 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002895 key.type = (u8)-1;
2896
Chris Mason85e21ba2008-01-29 15:11:36 -05002897search_again:
Chris Masonb9473432009-03-13 11:00:37 -04002898 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05002899 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00002900 if (ret < 0) {
2901 err = ret;
2902 goto out;
2903 }
Chris Masond3977122009-01-05 21:25:51 -05002904
Chris Mason85e21ba2008-01-29 15:11:36 -05002905 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002906 /* there are no items in the tree for us to truncate, we're
2907 * done
2908 */
Yan, Zheng80825102009-11-12 09:35:36 +00002909 if (path->slots[0] == 0)
2910 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05002911 path->slots[0]--;
2912 }
2913
Chris Masond3977122009-01-05 21:25:51 -05002914 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002915 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002916 leaf = path->nodes[0];
2917 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2918 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002919 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002920
Chris Mason5f39d392007-10-15 16:14:19 -04002921 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002922 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002923
Chris Mason85e21ba2008-01-29 15:11:36 -05002924 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002925 break;
2926
Chris Mason5f39d392007-10-15 16:14:19 -04002927 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002928 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002929 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002930 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002931 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002932 encoding = btrfs_file_extent_compression(leaf, fi);
2933 encoding |= btrfs_file_extent_encryption(leaf, fi);
2934 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2935
Chris Mason179e29e2007-11-01 11:28:41 -04002936 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002937 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002938 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002939 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002940 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002941 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002942 }
Yan008630c2007-11-07 13:31:09 -05002943 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002944 }
Yan, Zheng80825102009-11-12 09:35:36 +00002945 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04002946 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00002947 } else {
2948 if (item_end < new_size)
2949 break;
2950 if (found_key.offset >= new_size)
2951 del_item = 1;
2952 else
2953 del_item = 0;
2954 }
Chris Mason39279cc2007-06-12 06:35:45 -04002955 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002956 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002957 if (found_type != BTRFS_EXTENT_DATA_KEY)
2958 goto delete;
2959
2960 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002961 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002962 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002963 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002964 u64 orig_num_bytes =
2965 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002966 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002967 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002968 extent_num_bytes = extent_num_bytes &
2969 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002970 btrfs_set_file_extent_num_bytes(leaf, fi,
2971 extent_num_bytes);
2972 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002973 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002974 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002975 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002976 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002977 } else {
Chris Masondb945352007-10-15 16:15:53 -04002978 extent_num_bytes =
2979 btrfs_file_extent_disk_num_bytes(leaf,
2980 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002981 extent_offset = found_key.offset -
2982 btrfs_file_extent_offset(leaf, fi);
2983
Chris Mason39279cc2007-06-12 06:35:45 -04002984 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002985 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002986 if (extent_start != 0) {
2987 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002988 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002989 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002990 }
2991 }
Chris Mason90692182008-02-08 13:49:28 -05002992 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002993 /*
2994 * we can't truncate inline items that have had
2995 * special encodings
2996 */
2997 if (!del_item &&
2998 btrfs_file_extent_compression(leaf, fi) == 0 &&
2999 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3000 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003001 u32 size = new_size - found_key.offset;
3002
3003 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003004 inode_sub_bytes(inode, item_end + 1 -
3005 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003006 }
3007 size =
3008 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003009 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003010 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003011 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003012 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003013 inode_sub_bytes(inode, item_end + 1 -
3014 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003015 }
Chris Mason39279cc2007-06-12 06:35:45 -04003016 }
Chris Mason179e29e2007-11-01 11:28:41 -04003017delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003018 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003019 if (!pending_del_nr) {
3020 /* no pending yet, add ourselves */
3021 pending_del_slot = path->slots[0];
3022 pending_del_nr = 1;
3023 } else if (pending_del_nr &&
3024 path->slots[0] + 1 == pending_del_slot) {
3025 /* hop on the pending chunk */
3026 pending_del_nr++;
3027 pending_del_slot = path->slots[0];
3028 } else {
Chris Masond3977122009-01-05 21:25:51 -05003029 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003030 }
Chris Mason39279cc2007-06-12 06:35:45 -04003031 } else {
3032 break;
3033 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003034 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04003035 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003036 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003037 extent_num_bytes, 0,
3038 btrfs_header_owner(leaf),
3039 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003040 BUG_ON(ret);
3041 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003042
Yan, Zheng80825102009-11-12 09:35:36 +00003043 if (found_type == BTRFS_INODE_ITEM_KEY)
3044 break;
3045
3046 if (path->slots[0] == 0 ||
3047 path->slots[0] != pending_del_slot) {
3048 if (root->ref_cows) {
3049 err = -EAGAIN;
3050 goto out;
3051 }
3052 if (pending_del_nr) {
3053 ret = btrfs_del_items(trans, root, path,
3054 pending_del_slot,
3055 pending_del_nr);
3056 BUG_ON(ret);
3057 pending_del_nr = 0;
3058 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003059 btrfs_release_path(root, path);
3060 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003061 } else {
3062 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003063 }
Chris Mason39279cc2007-06-12 06:35:45 -04003064 }
Yan, Zheng80825102009-11-12 09:35:36 +00003065out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003066 if (pending_del_nr) {
3067 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3068 pending_del_nr);
3069 }
Chris Mason39279cc2007-06-12 06:35:45 -04003070 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003071 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003072}
3073
Chris Masona52d9a82007-08-27 16:49:44 -04003074/*
3075 * taken from block_truncate_page, but does cow as it zeros out
3076 * any bytes left in the last page in the file.
3077 */
3078static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3079{
3080 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003081 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003082 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3083 struct btrfs_ordered_extent *ordered;
3084 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003085 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003086 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3087 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3088 struct page *page;
3089 int ret = 0;
3090 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003091 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003092
3093 if ((offset & (blocksize - 1)) == 0)
3094 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003095 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
3096 if (ret)
3097 goto out;
3098
3099 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
3100 if (ret)
3101 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003102
3103 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003104again:
Chris Masona52d9a82007-08-27 16:49:44 -04003105 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003106 if (!page) {
3107 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3108 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04003109 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003110 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003111
3112 page_start = page_offset(page);
3113 page_end = page_start + PAGE_CACHE_SIZE - 1;
3114
Chris Masona52d9a82007-08-27 16:49:44 -04003115 if (!PageUptodate(page)) {
3116 ret = btrfs_readpage(NULL, page);
3117 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003118 if (page->mapping != mapping) {
3119 unlock_page(page);
3120 page_cache_release(page);
3121 goto again;
3122 }
Chris Masona52d9a82007-08-27 16:49:44 -04003123 if (!PageUptodate(page)) {
3124 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003125 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003126 }
3127 }
Chris Mason211c17f2008-05-15 09:13:45 -04003128 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003129
3130 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3131 set_page_extent_mapped(page);
3132
3133 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3134 if (ordered) {
3135 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3136 unlock_page(page);
3137 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003138 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003139 btrfs_put_ordered_extent(ordered);
3140 goto again;
3141 }
3142
Josef Bacik5d5e1032009-10-13 16:46:49 -04003143 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
3144 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3145 GFP_NOFS);
3146
Josef Bacik9ed74f22009-09-11 16:12:44 -04003147 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3148 if (ret) {
3149 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3150 goto out_unlock;
3151 }
3152
Chris Masone6dcd2d2008-07-17 12:53:50 -04003153 ret = 0;
3154 if (offset != PAGE_CACHE_SIZE) {
3155 kaddr = kmap(page);
3156 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3157 flush_dcache_page(page);
3158 kunmap(page);
3159 }
Chris Mason247e7432008-07-17 12:53:51 -04003160 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003161 set_page_dirty(page);
3162 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003163
Chris Mason89642222008-07-24 09:41:53 -04003164out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003165 if (ret)
3166 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3167 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003168 unlock_page(page);
3169 page_cache_release(page);
3170out:
3171 return ret;
3172}
3173
Yan Zheng9036c102008-10-30 14:19:41 -04003174int btrfs_cont_expand(struct inode *inode, loff_t size)
3175{
3176 struct btrfs_trans_handle *trans;
3177 struct btrfs_root *root = BTRFS_I(inode)->root;
3178 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3179 struct extent_map *em;
3180 u64 mask = root->sectorsize - 1;
3181 u64 hole_start = (inode->i_size + mask) & ~mask;
3182 u64 block_end = (size + mask) & ~mask;
3183 u64 last_byte;
3184 u64 cur_offset;
3185 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003186 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003187
3188 if (size <= hole_start)
3189 return 0;
3190
Yan Zheng9036c102008-10-30 14:19:41 -04003191 while (1) {
3192 struct btrfs_ordered_extent *ordered;
3193 btrfs_wait_ordered_range(inode, hole_start,
3194 block_end - hole_start);
3195 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3196 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3197 if (!ordered)
3198 break;
3199 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3200 btrfs_put_ordered_extent(ordered);
3201 }
3202
Yan Zheng9036c102008-10-30 14:19:41 -04003203 cur_offset = hole_start;
3204 while (1) {
3205 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3206 block_end - cur_offset, 0);
3207 BUG_ON(IS_ERR(em) || !em);
3208 last_byte = min(extent_map_end(em), block_end);
3209 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003210 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003211 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003212 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003213
3214 err = btrfs_reserve_metadata_space(root, 2);
Chris Mason771ed682008-11-06 22:02:51 -05003215 if (err)
3216 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003217
Yan, Zheng80825102009-11-12 09:35:36 +00003218 trans = btrfs_start_transaction(root, 1);
3219 btrfs_set_trans_block_group(trans, inode);
3220
3221 err = btrfs_drop_extents(trans, inode, cur_offset,
3222 cur_offset + hole_size,
3223 &hint_byte, 1);
3224 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003225
Yan Zheng9036c102008-10-30 14:19:41 -04003226 err = btrfs_insert_file_extent(trans, root,
3227 inode->i_ino, cur_offset, 0,
3228 0, hole_size, 0, hole_size,
3229 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003230 BUG_ON(err);
3231
Yan Zheng9036c102008-10-30 14:19:41 -04003232 btrfs_drop_extent_cache(inode, hole_start,
3233 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003234
3235 btrfs_end_transaction(trans, root);
3236 btrfs_unreserve_metadata_space(root, 2);
Yan Zheng9036c102008-10-30 14:19:41 -04003237 }
3238 free_extent_map(em);
3239 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003240 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003241 break;
3242 }
3243
Yan Zheng9036c102008-10-30 14:19:41 -04003244 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3245 return err;
3246}
3247
Yan, Zheng80825102009-11-12 09:35:36 +00003248static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3249{
3250 struct btrfs_root *root = BTRFS_I(inode)->root;
3251 struct btrfs_trans_handle *trans;
3252 unsigned long nr;
3253 int ret;
3254
3255 if (attr->ia_size == inode->i_size)
3256 return 0;
3257
3258 if (attr->ia_size > inode->i_size) {
3259 unsigned long limit;
3260 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3261 if (attr->ia_size > inode->i_sb->s_maxbytes)
3262 return -EFBIG;
3263 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3264 send_sig(SIGXFSZ, current, 0);
3265 return -EFBIG;
3266 }
3267 }
3268
3269 ret = btrfs_reserve_metadata_space(root, 1);
3270 if (ret)
3271 return ret;
3272
3273 trans = btrfs_start_transaction(root, 1);
3274 btrfs_set_trans_block_group(trans, inode);
3275
3276 ret = btrfs_orphan_add(trans, inode);
3277 BUG_ON(ret);
3278
3279 nr = trans->blocks_used;
3280 btrfs_end_transaction(trans, root);
3281 btrfs_unreserve_metadata_space(root, 1);
3282 btrfs_btree_balance_dirty(root, nr);
3283
3284 if (attr->ia_size > inode->i_size) {
3285 ret = btrfs_cont_expand(inode, attr->ia_size);
3286 if (ret) {
3287 btrfs_truncate(inode);
3288 return ret;
3289 }
3290
3291 i_size_write(inode, attr->ia_size);
3292 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3293
3294 trans = btrfs_start_transaction(root, 1);
3295 btrfs_set_trans_block_group(trans, inode);
3296
3297 ret = btrfs_update_inode(trans, root, inode);
3298 BUG_ON(ret);
3299 if (inode->i_nlink > 0) {
3300 ret = btrfs_orphan_del(trans, inode);
3301 BUG_ON(ret);
3302 }
3303 nr = trans->blocks_used;
3304 btrfs_end_transaction(trans, root);
3305 btrfs_btree_balance_dirty(root, nr);
3306 return 0;
3307 }
3308
3309 /*
3310 * We're truncating a file that used to have good data down to
3311 * zero. Make sure it gets into the ordered flush list so that
3312 * any new writes get down to disk quickly.
3313 */
3314 if (attr->ia_size == 0)
3315 BTRFS_I(inode)->ordered_data_close = 1;
3316
3317 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3318 ret = vmtruncate(inode, attr->ia_size);
3319 BUG_ON(ret);
3320
3321 return 0;
3322}
3323
Chris Mason39279cc2007-06-12 06:35:45 -04003324static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3325{
3326 struct inode *inode = dentry->d_inode;
3327 int err;
3328
3329 err = inode_change_ok(inode, attr);
3330 if (err)
3331 return err;
3332
Chris Mason5a3f23d2009-03-31 13:27:11 -04003333 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003334 err = btrfs_setattr_size(inode, attr);
3335 if (err)
3336 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003337 }
Yan, Zheng80825102009-11-12 09:35:36 +00003338 attr->ia_valid &= ~ATTR_SIZE;
Yan Zheng9036c102008-10-30 14:19:41 -04003339
Yan, Zheng80825102009-11-12 09:35:36 +00003340 if (attr->ia_valid)
3341 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003342
3343 if (!err && ((attr->ia_valid & ATTR_MODE)))
3344 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003345 return err;
3346}
Chris Mason61295eb2008-01-14 16:24:38 -05003347
Chris Mason39279cc2007-06-12 06:35:45 -04003348void btrfs_delete_inode(struct inode *inode)
3349{
3350 struct btrfs_trans_handle *trans;
3351 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003352 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003353 int ret;
3354
3355 truncate_inode_pages(&inode->i_data, 0);
3356 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003357 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003358 goto no_delete;
3359 }
Chris Mason4a096752008-07-21 10:29:44 -04003360 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003361
Yan, Zhengc71bf092009-11-12 09:34:40 +00003362 if (root->fs_info->log_root_recovering) {
3363 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3364 goto no_delete;
3365 }
3366
Yan, Zheng76dda932009-09-21 16:00:26 -04003367 if (inode->i_nlink > 0) {
3368 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3369 goto no_delete;
3370 }
3371
Chris Masondbe674a2008-07-17 12:54:05 -04003372 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003373
Yan, Zheng80825102009-11-12 09:35:36 +00003374 while (1) {
3375 trans = btrfs_start_transaction(root, 1);
3376 btrfs_set_trans_block_group(trans, inode);
3377 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3378
3379 if (ret != -EAGAIN)
3380 break;
3381
3382 nr = trans->blocks_used;
3383 btrfs_end_transaction(trans, root);
3384 trans = NULL;
3385 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003386 }
3387
Yan, Zheng80825102009-11-12 09:35:36 +00003388 if (ret == 0) {
3389 ret = btrfs_orphan_del(trans, inode);
3390 BUG_ON(ret);
3391 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003392
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003393 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003394 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003395 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003396no_delete:
3397 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003398 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003399}
3400
3401/*
3402 * this returns the key found in the dir entry in the location pointer.
3403 * If no dir entries were found, location->objectid is 0.
3404 */
3405static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3406 struct btrfs_key *location)
3407{
3408 const char *name = dentry->d_name.name;
3409 int namelen = dentry->d_name.len;
3410 struct btrfs_dir_item *di;
3411 struct btrfs_path *path;
3412 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003413 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003414
3415 path = btrfs_alloc_path();
3416 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003417
Chris Mason39279cc2007-06-12 06:35:45 -04003418 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3419 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003420 if (IS_ERR(di))
3421 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003422
3423 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003424 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003425
Chris Mason5f39d392007-10-15 16:14:19 -04003426 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003427out:
Chris Mason39279cc2007-06-12 06:35:45 -04003428 btrfs_free_path(path);
3429 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003430out_err:
3431 location->objectid = 0;
3432 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003433}
3434
3435/*
3436 * when we hit a tree root in a directory, the btrfs part of the inode
3437 * needs to be changed to reflect the root directory of the tree root. This
3438 * is kind of like crossing a mount point.
3439 */
3440static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003441 struct inode *dir,
3442 struct dentry *dentry,
3443 struct btrfs_key *location,
3444 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003445{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003446 struct btrfs_path *path;
3447 struct btrfs_root *new_root;
3448 struct btrfs_root_ref *ref;
3449 struct extent_buffer *leaf;
3450 int ret;
3451 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003452
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003453 path = btrfs_alloc_path();
3454 if (!path) {
3455 err = -ENOMEM;
3456 goto out;
3457 }
Chris Mason39279cc2007-06-12 06:35:45 -04003458
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003459 err = -ENOENT;
3460 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3461 BTRFS_I(dir)->root->root_key.objectid,
3462 location->objectid);
3463 if (ret) {
3464 if (ret < 0)
3465 err = ret;
3466 goto out;
3467 }
Chris Mason39279cc2007-06-12 06:35:45 -04003468
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003469 leaf = path->nodes[0];
3470 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3471 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3472 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3473 goto out;
3474
3475 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3476 (unsigned long)(ref + 1),
3477 dentry->d_name.len);
3478 if (ret)
3479 goto out;
3480
3481 btrfs_release_path(root->fs_info->tree_root, path);
3482
3483 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3484 if (IS_ERR(new_root)) {
3485 err = PTR_ERR(new_root);
3486 goto out;
3487 }
3488
3489 if (btrfs_root_refs(&new_root->root_item) == 0) {
3490 err = -ENOENT;
3491 goto out;
3492 }
3493
3494 *sub_root = new_root;
3495 location->objectid = btrfs_root_dirid(&new_root->root_item);
3496 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003497 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003498 err = 0;
3499out:
3500 btrfs_free_path(path);
3501 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003502}
3503
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003504static void inode_tree_add(struct inode *inode)
3505{
3506 struct btrfs_root *root = BTRFS_I(inode)->root;
3507 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003508 struct rb_node **p;
3509 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003510again:
3511 p = &root->inode_tree.rb_node;
3512 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003513
Yan, Zheng76dda932009-09-21 16:00:26 -04003514 if (hlist_unhashed(&inode->i_hash))
3515 return;
3516
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003517 spin_lock(&root->inode_lock);
3518 while (*p) {
3519 parent = *p;
3520 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3521
3522 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003523 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003524 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003525 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003526 else {
3527 WARN_ON(!(entry->vfs_inode.i_state &
3528 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003529 rb_erase(parent, &root->inode_tree);
3530 RB_CLEAR_NODE(parent);
3531 spin_unlock(&root->inode_lock);
3532 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003533 }
3534 }
3535 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3536 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3537 spin_unlock(&root->inode_lock);
3538}
3539
3540static void inode_tree_del(struct inode *inode)
3541{
3542 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003543 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003544
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003545 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003546 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003547 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003548 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003549 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003550 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003551 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003552
3553 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3554 synchronize_srcu(&root->fs_info->subvol_srcu);
3555 spin_lock(&root->inode_lock);
3556 empty = RB_EMPTY_ROOT(&root->inode_tree);
3557 spin_unlock(&root->inode_lock);
3558 if (empty)
3559 btrfs_add_dead_root(root);
3560 }
3561}
3562
3563int btrfs_invalidate_inodes(struct btrfs_root *root)
3564{
3565 struct rb_node *node;
3566 struct rb_node *prev;
3567 struct btrfs_inode *entry;
3568 struct inode *inode;
3569 u64 objectid = 0;
3570
3571 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3572
3573 spin_lock(&root->inode_lock);
3574again:
3575 node = root->inode_tree.rb_node;
3576 prev = NULL;
3577 while (node) {
3578 prev = node;
3579 entry = rb_entry(node, struct btrfs_inode, rb_node);
3580
3581 if (objectid < entry->vfs_inode.i_ino)
3582 node = node->rb_left;
3583 else if (objectid > entry->vfs_inode.i_ino)
3584 node = node->rb_right;
3585 else
3586 break;
3587 }
3588 if (!node) {
3589 while (prev) {
3590 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3591 if (objectid <= entry->vfs_inode.i_ino) {
3592 node = prev;
3593 break;
3594 }
3595 prev = rb_next(prev);
3596 }
3597 }
3598 while (node) {
3599 entry = rb_entry(node, struct btrfs_inode, rb_node);
3600 objectid = entry->vfs_inode.i_ino + 1;
3601 inode = igrab(&entry->vfs_inode);
3602 if (inode) {
3603 spin_unlock(&root->inode_lock);
3604 if (atomic_read(&inode->i_count) > 1)
3605 d_prune_aliases(inode);
3606 /*
3607 * btrfs_drop_inode will remove it from
3608 * the inode cache when its usage count
3609 * hits zero.
3610 */
3611 iput(inode);
3612 cond_resched();
3613 spin_lock(&root->inode_lock);
3614 goto again;
3615 }
3616
3617 if (cond_resched_lock(&root->inode_lock))
3618 goto again;
3619
3620 node = rb_next(node);
3621 }
3622 spin_unlock(&root->inode_lock);
3623 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003624}
3625
Chris Masone02119d2008-09-05 16:13:11 -04003626static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003627{
Chris Masone02119d2008-09-05 16:13:11 -04003628 struct btrfs_inode *bi = BTRFS_I(inode);
3629
Chris Masone02119d2008-09-05 16:13:11 -04003630 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05003631 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003632 bi->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04003633 bi->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003634 bi->logged_trans = 0;
3635 bi->delalloc_bytes = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003636 bi->reserved_bytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003637 bi->disk_i_size = 0;
3638 bi->flags = 0;
3639 bi->index_cnt = (u64)-1;
Chris Mason12fcfd22009-03-24 10:24:20 -04003640 bi->last_unlink_trans = 0;
Chris Mason27574952009-05-14 13:10:02 -04003641 bi->ordered_data_close = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003642 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3643 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04003644 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003645 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3646 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003647 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04003648 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003649 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Chris Masonba1da2f2008-07-17 12:54:15 -04003650 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masone02119d2008-09-05 16:13:11 -04003651 mutex_init(&BTRFS_I(inode)->log_mutex);
3652}
3653
3654static int btrfs_init_locked_inode(struct inode *inode, void *p)
3655{
3656 struct btrfs_iget_args *args = p;
3657 inode->i_ino = args->ino;
3658 init_btrfs_i(inode);
3659 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003660 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003661 return 0;
3662}
3663
3664static int btrfs_find_actor(struct inode *inode, void *opaque)
3665{
3666 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003667 return args->ino == inode->i_ino &&
3668 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003669}
3670
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003671static struct inode *btrfs_iget_locked(struct super_block *s,
3672 u64 objectid,
3673 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003674{
3675 struct inode *inode;
3676 struct btrfs_iget_args args;
3677 args.ino = objectid;
3678 args.root = root;
3679
3680 inode = iget5_locked(s, objectid, btrfs_find_actor,
3681 btrfs_init_locked_inode,
3682 (void *)&args);
3683 return inode;
3684}
3685
Balaji Rao1a54ef82008-07-21 02:01:04 +05303686/* Get an inode object given its location and corresponding root.
3687 * Returns in *is_new if the inode was read from disk
3688 */
3689struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003690 struct btrfs_root *root)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303691{
3692 struct inode *inode;
3693
3694 inode = btrfs_iget_locked(s, location->objectid, root);
3695 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003696 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303697
3698 if (inode->i_state & I_NEW) {
3699 BTRFS_I(inode)->root = root;
3700 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3701 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003702
3703 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303704 unlock_new_inode(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303705 }
3706
3707 return inode;
3708}
3709
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003710static struct inode *new_simple_dir(struct super_block *s,
3711 struct btrfs_key *key,
3712 struct btrfs_root *root)
3713{
3714 struct inode *inode = new_inode(s);
3715
3716 if (!inode)
3717 return ERR_PTR(-ENOMEM);
3718
3719 init_btrfs_i(inode);
3720
3721 BTRFS_I(inode)->root = root;
3722 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3723 BTRFS_I(inode)->dummy_inode = 1;
3724
3725 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3726 inode->i_op = &simple_dir_inode_operations;
3727 inode->i_fop = &simple_dir_operations;
3728 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3729 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3730
3731 return inode;
3732}
3733
Chris Mason3de45862008-11-17 21:02:50 -05003734struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003735{
Chris Masond3977122009-01-05 21:25:51 -05003736 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003737 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003738 struct btrfs_root *sub_root = root;
3739 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003740 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003741 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003742
Yan, Zheng76dda932009-09-21 16:00:26 -04003743 dentry->d_op = &btrfs_dentry_operations;
3744
Chris Mason39279cc2007-06-12 06:35:45 -04003745 if (dentry->d_name.len > BTRFS_NAME_LEN)
3746 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003747
Chris Mason39279cc2007-06-12 06:35:45 -04003748 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003749
Chris Mason39279cc2007-06-12 06:35:45 -04003750 if (ret < 0)
3751 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003752
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003753 if (location.objectid == 0)
3754 return NULL;
3755
3756 if (location.type == BTRFS_INODE_ITEM_KEY) {
3757 inode = btrfs_iget(dir->i_sb, &location, root);
3758 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003759 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003760
3761 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3762
Yan, Zheng76dda932009-09-21 16:00:26 -04003763 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003764 ret = fixup_tree_root_location(root, dir, dentry,
3765 &location, &sub_root);
3766 if (ret < 0) {
3767 if (ret != -ENOENT)
3768 inode = ERR_PTR(ret);
3769 else
3770 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3771 } else {
Chris Mason39279cc2007-06-12 06:35:45 -04003772 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04003773 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003774 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3775
Yan, Zhengc71bf092009-11-12 09:34:40 +00003776 if (root != sub_root) {
3777 down_read(&root->fs_info->cleanup_work_sem);
3778 if (!(inode->i_sb->s_flags & MS_RDONLY))
3779 btrfs_orphan_cleanup(sub_root);
3780 up_read(&root->fs_info->cleanup_work_sem);
3781 }
3782
Chris Mason3de45862008-11-17 21:02:50 -05003783 return inode;
3784}
3785
Yan, Zheng76dda932009-09-21 16:00:26 -04003786static int btrfs_dentry_delete(struct dentry *dentry)
3787{
3788 struct btrfs_root *root;
3789
Yan, Zhengefefb142009-10-09 09:25:16 -04003790 if (!dentry->d_inode && !IS_ROOT(dentry))
3791 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04003792
Yan, Zhengefefb142009-10-09 09:25:16 -04003793 if (dentry->d_inode) {
3794 root = BTRFS_I(dentry->d_inode)->root;
3795 if (btrfs_root_refs(&root->root_item) == 0)
3796 return 1;
3797 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003798 return 0;
3799}
3800
Chris Mason3de45862008-11-17 21:02:50 -05003801static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3802 struct nameidata *nd)
3803{
3804 struct inode *inode;
3805
Chris Mason3de45862008-11-17 21:02:50 -05003806 inode = btrfs_lookup_dentry(dir, dentry);
3807 if (IS_ERR(inode))
3808 return ERR_CAST(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003809
Chris Mason39279cc2007-06-12 06:35:45 -04003810 return d_splice_alias(inode, dentry);
3811}
3812
3813static unsigned char btrfs_filetype_table[] = {
3814 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3815};
3816
David Woodhousecbdf5a22008-08-06 19:42:33 +01003817static int btrfs_real_readdir(struct file *filp, void *dirent,
3818 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003819{
3820 struct inode *inode = filp->f_dentry->d_inode;
3821 struct btrfs_root *root = BTRFS_I(inode)->root;
3822 struct btrfs_item *item;
3823 struct btrfs_dir_item *di;
3824 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003825 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003826 struct btrfs_path *path;
3827 int ret;
3828 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003829 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003830 int slot;
3831 int advance;
3832 unsigned char d_type;
3833 int over = 0;
3834 u32 di_cur;
3835 u32 di_total;
3836 u32 di_len;
3837 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003838 char tmp_name[32];
3839 char *name_ptr;
3840 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003841
3842 /* FIXME, use a real flag for deciding about the key type */
3843 if (root->fs_info->tree_root == root)
3844 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003845
Chris Mason39544012007-12-12 14:38:19 -05003846 /* special case for "." */
3847 if (filp->f_pos == 0) {
3848 over = filldir(dirent, ".", 1,
3849 1, inode->i_ino,
3850 DT_DIR);
3851 if (over)
3852 return 0;
3853 filp->f_pos = 1;
3854 }
Chris Mason39544012007-12-12 14:38:19 -05003855 /* special case for .., just use the back ref */
3856 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003857 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003858 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003859 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003860 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003861 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003862 filp->f_pos = 2;
3863 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003864 path = btrfs_alloc_path();
3865 path->reada = 2;
3866
Chris Mason39279cc2007-06-12 06:35:45 -04003867 btrfs_set_key_type(&key, key_type);
3868 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003869 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003870
Chris Mason39279cc2007-06-12 06:35:45 -04003871 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3872 if (ret < 0)
3873 goto err;
3874 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003875
3876 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003877 leaf = path->nodes[0];
3878 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003879 slot = path->slots[0];
3880 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003881 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003882 ret = btrfs_next_leaf(root, path);
3883 if (ret)
3884 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003885 leaf = path->nodes[0];
3886 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003887 slot = path->slots[0];
3888 } else {
3889 slot++;
3890 path->slots[0]++;
3891 }
3892 }
Chris Mason3de45862008-11-17 21:02:50 -05003893
Chris Mason39279cc2007-06-12 06:35:45 -04003894 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003895 item = btrfs_item_nr(leaf, slot);
3896 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3897
3898 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003899 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003900 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003901 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003902 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003903 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003904
3905 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003906
Chris Mason39279cc2007-06-12 06:35:45 -04003907 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3908 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003909 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003910
3911 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003912 struct btrfs_key location;
3913
3914 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003915 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003916 name_ptr = tmp_name;
3917 } else {
3918 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003919 if (!name_ptr) {
3920 ret = -ENOMEM;
3921 goto err;
3922 }
Chris Mason5f39d392007-10-15 16:14:19 -04003923 }
3924 read_extent_buffer(leaf, name_ptr,
3925 (unsigned long)(di + 1), name_len);
3926
3927 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3928 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003929
3930 /* is this a reference to our own snapshot? If so
3931 * skip it
3932 */
3933 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3934 location.objectid == root->root_key.objectid) {
3935 over = 0;
3936 goto skip;
3937 }
Chris Mason5f39d392007-10-15 16:14:19 -04003938 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003939 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003940 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003941
Chris Mason3de45862008-11-17 21:02:50 -05003942skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003943 if (name_ptr != tmp_name)
3944 kfree(name_ptr);
3945
Chris Mason39279cc2007-06-12 06:35:45 -04003946 if (over)
3947 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003948 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003949 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003950 di_cur += di_len;
3951 di = (struct btrfs_dir_item *)((char *)di + di_len);
3952 }
3953 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003954
3955 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003956 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00003957 /*
3958 * 32-bit glibc will use getdents64, but then strtol -
3959 * so the last number we can serve is this.
3960 */
3961 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05003962 else
3963 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003964nopos:
3965 ret = 0;
3966err:
Chris Mason39279cc2007-06-12 06:35:45 -04003967 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003968 return ret;
3969}
3970
3971int btrfs_write_inode(struct inode *inode, int wait)
3972{
3973 struct btrfs_root *root = BTRFS_I(inode)->root;
3974 struct btrfs_trans_handle *trans;
3975 int ret = 0;
3976
Yan Zhengc146afa2008-11-12 14:34:12 -05003977 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003978 return 0;
3979
Chris Mason39279cc2007-06-12 06:35:45 -04003980 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003981 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003982 btrfs_set_trans_block_group(trans, inode);
3983 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003984 }
3985 return ret;
3986}
3987
3988/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003989 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003990 * inode changes. But, it is most likely to find the inode in cache.
3991 * FIXME, needs more benchmarking...there are no reasons other than performance
3992 * to keep or drop this code.
3993 */
3994void btrfs_dirty_inode(struct inode *inode)
3995{
3996 struct btrfs_root *root = BTRFS_I(inode)->root;
3997 struct btrfs_trans_handle *trans;
3998
Chris Masonf9295742008-07-17 12:54:14 -04003999 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004000 btrfs_set_trans_block_group(trans, inode);
4001 btrfs_update_inode(trans, root, inode);
4002 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004003}
4004
Chris Masond352ac62008-09-29 15:18:18 -04004005/*
4006 * find the highest existing sequence number in a directory
4007 * and then set the in-memory index_cnt variable to reflect
4008 * free sequence numbers
4009 */
Josef Bacikaec74772008-07-24 12:12:38 -04004010static int btrfs_set_inode_index_count(struct inode *inode)
4011{
4012 struct btrfs_root *root = BTRFS_I(inode)->root;
4013 struct btrfs_key key, found_key;
4014 struct btrfs_path *path;
4015 struct extent_buffer *leaf;
4016 int ret;
4017
4018 key.objectid = inode->i_ino;
4019 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4020 key.offset = (u64)-1;
4021
4022 path = btrfs_alloc_path();
4023 if (!path)
4024 return -ENOMEM;
4025
4026 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4027 if (ret < 0)
4028 goto out;
4029 /* FIXME: we should be able to handle this */
4030 if (ret == 0)
4031 goto out;
4032 ret = 0;
4033
4034 /*
4035 * MAGIC NUMBER EXPLANATION:
4036 * since we search a directory based on f_pos we have to start at 2
4037 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4038 * else has to start at 2
4039 */
4040 if (path->slots[0] == 0) {
4041 BTRFS_I(inode)->index_cnt = 2;
4042 goto out;
4043 }
4044
4045 path->slots[0]--;
4046
4047 leaf = path->nodes[0];
4048 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4049
4050 if (found_key.objectid != inode->i_ino ||
4051 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4052 BTRFS_I(inode)->index_cnt = 2;
4053 goto out;
4054 }
4055
4056 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4057out:
4058 btrfs_free_path(path);
4059 return ret;
4060}
4061
Chris Masond352ac62008-09-29 15:18:18 -04004062/*
4063 * helper to find a free sequence number in a given directory. This current
4064 * code is very simple, later versions will do smarter things in the btree
4065 */
Chris Mason3de45862008-11-17 21:02:50 -05004066int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004067{
4068 int ret = 0;
4069
4070 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4071 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004072 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004073 return ret;
4074 }
4075
Chris Mason00e4e6b2008-08-05 11:18:09 -04004076 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004077 BTRFS_I(dir)->index_cnt++;
4078
4079 return ret;
4080}
4081
Chris Mason39279cc2007-06-12 06:35:45 -04004082static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4083 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004084 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004085 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004086 u64 ref_objectid, u64 objectid,
4087 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004088{
4089 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004090 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004091 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004092 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004093 struct btrfs_inode_ref *ref;
4094 struct btrfs_key key[2];
4095 u32 sizes[2];
4096 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004097 int ret;
4098 int owner;
4099
Chris Mason5f39d392007-10-15 16:14:19 -04004100 path = btrfs_alloc_path();
4101 BUG_ON(!path);
4102
Chris Mason39279cc2007-06-12 06:35:45 -04004103 inode = new_inode(root->fs_info->sb);
4104 if (!inode)
4105 return ERR_PTR(-ENOMEM);
4106
Josef Bacikaec74772008-07-24 12:12:38 -04004107 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004108 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004109 if (ret) {
4110 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004111 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004112 }
Josef Bacikaec74772008-07-24 12:12:38 -04004113 }
4114 /*
4115 * index_cnt is ignored for everything but a dir,
4116 * btrfs_get_inode_index_count has an explanation for the magic
4117 * number
4118 */
Chris Masone02119d2008-09-05 16:13:11 -04004119 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004120 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004121 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004122 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05004123 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004124
Chris Mason39279cc2007-06-12 06:35:45 -04004125 if (mode & S_IFDIR)
4126 owner = 0;
4127 else
4128 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004129 BTRFS_I(inode)->block_group =
4130 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004131
4132 key[0].objectid = objectid;
4133 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4134 key[0].offset = 0;
4135
4136 key[1].objectid = objectid;
4137 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4138 key[1].offset = ref_objectid;
4139
4140 sizes[0] = sizeof(struct btrfs_inode_item);
4141 sizes[1] = name_len + sizeof(*ref);
4142
Chris Masonb9473432009-03-13 11:00:37 -04004143 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004144 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4145 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004146 goto fail;
4147
Chris Mason79683f22008-11-19 22:00:53 -05004148 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05004149
Chris Mason42f15d72009-02-06 11:35:57 -05004150 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05004151 inode->i_gid = dir->i_gid;
4152 if (S_ISDIR(mode))
4153 mode |= S_ISGID;
4154 } else
4155 inode->i_gid = current_fsgid();
4156
Chris Mason39279cc2007-06-12 06:35:45 -04004157 inode->i_mode = mode;
4158 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004159 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004160 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004161 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4162 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004163 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004164
4165 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4166 struct btrfs_inode_ref);
4167 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004168 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004169 ptr = (unsigned long)(ref + 1);
4170 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4171
Chris Mason5f39d392007-10-15 16:14:19 -04004172 btrfs_mark_buffer_dirty(path->nodes[0]);
4173 btrfs_free_path(path);
4174
Chris Mason39279cc2007-06-12 06:35:45 -04004175 location = &BTRFS_I(inode)->location;
4176 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004177 location->offset = 0;
4178 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4179
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004180 btrfs_inherit_iflags(inode, dir);
4181
Chris Mason94272162009-07-02 12:26:06 -04004182 if ((mode & S_IFREG)) {
4183 if (btrfs_test_opt(root, NODATASUM))
4184 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4185 if (btrfs_test_opt(root, NODATACOW))
4186 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4187 }
4188
Chris Mason39279cc2007-06-12 06:35:45 -04004189 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004190 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004191 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004192fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004193 if (dir)
4194 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004195 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004196 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004197 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004198}
4199
4200static inline u8 btrfs_inode_type(struct inode *inode)
4201{
4202 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4203}
4204
Chris Masond352ac62008-09-29 15:18:18 -04004205/*
4206 * utility function to add 'inode' into 'parent_inode' with
4207 * a give name and a given sequence number.
4208 * if 'add_backref' is true, also insert a backref from the
4209 * inode to the parent directory.
4210 */
Chris Masone02119d2008-09-05 16:13:11 -04004211int btrfs_add_link(struct btrfs_trans_handle *trans,
4212 struct inode *parent_inode, struct inode *inode,
4213 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004214{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004215 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004216 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004217 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004218
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004219 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4220 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4221 } else {
4222 key.objectid = inode->i_ino;
4223 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4224 key.offset = 0;
4225 }
Chris Mason39279cc2007-06-12 06:35:45 -04004226
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004227 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4228 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4229 key.objectid, root->root_key.objectid,
4230 parent_inode->i_ino,
4231 index, name, name_len);
4232 } else if (add_backref) {
4233 ret = btrfs_insert_inode_ref(trans, root,
4234 name, name_len, inode->i_ino,
4235 parent_inode->i_ino, index);
4236 }
4237
Chris Mason39279cc2007-06-12 06:35:45 -04004238 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004239 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4240 parent_inode->i_ino, &key,
4241 btrfs_inode_type(inode), index);
4242 BUG_ON(ret);
4243
Chris Masondbe674a2008-07-17 12:54:05 -04004244 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004245 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004246 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004247 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004248 }
4249 return ret;
4250}
4251
4252static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004253 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004254 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004255{
Chris Masone02119d2008-09-05 16:13:11 -04004256 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4257 inode, dentry->d_name.name,
4258 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004259 if (!err) {
4260 d_instantiate(dentry, inode);
4261 return 0;
4262 }
4263 if (err > 0)
4264 err = -EEXIST;
4265 return err;
4266}
4267
Josef Bacik618e21d2007-07-11 10:18:17 -04004268static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4269 int mode, dev_t rdev)
4270{
4271 struct btrfs_trans_handle *trans;
4272 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004273 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004274 int err;
4275 int drop_inode = 0;
4276 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004277 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004278 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004279
4280 if (!new_valid_dev(rdev))
4281 return -EINVAL;
4282
Josef Bacik9ed74f22009-09-11 16:12:44 -04004283 /*
4284 * 2 for inode item and ref
4285 * 2 for dir items
4286 * 1 for xattr if selinux is on
4287 */
4288 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004289 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004290 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004291
Josef Bacik618e21d2007-07-11 10:18:17 -04004292 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004293 if (!trans)
4294 goto fail;
Josef Bacik618e21d2007-07-11 10:18:17 -04004295 btrfs_set_trans_block_group(trans, dir);
4296
4297 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4298 if (err) {
4299 err = -ENOSPC;
4300 goto out_unlock;
4301 }
4302
Josef Bacikaec74772008-07-24 12:12:38 -04004303 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004304 dentry->d_name.len,
4305 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004306 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004307 err = PTR_ERR(inode);
4308 if (IS_ERR(inode))
4309 goto out_unlock;
4310
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004311 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004312 if (err) {
4313 drop_inode = 1;
4314 goto out_unlock;
4315 }
4316
Josef Bacik618e21d2007-07-11 10:18:17 -04004317 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004318 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004319 if (err)
4320 drop_inode = 1;
4321 else {
4322 inode->i_op = &btrfs_special_inode_operations;
4323 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004324 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004325 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004326 btrfs_update_inode_block_group(trans, inode);
4327 btrfs_update_inode_block_group(trans, dir);
4328out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004329 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004330 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004331fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004332 btrfs_unreserve_metadata_space(root, 5);
Josef Bacik618e21d2007-07-11 10:18:17 -04004333 if (drop_inode) {
4334 inode_dec_link_count(inode);
4335 iput(inode);
4336 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004337 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004338 return err;
4339}
4340
Chris Mason39279cc2007-06-12 06:35:45 -04004341static int btrfs_create(struct inode *dir, struct dentry *dentry,
4342 int mode, struct nameidata *nd)
4343{
4344 struct btrfs_trans_handle *trans;
4345 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004346 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004347 int err;
4348 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05004349 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004350 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004351 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004352
Josef Bacik9ed74f22009-09-11 16:12:44 -04004353 /*
4354 * 2 for inode item and ref
4355 * 2 for dir items
4356 * 1 for xattr if selinux is on
4357 */
4358 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004359 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004360 return err;
4361
Chris Mason39279cc2007-06-12 06:35:45 -04004362 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004363 if (!trans)
4364 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04004365 btrfs_set_trans_block_group(trans, dir);
4366
4367 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4368 if (err) {
4369 err = -ENOSPC;
4370 goto out_unlock;
4371 }
4372
Josef Bacikaec74772008-07-24 12:12:38 -04004373 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004374 dentry->d_name.len,
4375 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004376 objectid, BTRFS_I(dir)->block_group, mode,
4377 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004378 err = PTR_ERR(inode);
4379 if (IS_ERR(inode))
4380 goto out_unlock;
4381
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004382 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004383 if (err) {
4384 drop_inode = 1;
4385 goto out_unlock;
4386 }
4387
Chris Mason39279cc2007-06-12 06:35:45 -04004388 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004389 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004390 if (err)
4391 drop_inode = 1;
4392 else {
4393 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004394 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004395 inode->i_fop = &btrfs_file_operations;
4396 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004397 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004398 }
Chris Mason39279cc2007-06-12 06:35:45 -04004399 btrfs_update_inode_block_group(trans, inode);
4400 btrfs_update_inode_block_group(trans, dir);
4401out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004402 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004403 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004404fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004405 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004406 if (drop_inode) {
4407 inode_dec_link_count(inode);
4408 iput(inode);
4409 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004410 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004411 return err;
4412}
4413
4414static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4415 struct dentry *dentry)
4416{
4417 struct btrfs_trans_handle *trans;
4418 struct btrfs_root *root = BTRFS_I(dir)->root;
4419 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004420 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004421 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004422 int err;
4423 int drop_inode = 0;
4424
4425 if (inode->i_nlink == 0)
4426 return -ENOENT;
4427
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004428 /* do not allow sys_link's with other subvols of the same device */
4429 if (root->objectid != BTRFS_I(inode)->root->objectid)
4430 return -EPERM;
4431
Josef Bacik9ed74f22009-09-11 16:12:44 -04004432 /*
4433 * 1 item for inode ref
4434 * 2 items for dir items
4435 */
4436 err = btrfs_reserve_metadata_space(root, 3);
Chris Mason1832a6d2007-12-21 16:27:21 -05004437 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004438 return err;
4439
4440 btrfs_inc_nlink(inode);
4441
Chris Mason3de45862008-11-17 21:02:50 -05004442 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004443 if (err)
4444 goto fail;
4445
Chris Mason39279cc2007-06-12 06:35:45 -04004446 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004447
Chris Mason39279cc2007-06-12 06:35:45 -04004448 btrfs_set_trans_block_group(trans, dir);
4449 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004450
Chris Mason00e4e6b2008-08-05 11:18:09 -04004451 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004452
Yan, Zhenga5719522009-09-24 09:17:31 -04004453 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004454 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004455 } else {
4456 btrfs_update_inode_block_group(trans, dir);
4457 err = btrfs_update_inode(trans, root, inode);
4458 BUG_ON(err);
4459 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4460 }
Chris Mason39279cc2007-06-12 06:35:45 -04004461
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004462 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004463 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004464fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004465 btrfs_unreserve_metadata_space(root, 3);
Chris Mason39279cc2007-06-12 06:35:45 -04004466 if (drop_inode) {
4467 inode_dec_link_count(inode);
4468 iput(inode);
4469 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004470 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004471 return err;
4472}
4473
Chris Mason39279cc2007-06-12 06:35:45 -04004474static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4475{
Chris Masonb9d86662008-05-02 16:13:49 -04004476 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004477 struct btrfs_trans_handle *trans;
4478 struct btrfs_root *root = BTRFS_I(dir)->root;
4479 int err = 0;
4480 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004481 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004482 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004483 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004484
Josef Bacik9ed74f22009-09-11 16:12:44 -04004485 /*
4486 * 2 items for inode and ref
4487 * 2 items for dir items
4488 * 1 for xattr if selinux is on
4489 */
4490 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004491 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004492 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004493
Chris Mason39279cc2007-06-12 06:35:45 -04004494 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004495 if (!trans) {
4496 err = -ENOMEM;
Chris Mason39279cc2007-06-12 06:35:45 -04004497 goto out_unlock;
4498 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004499 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004500
4501 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4502 if (err) {
4503 err = -ENOSPC;
4504 goto out_unlock;
4505 }
4506
Josef Bacikaec74772008-07-24 12:12:38 -04004507 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004508 dentry->d_name.len,
4509 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004510 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4511 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004512 if (IS_ERR(inode)) {
4513 err = PTR_ERR(inode);
4514 goto out_fail;
4515 }
Chris Mason5f39d392007-10-15 16:14:19 -04004516
Chris Mason39279cc2007-06-12 06:35:45 -04004517 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004518
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004519 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004520 if (err)
4521 goto out_fail;
4522
Chris Mason39279cc2007-06-12 06:35:45 -04004523 inode->i_op = &btrfs_dir_inode_operations;
4524 inode->i_fop = &btrfs_dir_file_operations;
4525 btrfs_set_trans_block_group(trans, inode);
4526
Chris Masondbe674a2008-07-17 12:54:05 -04004527 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004528 err = btrfs_update_inode(trans, root, inode);
4529 if (err)
4530 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004531
Chris Masone02119d2008-09-05 16:13:11 -04004532 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4533 inode, dentry->d_name.name,
4534 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004535 if (err)
4536 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004537
Chris Mason39279cc2007-06-12 06:35:45 -04004538 d_instantiate(dentry, inode);
4539 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004540 btrfs_update_inode_block_group(trans, inode);
4541 btrfs_update_inode_block_group(trans, dir);
4542
4543out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004544 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004545 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04004546
Chris Mason39279cc2007-06-12 06:35:45 -04004547out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004548 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004549 if (drop_on_err)
4550 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004551 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004552 return err;
4553}
4554
Chris Masond352ac62008-09-29 15:18:18 -04004555/* helper for btfs_get_extent. Given an existing extent in the tree,
4556 * and an extent that you want to insert, deal with overlap and insert
4557 * the new extent into the tree.
4558 */
Chris Mason3b951512008-04-17 11:29:12 -04004559static int merge_extent_mapping(struct extent_map_tree *em_tree,
4560 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004561 struct extent_map *em,
4562 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004563{
4564 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004565
Chris Masone6dcd2d2008-07-17 12:53:50 -04004566 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4567 start_diff = map_start - em->start;
4568 em->start = map_start;
4569 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004570 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4571 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004572 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004573 em->block_len -= start_diff;
4574 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004575 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004576}
4577
Chris Masonc8b97812008-10-29 14:49:59 -04004578static noinline int uncompress_inline(struct btrfs_path *path,
4579 struct inode *inode, struct page *page,
4580 size_t pg_offset, u64 extent_offset,
4581 struct btrfs_file_extent_item *item)
4582{
4583 int ret;
4584 struct extent_buffer *leaf = path->nodes[0];
4585 char *tmp;
4586 size_t max_size;
4587 unsigned long inline_size;
4588 unsigned long ptr;
4589
4590 WARN_ON(pg_offset != 0);
4591 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4592 inline_size = btrfs_file_extent_inline_item_len(leaf,
4593 btrfs_item_nr(leaf, path->slots[0]));
4594 tmp = kmalloc(inline_size, GFP_NOFS);
4595 ptr = btrfs_file_extent_inline_start(item);
4596
4597 read_extent_buffer(leaf, tmp, ptr, inline_size);
4598
Chris Mason5b050f02008-11-11 09:34:41 -05004599 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004600 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4601 inline_size, max_size);
4602 if (ret) {
4603 char *kaddr = kmap_atomic(page, KM_USER0);
4604 unsigned long copy_size = min_t(u64,
4605 PAGE_CACHE_SIZE - pg_offset,
4606 max_size - extent_offset);
4607 memset(kaddr + pg_offset, 0, copy_size);
4608 kunmap_atomic(kaddr, KM_USER0);
4609 }
4610 kfree(tmp);
4611 return 0;
4612}
4613
Chris Masond352ac62008-09-29 15:18:18 -04004614/*
4615 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004616 * the ugly parts come from merging extents from the disk with the in-ram
4617 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004618 * where the in-ram extents might be locked pending data=ordered completion.
4619 *
4620 * This also copies inline extents directly into the page.
4621 */
Chris Masond3977122009-01-05 21:25:51 -05004622
Chris Masona52d9a82007-08-27 16:49:44 -04004623struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004624 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004625 int create)
4626{
4627 int ret;
4628 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004629 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004630 u64 extent_start = 0;
4631 u64 extent_end = 0;
4632 u64 objectid = inode->i_ino;
4633 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004634 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004635 struct btrfs_root *root = BTRFS_I(inode)->root;
4636 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004637 struct extent_buffer *leaf;
4638 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004639 struct extent_map *em = NULL;
4640 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004641 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004642 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004643 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004644
Chris Masona52d9a82007-08-27 16:49:44 -04004645again:
Chris Mason890871b2009-09-02 16:24:52 -04004646 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004647 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004648 if (em)
4649 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004650 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004651
Chris Masona52d9a82007-08-27 16:49:44 -04004652 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004653 if (em->start > start || em->start + em->len <= start)
4654 free_extent_map(em);
4655 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004656 free_extent_map(em);
4657 else
4658 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004659 }
Chris Masond1310b22008-01-24 16:13:08 -05004660 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004661 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004662 err = -ENOMEM;
4663 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004664 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004665 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004666 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004667 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004668 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004669 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004670
4671 if (!path) {
4672 path = btrfs_alloc_path();
4673 BUG_ON(!path);
4674 }
4675
Chris Mason179e29e2007-11-01 11:28:41 -04004676 ret = btrfs_lookup_file_extent(trans, root, path,
4677 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004678 if (ret < 0) {
4679 err = ret;
4680 goto out;
4681 }
4682
4683 if (ret != 0) {
4684 if (path->slots[0] == 0)
4685 goto not_found;
4686 path->slots[0]--;
4687 }
4688
Chris Mason5f39d392007-10-15 16:14:19 -04004689 leaf = path->nodes[0];
4690 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004691 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004692 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004693 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4694 found_type = btrfs_key_type(&found_key);
4695 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004696 found_type != BTRFS_EXTENT_DATA_KEY) {
4697 goto not_found;
4698 }
4699
Chris Mason5f39d392007-10-15 16:14:19 -04004700 found_type = btrfs_file_extent_type(leaf, item);
4701 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004702 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004703 if (found_type == BTRFS_FILE_EXTENT_REG ||
4704 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004705 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004706 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004707 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4708 size_t size;
4709 size = btrfs_file_extent_inline_len(leaf, item);
4710 extent_end = (extent_start + size + root->sectorsize - 1) &
4711 ~((u64)root->sectorsize - 1);
4712 }
4713
4714 if (start >= extent_end) {
4715 path->slots[0]++;
4716 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4717 ret = btrfs_next_leaf(root, path);
4718 if (ret < 0) {
4719 err = ret;
4720 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004721 }
Yan Zheng9036c102008-10-30 14:19:41 -04004722 if (ret > 0)
4723 goto not_found;
4724 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004725 }
Yan Zheng9036c102008-10-30 14:19:41 -04004726 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4727 if (found_key.objectid != objectid ||
4728 found_key.type != BTRFS_EXTENT_DATA_KEY)
4729 goto not_found;
4730 if (start + len <= found_key.offset)
4731 goto not_found;
4732 em->start = start;
4733 em->len = found_key.offset - start;
4734 goto not_found_em;
4735 }
4736
Yan Zhengd899e052008-10-30 14:25:28 -04004737 if (found_type == BTRFS_FILE_EXTENT_REG ||
4738 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004739 em->start = extent_start;
4740 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004741 em->orig_start = extent_start -
4742 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004743 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4744 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004745 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004746 goto insert;
4747 }
Chris Masonc8b97812008-10-29 14:49:59 -04004748 if (compressed) {
4749 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4750 em->block_start = bytenr;
4751 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4752 item);
4753 } else {
4754 bytenr += btrfs_file_extent_offset(leaf, item);
4755 em->block_start = bytenr;
4756 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004757 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4758 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004759 }
Chris Masona52d9a82007-08-27 16:49:44 -04004760 goto insert;
4761 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004762 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004763 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004764 size_t size;
4765 size_t extent_offset;
4766 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004767
Chris Masona52d9a82007-08-27 16:49:44 -04004768 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004769 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004770 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004771 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004772 goto out;
4773 }
4774
Yan Zheng9036c102008-10-30 14:19:41 -04004775 size = btrfs_file_extent_inline_len(leaf, item);
4776 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004777 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004778 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004779 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004780 em->len = (copy_size + root->sectorsize - 1) &
4781 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004782 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004783 if (compressed)
4784 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004785 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004786 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004787 if (btrfs_file_extent_compression(leaf, item) ==
4788 BTRFS_COMPRESS_ZLIB) {
4789 ret = uncompress_inline(path, inode, page,
4790 pg_offset,
4791 extent_offset, item);
4792 BUG_ON(ret);
4793 } else {
4794 map = kmap(page);
4795 read_extent_buffer(leaf, map + pg_offset, ptr,
4796 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04004797 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4798 memset(map + pg_offset + copy_size, 0,
4799 PAGE_CACHE_SIZE - pg_offset -
4800 copy_size);
4801 }
Chris Masonc8b97812008-10-29 14:49:59 -04004802 kunmap(page);
4803 }
Chris Mason179e29e2007-11-01 11:28:41 -04004804 flush_dcache_page(page);
4805 } else if (create && PageUptodate(page)) {
4806 if (!trans) {
4807 kunmap(page);
4808 free_extent_map(em);
4809 em = NULL;
4810 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004811 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004812 goto again;
4813 }
Chris Masonc8b97812008-10-29 14:49:59 -04004814 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004815 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004816 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004817 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004818 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004819 }
Chris Masond1310b22008-01-24 16:13:08 -05004820 set_extent_uptodate(io_tree, em->start,
4821 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004822 goto insert;
4823 } else {
Chris Masond3977122009-01-05 21:25:51 -05004824 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004825 WARN_ON(1);
4826 }
4827not_found:
4828 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004829 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004830not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004831 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004832 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004833insert:
4834 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004835 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004836 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4837 "[%llu %llu]\n", (unsigned long long)em->start,
4838 (unsigned long long)em->len,
4839 (unsigned long long)start,
4840 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004841 err = -EIO;
4842 goto out;
4843 }
Chris Masond1310b22008-01-24 16:13:08 -05004844
4845 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04004846 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004847 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004848 /* it is possible that someone inserted the extent into the tree
4849 * while we had the lock dropped. It is also possible that
4850 * an overlapping map exists in the tree
4851 */
Chris Masona52d9a82007-08-27 16:49:44 -04004852 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004853 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004854
4855 ret = 0;
4856
Chris Mason3b951512008-04-17 11:29:12 -04004857 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004858 if (existing && (existing->start > start ||
4859 existing->start + existing->len <= start)) {
4860 free_extent_map(existing);
4861 existing = NULL;
4862 }
Chris Mason3b951512008-04-17 11:29:12 -04004863 if (!existing) {
4864 existing = lookup_extent_mapping(em_tree, em->start,
4865 em->len);
4866 if (existing) {
4867 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004868 em, start,
4869 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004870 free_extent_map(existing);
4871 if (err) {
4872 free_extent_map(em);
4873 em = NULL;
4874 }
4875 } else {
4876 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004877 free_extent_map(em);
4878 em = NULL;
4879 }
4880 } else {
4881 free_extent_map(em);
4882 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004883 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004884 }
Chris Masona52d9a82007-08-27 16:49:44 -04004885 }
Chris Mason890871b2009-09-02 16:24:52 -04004886 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004887out:
Chris Masonf4219502008-07-22 11:18:09 -04004888 if (path)
4889 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004890 if (trans) {
4891 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004892 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004893 err = ret;
4894 }
Chris Masona52d9a82007-08-27 16:49:44 -04004895 if (err) {
4896 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04004897 return ERR_PTR(err);
4898 }
4899 return em;
4900}
4901
Chris Mason16432982008-04-10 10:23:21 -04004902static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4903 const struct iovec *iov, loff_t offset,
4904 unsigned long nr_segs)
4905{
Chris Masone1c4b742008-04-22 13:26:46 -04004906 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004907}
4908
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004909static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4910 __u64 start, __u64 len)
4911{
4912 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4913}
4914
Chris Mason9ebefb182007-06-15 13:50:00 -04004915int btrfs_readpage(struct file *file, struct page *page)
4916{
Chris Masond1310b22008-01-24 16:13:08 -05004917 struct extent_io_tree *tree;
4918 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004919 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004920}
Chris Mason1832a6d2007-12-21 16:27:21 -05004921
Chris Mason39279cc2007-06-12 06:35:45 -04004922static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4923{
Chris Masond1310b22008-01-24 16:13:08 -05004924 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004925
4926
4927 if (current->flags & PF_MEMALLOC) {
4928 redirty_page_for_writepage(wbc, page);
4929 unlock_page(page);
4930 return 0;
4931 }
Chris Masond1310b22008-01-24 16:13:08 -05004932 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004933 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4934}
Chris Mason39279cc2007-06-12 06:35:45 -04004935
Chris Masonf4219502008-07-22 11:18:09 -04004936int btrfs_writepages(struct address_space *mapping,
4937 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004938{
Chris Masond1310b22008-01-24 16:13:08 -05004939 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004940
Chris Masond1310b22008-01-24 16:13:08 -05004941 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004942 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4943}
4944
Chris Mason3ab2fb52007-11-08 10:59:22 -05004945static int
4946btrfs_readpages(struct file *file, struct address_space *mapping,
4947 struct list_head *pages, unsigned nr_pages)
4948{
Chris Masond1310b22008-01-24 16:13:08 -05004949 struct extent_io_tree *tree;
4950 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004951 return extent_readpages(tree, mapping, pages, nr_pages,
4952 btrfs_get_extent);
4953}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004954static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004955{
Chris Masond1310b22008-01-24 16:13:08 -05004956 struct extent_io_tree *tree;
4957 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004958 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004959
Chris Masond1310b22008-01-24 16:13:08 -05004960 tree = &BTRFS_I(page->mapping->host)->io_tree;
4961 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004962 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004963 if (ret == 1) {
4964 ClearPagePrivate(page);
4965 set_page_private(page, 0);
4966 page_cache_release(page);
4967 }
4968 return ret;
4969}
Chris Mason39279cc2007-06-12 06:35:45 -04004970
Chris Masone6dcd2d2008-07-17 12:53:50 -04004971static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4972{
Chris Mason98509cf2008-09-11 15:51:43 -04004973 if (PageWriteback(page) || PageDirty(page))
4974 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05004975 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004976}
4977
Chris Masona52d9a82007-08-27 16:49:44 -04004978static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4979{
Chris Masond1310b22008-01-24 16:13:08 -05004980 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004981 struct btrfs_ordered_extent *ordered;
4982 u64 page_start = page_offset(page);
4983 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004984
Chris Mason8b62b722009-09-02 16:53:46 -04004985
4986 /*
4987 * we have the page locked, so new writeback can't start,
4988 * and the dirty bit won't be cleared while we are here.
4989 *
4990 * Wait for IO on this page so that we can safely clear
4991 * the PagePrivate2 bit and do ordered accounting
4992 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004993 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04004994
Chris Masond1310b22008-01-24 16:13:08 -05004995 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004996 if (offset) {
4997 btrfs_releasepage(page, GFP_NOFS);
4998 return;
4999 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005000 lock_extent(tree, page_start, page_end, GFP_NOFS);
5001 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
5002 page_offset(page));
5003 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04005004 /*
5005 * IO on this page will never be started, so we need
5006 * to account for any ordered extents now
5007 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005008 clear_extent_bit(tree, page_start, page_end,
5009 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04005010 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
5011 NULL, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04005012 /*
5013 * whoever cleared the private bit is responsible
5014 * for the finish_ordered_io
5015 */
5016 if (TestClearPagePrivate2(page)) {
5017 btrfs_finish_ordered_io(page->mapping->host,
5018 page_start, page_end);
5019 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005020 btrfs_put_ordered_extent(ordered);
5021 lock_extent(tree, page_start, page_end, GFP_NOFS);
5022 }
5023 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005024 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
5025 EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005026 __btrfs_releasepage(page, GFP_NOFS);
5027
Chris Mason4a096752008-07-21 10:29:44 -04005028 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005029 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005030 ClearPagePrivate(page);
5031 set_page_private(page, 0);
5032 page_cache_release(page);
5033 }
Chris Mason39279cc2007-06-12 06:35:45 -04005034}
5035
Chris Mason9ebefb182007-06-15 13:50:00 -04005036/*
5037 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
5038 * called from a page fault handler when a page is first dirtied. Hence we must
5039 * be careful to check for EOF conditions here. We set the page up correctly
5040 * for a written page which means we get ENOSPC checking when writing into
5041 * holes and correct delalloc and unwritten extent mapping on filesystems that
5042 * support these features.
5043 *
5044 * We are not allowed to take the i_mutex here so we have to play games to
5045 * protect against truncate races as the page could now be beyond EOF. Because
5046 * vmtruncate() writes the inode size before removing pages, once we have the
5047 * page lock we can determine safely if the page is beyond EOF. If it is not
5048 * beyond EOF, then the page is guaranteed safe against truncation until we
5049 * unlock the page.
5050 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07005051int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04005052{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005053 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05005054 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05005055 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005056 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5057 struct btrfs_ordered_extent *ordered;
5058 char *kaddr;
5059 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04005060 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05005061 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04005062 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005063 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04005064
Josef Bacik6a632092009-02-20 11:00:09 -05005065 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07005066 if (ret) {
5067 if (ret == -ENOMEM)
5068 ret = VM_FAULT_OOM;
5069 else /* -ENOSPC, -EIO, etc */
5070 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05005071 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07005072 }
Chris Mason1832a6d2007-12-21 16:27:21 -05005073
Josef Bacik9ed74f22009-09-11 16:12:44 -04005074 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
5075 if (ret) {
5076 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5077 ret = VM_FAULT_SIGBUS;
5078 goto out;
5079 }
5080
Nick Piggin56a76f82009-03-31 15:23:23 -07005081 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005082again:
Chris Mason9ebefb182007-06-15 13:50:00 -04005083 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04005084 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005085 page_start = page_offset(page);
5086 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005087
Chris Mason9ebefb182007-06-15 13:50:00 -04005088 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04005089 (page_start >= size)) {
Josef Bacik6a632092009-02-20 11:00:09 -05005090 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason9ebefb182007-06-15 13:50:00 -04005091 /* page got truncated out from underneath us */
5092 goto out_unlock;
5093 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005094 wait_on_page_writeback(page);
5095
5096 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
5097 set_page_extent_mapped(page);
5098
Chris Masoneb84ae02008-07-17 13:53:27 -04005099 /*
5100 * we can't set the delalloc bits if there are pending ordered
5101 * extents. Drop our locks and wait for them to finish
5102 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005103 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5104 if (ordered) {
5105 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5106 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005107 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005108 btrfs_put_ordered_extent(ordered);
5109 goto again;
5110 }
5111
Josef Bacikfbf19082009-10-01 17:10:23 -04005112 /*
5113 * XXX - page_mkwrite gets called every time the page is dirtied, even
5114 * if it was already dirty, so for space accounting reasons we need to
5115 * clear any delalloc bits for the range we are fixing to save. There
5116 * is probably a better way to do this, but for now keep consistent with
5117 * prepare_pages in the normal write path.
5118 */
5119 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005120 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
5121 GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04005122
Josef Bacik9ed74f22009-09-11 16:12:44 -04005123 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
5124 if (ret) {
5125 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5126 ret = VM_FAULT_SIGBUS;
Josef Bacikfbf19082009-10-01 17:10:23 -04005127 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005128 goto out_unlock;
5129 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005130 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04005131
5132 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04005133 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005134 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04005135 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04005136 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04005137
Chris Masone6dcd2d2008-07-17 12:53:50 -04005138 if (zero_start != PAGE_CACHE_SIZE) {
5139 kaddr = kmap(page);
5140 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5141 flush_dcache_page(page);
5142 kunmap(page);
5143 }
Chris Mason247e7432008-07-17 12:53:51 -04005144 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005145 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04005146 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005147
Chris Mason257c62e2009-10-13 13:21:08 -04005148 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5149 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5150
Chris Masone6dcd2d2008-07-17 12:53:50 -04005151 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04005152
5153out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005154 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason50a9b212009-09-11 12:33:12 -04005155 if (!ret)
5156 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04005157 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05005158out:
Chris Mason9ebefb182007-06-15 13:50:00 -04005159 return ret;
5160}
5161
Chris Mason39279cc2007-06-12 06:35:45 -04005162static void btrfs_truncate(struct inode *inode)
5163{
5164 struct btrfs_root *root = BTRFS_I(inode)->root;
5165 int ret;
5166 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005167 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04005168 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005169
Yan, Zheng80825102009-11-12 09:35:36 +00005170 if (!S_ISREG(inode->i_mode)) {
5171 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04005172 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005173 }
Chris Mason39279cc2007-06-12 06:35:45 -04005174
Josef Bacik5d5e1032009-10-13 16:46:49 -04005175 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5176 if (ret)
5177 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005178
Chris Mason4a096752008-07-21 10:29:44 -04005179 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00005180 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005181
Chris Mason39279cc2007-06-12 06:35:45 -04005182 trans = btrfs_start_transaction(root, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00005183 btrfs_set_trans_block_group(trans, inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005184
5185 /*
5186 * setattr is responsible for setting the ordered_data_close flag,
5187 * but that is only tested during the last file release. That
5188 * could happen well after the next commit, leaving a great big
5189 * window where new writes may get lost if someone chooses to write
5190 * to this file after truncating to zero
5191 *
5192 * The inode doesn't have any dirty data here, and so if we commit
5193 * this is a noop. If someone immediately starts writing to the inode
5194 * it is very likely we'll catch some of their writes in this
5195 * transaction, and the commit will find this file on the ordered
5196 * data list with good things to send down.
5197 *
5198 * This is a best effort solution, there is still a window where
5199 * using truncate to replace the contents of the file will
5200 * end up with a zero length file after a crash.
5201 */
5202 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5203 btrfs_add_ordered_operation(trans, root, inode);
5204
Yan, Zheng80825102009-11-12 09:35:36 +00005205 while (1) {
5206 ret = btrfs_truncate_inode_items(trans, root, inode,
5207 inode->i_size,
5208 BTRFS_EXTENT_DATA_KEY);
5209 if (ret != -EAGAIN)
5210 break;
Chris Mason39279cc2007-06-12 06:35:45 -04005211
Yan, Zheng80825102009-11-12 09:35:36 +00005212 ret = btrfs_update_inode(trans, root, inode);
5213 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005214
Yan, Zheng80825102009-11-12 09:35:36 +00005215 nr = trans->blocks_used;
5216 btrfs_end_transaction(trans, root);
5217 btrfs_btree_balance_dirty(root, nr);
5218
5219 trans = btrfs_start_transaction(root, 1);
5220 btrfs_set_trans_block_group(trans, inode);
5221 }
5222
5223 if (ret == 0 && inode->i_nlink > 0) {
5224 ret = btrfs_orphan_del(trans, inode);
5225 BUG_ON(ret);
5226 }
5227
5228 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04005229 BUG_ON(ret);
5230
Josef Bacik7b128762008-07-24 12:17:14 -04005231 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04005232 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005233 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005234 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005235}
5236
Sven Wegener3b963622008-06-09 21:57:42 -04005237/*
Chris Masond352ac62008-09-29 15:18:18 -04005238 * create a new subvolume directory/inode (helper for the ioctl).
5239 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05005240int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04005241 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005242 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04005243{
Chris Mason39279cc2007-06-12 06:35:45 -04005244 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005245 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005246 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005247
Josef Bacikaec74772008-07-24 12:12:38 -04005248 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005249 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04005250 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005251 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005252 inode->i_op = &btrfs_dir_inode_operations;
5253 inode->i_fop = &btrfs_dir_file_operations;
5254
Chris Mason39279cc2007-06-12 06:35:45 -04005255 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04005256 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04005257
Yan, Zheng76dda932009-09-21 16:00:26 -04005258 err = btrfs_update_inode(trans, new_root, inode);
5259 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005260
Yan, Zheng76dda932009-09-21 16:00:26 -04005261 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005262 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005263}
5264
Chris Masond352ac62008-09-29 15:18:18 -04005265/* helper function for file defrag and space balancing. This
5266 * forces readahead on a given range of bytes in an inode
5267 */
Chris Masonedbd8d42007-12-21 16:27:24 -05005268unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04005269 struct file_ra_state *ra, struct file *file,
5270 pgoff_t offset, pgoff_t last_index)
5271{
Chris Mason8e7bf942008-04-28 09:02:36 -04005272 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04005273
Chris Mason86479a02007-09-10 19:58:16 -04005274 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5275 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04005276}
5277
Chris Mason39279cc2007-06-12 06:35:45 -04005278struct inode *btrfs_alloc_inode(struct super_block *sb)
5279{
5280 struct btrfs_inode *ei;
5281
5282 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5283 if (!ei)
5284 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04005285 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04005286 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005287 ei->logged_trans = 0;
Josef Bacik32c00af2009-10-08 13:34:05 -04005288 ei->outstanding_extents = 0;
5289 ei->reserved_extents = 0;
Josef Bacika6dbd422009-11-11 15:53:34 -05005290 ei->root = NULL;
Josef Bacik32c00af2009-10-08 13:34:05 -04005291 spin_lock_init(&ei->accounting_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005292 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04005293 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005294 INIT_LIST_HEAD(&ei->ordered_operations);
Chris Mason39279cc2007-06-12 06:35:45 -04005295 return &ei->vfs_inode;
5296}
5297
5298void btrfs_destroy_inode(struct inode *inode)
5299{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005300 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005301 struct btrfs_root *root = BTRFS_I(inode)->root;
5302
Chris Mason39279cc2007-06-12 06:35:45 -04005303 WARN_ON(!list_empty(&inode->i_dentry));
5304 WARN_ON(inode->i_data.nrpages);
5305
Chris Mason5a3f23d2009-03-31 13:27:11 -04005306 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05005307 * This can happen where we create an inode, but somebody else also
5308 * created the same inode and we need to destroy the one we already
5309 * created.
5310 */
5311 if (!root)
5312 goto free;
5313
5314 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005315 * Make sure we're properly removed from the ordered operation
5316 * lists.
5317 */
5318 smp_mb();
5319 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5320 spin_lock(&root->fs_info->ordered_extent_lock);
5321 list_del_init(&BTRFS_I(inode)->ordered_operations);
5322 spin_unlock(&root->fs_info->ordered_extent_lock);
5323 }
5324
5325 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005326 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00005327 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
5328 inode->i_ino);
5329 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04005330 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04005331 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005332
Chris Masond3977122009-01-05 21:25:51 -05005333 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005334 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5335 if (!ordered)
5336 break;
5337 else {
Chris Masond3977122009-01-05 21:25:51 -05005338 printk(KERN_ERR "btrfs found ordered "
5339 "extent %llu %llu on inode cleanup\n",
5340 (unsigned long long)ordered->file_offset,
5341 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005342 btrfs_remove_ordered_extent(inode, ordered);
5343 btrfs_put_ordered_extent(ordered);
5344 btrfs_put_ordered_extent(ordered);
5345 }
5346 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005347 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005348 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05005349free:
Chris Mason39279cc2007-06-12 06:35:45 -04005350 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5351}
5352
Yan, Zheng76dda932009-09-21 16:00:26 -04005353void btrfs_drop_inode(struct inode *inode)
5354{
5355 struct btrfs_root *root = BTRFS_I(inode)->root;
5356
5357 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5358 generic_delete_inode(inode);
5359 else
5360 generic_drop_inode(inode);
5361}
5362
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005363static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04005364{
5365 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5366
5367 inode_init_once(&ei->vfs_inode);
5368}
5369
5370void btrfs_destroy_cachep(void)
5371{
5372 if (btrfs_inode_cachep)
5373 kmem_cache_destroy(btrfs_inode_cachep);
5374 if (btrfs_trans_handle_cachep)
5375 kmem_cache_destroy(btrfs_trans_handle_cachep);
5376 if (btrfs_transaction_cachep)
5377 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04005378 if (btrfs_path_cachep)
5379 kmem_cache_destroy(btrfs_path_cachep);
5380}
5381
5382int btrfs_init_cachep(void)
5383{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005384 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5385 sizeof(struct btrfs_inode), 0,
5386 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04005387 if (!btrfs_inode_cachep)
5388 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005389
5390 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5391 sizeof(struct btrfs_trans_handle), 0,
5392 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005393 if (!btrfs_trans_handle_cachep)
5394 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005395
5396 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5397 sizeof(struct btrfs_transaction), 0,
5398 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005399 if (!btrfs_transaction_cachep)
5400 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005401
5402 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5403 sizeof(struct btrfs_path), 0,
5404 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005405 if (!btrfs_path_cachep)
5406 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005407
Chris Mason39279cc2007-06-12 06:35:45 -04005408 return 0;
5409fail:
5410 btrfs_destroy_cachep();
5411 return -ENOMEM;
5412}
5413
5414static int btrfs_getattr(struct vfsmount *mnt,
5415 struct dentry *dentry, struct kstat *stat)
5416{
5417 struct inode *inode = dentry->d_inode;
5418 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05005419 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05005420 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005421 stat->blocks = (inode_get_bytes(inode) +
5422 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04005423 return 0;
5424}
5425
Chris Masond3977122009-01-05 21:25:51 -05005426static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5427 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005428{
5429 struct btrfs_trans_handle *trans;
5430 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005431 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005432 struct inode *new_inode = new_dentry->d_inode;
5433 struct inode *old_inode = old_dentry->d_inode;
5434 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005435 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005436 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005437 int ret;
5438
Yan, Zhengf679a842009-09-24 09:17:31 -04005439 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5440 return -EPERM;
5441
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005442 /* we only allow rename subvolume link between subvolumes */
5443 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05005444 return -EXDEV;
5445
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005446 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5447 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5448 return -ENOTEMPTY;
5449
Chris Mason39279cc2007-06-12 06:35:45 -04005450 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005451 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04005452 return -ENOTEMPTY;
Chris Mason0660b5a2008-11-17 20:37:39 -05005453
Josef Bacik9ed74f22009-09-11 16:12:44 -04005454 /*
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005455 * We want to reserve the absolute worst case amount of items. So if
5456 * both inodes are subvols and we need to unlink them then that would
5457 * require 4 item modifications, but if they are both normal inodes it
5458 * would require 5 item modifications, so we'll assume their normal
5459 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5460 * should cover the worst case number of items we'll modify.
Josef Bacik9ed74f22009-09-11 16:12:44 -04005461 */
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005462 ret = btrfs_reserve_metadata_space(root, 11);
Chris Mason1832a6d2007-12-21 16:27:21 -05005463 if (ret)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005464 return ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05005465
Chris Mason5a3f23d2009-03-31 13:27:11 -04005466 /*
5467 * we're using rename to replace one file with another.
5468 * and the replacement file is large. Start IO on it now so
5469 * we don't add too much work to the end of the transaction
5470 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04005471 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04005472 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5473 filemap_flush(old_inode->i_mapping);
5474
Yan, Zheng76dda932009-09-21 16:00:26 -04005475 /* close the racy window with snapshot create/destroy ioctl */
5476 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5477 down_read(&root->fs_info->subvol_sem);
Chris Mason5f39d392007-10-15 16:14:19 -04005478
Chris Mason39279cc2007-06-12 06:35:45 -04005479 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga5719522009-09-24 09:17:31 -04005480 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04005481
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005482 if (dest != root)
5483 btrfs_record_root_in_trans(trans, dest);
5484
Yan, Zhenga5719522009-09-24 09:17:31 -04005485 ret = btrfs_set_inode_index(new_dir, &index);
5486 if (ret)
5487 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005488
Yan, Zhenga5719522009-09-24 09:17:31 -04005489 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005490 /* force full log commit if subvolume involved. */
5491 root->fs_info->last_trans_log_full_commit = trans->transid;
5492 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04005493 ret = btrfs_insert_inode_ref(trans, dest,
5494 new_dentry->d_name.name,
5495 new_dentry->d_name.len,
5496 old_inode->i_ino,
5497 new_dir->i_ino, index);
5498 if (ret)
5499 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005500 /*
5501 * this is an ugly little race, but the rename is required
5502 * to make sure that if we crash, the inode is either at the
5503 * old name or the new one. pinning the log transaction lets
5504 * us make sure we don't allow a log commit to come in after
5505 * we unlink the name but before we add the new name back in.
5506 */
5507 btrfs_pin_log_trans(root);
5508 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005509 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005510 * make sure the inode gets flushed if it is replacing
5511 * something.
5512 */
5513 if (new_inode && new_inode->i_size &&
5514 old_inode && S_ISREG(old_inode->i_mode)) {
5515 btrfs_add_ordered_operation(trans, root, old_inode);
5516 }
5517
Chris Mason39279cc2007-06-12 06:35:45 -04005518 old_dir->i_ctime = old_dir->i_mtime = ctime;
5519 new_dir->i_ctime = new_dir->i_mtime = ctime;
5520 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04005521
Chris Mason12fcfd22009-03-24 10:24:20 -04005522 if (old_dentry->d_parent != new_dentry->d_parent)
5523 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5524
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005525 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5526 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5527 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5528 old_dentry->d_name.name,
5529 old_dentry->d_name.len);
5530 } else {
5531 btrfs_inc_nlink(old_dentry->d_inode);
5532 ret = btrfs_unlink_inode(trans, root, old_dir,
5533 old_dentry->d_inode,
5534 old_dentry->d_name.name,
5535 old_dentry->d_name.len);
5536 }
5537 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005538
5539 if (new_inode) {
5540 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541 if (unlikely(new_inode->i_ino ==
5542 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5543 root_objectid = BTRFS_I(new_inode)->location.objectid;
5544 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5545 root_objectid,
5546 new_dentry->d_name.name,
5547 new_dentry->d_name.len);
5548 BUG_ON(new_inode->i_nlink == 0);
5549 } else {
5550 ret = btrfs_unlink_inode(trans, dest, new_dir,
5551 new_dentry->d_inode,
5552 new_dentry->d_name.name,
5553 new_dentry->d_name.len);
5554 }
5555 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005556 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04005557 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005558 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005559 }
Chris Mason39279cc2007-06-12 06:35:45 -04005560 }
Josef Bacikaec74772008-07-24 12:12:38 -04005561
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005562 ret = btrfs_add_link(trans, new_dir, old_inode,
5563 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04005564 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005565 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005566
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005567 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5568 btrfs_log_new_name(trans, old_inode, old_dir,
5569 new_dentry->d_parent);
5570 btrfs_end_log_trans(root);
5571 }
Chris Mason39279cc2007-06-12 06:35:45 -04005572out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04005573 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005574
Yan, Zheng76dda932009-09-21 16:00:26 -04005575 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5576 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005577
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005578 btrfs_unreserve_metadata_space(root, 11);
Chris Mason39279cc2007-06-12 06:35:45 -04005579 return ret;
5580}
5581
Chris Masond352ac62008-09-29 15:18:18 -04005582/*
5583 * some fairly slow code that needs optimization. This walks the list
5584 * of all the inodes with pending delalloc and forces them to disk.
5585 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005586int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04005587{
5588 struct list_head *head = &root->fs_info->delalloc_inodes;
5589 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005590 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04005591
Yan Zhengc146afa2008-11-12 14:34:12 -05005592 if (root->fs_info->sb->s_flags & MS_RDONLY)
5593 return -EROFS;
5594
Chris Mason75eff682008-12-15 15:54:40 -05005595 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05005596 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04005597 binode = list_entry(head->next, struct btrfs_inode,
5598 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005599 inode = igrab(&binode->vfs_inode);
5600 if (!inode)
5601 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05005602 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005603 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005604 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005605 if (delay_iput)
5606 btrfs_add_delayed_iput(inode);
5607 else
5608 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005609 }
5610 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05005611 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04005612 }
Chris Mason75eff682008-12-15 15:54:40 -05005613 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04005614
5615 /* the filemap_flush will queue IO into the worker threads, but
5616 * we have to make sure the IO is actually started and that
5617 * ordered extents get created before we return
5618 */
5619 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05005620 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05005621 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005622 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05005623 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5624 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04005625 }
5626 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04005627 return 0;
5628}
5629
Chris Mason39279cc2007-06-12 06:35:45 -04005630static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5631 const char *symname)
5632{
5633 struct btrfs_trans_handle *trans;
5634 struct btrfs_root *root = BTRFS_I(dir)->root;
5635 struct btrfs_path *path;
5636 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05005637 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005638 int err;
5639 int drop_inode = 0;
5640 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005641 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04005642 int name_len;
5643 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04005644 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005645 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04005646 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05005647 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005648
5649 name_len = strlen(symname) + 1;
5650 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5651 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05005652
Josef Bacik9ed74f22009-09-11 16:12:44 -04005653 /*
5654 * 2 items for inode item and ref
5655 * 2 items for dir items
5656 * 1 item for xattr if selinux is on
5657 */
5658 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05005659 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04005660 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05005661
Chris Mason39279cc2007-06-12 06:35:45 -04005662 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005663 if (!trans)
5664 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04005665 btrfs_set_trans_block_group(trans, dir);
5666
5667 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5668 if (err) {
5669 err = -ENOSPC;
5670 goto out_unlock;
5671 }
5672
Josef Bacikaec74772008-07-24 12:12:38 -04005673 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05005674 dentry->d_name.len,
5675 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04005676 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5677 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005678 err = PTR_ERR(inode);
5679 if (IS_ERR(inode))
5680 goto out_unlock;
5681
Yan, Zhengf34f57a2009-11-12 09:35:27 +00005682 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04005683 if (err) {
5684 drop_inode = 1;
5685 goto out_unlock;
5686 }
5687
Chris Mason39279cc2007-06-12 06:35:45 -04005688 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005689 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005690 if (err)
5691 drop_inode = 1;
5692 else {
5693 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005694 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04005695 inode->i_fop = &btrfs_file_operations;
5696 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05005697 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04005698 }
Chris Mason39279cc2007-06-12 06:35:45 -04005699 btrfs_update_inode_block_group(trans, inode);
5700 btrfs_update_inode_block_group(trans, dir);
5701 if (drop_inode)
5702 goto out_unlock;
5703
5704 path = btrfs_alloc_path();
5705 BUG_ON(!path);
5706 key.objectid = inode->i_ino;
5707 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005708 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5709 datasize = btrfs_file_extent_calc_inline_size(name_len);
5710 err = btrfs_insert_empty_item(trans, root, path, &key,
5711 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005712 if (err) {
5713 drop_inode = 1;
5714 goto out_unlock;
5715 }
Chris Mason5f39d392007-10-15 16:14:19 -04005716 leaf = path->nodes[0];
5717 ei = btrfs_item_ptr(leaf, path->slots[0],
5718 struct btrfs_file_extent_item);
5719 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5720 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04005721 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04005722 btrfs_set_file_extent_encryption(leaf, ei, 0);
5723 btrfs_set_file_extent_compression(leaf, ei, 0);
5724 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5725 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5726
Chris Mason39279cc2007-06-12 06:35:45 -04005727 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04005728 write_extent_buffer(leaf, symname, ptr, name_len);
5729 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04005730 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04005731
Chris Mason39279cc2007-06-12 06:35:45 -04005732 inode->i_op = &btrfs_symlink_inode_operations;
5733 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04005734 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04005735 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04005736 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005737 err = btrfs_update_inode(trans, root, inode);
5738 if (err)
5739 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005740
5741out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005742 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04005743 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005744out_fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005745 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04005746 if (drop_inode) {
5747 inode_dec_link_count(inode);
5748 iput(inode);
5749 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005750 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005751 return err;
5752}
Chris Mason16432982008-04-10 10:23:21 -04005753
Yan, Zheng5a303d52009-11-12 09:34:52 +00005754static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005755 u64 alloc_hint, int mode, loff_t actual_len)
Yan Zhengd899e052008-10-30 14:25:28 -04005756{
Yan, Zheng5a303d52009-11-12 09:34:52 +00005757 struct btrfs_trans_handle *trans;
Yan Zhengd899e052008-10-30 14:25:28 -04005758 struct btrfs_root *root = BTRFS_I(inode)->root;
5759 struct btrfs_key ins;
5760 u64 alloc_size;
5761 u64 cur_offset = start;
5762 u64 num_bytes = end - start;
5763 int ret = 0;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005764 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04005765
Yan Zhengd899e052008-10-30 14:25:28 -04005766 while (num_bytes > 0) {
5767 alloc_size = min(num_bytes, root->fs_info->max_extent);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005768
Chris Mason3a1abec2009-12-17 15:47:17 -05005769 trans = btrfs_start_transaction(root, 1);
5770
Yan Zhengd899e052008-10-30 14:25:28 -04005771 ret = btrfs_reserve_extent(trans, root, alloc_size,
5772 root->sectorsize, 0, alloc_hint,
5773 (u64)-1, &ins, 1);
5774 if (ret) {
5775 WARN_ON(1);
Chris Mason3a1abec2009-12-17 15:47:17 -05005776 goto stop_trans;
Yan Zhengd899e052008-10-30 14:25:28 -04005777 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00005778
5779 ret = btrfs_reserve_metadata_space(root, 3);
5780 if (ret) {
5781 btrfs_free_reserved_extent(root, ins.objectid,
5782 ins.offset);
Chris Mason3a1abec2009-12-17 15:47:17 -05005783 goto stop_trans;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005784 }
5785
Yan Zhengd899e052008-10-30 14:25:28 -04005786 ret = insert_reserved_file_extent(trans, inode,
5787 cur_offset, ins.objectid,
5788 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00005789 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04005790 BTRFS_FILE_EXTENT_PREALLOC);
5791 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04005792 btrfs_drop_extent_cache(inode, cur_offset,
5793 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005794
Yan Zhengd899e052008-10-30 14:25:28 -04005795 num_bytes -= ins.offset;
5796 cur_offset += ins.offset;
5797 alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005798
Yan Zhengd899e052008-10-30 14:25:28 -04005799 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005800 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04005801 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Aneesh Kumar K.V23b5c502010-02-04 11:33:03 -05005802 (actual_len > inode->i_size) &&
5803 (cur_offset > inode->i_size)) {
5804
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005805 if (cur_offset > actual_len)
5806 i_size = actual_len;
5807 else
5808 i_size = cur_offset;
5809 i_size_write(inode, i_size);
5810 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005811 }
5812
Yan Zhengd899e052008-10-30 14:25:28 -04005813 ret = btrfs_update_inode(trans, root, inode);
5814 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04005815
Yan, Zheng5a303d52009-11-12 09:34:52 +00005816 btrfs_end_transaction(trans, root);
5817 btrfs_unreserve_metadata_space(root, 3);
5818 }
Yan Zhengd899e052008-10-30 14:25:28 -04005819 return ret;
Chris Mason3a1abec2009-12-17 15:47:17 -05005820
5821stop_trans:
5822 btrfs_end_transaction(trans, root);
5823 return ret;
5824
Yan Zhengd899e052008-10-30 14:25:28 -04005825}
5826
5827static long btrfs_fallocate(struct inode *inode, int mode,
5828 loff_t offset, loff_t len)
5829{
5830 u64 cur_offset;
5831 u64 last_byte;
5832 u64 alloc_start;
5833 u64 alloc_end;
5834 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04005835 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04005836 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5837 struct extent_map *em;
5838 int ret;
5839
5840 alloc_start = offset & ~mask;
5841 alloc_end = (offset + len + mask) & ~mask;
5842
Chris Mason546888d2009-04-21 11:53:38 -04005843 /*
5844 * wait for ordered IO before we have any locks. We'll loop again
5845 * below with the locks held.
5846 */
5847 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5848
Yan Zhengd899e052008-10-30 14:25:28 -04005849 mutex_lock(&inode->i_mutex);
5850 if (alloc_start > inode->i_size) {
5851 ret = btrfs_cont_expand(inode, alloc_start);
5852 if (ret)
5853 goto out;
5854 }
5855
Yan, Zheng5a303d52009-11-12 09:34:52 +00005856 ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode,
Josef Bacika970b0a2009-06-27 21:07:34 -04005857 alloc_end - alloc_start);
5858 if (ret)
5859 goto out;
5860
Chris Masone980b502009-04-24 14:39:24 -04005861 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04005862 while (1) {
5863 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04005864
Chris Mason546888d2009-04-21 11:53:38 -04005865 /* the extent lock is ordered inside the running
5866 * transaction
5867 */
Chris Masone980b502009-04-24 14:39:24 -04005868 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5869 GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04005870 ordered = btrfs_lookup_first_ordered_extent(inode,
5871 alloc_end - 1);
5872 if (ordered &&
5873 ordered->file_offset + ordered->len > alloc_start &&
5874 ordered->file_offset < alloc_end) {
5875 btrfs_put_ordered_extent(ordered);
5876 unlock_extent(&BTRFS_I(inode)->io_tree,
Chris Masone980b502009-04-24 14:39:24 -04005877 alloc_start, locked_end, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005878 /*
5879 * we can't wait on the range with the transaction
5880 * running or with the extent lock held
5881 */
Yan Zhengd899e052008-10-30 14:25:28 -04005882 btrfs_wait_ordered_range(inode, alloc_start,
5883 alloc_end - alloc_start);
5884 } else {
5885 if (ordered)
5886 btrfs_put_ordered_extent(ordered);
5887 break;
5888 }
5889 }
5890
5891 cur_offset = alloc_start;
5892 while (1) {
5893 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5894 alloc_end - cur_offset, 0);
5895 BUG_ON(IS_ERR(em) || !em);
5896 last_byte = min(extent_map_end(em), alloc_end);
5897 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005898 if (em->block_start == EXTENT_MAP_HOLE ||
5899 (cur_offset >= inode->i_size &&
5900 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5901 ret = prealloc_file_range(inode,
5902 cur_offset, last_byte,
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005903 alloc_hint, mode, offset+len);
Yan Zhengd899e052008-10-30 14:25:28 -04005904 if (ret < 0) {
5905 free_extent_map(em);
5906 break;
5907 }
5908 }
5909 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5910 alloc_hint = em->block_start;
5911 free_extent_map(em);
5912
5913 cur_offset = last_byte;
5914 if (cur_offset >= alloc_end) {
5915 ret = 0;
5916 break;
5917 }
5918 }
Chris Masone980b502009-04-24 14:39:24 -04005919 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04005920 GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005921
Yan, Zheng5a303d52009-11-12 09:34:52 +00005922 btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode,
5923 alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04005924out:
5925 mutex_unlock(&inode->i_mutex);
5926 return ret;
5927}
5928
Chris Masone6dcd2d2008-07-17 12:53:50 -04005929static int btrfs_set_page_dirty(struct page *page)
5930{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005931 return __set_page_dirty_nobuffers(page);
5932}
5933
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005934static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05005935{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005936 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05005937 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04005938 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05005939}
Chris Mason39279cc2007-06-12 06:35:45 -04005940
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005941static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05005942 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005943 .lookup = btrfs_lookup,
5944 .create = btrfs_create,
5945 .unlink = btrfs_unlink,
5946 .link = btrfs_link,
5947 .mkdir = btrfs_mkdir,
5948 .rmdir = btrfs_rmdir,
5949 .rename = btrfs_rename,
5950 .symlink = btrfs_symlink,
5951 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005952 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005953 .setxattr = btrfs_setxattr,
5954 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005955 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005956 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005957 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005958};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005959static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005960 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05005961 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005962};
Yan, Zheng76dda932009-09-21 16:00:26 -04005963
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005964static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005965 .llseek = generic_file_llseek,
5966 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01005967 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005968 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005969#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005970 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005971#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04005972 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04005973 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04005974};
5975
Chris Masond1310b22008-01-24 16:13:08 -05005976static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04005977 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05005978 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04005979 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005980 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005981 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04005982 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04005983 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05005984 .set_bit_hook = btrfs_set_bit_hook,
5985 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005986 .merge_extent_hook = btrfs_merge_extent_hook,
5987 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005988};
5989
Chris Mason35054392009-01-21 13:11:13 -05005990/*
5991 * btrfs doesn't support the bmap operation because swapfiles
5992 * use bmap to make a mapping of extents in the file. They assume
5993 * these extents won't change over the life of the file and they
5994 * use the bmap result to do IO directly to the drive.
5995 *
5996 * the btrfs bmap call would return logical addresses that aren't
5997 * suitable for IO and they also will change frequently as COW
5998 * operations happen. So, swapfile + btrfs == corruption.
5999 *
6000 * For now we're avoiding this by dropping bmap.
6001 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006002static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006003 .readpage = btrfs_readpage,
6004 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04006005 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05006006 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04006007 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04006008 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04006009 .invalidatepage = btrfs_invalidatepage,
6010 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006011 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02006012 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04006013};
6014
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006015static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006016 .readpage = btrfs_readpage,
6017 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04006018 .invalidatepage = btrfs_invalidatepage,
6019 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04006020};
6021
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006022static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006023 .truncate = btrfs_truncate,
6024 .getattr = btrfs_getattr,
6025 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006026 .setxattr = btrfs_setxattr,
6027 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05006028 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006029 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05006030 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04006031 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006032 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04006033};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006034static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04006035 .getattr = btrfs_getattr,
6036 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05006037 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006038 .setxattr = btrfs_setxattr,
6039 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04006040 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006041 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04006042};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006043static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006044 .readlink = generic_readlink,
6045 .follow_link = page_follow_link_light,
6046 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05006047 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05006048 .setxattr = btrfs_setxattr,
6049 .getxattr = btrfs_getxattr,
6050 .listxattr = btrfs_listxattr,
6051 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04006052};
Yan, Zheng76dda932009-09-21 16:00:26 -04006053
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04006054const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04006055 .d_delete = btrfs_dentry_delete,
6056};