blob: d5aa97310943ed6a6b75339411111e7b09f414d8 [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 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200486 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Masonc8b97812008-10-29 14:49:59 -0400487 }
Chris Mason771ed682008-11-06 22:02:51 -0500488 if (will_compress) {
489 *num_added += 1;
490
491 /* the async work queues will take care of doing actual
492 * allocation on disk for these compressed pages,
493 * and will submit them to the elevator.
494 */
495 add_async_extent(async_cow, start, num_bytes,
496 total_compressed, pages, nr_pages_ret);
497
Chris Mason42dc7ba2008-12-15 11:44:56 -0500498 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500499 start += num_bytes;
500 pages = NULL;
501 cond_resched();
502 goto again;
503 }
504 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500505cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500506 /*
507 * No compression, but we still need to write the pages in
508 * the file we've been given so far. redirty the locked
509 * page if it corresponds to our extent and set things up
510 * for the async work queue to run cow_file_range to do
511 * the normal delalloc dance
512 */
513 if (page_offset(locked_page) >= start &&
514 page_offset(locked_page) <= end) {
515 __set_page_dirty_nobuffers(locked_page);
516 /* unlocked later on in the async handlers */
517 }
518 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
519 *num_added += 1;
520 }
521
522out:
523 return 0;
524
525free_pages_out:
526 for (i = 0; i < nr_pages_ret; i++) {
527 WARN_ON(pages[i]->mapping);
528 page_cache_release(pages[i]);
529 }
Chris Masond3977122009-01-05 21:25:51 -0500530 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500531
532 goto out;
533}
534
535/*
536 * phase two of compressed writeback. This is the ordered portion
537 * of the code, which only gets called in the order the work was
538 * queued. We walk all the async extents created by compress_file_range
539 * and send them down to the disk.
540 */
541static noinline int submit_compressed_extents(struct inode *inode,
542 struct async_cow *async_cow)
543{
544 struct async_extent *async_extent;
545 u64 alloc_hint = 0;
546 struct btrfs_trans_handle *trans;
547 struct btrfs_key ins;
548 struct extent_map *em;
549 struct btrfs_root *root = BTRFS_I(inode)->root;
550 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
551 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500552 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500553
554 if (list_empty(&async_cow->extents))
555 return 0;
556
Chris Mason771ed682008-11-06 22:02:51 -0500557
Chris Masond3977122009-01-05 21:25:51 -0500558 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500559 async_extent = list_entry(async_cow->extents.next,
560 struct async_extent, list);
561 list_del(&async_extent->list);
562
563 io_tree = &BTRFS_I(inode)->io_tree;
564
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500565retry:
Chris Mason771ed682008-11-06 22:02:51 -0500566 /* did the compression code fall back to uncompressed IO? */
567 if (!async_extent->pages) {
568 int page_started = 0;
569 unsigned long nr_written = 0;
570
571 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500572 async_extent->start +
573 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500574
575 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500576 ret = cow_file_range(inode, async_cow->locked_page,
577 async_extent->start,
578 async_extent->start +
579 async_extent->ram_size - 1,
580 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500581
582 /*
583 * if page_started, cow_file_range inserted an
584 * inline extent and took care of all the unlocking
585 * and IO for us. Otherwise, we need to submit
586 * all those pages down to the drive.
587 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500588 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500589 extent_write_locked_range(io_tree,
590 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500591 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500592 async_extent->ram_size - 1,
593 btrfs_get_extent,
594 WB_SYNC_ALL);
595 kfree(async_extent);
596 cond_resched();
597 continue;
598 }
599
600 lock_extent(io_tree, async_extent->start,
601 async_extent->start + async_extent->ram_size - 1,
602 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500603
Yan, Zhengc2167752009-11-12 09:34:21 +0000604 trans = btrfs_join_transaction(root, 1);
Chris Mason771ed682008-11-06 22:02:51 -0500605 ret = btrfs_reserve_extent(trans, root,
606 async_extent->compressed_size,
607 async_extent->compressed_size,
608 0, alloc_hint,
609 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000610 btrfs_end_transaction(trans, root);
611
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500612 if (ret) {
613 int i;
614 for (i = 0; i < async_extent->nr_pages; i++) {
615 WARN_ON(async_extent->pages[i]->mapping);
616 page_cache_release(async_extent->pages[i]);
617 }
618 kfree(async_extent->pages);
619 async_extent->nr_pages = 0;
620 async_extent->pages = NULL;
621 unlock_extent(io_tree, async_extent->start,
622 async_extent->start +
623 async_extent->ram_size - 1, GFP_NOFS);
624 goto retry;
625 }
626
Yan, Zhengc2167752009-11-12 09:34:21 +0000627 /*
628 * here we're doing allocation and writeback of the
629 * compressed pages
630 */
631 btrfs_drop_extent_cache(inode, async_extent->start,
632 async_extent->start +
633 async_extent->ram_size - 1, 0);
634
Chris Mason771ed682008-11-06 22:02:51 -0500635 em = alloc_extent_map(GFP_NOFS);
636 em->start = async_extent->start;
637 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500638 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500639
640 em->block_start = ins.objectid;
641 em->block_len = ins.offset;
642 em->bdev = root->fs_info->fs_devices->latest_bdev;
643 set_bit(EXTENT_FLAG_PINNED, &em->flags);
644 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
645
Chris Masond3977122009-01-05 21:25:51 -0500646 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400647 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500648 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400649 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500650 if (ret != -EEXIST) {
651 free_extent_map(em);
652 break;
653 }
654 btrfs_drop_extent_cache(inode, async_extent->start,
655 async_extent->start +
656 async_extent->ram_size - 1, 0);
657 }
658
659 ret = btrfs_add_ordered_extent(inode, async_extent->start,
660 ins.objectid,
661 async_extent->ram_size,
662 ins.offset,
663 BTRFS_ORDERED_COMPRESSED);
664 BUG_ON(ret);
665
Chris Mason771ed682008-11-06 22:02:51 -0500666 /*
667 * clear dirty, set writeback and unlock the pages.
668 */
669 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400670 &BTRFS_I(inode)->io_tree,
671 async_extent->start,
672 async_extent->start +
673 async_extent->ram_size - 1,
674 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
675 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400676 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400677 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500678
679 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500680 async_extent->start,
681 async_extent->ram_size,
682 ins.objectid,
683 ins.offset, async_extent->pages,
684 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500685
686 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500687 alloc_hint = ins.objectid + ins.offset;
688 kfree(async_extent);
689 cond_resched();
690 }
691
Chris Mason771ed682008-11-06 22:02:51 -0500692 return 0;
693}
694
695/*
696 * when extent_io.c finds a delayed allocation range in the file,
697 * the call backs end up in this code. The basic idea is to
698 * allocate extents on disk for the range, and create ordered data structs
699 * in ram to track those extents.
700 *
701 * locked_page is the page that writepage had locked already. We use
702 * it to make sure we don't do extra locks or unlocks.
703 *
704 * *page_started is set to one if we unlock locked_page and do everything
705 * required to start IO on it. It may be clean and already done with
706 * IO when we return.
707 */
708static noinline int cow_file_range(struct inode *inode,
709 struct page *locked_page,
710 u64 start, u64 end, int *page_started,
711 unsigned long *nr_written,
712 int unlock)
713{
714 struct btrfs_root *root = BTRFS_I(inode)->root;
715 struct btrfs_trans_handle *trans;
716 u64 alloc_hint = 0;
717 u64 num_bytes;
718 unsigned long ram_size;
719 u64 disk_num_bytes;
720 u64 cur_alloc_size;
721 u64 blocksize = root->sectorsize;
722 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500723 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500724 struct btrfs_key ins;
725 struct extent_map *em;
726 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
727 int ret = 0;
728
729 trans = btrfs_join_transaction(root, 1);
730 BUG_ON(!trans);
731 btrfs_set_trans_block_group(trans, inode);
732
Chris Mason42dc7ba2008-12-15 11:44:56 -0500733 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500734
735 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
736 num_bytes = max(blocksize, num_bytes);
737 disk_num_bytes = num_bytes;
738 ret = 0;
739
740 if (start == 0) {
741 /* lets try to make an inline extent */
742 ret = cow_file_range_inline(trans, root, inode,
743 start, end, 0, NULL);
744 if (ret == 0) {
745 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400746 &BTRFS_I(inode)->io_tree,
747 start, end, NULL,
748 EXTENT_CLEAR_UNLOCK_PAGE |
749 EXTENT_CLEAR_UNLOCK |
750 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400751 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400752 EXTENT_CLEAR_DIRTY |
753 EXTENT_SET_WRITEBACK |
754 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000755
Chris Mason771ed682008-11-06 22:02:51 -0500756 *nr_written = *nr_written +
757 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
758 *page_started = 1;
759 ret = 0;
760 goto out;
761 }
762 }
Chris Masonc8b97812008-10-29 14:49:59 -0400763
764 BUG_ON(disk_num_bytes >
765 btrfs_super_total_bytes(&root->fs_info->super_copy));
766
Chris Masonb917b7c2009-09-18 16:07:03 -0400767
768 read_lock(&BTRFS_I(inode)->extent_tree.lock);
769 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
770 start, num_bytes);
771 if (em) {
Josef Bacik6346c932009-11-10 21:23:47 -0500772 /*
773 * if block start isn't an actual block number then find the
774 * first block in this inode and use that as a hint. If that
775 * block is also bogus then just don't worry about it.
776 */
777 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
778 free_extent_map(em);
779 em = search_extent_mapping(em_tree, 0, 0);
780 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
781 alloc_hint = em->block_start;
782 if (em)
783 free_extent_map(em);
784 } else {
785 alloc_hint = em->block_start;
786 free_extent_map(em);
787 }
Chris Masonb917b7c2009-09-18 16:07:03 -0400788 }
789 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400790 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400791
Chris Masond3977122009-01-05 21:25:51 -0500792 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400793 unsigned long op;
794
Chris Masonc8b97812008-10-29 14:49:59 -0400795 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400796 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500797 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400798 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500799 BUG_ON(ret);
800
Chris Masone6dcd2d2008-07-17 12:53:50 -0400801 em = alloc_extent_map(GFP_NOFS);
802 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500803 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500804 ram_size = ins.offset;
805 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400806
Chris Masone6dcd2d2008-07-17 12:53:50 -0400807 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400808 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400809 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400810 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400811
Chris Masond3977122009-01-05 21:25:51 -0500812 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400813 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400814 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400815 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400816 if (ret != -EEXIST) {
817 free_extent_map(em);
818 break;
819 }
820 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400821 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400822 }
823
Chris Mason98d20f62008-04-14 09:46:10 -0400824 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400825 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500826 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400827 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400828
Yan Zheng17d217f2008-12-12 10:03:38 -0500829 if (root->root_key.objectid ==
830 BTRFS_DATA_RELOC_TREE_OBJECTID) {
831 ret = btrfs_reloc_clone_csums(inode, start,
832 cur_alloc_size);
833 BUG_ON(ret);
834 }
835
Chris Masond3977122009-01-05 21:25:51 -0500836 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400837 break;
Chris Masond3977122009-01-05 21:25:51 -0500838
Chris Masonc8b97812008-10-29 14:49:59 -0400839 /* we're not doing compressed IO, don't unlock the first
840 * page (which the caller expects to stay locked), don't
841 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400842 *
843 * Do set the Private2 bit so we know this page was properly
844 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400845 */
Chris Masona791e352009-10-08 11:27:10 -0400846 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
847 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
848 EXTENT_SET_PRIVATE2;
849
Chris Masonc8b97812008-10-29 14:49:59 -0400850 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
851 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400852 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400853 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500854 num_bytes -= cur_alloc_size;
855 alloc_hint = ins.objectid + ins.offset;
856 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400857 }
Chris Masonb888db22007-08-27 16:49:44 -0400858out:
Chris Mason771ed682008-11-06 22:02:51 -0500859 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400860 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400861
Chris Masonbe20aa92007-12-17 20:14:01 -0500862 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500863}
Chris Masonc8b97812008-10-29 14:49:59 -0400864
Chris Mason771ed682008-11-06 22:02:51 -0500865/*
866 * work queue call back to started compression on a file and pages
867 */
868static noinline void async_cow_start(struct btrfs_work *work)
869{
870 struct async_cow *async_cow;
871 int num_added = 0;
872 async_cow = container_of(work, struct async_cow, work);
873
874 compress_file_range(async_cow->inode, async_cow->locked_page,
875 async_cow->start, async_cow->end, async_cow,
876 &num_added);
877 if (num_added == 0)
878 async_cow->inode = NULL;
879}
880
881/*
882 * work queue call back to submit previously compressed pages
883 */
884static noinline void async_cow_submit(struct btrfs_work *work)
885{
886 struct async_cow *async_cow;
887 struct btrfs_root *root;
888 unsigned long nr_pages;
889
890 async_cow = container_of(work, struct async_cow, work);
891
892 root = async_cow->root;
893 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
894 PAGE_CACHE_SHIFT;
895
896 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
897
898 if (atomic_read(&root->fs_info->async_delalloc_pages) <
899 5 * 1042 * 1024 &&
900 waitqueue_active(&root->fs_info->async_submit_wait))
901 wake_up(&root->fs_info->async_submit_wait);
902
Chris Masond3977122009-01-05 21:25:51 -0500903 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500904 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500905}
Chris Masonc8b97812008-10-29 14:49:59 -0400906
Chris Mason771ed682008-11-06 22:02:51 -0500907static noinline void async_cow_free(struct btrfs_work *work)
908{
909 struct async_cow *async_cow;
910 async_cow = container_of(work, struct async_cow, work);
911 kfree(async_cow);
912}
913
914static int cow_file_range_async(struct inode *inode, struct page *locked_page,
915 u64 start, u64 end, int *page_started,
916 unsigned long *nr_written)
917{
918 struct async_cow *async_cow;
919 struct btrfs_root *root = BTRFS_I(inode)->root;
920 unsigned long nr_pages;
921 u64 cur_end;
922 int limit = 10 * 1024 * 1042;
923
Chris Masona3429ab2009-10-08 12:30:20 -0400924 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
925 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500926 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500927 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
928 async_cow->inode = inode;
929 async_cow->root = root;
930 async_cow->locked_page = locked_page;
931 async_cow->start = start;
932
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200933 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500934 cur_end = end;
935 else
936 cur_end = min(end, start + 512 * 1024 - 1);
937
938 async_cow->end = cur_end;
939 INIT_LIST_HEAD(&async_cow->extents);
940
941 async_cow->work.func = async_cow_start;
942 async_cow->work.ordered_func = async_cow_submit;
943 async_cow->work.ordered_free = async_cow_free;
944 async_cow->work.flags = 0;
945
Chris Mason771ed682008-11-06 22:02:51 -0500946 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
947 PAGE_CACHE_SHIFT;
948 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
949
950 btrfs_queue_worker(&root->fs_info->delalloc_workers,
951 &async_cow->work);
952
953 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
954 wait_event(root->fs_info->async_submit_wait,
955 (atomic_read(&root->fs_info->async_delalloc_pages) <
956 limit));
957 }
958
Chris Masond3977122009-01-05 21:25:51 -0500959 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500960 atomic_read(&root->fs_info->async_delalloc_pages)) {
961 wait_event(root->fs_info->async_submit_wait,
962 (atomic_read(&root->fs_info->async_delalloc_pages) ==
963 0));
964 }
965
966 *nr_written += nr_pages;
967 start = cur_end + 1;
968 }
969 *page_started = 1;
970 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500971}
972
Chris Masond3977122009-01-05 21:25:51 -0500973static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500974 u64 bytenr, u64 num_bytes)
975{
976 int ret;
977 struct btrfs_ordered_sum *sums;
978 LIST_HEAD(list);
979
Yan Zheng07d400a2009-01-06 11:42:00 -0500980 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
981 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500982 if (ret == 0 && list_empty(&list))
983 return 0;
984
985 while (!list_empty(&list)) {
986 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
987 list_del(&sums->list);
988 kfree(sums);
989 }
990 return 1;
991}
992
Chris Masond352ac62008-09-29 15:18:18 -0400993/*
994 * when nowcow writeback call back. This checks for snapshots or COW copies
995 * of the extents that exist in the file, and COWs the file as required.
996 *
997 * If no cow copies or snapshots exist, we write directly to the existing
998 * blocks on disk
999 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001000static noinline int run_delalloc_nocow(struct inode *inode,
1001 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001002 u64 start, u64 end, int *page_started, int force,
1003 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001004{
Chris Masonbe20aa92007-12-17 20:14:01 -05001005 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001006 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001007 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001008 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001009 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001010 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001011 u64 cow_start;
1012 u64 cur_offset;
1013 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001014 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001015 u64 disk_bytenr;
1016 u64 num_bytes;
1017 int extent_type;
1018 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001019 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001020 int nocow;
1021 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -05001022
1023 path = btrfs_alloc_path();
1024 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001025 trans = btrfs_join_transaction(root, 1);
1026 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -05001027
Yan Zheng80ff3852008-10-30 14:20:02 -04001028 cow_start = (u64)-1;
1029 cur_offset = start;
1030 while (1) {
1031 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1032 cur_offset, 0);
1033 BUG_ON(ret < 0);
1034 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1035 leaf = path->nodes[0];
1036 btrfs_item_key_to_cpu(leaf, &found_key,
1037 path->slots[0] - 1);
1038 if (found_key.objectid == inode->i_ino &&
1039 found_key.type == BTRFS_EXTENT_DATA_KEY)
1040 path->slots[0]--;
1041 }
1042 check_prev = 0;
1043next_slot:
1044 leaf = path->nodes[0];
1045 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1046 ret = btrfs_next_leaf(root, path);
1047 if (ret < 0)
1048 BUG_ON(1);
1049 if (ret > 0)
1050 break;
1051 leaf = path->nodes[0];
1052 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001053
Yan Zheng80ff3852008-10-30 14:20:02 -04001054 nocow = 0;
1055 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001056 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001057 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001058
Yan Zheng80ff3852008-10-30 14:20:02 -04001059 if (found_key.objectid > inode->i_ino ||
1060 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1061 found_key.offset > end)
1062 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001063
Yan Zheng80ff3852008-10-30 14:20:02 -04001064 if (found_key.offset > cur_offset) {
1065 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001066 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001067 goto out_check;
1068 }
Chris Masonc31f8832008-01-08 15:46:31 -05001069
Yan Zheng80ff3852008-10-30 14:20:02 -04001070 fi = btrfs_item_ptr(leaf, path->slots[0],
1071 struct btrfs_file_extent_item);
1072 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001073
Yan Zhengd899e052008-10-30 14:25:28 -04001074 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1075 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001076 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001077 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001078 extent_end = found_key.offset +
1079 btrfs_file_extent_num_bytes(leaf, fi);
1080 if (extent_end <= start) {
1081 path->slots[0]++;
1082 goto next_slot;
1083 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001084 if (disk_bytenr == 0)
1085 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001086 if (btrfs_file_extent_compression(leaf, fi) ||
1087 btrfs_file_extent_encryption(leaf, fi) ||
1088 btrfs_file_extent_other_encoding(leaf, fi))
1089 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001090 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1091 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001092 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001093 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001094 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001095 found_key.offset -
1096 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001097 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001098 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001099 disk_bytenr += cur_offset - found_key.offset;
1100 num_bytes = min(end + 1, extent_end) - cur_offset;
1101 /*
1102 * force cow if csum exists in the range.
1103 * this ensure that csum for a given extent are
1104 * either valid or do not exist.
1105 */
1106 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1107 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001108 nocow = 1;
1109 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1110 extent_end = found_key.offset +
1111 btrfs_file_extent_inline_len(leaf, fi);
1112 extent_end = ALIGN(extent_end, root->sectorsize);
1113 } else {
1114 BUG_ON(1);
1115 }
1116out_check:
1117 if (extent_end <= start) {
1118 path->slots[0]++;
1119 goto next_slot;
1120 }
1121 if (!nocow) {
1122 if (cow_start == (u64)-1)
1123 cow_start = cur_offset;
1124 cur_offset = extent_end;
1125 if (cur_offset > end)
1126 break;
1127 path->slots[0]++;
1128 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001129 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001130
Yan Zheng7ea394f2008-08-05 13:05:02 -04001131 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001132 if (cow_start != (u64)-1) {
1133 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001134 found_key.offset - 1, page_started,
1135 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001136 BUG_ON(ret);
1137 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001138 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001139
Yan Zhengd899e052008-10-30 14:25:28 -04001140 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1141 struct extent_map *em;
1142 struct extent_map_tree *em_tree;
1143 em_tree = &BTRFS_I(inode)->extent_tree;
1144 em = alloc_extent_map(GFP_NOFS);
1145 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001146 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001147 em->len = num_bytes;
1148 em->block_len = num_bytes;
1149 em->block_start = disk_bytenr;
1150 em->bdev = root->fs_info->fs_devices->latest_bdev;
1151 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1152 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001153 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001154 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001155 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001156 if (ret != -EEXIST) {
1157 free_extent_map(em);
1158 break;
1159 }
1160 btrfs_drop_extent_cache(inode, em->start,
1161 em->start + em->len - 1, 0);
1162 }
1163 type = BTRFS_ORDERED_PREALLOC;
1164 } else {
1165 type = BTRFS_ORDERED_NOCOW;
1166 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001167
1168 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001169 num_bytes, num_bytes, type);
1170 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001171
Yan Zhengd899e052008-10-30 14:25:28 -04001172 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001173 cur_offset, cur_offset + num_bytes - 1,
1174 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1175 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1176 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001177 cur_offset = extent_end;
1178 if (cur_offset > end)
1179 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001180 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001181 btrfs_release_path(root, path);
1182
1183 if (cur_offset <= end && cow_start == (u64)-1)
1184 cow_start = cur_offset;
1185 if (cow_start != (u64)-1) {
1186 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001187 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001188 BUG_ON(ret);
1189 }
1190
1191 ret = btrfs_end_transaction(trans, root);
1192 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001193 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001194 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001195}
1196
Chris Masond352ac62008-09-29 15:18:18 -04001197/*
1198 * extent_io.c call back to do delayed allocation processing
1199 */
Chris Masonc8b97812008-10-29 14:49:59 -04001200static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001201 u64 start, u64 end, int *page_started,
1202 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001203{
Chris Masonbe20aa92007-12-17 20:14:01 -05001204 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001205 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001206
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001207 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001208 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001209 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001210 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001211 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001212 page_started, 0, nr_written);
Chris Mason7f366cf2009-03-12 20:12:45 -04001213 else if (!btrfs_test_opt(root, COMPRESS))
1214 ret = cow_file_range(inode, locked_page, start, end,
1215 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001216 else
Chris Mason771ed682008-11-06 22:02:51 -05001217 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001218 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001219 return ret;
1220}
1221
Josef Bacik9ed74f22009-09-11 16:12:44 -04001222static int btrfs_split_extent_hook(struct inode *inode,
1223 struct extent_state *orig, u64 split)
1224{
1225 struct btrfs_root *root = BTRFS_I(inode)->root;
1226 u64 size;
1227
1228 if (!(orig->state & EXTENT_DELALLOC))
1229 return 0;
1230
1231 size = orig->end - orig->start + 1;
1232 if (size > root->fs_info->max_extent) {
1233 u64 num_extents;
1234 u64 new_size;
1235
1236 new_size = orig->end - split + 1;
1237 num_extents = div64_u64(size + root->fs_info->max_extent - 1,
1238 root->fs_info->max_extent);
1239
1240 /*
Josef Bacik32c00af2009-10-08 13:34:05 -04001241 * if we break a large extent up then leave oustanding_extents
1242 * be, since we've already accounted for the large extent.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001243 */
1244 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1245 root->fs_info->max_extent) < num_extents)
1246 return 0;
1247 }
1248
Josef Bacik32c00af2009-10-08 13:34:05 -04001249 spin_lock(&BTRFS_I(inode)->accounting_lock);
1250 BTRFS_I(inode)->outstanding_extents++;
1251 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001252
1253 return 0;
1254}
1255
1256/*
1257 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1258 * extents so we can keep track of new extents that are just merged onto old
1259 * extents, such as when we are doing sequential writes, so we can properly
1260 * account for the metadata space we'll need.
1261 */
1262static int btrfs_merge_extent_hook(struct inode *inode,
1263 struct extent_state *new,
1264 struct extent_state *other)
1265{
1266 struct btrfs_root *root = BTRFS_I(inode)->root;
1267 u64 new_size, old_size;
1268 u64 num_extents;
1269
1270 /* not delalloc, ignore it */
1271 if (!(other->state & EXTENT_DELALLOC))
1272 return 0;
1273
1274 old_size = other->end - other->start + 1;
1275 if (new->start < other->start)
1276 new_size = other->end - new->start + 1;
1277 else
1278 new_size = new->end - other->start + 1;
1279
1280 /* we're not bigger than the max, unreserve the space and go */
1281 if (new_size <= root->fs_info->max_extent) {
Josef Bacik32c00af2009-10-08 13:34:05 -04001282 spin_lock(&BTRFS_I(inode)->accounting_lock);
1283 BTRFS_I(inode)->outstanding_extents--;
1284 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001285 return 0;
1286 }
1287
1288 /*
1289 * If we grew by another max_extent, just return, we want to keep that
1290 * reserved amount.
1291 */
1292 num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
1293 root->fs_info->max_extent);
1294 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1295 root->fs_info->max_extent) > num_extents)
1296 return 0;
1297
Josef Bacik32c00af2009-10-08 13:34:05 -04001298 spin_lock(&BTRFS_I(inode)->accounting_lock);
1299 BTRFS_I(inode)->outstanding_extents--;
1300 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001301
1302 return 0;
1303}
1304
Chris Masond352ac62008-09-29 15:18:18 -04001305/*
1306 * extent_io.c set_bit_hook, used to track delayed allocation
1307 * bytes in this file, and to maintain the list of inodes that
1308 * have pending delalloc work to be done.
1309 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001310static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001311 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001312{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001313
Chris Mason75eff682008-12-15 15:54:40 -05001314 /*
1315 * set_bit and clear bit hooks normally require _irqsave/restore
1316 * but in this case, we are only testeing for the DELALLOC
1317 * bit, which is only set or cleared with irqs on
1318 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001319 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001320 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001321
Josef Bacik32c00af2009-10-08 13:34:05 -04001322 spin_lock(&BTRFS_I(inode)->accounting_lock);
1323 BTRFS_I(inode)->outstanding_extents++;
1324 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik6a632092009-02-20 11:00:09 -05001325 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
Chris Mason75eff682008-12-15 15:54:40 -05001326 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001327 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001328 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001329 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1330 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1331 &root->fs_info->delalloc_inodes);
1332 }
Chris Mason75eff682008-12-15 15:54:40 -05001333 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001334 }
1335 return 0;
1336}
1337
Chris Masond352ac62008-09-29 15:18:18 -04001338/*
1339 * extent_io.c clear_bit_hook, see set_bit_hook for why
1340 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001341static int btrfs_clear_bit_hook(struct inode *inode,
1342 struct extent_state *state, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001343{
Chris Mason75eff682008-12-15 15:54:40 -05001344 /*
1345 * set_bit and clear bit hooks normally require _irqsave/restore
1346 * but in this case, we are only testeing for the DELALLOC
1347 * bit, which is only set or cleared with irqs on
1348 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001349 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001350 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001351
Josef Bacik32c00af2009-10-08 13:34:05 -04001352 if (bits & EXTENT_DO_ACCOUNTING) {
1353 spin_lock(&BTRFS_I(inode)->accounting_lock);
1354 BTRFS_I(inode)->outstanding_extents--;
1355 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1356 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1357 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04001358
Chris Mason75eff682008-12-15 15:54:40 -05001359 spin_lock(&root->fs_info->delalloc_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001360 if (state->end - state->start + 1 >
1361 root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001362 printk(KERN_INFO "btrfs warning: delalloc account "
1363 "%llu %llu\n",
Josef Bacik9ed74f22009-09-11 16:12:44 -04001364 (unsigned long long)
1365 state->end - state->start + 1,
Chris Masond3977122009-01-05 21:25:51 -05001366 (unsigned long long)
1367 root->fs_info->delalloc_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001368 btrfs_delalloc_free_space(root, inode, (u64)-1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001369 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001370 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001371 } else {
Josef Bacik6a632092009-02-20 11:00:09 -05001372 btrfs_delalloc_free_space(root, inode,
Josef Bacik9ed74f22009-09-11 16:12:44 -04001373 state->end -
1374 state->start + 1);
1375 root->fs_info->delalloc_bytes -= state->end -
1376 state->start + 1;
1377 BTRFS_I(inode)->delalloc_bytes -= state->end -
1378 state->start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001379 }
Chris Masonea8c2812008-08-04 23:17:27 -04001380 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1381 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1382 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1383 }
Chris Mason75eff682008-12-15 15:54:40 -05001384 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001385 }
1386 return 0;
1387}
1388
Chris Masond352ac62008-09-29 15:18:18 -04001389/*
1390 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1391 * we don't create bios that span stripes or chunks
1392 */
Chris Mason239b14b2008-03-24 15:02:07 -04001393int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001394 size_t size, struct bio *bio,
1395 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001396{
1397 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1398 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001399 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001400 u64 length = 0;
1401 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001402 int ret;
1403
Chris Mason771ed682008-11-06 22:02:51 -05001404 if (bio_flags & EXTENT_BIO_COMPRESSED)
1405 return 0;
1406
Chris Masonf2d8d742008-04-21 10:03:05 -04001407 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001408 map_tree = &root->fs_info->mapping_tree;
1409 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001410 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001411 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001412
Chris Masond3977122009-01-05 21:25:51 -05001413 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001414 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001415 return 0;
1416}
1417
Chris Masond352ac62008-09-29 15:18:18 -04001418/*
1419 * in order to insert checksums into the metadata in large chunks,
1420 * we wait until bio submission time. All the pages in the bio are
1421 * checksummed and sums are attached onto the ordered extent record.
1422 *
1423 * At IO completion time the cums attached on the ordered extent record
1424 * are inserted into the btree
1425 */
Chris Masond3977122009-01-05 21:25:51 -05001426static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1427 struct bio *bio, int mirror_num,
1428 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001429{
Chris Mason065631f2008-02-20 12:07:25 -05001430 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001431 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001432
Chris Masond20f7042008-12-08 16:58:54 -05001433 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001434 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001435 return 0;
1436}
Chris Masone0156402008-04-16 11:15:20 -04001437
Chris Mason4a69a412008-11-06 22:03:00 -05001438/*
1439 * in order to insert checksums into the metadata in large chunks,
1440 * we wait until bio submission time. All the pages in the bio are
1441 * checksummed and sums are attached onto the ordered extent record.
1442 *
1443 * At IO completion time the cums attached on the ordered extent record
1444 * are inserted into the btree
1445 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001446static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001447 int mirror_num, unsigned long bio_flags)
1448{
1449 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001450 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001451}
1452
Chris Masond352ac62008-09-29 15:18:18 -04001453/*
Chris Masoncad321a2008-12-17 14:51:42 -05001454 * extent_io.c submission hook. This does the right thing for csum calculation
1455 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001456 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001457static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001458 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001459{
1460 struct btrfs_root *root = BTRFS_I(inode)->root;
1461 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001462 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001463
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001464 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001465
Chris Masone6dcd2d2008-07-17 12:53:50 -04001466 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1467 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001468
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001469 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001470 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001471 return btrfs_submit_compressed_read(inode, bio,
1472 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001473 } else if (!skip_sum)
1474 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001475 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001476 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001477 /* csum items have already been cloned */
1478 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1479 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001480 /* we're doing a write, do the async checksumming */
1481 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001482 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001483 bio_flags, __btrfs_submit_bio_start,
1484 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001485 }
1486
Chris Mason0b86a832008-03-24 15:01:56 -04001487mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001488 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001489}
Chris Mason6885f302008-02-20 16:11:05 -05001490
Chris Masond352ac62008-09-29 15:18:18 -04001491/*
1492 * given a list of ordered sums record them in the inode. This happens
1493 * at IO completion time based on sums calculated at bio submission time.
1494 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001495static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001496 struct inode *inode, u64 file_offset,
1497 struct list_head *list)
1498{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001499 struct btrfs_ordered_sum *sum;
1500
1501 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001502
1503 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001504 btrfs_csum_file_blocks(trans,
1505 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001506 }
1507 return 0;
1508}
1509
Chris Masonea8c2812008-08-04 23:17:27 -04001510int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1511{
Chris Masond3977122009-01-05 21:25:51 -05001512 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001513 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001514 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1515 GFP_NOFS);
1516}
1517
Chris Masond352ac62008-09-29 15:18:18 -04001518/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001519struct btrfs_writepage_fixup {
1520 struct page *page;
1521 struct btrfs_work work;
1522};
1523
Christoph Hellwigb2950862008-12-02 09:54:17 -05001524static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001525{
1526 struct btrfs_writepage_fixup *fixup;
1527 struct btrfs_ordered_extent *ordered;
1528 struct page *page;
1529 struct inode *inode;
1530 u64 page_start;
1531 u64 page_end;
1532
1533 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1534 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001535again:
Chris Mason247e7432008-07-17 12:53:51 -04001536 lock_page(page);
1537 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1538 ClearPageChecked(page);
1539 goto out_page;
1540 }
1541
1542 inode = page->mapping->host;
1543 page_start = page_offset(page);
1544 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1545
1546 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001547
1548 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001549 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001550 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001551
1552 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1553 if (ordered) {
1554 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1555 page_end, GFP_NOFS);
1556 unlock_page(page);
1557 btrfs_start_ordered_extent(inode, ordered, 1);
1558 goto again;
1559 }
Chris Mason247e7432008-07-17 12:53:51 -04001560
Chris Masonea8c2812008-08-04 23:17:27 -04001561 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001562 ClearPageChecked(page);
1563out:
1564 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1565out_page:
1566 unlock_page(page);
1567 page_cache_release(page);
1568}
1569
1570/*
1571 * There are a few paths in the higher layers of the kernel that directly
1572 * set the page dirty bit without asking the filesystem if it is a
1573 * good idea. This causes problems because we want to make sure COW
1574 * properly happens and the data=ordered rules are followed.
1575 *
Chris Masonc8b97812008-10-29 14:49:59 -04001576 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001577 * hasn't been properly setup for IO. We kick off an async process
1578 * to fix it up. The async helper will wait for ordered extents, set
1579 * the delalloc bit and make it safe to write the page.
1580 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001581static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001582{
1583 struct inode *inode = page->mapping->host;
1584 struct btrfs_writepage_fixup *fixup;
1585 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001586
Chris Mason8b62b722009-09-02 16:53:46 -04001587 /* this page is properly in the ordered list */
1588 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001589 return 0;
1590
1591 if (PageChecked(page))
1592 return -EAGAIN;
1593
1594 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1595 if (!fixup)
1596 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001597
Chris Mason247e7432008-07-17 12:53:51 -04001598 SetPageChecked(page);
1599 page_cache_get(page);
1600 fixup->work.func = btrfs_writepage_fixup_worker;
1601 fixup->page = page;
1602 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1603 return -EAGAIN;
1604}
1605
Yan Zhengd899e052008-10-30 14:25:28 -04001606static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1607 struct inode *inode, u64 file_pos,
1608 u64 disk_bytenr, u64 disk_num_bytes,
1609 u64 num_bytes, u64 ram_bytes,
1610 u8 compression, u8 encryption,
1611 u16 other_encoding, int extent_type)
1612{
1613 struct btrfs_root *root = BTRFS_I(inode)->root;
1614 struct btrfs_file_extent_item *fi;
1615 struct btrfs_path *path;
1616 struct extent_buffer *leaf;
1617 struct btrfs_key ins;
1618 u64 hint;
1619 int ret;
1620
1621 path = btrfs_alloc_path();
1622 BUG_ON(!path);
1623
Chris Masonb9473432009-03-13 11:00:37 -04001624 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001625
1626 /*
1627 * we may be replacing one extent in the tree with another.
1628 * The new extent is pinned in the extent map, and we don't want
1629 * to drop it from the cache until it is completely in the btree.
1630 *
1631 * So, tell btrfs_drop_extents to leave this extent in the cache.
1632 * the caller is expected to unpin it and allow it to be merged
1633 * with the others.
1634 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001635 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1636 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001637 BUG_ON(ret);
1638
1639 ins.objectid = inode->i_ino;
1640 ins.offset = file_pos;
1641 ins.type = BTRFS_EXTENT_DATA_KEY;
1642 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1643 BUG_ON(ret);
1644 leaf = path->nodes[0];
1645 fi = btrfs_item_ptr(leaf, path->slots[0],
1646 struct btrfs_file_extent_item);
1647 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1648 btrfs_set_file_extent_type(leaf, fi, extent_type);
1649 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1650 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1651 btrfs_set_file_extent_offset(leaf, fi, 0);
1652 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1653 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1654 btrfs_set_file_extent_compression(leaf, fi, compression);
1655 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1656 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001657
1658 btrfs_unlock_up_safe(path, 1);
1659 btrfs_set_lock_blocking(leaf);
1660
Yan Zhengd899e052008-10-30 14:25:28 -04001661 btrfs_mark_buffer_dirty(leaf);
1662
1663 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001664
1665 ins.objectid = disk_bytenr;
1666 ins.offset = disk_num_bytes;
1667 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001668 ret = btrfs_alloc_reserved_file_extent(trans, root,
1669 root->root_key.objectid,
1670 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001671 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001672 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001673
Yan Zhengd899e052008-10-30 14:25:28 -04001674 return 0;
1675}
1676
Chris Mason5d13a982009-03-13 11:41:46 -04001677/*
1678 * helper function for btrfs_finish_ordered_io, this
1679 * just reads in some of the csum leaves to prime them into ram
1680 * before we start the transaction. It limits the amount of btree
1681 * reads required while inside the transaction.
1682 */
1683static noinline void reada_csum(struct btrfs_root *root,
1684 struct btrfs_path *path,
1685 struct btrfs_ordered_extent *ordered_extent)
1686{
1687 struct btrfs_ordered_sum *sum;
1688 u64 bytenr;
1689
1690 sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
1691 list);
1692 bytenr = sum->sums[0].bytenr;
1693
1694 /*
1695 * we don't care about the results, the point of this search is
1696 * just to get the btree leaves into ram
1697 */
1698 btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
1699}
1700
Chris Masond352ac62008-09-29 15:18:18 -04001701/* as ordered data IO finishes, this gets called so we can finish
1702 * an ordered extent if the range of bytes in the file it covers are
1703 * fully written.
1704 */
Chris Mason211f90e2008-07-18 11:56:15 -04001705static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001706{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001707 struct btrfs_root *root = BTRFS_I(inode)->root;
1708 struct btrfs_trans_handle *trans;
Chris Mason5d13a982009-03-13 11:41:46 -04001709 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001710 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001711 struct btrfs_path *path;
Yan Zhengd899e052008-10-30 14:25:28 -04001712 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001713 int ret;
1714
1715 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001716 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001717 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001718
Chris Masonb7ec40d2009-03-12 20:12:45 -04001719 /*
1720 * before we join the transaction, try to do some of our IO.
1721 * This will limit the amount of IO that we have to do with
1722 * the transaction running. We're unlikely to need to do any
1723 * IO if the file extents are new, the disk_i_size checks
1724 * covers the most common case.
1725 */
1726 if (start < BTRFS_I(inode)->disk_i_size) {
1727 path = btrfs_alloc_path();
1728 if (path) {
1729 ret = btrfs_lookup_file_extent(NULL, root, path,
1730 inode->i_ino,
1731 start, 0);
Chris Mason5d13a982009-03-13 11:41:46 -04001732 ordered_extent = btrfs_lookup_ordered_extent(inode,
1733 start);
1734 if (!list_empty(&ordered_extent->list)) {
1735 btrfs_release_path(root, path);
1736 reada_csum(root, path, ordered_extent);
1737 }
Chris Masonb7ec40d2009-03-12 20:12:45 -04001738 btrfs_free_path(path);
1739 }
1740 }
1741
Chris Mason5d13a982009-03-13 11:41:46 -04001742 if (!ordered_extent)
1743 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001744 BUG_ON(!ordered_extent);
Yan, Zhengc2167752009-11-12 09:34:21 +00001745 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1746 BUG_ON(!list_empty(&ordered_extent->list));
1747 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1748 if (!ret) {
1749 trans = btrfs_join_transaction(root, 1);
1750 ret = btrfs_update_inode(trans, root, inode);
1751 BUG_ON(ret);
1752 btrfs_end_transaction(trans, root);
1753 }
1754 goto out;
1755 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001756
1757 lock_extent(io_tree, ordered_extent->file_offset,
1758 ordered_extent->file_offset + ordered_extent->len - 1,
1759 GFP_NOFS);
1760
Yan, Zhengc2167752009-11-12 09:34:21 +00001761 trans = btrfs_join_transaction(root, 1);
1762
Chris Masonc8b97812008-10-29 14:49:59 -04001763 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001764 compressed = 1;
1765 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1766 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001767 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001768 ordered_extent->file_offset,
1769 ordered_extent->file_offset +
1770 ordered_extent->len);
1771 BUG_ON(ret);
1772 } else {
1773 ret = insert_reserved_file_extent(trans, inode,
1774 ordered_extent->file_offset,
1775 ordered_extent->start,
1776 ordered_extent->disk_len,
1777 ordered_extent->len,
1778 ordered_extent->len,
1779 compressed, 0, 0,
1780 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001781 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1782 ordered_extent->file_offset,
1783 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001784 BUG_ON(ret);
1785 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001786 unlock_extent(io_tree, ordered_extent->file_offset,
1787 ordered_extent->file_offset + ordered_extent->len - 1,
1788 GFP_NOFS);
1789 add_pending_csums(trans, inode, ordered_extent->file_offset,
1790 &ordered_extent->list);
1791
Yan, Zhengc2167752009-11-12 09:34:21 +00001792 /* this also removes the ordered extent from the tree */
1793 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1794 ret = btrfs_update_inode(trans, root, inode);
1795 BUG_ON(ret);
1796 btrfs_end_transaction(trans, root);
1797out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001798 /* once for us */
1799 btrfs_put_ordered_extent(ordered_extent);
1800 /* once for the tree */
1801 btrfs_put_ordered_extent(ordered_extent);
1802
Chris Masone6dcd2d2008-07-17 12:53:50 -04001803 return 0;
1804}
1805
Christoph Hellwigb2950862008-12-02 09:54:17 -05001806static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001807 struct extent_state *state, int uptodate)
1808{
Chris Mason8b62b722009-09-02 16:53:46 -04001809 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001810 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1811}
1812
Chris Masond352ac62008-09-29 15:18:18 -04001813/*
1814 * When IO fails, either with EIO or csum verification fails, we
1815 * try other mirrors that might have a good copy of the data. This
1816 * io_failure_record is used to record state as we go through all the
1817 * mirrors. If another mirror has good data, the page is set up to date
1818 * and things continue. If a good mirror can't be found, the original
1819 * bio end_io callback is called to indicate things have failed.
1820 */
Chris Mason7e383262008-04-09 16:28:12 -04001821struct io_failure_record {
1822 struct page *page;
1823 u64 start;
1824 u64 len;
1825 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001826 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001827 int last_mirror;
1828};
1829
Christoph Hellwigb2950862008-12-02 09:54:17 -05001830static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001831 struct page *page, u64 start, u64 end,
1832 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001833{
1834 struct io_failure_record *failrec = NULL;
1835 u64 private;
1836 struct extent_map *em;
1837 struct inode *inode = page->mapping->host;
1838 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001839 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001840 struct bio *bio;
1841 int num_copies;
1842 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001843 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001844 u64 logical;
1845
1846 ret = get_state_private(failure_tree, start, &private);
1847 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001848 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1849 if (!failrec)
1850 return -ENOMEM;
1851 failrec->start = start;
1852 failrec->len = end - start + 1;
1853 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001854 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001855
Chris Mason890871b2009-09-02 16:24:52 -04001856 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001857 em = lookup_extent_mapping(em_tree, start, failrec->len);
1858 if (em->start > start || em->start + em->len < start) {
1859 free_extent_map(em);
1860 em = NULL;
1861 }
Chris Mason890871b2009-09-02 16:24:52 -04001862 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001863
1864 if (!em || IS_ERR(em)) {
1865 kfree(failrec);
1866 return -EIO;
1867 }
1868 logical = start - em->start;
1869 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001870 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1871 logical = em->block_start;
1872 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1873 }
Chris Mason7e383262008-04-09 16:28:12 -04001874 failrec->logical = logical;
1875 free_extent_map(em);
1876 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1877 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001878 set_state_private(failure_tree, start,
1879 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001880 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001881 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001882 }
1883 num_copies = btrfs_num_copies(
1884 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1885 failrec->logical, failrec->len);
1886 failrec->last_mirror++;
1887 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001888 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001889 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1890 failrec->start,
1891 EXTENT_LOCKED);
1892 if (state && state->start != failrec->start)
1893 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001894 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001895 }
1896 if (!state || failrec->last_mirror > num_copies) {
1897 set_state_private(failure_tree, failrec->start, 0);
1898 clear_extent_bits(failure_tree, failrec->start,
1899 failrec->start + failrec->len - 1,
1900 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1901 kfree(failrec);
1902 return -EIO;
1903 }
1904 bio = bio_alloc(GFP_NOFS, 1);
1905 bio->bi_private = state;
1906 bio->bi_end_io = failed_bio->bi_end_io;
1907 bio->bi_sector = failrec->logical >> 9;
1908 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001909 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001910
Chris Mason7e383262008-04-09 16:28:12 -04001911 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001912 if (failed_bio->bi_rw & (1 << BIO_RW))
1913 rw = WRITE;
1914 else
1915 rw = READ;
1916
1917 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001918 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001919 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001920 return 0;
1921}
1922
Chris Masond352ac62008-09-29 15:18:18 -04001923/*
1924 * each time an IO finishes, we do a fast check in the IO failure tree
1925 * to see if we need to process or clean up an io_failure_record
1926 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001927static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001928{
1929 u64 private;
1930 u64 private_failure;
1931 struct io_failure_record *failure;
1932 int ret;
1933
1934 private = 0;
1935 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1936 (u64)-1, 1, EXTENT_DIRTY)) {
1937 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1938 start, &private_failure);
1939 if (ret == 0) {
1940 failure = (struct io_failure_record *)(unsigned long)
1941 private_failure;
1942 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1943 failure->start, 0);
1944 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1945 failure->start,
1946 failure->start + failure->len - 1,
1947 EXTENT_DIRTY | EXTENT_LOCKED,
1948 GFP_NOFS);
1949 kfree(failure);
1950 }
1951 }
Chris Mason7e383262008-04-09 16:28:12 -04001952 return 0;
1953}
1954
Chris Masond352ac62008-09-29 15:18:18 -04001955/*
1956 * when reads are done, we need to check csums to verify the data is correct
1957 * if there's a match, we allow the bio to finish. If not, we go through
1958 * the io_failure_record routines to find good copies
1959 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001960static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001961 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001962{
Chris Mason35ebb932007-10-30 16:56:53 -04001963 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001964 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001965 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001966 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001967 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001968 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001969 struct btrfs_root *root = BTRFS_I(inode)->root;
1970 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001971
Chris Masond20f7042008-12-08 16:58:54 -05001972 if (PageChecked(page)) {
1973 ClearPageChecked(page);
1974 goto good;
1975 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001976
1977 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001978 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001979
Yan Zheng17d217f2008-12-12 10:03:38 -05001980 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001981 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001982 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1983 GFP_NOFS);
1984 return 0;
1985 }
1986
Yanc2e639f2008-02-04 08:57:25 -05001987 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001988 private = state->private;
1989 ret = 0;
1990 } else {
1991 ret = get_state_private(io_tree, start, &private);
1992 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001993 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001994 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001995 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001996
Chris Masonff79f812007-10-15 16:22:25 -04001997 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1998 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001999 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04002000 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002001
Chris Mason9ab86c82009-01-07 09:48:51 -05002002 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05002003good:
Chris Mason7e383262008-04-09 16:28:12 -04002004 /* if the io failure tree for this inode is non-empty,
2005 * check to see if we've recovered from a failed IO
2006 */
Chris Mason1259ab72008-05-12 13:39:03 -04002007 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04002008 return 0;
2009
2010zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04002011 if (printk_ratelimit()) {
2012 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
2013 "private %llu\n", page->mapping->host->i_ino,
2014 (unsigned long long)start, csum,
2015 (unsigned long long)private);
2016 }
Chris Masondb945352007-10-15 16:15:53 -04002017 memset(kaddr + offset, 1, end - start + 1);
2018 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05002019 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04002020 if (private == 0)
2021 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002022 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002023}
Chris Masonb888db22007-08-27 16:49:44 -04002024
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002025struct delayed_iput {
2026 struct list_head list;
2027 struct inode *inode;
2028};
2029
2030void btrfs_add_delayed_iput(struct inode *inode)
2031{
2032 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2033 struct delayed_iput *delayed;
2034
2035 if (atomic_add_unless(&inode->i_count, -1, 1))
2036 return;
2037
2038 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2039 delayed->inode = inode;
2040
2041 spin_lock(&fs_info->delayed_iput_lock);
2042 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2043 spin_unlock(&fs_info->delayed_iput_lock);
2044}
2045
2046void btrfs_run_delayed_iputs(struct btrfs_root *root)
2047{
2048 LIST_HEAD(list);
2049 struct btrfs_fs_info *fs_info = root->fs_info;
2050 struct delayed_iput *delayed;
2051 int empty;
2052
2053 spin_lock(&fs_info->delayed_iput_lock);
2054 empty = list_empty(&fs_info->delayed_iputs);
2055 spin_unlock(&fs_info->delayed_iput_lock);
2056 if (empty)
2057 return;
2058
2059 down_read(&root->fs_info->cleanup_work_sem);
2060 spin_lock(&fs_info->delayed_iput_lock);
2061 list_splice_init(&fs_info->delayed_iputs, &list);
2062 spin_unlock(&fs_info->delayed_iput_lock);
2063
2064 while (!list_empty(&list)) {
2065 delayed = list_entry(list.next, struct delayed_iput, list);
2066 list_del(&delayed->list);
2067 iput(delayed->inode);
2068 kfree(delayed);
2069 }
2070 up_read(&root->fs_info->cleanup_work_sem);
2071}
2072
Josef Bacik7b128762008-07-24 12:17:14 -04002073/*
2074 * This creates an orphan entry for the given inode in case something goes
2075 * wrong in the middle of an unlink/truncate.
2076 */
2077int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2078{
2079 struct btrfs_root *root = BTRFS_I(inode)->root;
2080 int ret = 0;
2081
Yanbcc63ab2008-07-30 16:29:20 -04002082 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002083
2084 /* already on the orphan list, we're good */
2085 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002086 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002087 return 0;
2088 }
2089
2090 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2091
Yanbcc63ab2008-07-30 16:29:20 -04002092 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002093
2094 /*
2095 * insert an orphan item to track this unlinked/truncated file
2096 */
2097 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2098
2099 return ret;
2100}
2101
2102/*
2103 * We have done the truncate/delete so we can go ahead and remove the orphan
2104 * item for this particular inode.
2105 */
2106int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2107{
2108 struct btrfs_root *root = BTRFS_I(inode)->root;
2109 int ret = 0;
2110
Yanbcc63ab2008-07-30 16:29:20 -04002111 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002112
2113 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002114 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002115 return 0;
2116 }
2117
2118 list_del_init(&BTRFS_I(inode)->i_orphan);
2119 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04002120 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002121 return 0;
2122 }
2123
Yanbcc63ab2008-07-30 16:29:20 -04002124 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002125
2126 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2127
2128 return ret;
2129}
2130
2131/*
2132 * this cleans up any orphans that may be left on the list from the last use
2133 * of this root.
2134 */
2135void btrfs_orphan_cleanup(struct btrfs_root *root)
2136{
2137 struct btrfs_path *path;
2138 struct extent_buffer *leaf;
2139 struct btrfs_item *item;
2140 struct btrfs_key key, found_key;
2141 struct btrfs_trans_handle *trans;
2142 struct inode *inode;
2143 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2144
Yan, Zhengc71bf092009-11-12 09:34:40 +00002145 if (!xchg(&root->clean_orphans, 0))
Josef Bacik7b128762008-07-24 12:17:14 -04002146 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002147
2148 path = btrfs_alloc_path();
2149 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002150 path->reada = -1;
2151
2152 key.objectid = BTRFS_ORPHAN_OBJECTID;
2153 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2154 key.offset = (u64)-1;
2155
Josef Bacik7b128762008-07-24 12:17:14 -04002156 while (1) {
2157 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2158 if (ret < 0) {
2159 printk(KERN_ERR "Error searching slot for orphan: %d"
2160 "\n", ret);
2161 break;
2162 }
2163
2164 /*
2165 * if ret == 0 means we found what we were searching for, which
2166 * is weird, but possible, so only screw with path if we didnt
2167 * find the key and see if we have stuff that matches
2168 */
2169 if (ret > 0) {
2170 if (path->slots[0] == 0)
2171 break;
2172 path->slots[0]--;
2173 }
2174
2175 /* pull out the item */
2176 leaf = path->nodes[0];
2177 item = btrfs_item_nr(leaf, path->slots[0]);
2178 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2179
2180 /* make sure the item matches what we want */
2181 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2182 break;
2183 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2184 break;
2185
2186 /* release the path since we're done with it */
2187 btrfs_release_path(root, path);
2188
2189 /*
2190 * this is where we are basically btrfs_lookup, without the
2191 * crossing root thing. we store the inode number in the
2192 * offset of the orphan item.
2193 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002194 found_key.objectid = found_key.offset;
2195 found_key.type = BTRFS_INODE_ITEM_KEY;
2196 found_key.offset = 0;
2197 inode = btrfs_iget(root->fs_info->sb, &found_key, root);
2198 if (IS_ERR(inode))
Josef Bacik7b128762008-07-24 12:17:14 -04002199 break;
2200
Josef Bacik7b128762008-07-24 12:17:14 -04002201 /*
2202 * add this inode to the orphan list so btrfs_orphan_del does
2203 * the proper thing when we hit it
2204 */
Yanbcc63ab2008-07-30 16:29:20 -04002205 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002206 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04002207 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002208
2209 /*
2210 * if this is a bad inode, means we actually succeeded in
2211 * removing the inode, but not the orphan record, which means
2212 * we need to manually delete the orphan since iput will just
2213 * do a destroy_inode
2214 */
2215 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002216 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04002217 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002218 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002219 iput(inode);
2220 continue;
2221 }
2222
2223 /* if we have links, this was a truncate, lets do that */
2224 if (inode->i_nlink) {
2225 nr_truncate++;
2226 btrfs_truncate(inode);
2227 } else {
2228 nr_unlink++;
2229 }
2230
2231 /* this will do delete_inode and everything for us */
2232 iput(inode);
2233 }
2234
2235 if (nr_unlink)
2236 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2237 if (nr_truncate)
2238 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2239
2240 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002241}
2242
Chris Masond352ac62008-09-29 15:18:18 -04002243/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002244 * very simple check to peek ahead in the leaf looking for xattrs. If we
2245 * don't find any xattrs, we know there can't be any acls.
2246 *
2247 * slot is the slot the inode is in, objectid is the objectid of the inode
2248 */
2249static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2250 int slot, u64 objectid)
2251{
2252 u32 nritems = btrfs_header_nritems(leaf);
2253 struct btrfs_key found_key;
2254 int scanned = 0;
2255
2256 slot++;
2257 while (slot < nritems) {
2258 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2259
2260 /* we found a different objectid, there must not be acls */
2261 if (found_key.objectid != objectid)
2262 return 0;
2263
2264 /* we found an xattr, assume we've got an acl */
2265 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2266 return 1;
2267
2268 /*
2269 * we found a key greater than an xattr key, there can't
2270 * be any acls later on
2271 */
2272 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2273 return 0;
2274
2275 slot++;
2276 scanned++;
2277
2278 /*
2279 * it goes inode, inode backrefs, xattrs, extents,
2280 * so if there are a ton of hard links to an inode there can
2281 * be a lot of backrefs. Don't waste time searching too hard,
2282 * this is just an optimization
2283 */
2284 if (scanned >= 8)
2285 break;
2286 }
2287 /* we hit the end of the leaf before we found an xattr or
2288 * something larger than an xattr. We have to assume the inode
2289 * has acls
2290 */
2291 return 1;
2292}
2293
2294/*
Chris Masond352ac62008-09-29 15:18:18 -04002295 * read an inode from the btree into the in-memory inode
2296 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002297static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002298{
2299 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002300 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002301 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002302 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002303 struct btrfs_root *root = BTRFS_I(inode)->root;
2304 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002305 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002306 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002307 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002308 int ret;
2309
2310 path = btrfs_alloc_path();
2311 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002312 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002313
Chris Mason39279cc2007-06-12 06:35:45 -04002314 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002315 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002316 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002317
Chris Mason5f39d392007-10-15 16:14:19 -04002318 leaf = path->nodes[0];
2319 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2320 struct btrfs_inode_item);
2321
2322 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2323 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2324 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2325 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002326 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002327
2328 tspec = btrfs_inode_atime(inode_item);
2329 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2330 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2331
2332 tspec = btrfs_inode_mtime(inode_item);
2333 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2334 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2335
2336 tspec = btrfs_inode_ctime(inode_item);
2337 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2338 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2339
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002340 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002341 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002342 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002343 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002344 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002345 rdev = btrfs_inode_rdev(leaf, inode_item);
2346
Josef Bacikaec74772008-07-24 12:12:38 -04002347 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002348 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002349
Chris Mason5f39d392007-10-15 16:14:19 -04002350 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002351
Chris Mason46a53cc2009-04-27 11:47:50 -04002352 /*
2353 * try to precache a NULL acl entry for files that don't have
2354 * any xattrs or acls
2355 */
2356 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002357 if (!maybe_acls)
2358 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002359
Yan Zhengd2fb3432008-12-11 16:30:39 -05002360 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2361 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002362 btrfs_free_path(path);
2363 inode_item = NULL;
2364
Chris Mason39279cc2007-06-12 06:35:45 -04002365 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002366 case S_IFREG:
2367 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002368 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002369 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002370 inode->i_fop = &btrfs_file_operations;
2371 inode->i_op = &btrfs_file_inode_operations;
2372 break;
2373 case S_IFDIR:
2374 inode->i_fop = &btrfs_dir_file_operations;
2375 if (root == root->fs_info->tree_root)
2376 inode->i_op = &btrfs_dir_ro_inode_operations;
2377 else
2378 inode->i_op = &btrfs_dir_inode_operations;
2379 break;
2380 case S_IFLNK:
2381 inode->i_op = &btrfs_symlink_inode_operations;
2382 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002383 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002384 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002385 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002386 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002387 init_special_inode(inode, inode->i_mode, rdev);
2388 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002389 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002390
2391 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002392 return;
2393
2394make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002395 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002396 make_bad_inode(inode);
2397}
2398
Chris Masond352ac62008-09-29 15:18:18 -04002399/*
2400 * given a leaf and an inode, copy the inode fields into the leaf
2401 */
Chris Masone02119d2008-09-05 16:13:11 -04002402static void fill_inode_item(struct btrfs_trans_handle *trans,
2403 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002404 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002405 struct inode *inode)
2406{
Chris Mason5f39d392007-10-15 16:14:19 -04002407 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2408 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002409 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002410 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2411 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2412
2413 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2414 inode->i_atime.tv_sec);
2415 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2416 inode->i_atime.tv_nsec);
2417
2418 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2419 inode->i_mtime.tv_sec);
2420 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2421 inode->i_mtime.tv_nsec);
2422
2423 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2424 inode->i_ctime.tv_sec);
2425 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2426 inode->i_ctime.tv_nsec);
2427
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002428 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002429 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002430 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002431 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002432 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002433 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002434 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002435}
2436
Chris Masond352ac62008-09-29 15:18:18 -04002437/*
2438 * copy everything in the in-memory inode into the btree.
2439 */
Chris Masond3977122009-01-05 21:25:51 -05002440noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2441 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002442{
2443 struct btrfs_inode_item *inode_item;
2444 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002445 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002446 int ret;
2447
2448 path = btrfs_alloc_path();
2449 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002450 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002451 ret = btrfs_lookup_inode(trans, root, path,
2452 &BTRFS_I(inode)->location, 1);
2453 if (ret) {
2454 if (ret > 0)
2455 ret = -ENOENT;
2456 goto failed;
2457 }
2458
Chris Masonb4ce94d2009-02-04 09:25:08 -05002459 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002460 leaf = path->nodes[0];
2461 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002462 struct btrfs_inode_item);
2463
Chris Masone02119d2008-09-05 16:13:11 -04002464 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002465 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002466 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002467 ret = 0;
2468failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002469 btrfs_free_path(path);
2470 return ret;
2471}
2472
2473
Chris Masond352ac62008-09-29 15:18:18 -04002474/*
2475 * unlink helper that gets used here in inode.c and in the tree logging
2476 * recovery code. It remove a link in a directory with a given name, and
2477 * also drops the back refs in the inode to the directory
2478 */
Chris Masone02119d2008-09-05 16:13:11 -04002479int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2480 struct btrfs_root *root,
2481 struct inode *dir, struct inode *inode,
2482 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002483{
2484 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002485 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002486 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002487 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002488 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002489 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002490
2491 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002492 if (!path) {
2493 ret = -ENOMEM;
2494 goto err;
2495 }
2496
Chris Masonb9473432009-03-13 11:00:37 -04002497 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002498 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2499 name, name_len, -1);
2500 if (IS_ERR(di)) {
2501 ret = PTR_ERR(di);
2502 goto err;
2503 }
2504 if (!di) {
2505 ret = -ENOENT;
2506 goto err;
2507 }
Chris Mason5f39d392007-10-15 16:14:19 -04002508 leaf = path->nodes[0];
2509 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002510 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002511 if (ret)
2512 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002513 btrfs_release_path(root, path);
2514
Josef Bacikaec74772008-07-24 12:12:38 -04002515 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002516 inode->i_ino,
2517 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002518 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002519 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002520 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002521 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002522 goto err;
2523 }
2524
Chris Mason39279cc2007-06-12 06:35:45 -04002525 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002526 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002527 if (IS_ERR(di)) {
2528 ret = PTR_ERR(di);
2529 goto err;
2530 }
2531 if (!di) {
2532 ret = -ENOENT;
2533 goto err;
2534 }
2535 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002536 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002537
Chris Masone02119d2008-09-05 16:13:11 -04002538 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2539 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002540 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002541
2542 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2543 dir, index);
2544 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002545err:
2546 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002547 if (ret)
2548 goto out;
2549
2550 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2551 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2552 btrfs_update_inode(trans, root, dir);
2553 btrfs_drop_nlink(inode);
2554 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002555out:
Chris Mason39279cc2007-06-12 06:35:45 -04002556 return ret;
2557}
2558
2559static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2560{
2561 struct btrfs_root *root;
2562 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002563 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002564 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002565 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002566
2567 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002568
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002569 /*
2570 * 5 items for unlink inode
2571 * 1 for orphan
2572 */
2573 ret = btrfs_reserve_metadata_space(root, 6);
2574 if (ret)
2575 return ret;
2576
Chris Mason39279cc2007-06-12 06:35:45 -04002577 trans = btrfs_start_transaction(root, 1);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002578 if (IS_ERR(trans)) {
2579 btrfs_unreserve_metadata_space(root, 6);
2580 return PTR_ERR(trans);
2581 }
Chris Mason5f39d392007-10-15 16:14:19 -04002582
Chris Mason39279cc2007-06-12 06:35:45 -04002583 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002584
2585 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2586
Chris Masone02119d2008-09-05 16:13:11 -04002587 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2588 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002589
2590 if (inode->i_nlink == 0)
2591 ret = btrfs_orphan_add(trans, inode);
2592
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002593 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002594
Chris Mason89ce8a62008-06-25 16:01:31 -04002595 btrfs_end_transaction_throttle(trans, root);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002596 btrfs_unreserve_metadata_space(root, 6);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002597 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002598 return ret;
2599}
2600
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002601int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2602 struct btrfs_root *root,
2603 struct inode *dir, u64 objectid,
2604 const char *name, int name_len)
2605{
2606 struct btrfs_path *path;
2607 struct extent_buffer *leaf;
2608 struct btrfs_dir_item *di;
2609 struct btrfs_key key;
2610 u64 index;
2611 int ret;
2612
2613 path = btrfs_alloc_path();
2614 if (!path)
2615 return -ENOMEM;
2616
2617 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2618 name, name_len, -1);
2619 BUG_ON(!di || IS_ERR(di));
2620
2621 leaf = path->nodes[0];
2622 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2623 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2624 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2625 BUG_ON(ret);
2626 btrfs_release_path(root, path);
2627
2628 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2629 objectid, root->root_key.objectid,
2630 dir->i_ino, &index, name, name_len);
2631 if (ret < 0) {
2632 BUG_ON(ret != -ENOENT);
2633 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2634 name, name_len);
2635 BUG_ON(!di || IS_ERR(di));
2636
2637 leaf = path->nodes[0];
2638 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2639 btrfs_release_path(root, path);
2640 index = key.offset;
2641 }
2642
2643 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2644 index, name, name_len, -1);
2645 BUG_ON(!di || IS_ERR(di));
2646
2647 leaf = path->nodes[0];
2648 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2649 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2650 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2651 BUG_ON(ret);
2652 btrfs_release_path(root, path);
2653
2654 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2655 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2656 ret = btrfs_update_inode(trans, root, dir);
2657 BUG_ON(ret);
2658 dir->i_sb->s_dirt = 1;
2659
2660 btrfs_free_path(path);
2661 return 0;
2662}
2663
Chris Mason39279cc2007-06-12 06:35:45 -04002664static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2665{
2666 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002667 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002668 int ret;
2669 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002670 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002671 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002672
Chris Mason3394e162008-11-17 20:42:26 -05002673 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002674 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002675 return -ENOTEMPTY;
2676
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002677 ret = btrfs_reserve_metadata_space(root, 5);
2678 if (ret)
2679 return ret;
2680
Chris Mason39279cc2007-06-12 06:35:45 -04002681 trans = btrfs_start_transaction(root, 1);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002682 if (IS_ERR(trans)) {
2683 btrfs_unreserve_metadata_space(root, 5);
2684 return PTR_ERR(trans);
2685 }
2686
Chris Mason39279cc2007-06-12 06:35:45 -04002687 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002688
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002689 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2690 err = btrfs_unlink_subvol(trans, root, dir,
2691 BTRFS_I(inode)->location.objectid,
2692 dentry->d_name.name,
2693 dentry->d_name.len);
2694 goto out;
2695 }
2696
Josef Bacik7b128762008-07-24 12:17:14 -04002697 err = btrfs_orphan_add(trans, inode);
2698 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002699 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002700
Chris Mason39279cc2007-06-12 06:35:45 -04002701 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002702 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2703 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002704 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002705 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002706out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002707 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002708 ret = btrfs_end_transaction_throttle(trans, root);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002709 btrfs_unreserve_metadata_space(root, 5);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002710 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002711
Chris Mason39279cc2007-06-12 06:35:45 -04002712 if (ret && !err)
2713 err = ret;
2714 return err;
2715}
2716
Chris Masond20f7042008-12-08 16:58:54 -05002717#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002718/*
Chris Mason323ac952008-10-01 19:05:46 -04002719 * when truncating bytes in a file, it is possible to avoid reading
2720 * the leaves that contain only checksum items. This can be the
2721 * majority of the IO required to delete a large file, but it must
2722 * be done carefully.
2723 *
2724 * The keys in the level just above the leaves are checked to make sure
2725 * the lowest key in a given leaf is a csum key, and starts at an offset
2726 * after the new size.
2727 *
2728 * Then the key for the next leaf is checked to make sure it also has
2729 * a checksum item for the same file. If it does, we know our target leaf
2730 * contains only checksum items, and it can be safely freed without reading
2731 * it.
2732 *
2733 * This is just an optimization targeted at large files. It may do
2734 * nothing. It will return 0 unless things went badly.
2735 */
2736static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2737 struct btrfs_root *root,
2738 struct btrfs_path *path,
2739 struct inode *inode, u64 new_size)
2740{
2741 struct btrfs_key key;
2742 int ret;
2743 int nritems;
2744 struct btrfs_key found_key;
2745 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002746 struct btrfs_leaf_ref *ref;
2747 u64 leaf_gen;
2748 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002749
2750 path->lowest_level = 1;
2751 key.objectid = inode->i_ino;
2752 key.type = BTRFS_CSUM_ITEM_KEY;
2753 key.offset = new_size;
2754again:
2755 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2756 if (ret < 0)
2757 goto out;
2758
2759 if (path->nodes[1] == NULL) {
2760 ret = 0;
2761 goto out;
2762 }
2763 ret = 0;
2764 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2765 nritems = btrfs_header_nritems(path->nodes[1]);
2766
2767 if (!nritems)
2768 goto out;
2769
2770 if (path->slots[1] >= nritems)
2771 goto next_node;
2772
2773 /* did we find a key greater than anything we want to delete? */
2774 if (found_key.objectid > inode->i_ino ||
2775 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2776 goto out;
2777
2778 /* we check the next key in the node to make sure the leave contains
2779 * only checksum items. This comparison doesn't work if our
2780 * leaf is the last one in the node
2781 */
2782 if (path->slots[1] + 1 >= nritems) {
2783next_node:
2784 /* search forward from the last key in the node, this
2785 * will bring us into the next node in the tree
2786 */
2787 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2788
2789 /* unlikely, but we inc below, so check to be safe */
2790 if (found_key.offset == (u64)-1)
2791 goto out;
2792
2793 /* search_forward needs a path with locks held, do the
2794 * search again for the original key. It is possible
2795 * this will race with a balance and return a path that
2796 * we could modify, but this drop is just an optimization
2797 * and is allowed to miss some leaves.
2798 */
2799 btrfs_release_path(root, path);
2800 found_key.offset++;
2801
2802 /* setup a max key for search_forward */
2803 other_key.offset = (u64)-1;
2804 other_key.type = key.type;
2805 other_key.objectid = key.objectid;
2806
2807 path->keep_locks = 1;
2808 ret = btrfs_search_forward(root, &found_key, &other_key,
2809 path, 0, 0);
2810 path->keep_locks = 0;
2811 if (ret || found_key.objectid != key.objectid ||
2812 found_key.type != key.type) {
2813 ret = 0;
2814 goto out;
2815 }
2816
2817 key.offset = found_key.offset;
2818 btrfs_release_path(root, path);
2819 cond_resched();
2820 goto again;
2821 }
2822
2823 /* we know there's one more slot after us in the tree,
2824 * read that key so we can verify it is also a checksum item
2825 */
2826 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2827
2828 if (found_key.objectid < inode->i_ino)
2829 goto next_key;
2830
2831 if (found_key.type != key.type || found_key.offset < new_size)
2832 goto next_key;
2833
2834 /*
2835 * if the key for the next leaf isn't a csum key from this objectid,
2836 * we can't be sure there aren't good items inside this leaf.
2837 * Bail out
2838 */
2839 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2840 goto out;
2841
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002842 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2843 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002844 /*
2845 * it is safe to delete this leaf, it contains only
2846 * csum items from this inode at an offset >= new_size
2847 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002848 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002849 BUG_ON(ret);
2850
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002851 if (root->ref_cows && leaf_gen < trans->transid) {
2852 ref = btrfs_alloc_leaf_ref(root, 0);
2853 if (ref) {
2854 ref->root_gen = root->root_key.offset;
2855 ref->bytenr = leaf_start;
2856 ref->owner = 0;
2857 ref->generation = leaf_gen;
2858 ref->nritems = 0;
2859
Chris Masonbd56b302009-02-04 09:27:02 -05002860 btrfs_sort_leaf_ref(ref);
2861
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002862 ret = btrfs_add_leaf_ref(root, ref, 0);
2863 WARN_ON(ret);
2864 btrfs_free_leaf_ref(root, ref);
2865 } else {
2866 WARN_ON(1);
2867 }
2868 }
Chris Mason323ac952008-10-01 19:05:46 -04002869next_key:
2870 btrfs_release_path(root, path);
2871
2872 if (other_key.objectid == inode->i_ino &&
2873 other_key.type == key.type && other_key.offset > key.offset) {
2874 key.offset = other_key.offset;
2875 cond_resched();
2876 goto again;
2877 }
2878 ret = 0;
2879out:
2880 /* fixup any changes we've made to the path */
2881 path->lowest_level = 0;
2882 path->keep_locks = 0;
2883 btrfs_release_path(root, path);
2884 return ret;
2885}
2886
Chris Masond20f7042008-12-08 16:58:54 -05002887#endif
2888
Chris Mason323ac952008-10-01 19:05:46 -04002889/*
Chris Mason39279cc2007-06-12 06:35:45 -04002890 * this can truncate away extent items, csum items and directory items.
2891 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002892 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002893 *
2894 * csum items that cross the new i_size are truncated to the new size
2895 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002896 *
2897 * min_type is the minimum key type to truncate down to. If set to 0, this
2898 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002899 */
Yan, Zheng80825102009-11-12 09:35:36 +00002900int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2901 struct btrfs_root *root,
2902 struct inode *inode,
2903 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002904{
Chris Mason39279cc2007-06-12 06:35:45 -04002905 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002906 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002907 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00002908 struct btrfs_key key;
2909 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002910 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002911 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002912 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002913 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00002914 u64 mask = root->sectorsize - 1;
2915 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04002916 int found_extent;
2917 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002918 int pending_del_nr = 0;
2919 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002920 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002921 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00002922 int ret;
2923 int err = 0;
2924
2925 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002926
Chris Masone02119d2008-09-05 16:13:11 -04002927 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002928 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00002929
Chris Mason39279cc2007-06-12 06:35:45 -04002930 path = btrfs_alloc_path();
2931 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04002932 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04002933
Chris Mason39279cc2007-06-12 06:35:45 -04002934 key.objectid = inode->i_ino;
2935 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002936 key.type = (u8)-1;
2937
Chris Mason85e21ba2008-01-29 15:11:36 -05002938search_again:
Chris Masonb9473432009-03-13 11:00:37 -04002939 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05002940 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00002941 if (ret < 0) {
2942 err = ret;
2943 goto out;
2944 }
Chris Masond3977122009-01-05 21:25:51 -05002945
Chris Mason85e21ba2008-01-29 15:11:36 -05002946 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002947 /* there are no items in the tree for us to truncate, we're
2948 * done
2949 */
Yan, Zheng80825102009-11-12 09:35:36 +00002950 if (path->slots[0] == 0)
2951 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05002952 path->slots[0]--;
2953 }
2954
Chris Masond3977122009-01-05 21:25:51 -05002955 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002956 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002957 leaf = path->nodes[0];
2958 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2959 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002960 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002961
Chris Mason5f39d392007-10-15 16:14:19 -04002962 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002963 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002964
Chris Mason85e21ba2008-01-29 15:11:36 -05002965 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002966 break;
2967
Chris Mason5f39d392007-10-15 16:14:19 -04002968 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002969 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002970 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002971 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002972 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002973 encoding = btrfs_file_extent_compression(leaf, fi);
2974 encoding |= btrfs_file_extent_encryption(leaf, fi);
2975 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2976
Chris Mason179e29e2007-11-01 11:28:41 -04002977 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002978 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002979 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002980 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002981 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002982 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002983 }
Yan008630c2007-11-07 13:31:09 -05002984 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002985 }
Yan, Zheng80825102009-11-12 09:35:36 +00002986 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04002987 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00002988 } else {
2989 if (item_end < new_size)
2990 break;
2991 if (found_key.offset >= new_size)
2992 del_item = 1;
2993 else
2994 del_item = 0;
2995 }
Chris Mason39279cc2007-06-12 06:35:45 -04002996 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002997 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002998 if (found_type != BTRFS_EXTENT_DATA_KEY)
2999 goto delete;
3000
3001 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003002 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003003 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003004 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003005 u64 orig_num_bytes =
3006 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003007 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003008 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003009 extent_num_bytes = extent_num_bytes &
3010 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003011 btrfs_set_file_extent_num_bytes(leaf, fi,
3012 extent_num_bytes);
3013 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003014 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003015 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003016 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003017 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003018 } else {
Chris Masondb945352007-10-15 16:15:53 -04003019 extent_num_bytes =
3020 btrfs_file_extent_disk_num_bytes(leaf,
3021 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003022 extent_offset = found_key.offset -
3023 btrfs_file_extent_offset(leaf, fi);
3024
Chris Mason39279cc2007-06-12 06:35:45 -04003025 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003026 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003027 if (extent_start != 0) {
3028 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003029 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003030 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003031 }
3032 }
Chris Mason90692182008-02-08 13:49:28 -05003033 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003034 /*
3035 * we can't truncate inline items that have had
3036 * special encodings
3037 */
3038 if (!del_item &&
3039 btrfs_file_extent_compression(leaf, fi) == 0 &&
3040 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3041 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003042 u32 size = new_size - found_key.offset;
3043
3044 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003045 inode_sub_bytes(inode, item_end + 1 -
3046 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003047 }
3048 size =
3049 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003050 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003051 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003052 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003053 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003054 inode_sub_bytes(inode, item_end + 1 -
3055 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003056 }
Chris Mason39279cc2007-06-12 06:35:45 -04003057 }
Chris Mason179e29e2007-11-01 11:28:41 -04003058delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003059 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003060 if (!pending_del_nr) {
3061 /* no pending yet, add ourselves */
3062 pending_del_slot = path->slots[0];
3063 pending_del_nr = 1;
3064 } else if (pending_del_nr &&
3065 path->slots[0] + 1 == pending_del_slot) {
3066 /* hop on the pending chunk */
3067 pending_del_nr++;
3068 pending_del_slot = path->slots[0];
3069 } else {
Chris Masond3977122009-01-05 21:25:51 -05003070 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003071 }
Chris Mason39279cc2007-06-12 06:35:45 -04003072 } else {
3073 break;
3074 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003075 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04003076 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003077 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003078 extent_num_bytes, 0,
3079 btrfs_header_owner(leaf),
3080 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003081 BUG_ON(ret);
3082 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003083
Yan, Zheng80825102009-11-12 09:35:36 +00003084 if (found_type == BTRFS_INODE_ITEM_KEY)
3085 break;
3086
3087 if (path->slots[0] == 0 ||
3088 path->slots[0] != pending_del_slot) {
3089 if (root->ref_cows) {
3090 err = -EAGAIN;
3091 goto out;
3092 }
3093 if (pending_del_nr) {
3094 ret = btrfs_del_items(trans, root, path,
3095 pending_del_slot,
3096 pending_del_nr);
3097 BUG_ON(ret);
3098 pending_del_nr = 0;
3099 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003100 btrfs_release_path(root, path);
3101 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003102 } else {
3103 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003104 }
Chris Mason39279cc2007-06-12 06:35:45 -04003105 }
Yan, Zheng80825102009-11-12 09:35:36 +00003106out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003107 if (pending_del_nr) {
3108 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3109 pending_del_nr);
3110 }
Chris Mason39279cc2007-06-12 06:35:45 -04003111 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003112 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003113}
3114
Chris Masona52d9a82007-08-27 16:49:44 -04003115/*
3116 * taken from block_truncate_page, but does cow as it zeros out
3117 * any bytes left in the last page in the file.
3118 */
3119static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3120{
3121 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003122 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003123 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3124 struct btrfs_ordered_extent *ordered;
3125 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003126 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003127 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3128 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3129 struct page *page;
3130 int ret = 0;
3131 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003132 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003133
3134 if ((offset & (blocksize - 1)) == 0)
3135 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003136 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
3137 if (ret)
3138 goto out;
3139
3140 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
3141 if (ret)
3142 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003143
3144 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003145again:
Chris Masona52d9a82007-08-27 16:49:44 -04003146 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003147 if (!page) {
3148 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3149 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04003150 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003151 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003152
3153 page_start = page_offset(page);
3154 page_end = page_start + PAGE_CACHE_SIZE - 1;
3155
Chris Masona52d9a82007-08-27 16:49:44 -04003156 if (!PageUptodate(page)) {
3157 ret = btrfs_readpage(NULL, page);
3158 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003159 if (page->mapping != mapping) {
3160 unlock_page(page);
3161 page_cache_release(page);
3162 goto again;
3163 }
Chris Masona52d9a82007-08-27 16:49:44 -04003164 if (!PageUptodate(page)) {
3165 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003166 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003167 }
3168 }
Chris Mason211c17f2008-05-15 09:13:45 -04003169 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003170
3171 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3172 set_page_extent_mapped(page);
3173
3174 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3175 if (ordered) {
3176 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3177 unlock_page(page);
3178 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003179 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003180 btrfs_put_ordered_extent(ordered);
3181 goto again;
3182 }
3183
Josef Bacik5d5e1032009-10-13 16:46:49 -04003184 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
3185 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3186 GFP_NOFS);
3187
Josef Bacik9ed74f22009-09-11 16:12:44 -04003188 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3189 if (ret) {
3190 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3191 goto out_unlock;
3192 }
3193
Chris Masone6dcd2d2008-07-17 12:53:50 -04003194 ret = 0;
3195 if (offset != PAGE_CACHE_SIZE) {
3196 kaddr = kmap(page);
3197 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3198 flush_dcache_page(page);
3199 kunmap(page);
3200 }
Chris Mason247e7432008-07-17 12:53:51 -04003201 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003202 set_page_dirty(page);
3203 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003204
Chris Mason89642222008-07-24 09:41:53 -04003205out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003206 if (ret)
3207 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3208 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003209 unlock_page(page);
3210 page_cache_release(page);
3211out:
3212 return ret;
3213}
3214
Yan Zheng9036c102008-10-30 14:19:41 -04003215int btrfs_cont_expand(struct inode *inode, loff_t size)
3216{
3217 struct btrfs_trans_handle *trans;
3218 struct btrfs_root *root = BTRFS_I(inode)->root;
3219 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3220 struct extent_map *em;
3221 u64 mask = root->sectorsize - 1;
3222 u64 hole_start = (inode->i_size + mask) & ~mask;
3223 u64 block_end = (size + mask) & ~mask;
3224 u64 last_byte;
3225 u64 cur_offset;
3226 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003227 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003228
3229 if (size <= hole_start)
3230 return 0;
3231
Yan Zheng9036c102008-10-30 14:19:41 -04003232 while (1) {
3233 struct btrfs_ordered_extent *ordered;
3234 btrfs_wait_ordered_range(inode, hole_start,
3235 block_end - hole_start);
3236 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3237 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3238 if (!ordered)
3239 break;
3240 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3241 btrfs_put_ordered_extent(ordered);
3242 }
3243
Yan Zheng9036c102008-10-30 14:19:41 -04003244 cur_offset = hole_start;
3245 while (1) {
3246 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3247 block_end - cur_offset, 0);
3248 BUG_ON(IS_ERR(em) || !em);
3249 last_byte = min(extent_map_end(em), block_end);
3250 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003251 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003252 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003253 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003254
3255 err = btrfs_reserve_metadata_space(root, 2);
Chris Mason771ed682008-11-06 22:02:51 -05003256 if (err)
3257 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003258
Yan, Zheng80825102009-11-12 09:35:36 +00003259 trans = btrfs_start_transaction(root, 1);
3260 btrfs_set_trans_block_group(trans, inode);
3261
3262 err = btrfs_drop_extents(trans, inode, cur_offset,
3263 cur_offset + hole_size,
3264 &hint_byte, 1);
3265 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003266
Yan Zheng9036c102008-10-30 14:19:41 -04003267 err = btrfs_insert_file_extent(trans, root,
3268 inode->i_ino, cur_offset, 0,
3269 0, hole_size, 0, hole_size,
3270 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003271 BUG_ON(err);
3272
Yan Zheng9036c102008-10-30 14:19:41 -04003273 btrfs_drop_extent_cache(inode, hole_start,
3274 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003275
3276 btrfs_end_transaction(trans, root);
3277 btrfs_unreserve_metadata_space(root, 2);
Yan Zheng9036c102008-10-30 14:19:41 -04003278 }
3279 free_extent_map(em);
3280 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003281 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003282 break;
3283 }
3284
Yan Zheng9036c102008-10-30 14:19:41 -04003285 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3286 return err;
3287}
3288
Yan, Zheng80825102009-11-12 09:35:36 +00003289static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3290{
3291 struct btrfs_root *root = BTRFS_I(inode)->root;
3292 struct btrfs_trans_handle *trans;
3293 unsigned long nr;
3294 int ret;
3295
3296 if (attr->ia_size == inode->i_size)
3297 return 0;
3298
3299 if (attr->ia_size > inode->i_size) {
3300 unsigned long limit;
3301 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3302 if (attr->ia_size > inode->i_sb->s_maxbytes)
3303 return -EFBIG;
3304 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3305 send_sig(SIGXFSZ, current, 0);
3306 return -EFBIG;
3307 }
3308 }
3309
3310 ret = btrfs_reserve_metadata_space(root, 1);
3311 if (ret)
3312 return ret;
3313
3314 trans = btrfs_start_transaction(root, 1);
3315 btrfs_set_trans_block_group(trans, inode);
3316
3317 ret = btrfs_orphan_add(trans, inode);
3318 BUG_ON(ret);
3319
3320 nr = trans->blocks_used;
3321 btrfs_end_transaction(trans, root);
3322 btrfs_unreserve_metadata_space(root, 1);
3323 btrfs_btree_balance_dirty(root, nr);
3324
3325 if (attr->ia_size > inode->i_size) {
3326 ret = btrfs_cont_expand(inode, attr->ia_size);
3327 if (ret) {
3328 btrfs_truncate(inode);
3329 return ret;
3330 }
3331
3332 i_size_write(inode, attr->ia_size);
3333 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3334
3335 trans = btrfs_start_transaction(root, 1);
3336 btrfs_set_trans_block_group(trans, inode);
3337
3338 ret = btrfs_update_inode(trans, root, inode);
3339 BUG_ON(ret);
3340 if (inode->i_nlink > 0) {
3341 ret = btrfs_orphan_del(trans, inode);
3342 BUG_ON(ret);
3343 }
3344 nr = trans->blocks_used;
3345 btrfs_end_transaction(trans, root);
3346 btrfs_btree_balance_dirty(root, nr);
3347 return 0;
3348 }
3349
3350 /*
3351 * We're truncating a file that used to have good data down to
3352 * zero. Make sure it gets into the ordered flush list so that
3353 * any new writes get down to disk quickly.
3354 */
3355 if (attr->ia_size == 0)
3356 BTRFS_I(inode)->ordered_data_close = 1;
3357
3358 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3359 ret = vmtruncate(inode, attr->ia_size);
3360 BUG_ON(ret);
3361
3362 return 0;
3363}
3364
Chris Mason39279cc2007-06-12 06:35:45 -04003365static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3366{
3367 struct inode *inode = dentry->d_inode;
3368 int err;
3369
3370 err = inode_change_ok(inode, attr);
3371 if (err)
3372 return err;
3373
Chris Mason5a3f23d2009-03-31 13:27:11 -04003374 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003375 err = btrfs_setattr_size(inode, attr);
3376 if (err)
3377 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003378 }
Yan, Zheng80825102009-11-12 09:35:36 +00003379 attr->ia_valid &= ~ATTR_SIZE;
Yan Zheng9036c102008-10-30 14:19:41 -04003380
Yan, Zheng80825102009-11-12 09:35:36 +00003381 if (attr->ia_valid)
3382 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003383
3384 if (!err && ((attr->ia_valid & ATTR_MODE)))
3385 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003386 return err;
3387}
Chris Mason61295eb2008-01-14 16:24:38 -05003388
Chris Mason39279cc2007-06-12 06:35:45 -04003389void btrfs_delete_inode(struct inode *inode)
3390{
3391 struct btrfs_trans_handle *trans;
3392 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003393 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003394 int ret;
3395
3396 truncate_inode_pages(&inode->i_data, 0);
3397 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003398 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003399 goto no_delete;
3400 }
Chris Mason4a096752008-07-21 10:29:44 -04003401 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003402
Yan, Zhengc71bf092009-11-12 09:34:40 +00003403 if (root->fs_info->log_root_recovering) {
3404 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3405 goto no_delete;
3406 }
3407
Yan, Zheng76dda932009-09-21 16:00:26 -04003408 if (inode->i_nlink > 0) {
3409 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3410 goto no_delete;
3411 }
3412
Chris Masondbe674a2008-07-17 12:54:05 -04003413 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003414
Yan, Zheng80825102009-11-12 09:35:36 +00003415 while (1) {
3416 trans = btrfs_start_transaction(root, 1);
3417 btrfs_set_trans_block_group(trans, inode);
3418 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3419
3420 if (ret != -EAGAIN)
3421 break;
3422
3423 nr = trans->blocks_used;
3424 btrfs_end_transaction(trans, root);
3425 trans = NULL;
3426 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003427 }
3428
Yan, Zheng80825102009-11-12 09:35:36 +00003429 if (ret == 0) {
3430 ret = btrfs_orphan_del(trans, inode);
3431 BUG_ON(ret);
3432 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003433
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003434 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003435 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003436 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003437no_delete:
3438 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003439 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003440}
3441
3442/*
3443 * this returns the key found in the dir entry in the location pointer.
3444 * If no dir entries were found, location->objectid is 0.
3445 */
3446static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3447 struct btrfs_key *location)
3448{
3449 const char *name = dentry->d_name.name;
3450 int namelen = dentry->d_name.len;
3451 struct btrfs_dir_item *di;
3452 struct btrfs_path *path;
3453 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003454 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003455
3456 path = btrfs_alloc_path();
3457 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003458
Chris Mason39279cc2007-06-12 06:35:45 -04003459 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3460 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003461 if (IS_ERR(di))
3462 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003463
3464 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003465 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003466
Chris Mason5f39d392007-10-15 16:14:19 -04003467 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003468out:
Chris Mason39279cc2007-06-12 06:35:45 -04003469 btrfs_free_path(path);
3470 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003471out_err:
3472 location->objectid = 0;
3473 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003474}
3475
3476/*
3477 * when we hit a tree root in a directory, the btrfs part of the inode
3478 * needs to be changed to reflect the root directory of the tree root. This
3479 * is kind of like crossing a mount point.
3480 */
3481static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003482 struct inode *dir,
3483 struct dentry *dentry,
3484 struct btrfs_key *location,
3485 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003486{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003487 struct btrfs_path *path;
3488 struct btrfs_root *new_root;
3489 struct btrfs_root_ref *ref;
3490 struct extent_buffer *leaf;
3491 int ret;
3492 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003493
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003494 path = btrfs_alloc_path();
3495 if (!path) {
3496 err = -ENOMEM;
3497 goto out;
3498 }
Chris Mason39279cc2007-06-12 06:35:45 -04003499
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003500 err = -ENOENT;
3501 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3502 BTRFS_I(dir)->root->root_key.objectid,
3503 location->objectid);
3504 if (ret) {
3505 if (ret < 0)
3506 err = ret;
3507 goto out;
3508 }
Chris Mason39279cc2007-06-12 06:35:45 -04003509
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003510 leaf = path->nodes[0];
3511 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3512 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3513 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3514 goto out;
3515
3516 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3517 (unsigned long)(ref + 1),
3518 dentry->d_name.len);
3519 if (ret)
3520 goto out;
3521
3522 btrfs_release_path(root->fs_info->tree_root, path);
3523
3524 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3525 if (IS_ERR(new_root)) {
3526 err = PTR_ERR(new_root);
3527 goto out;
3528 }
3529
3530 if (btrfs_root_refs(&new_root->root_item) == 0) {
3531 err = -ENOENT;
3532 goto out;
3533 }
3534
3535 *sub_root = new_root;
3536 location->objectid = btrfs_root_dirid(&new_root->root_item);
3537 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003538 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003539 err = 0;
3540out:
3541 btrfs_free_path(path);
3542 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003543}
3544
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003545static void inode_tree_add(struct inode *inode)
3546{
3547 struct btrfs_root *root = BTRFS_I(inode)->root;
3548 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003549 struct rb_node **p;
3550 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003551again:
3552 p = &root->inode_tree.rb_node;
3553 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003554
Yan, Zheng76dda932009-09-21 16:00:26 -04003555 if (hlist_unhashed(&inode->i_hash))
3556 return;
3557
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003558 spin_lock(&root->inode_lock);
3559 while (*p) {
3560 parent = *p;
3561 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3562
3563 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003564 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003565 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003566 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003567 else {
3568 WARN_ON(!(entry->vfs_inode.i_state &
3569 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003570 rb_erase(parent, &root->inode_tree);
3571 RB_CLEAR_NODE(parent);
3572 spin_unlock(&root->inode_lock);
3573 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003574 }
3575 }
3576 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3577 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3578 spin_unlock(&root->inode_lock);
3579}
3580
3581static void inode_tree_del(struct inode *inode)
3582{
3583 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003584 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003585
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003586 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003587 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003588 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003589 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003590 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003591 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003592 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003593
3594 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3595 synchronize_srcu(&root->fs_info->subvol_srcu);
3596 spin_lock(&root->inode_lock);
3597 empty = RB_EMPTY_ROOT(&root->inode_tree);
3598 spin_unlock(&root->inode_lock);
3599 if (empty)
3600 btrfs_add_dead_root(root);
3601 }
3602}
3603
3604int btrfs_invalidate_inodes(struct btrfs_root *root)
3605{
3606 struct rb_node *node;
3607 struct rb_node *prev;
3608 struct btrfs_inode *entry;
3609 struct inode *inode;
3610 u64 objectid = 0;
3611
3612 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3613
3614 spin_lock(&root->inode_lock);
3615again:
3616 node = root->inode_tree.rb_node;
3617 prev = NULL;
3618 while (node) {
3619 prev = node;
3620 entry = rb_entry(node, struct btrfs_inode, rb_node);
3621
3622 if (objectid < entry->vfs_inode.i_ino)
3623 node = node->rb_left;
3624 else if (objectid > entry->vfs_inode.i_ino)
3625 node = node->rb_right;
3626 else
3627 break;
3628 }
3629 if (!node) {
3630 while (prev) {
3631 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3632 if (objectid <= entry->vfs_inode.i_ino) {
3633 node = prev;
3634 break;
3635 }
3636 prev = rb_next(prev);
3637 }
3638 }
3639 while (node) {
3640 entry = rb_entry(node, struct btrfs_inode, rb_node);
3641 objectid = entry->vfs_inode.i_ino + 1;
3642 inode = igrab(&entry->vfs_inode);
3643 if (inode) {
3644 spin_unlock(&root->inode_lock);
3645 if (atomic_read(&inode->i_count) > 1)
3646 d_prune_aliases(inode);
3647 /*
3648 * btrfs_drop_inode will remove it from
3649 * the inode cache when its usage count
3650 * hits zero.
3651 */
3652 iput(inode);
3653 cond_resched();
3654 spin_lock(&root->inode_lock);
3655 goto again;
3656 }
3657
3658 if (cond_resched_lock(&root->inode_lock))
3659 goto again;
3660
3661 node = rb_next(node);
3662 }
3663 spin_unlock(&root->inode_lock);
3664 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003665}
3666
Chris Masone02119d2008-09-05 16:13:11 -04003667static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003668{
Chris Masone02119d2008-09-05 16:13:11 -04003669 struct btrfs_inode *bi = BTRFS_I(inode);
3670
Chris Masone02119d2008-09-05 16:13:11 -04003671 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05003672 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003673 bi->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04003674 bi->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003675 bi->logged_trans = 0;
3676 bi->delalloc_bytes = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003677 bi->reserved_bytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003678 bi->disk_i_size = 0;
3679 bi->flags = 0;
3680 bi->index_cnt = (u64)-1;
Chris Mason12fcfd22009-03-24 10:24:20 -04003681 bi->last_unlink_trans = 0;
Chris Mason27574952009-05-14 13:10:02 -04003682 bi->ordered_data_close = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003683 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3684 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04003685 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003686 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3687 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003688 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04003689 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003690 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Chris Masonba1da2f2008-07-17 12:54:15 -04003691 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masone02119d2008-09-05 16:13:11 -04003692 mutex_init(&BTRFS_I(inode)->log_mutex);
3693}
3694
3695static int btrfs_init_locked_inode(struct inode *inode, void *p)
3696{
3697 struct btrfs_iget_args *args = p;
3698 inode->i_ino = args->ino;
3699 init_btrfs_i(inode);
3700 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003701 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003702 return 0;
3703}
3704
3705static int btrfs_find_actor(struct inode *inode, void *opaque)
3706{
3707 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003708 return args->ino == inode->i_ino &&
3709 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003710}
3711
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003712static struct inode *btrfs_iget_locked(struct super_block *s,
3713 u64 objectid,
3714 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003715{
3716 struct inode *inode;
3717 struct btrfs_iget_args args;
3718 args.ino = objectid;
3719 args.root = root;
3720
3721 inode = iget5_locked(s, objectid, btrfs_find_actor,
3722 btrfs_init_locked_inode,
3723 (void *)&args);
3724 return inode;
3725}
3726
Balaji Rao1a54ef82008-07-21 02:01:04 +05303727/* Get an inode object given its location and corresponding root.
3728 * Returns in *is_new if the inode was read from disk
3729 */
3730struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003731 struct btrfs_root *root)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303732{
3733 struct inode *inode;
3734
3735 inode = btrfs_iget_locked(s, location->objectid, root);
3736 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003737 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303738
3739 if (inode->i_state & I_NEW) {
3740 BTRFS_I(inode)->root = root;
3741 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3742 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003743
3744 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303745 unlock_new_inode(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303746 }
3747
3748 return inode;
3749}
3750
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003751static struct inode *new_simple_dir(struct super_block *s,
3752 struct btrfs_key *key,
3753 struct btrfs_root *root)
3754{
3755 struct inode *inode = new_inode(s);
3756
3757 if (!inode)
3758 return ERR_PTR(-ENOMEM);
3759
3760 init_btrfs_i(inode);
3761
3762 BTRFS_I(inode)->root = root;
3763 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3764 BTRFS_I(inode)->dummy_inode = 1;
3765
3766 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3767 inode->i_op = &simple_dir_inode_operations;
3768 inode->i_fop = &simple_dir_operations;
3769 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3770 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3771
3772 return inode;
3773}
3774
Chris Mason3de45862008-11-17 21:02:50 -05003775struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003776{
Chris Masond3977122009-01-05 21:25:51 -05003777 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003778 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003779 struct btrfs_root *sub_root = root;
3780 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003781 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003782 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003783
Yan, Zheng76dda932009-09-21 16:00:26 -04003784 dentry->d_op = &btrfs_dentry_operations;
3785
Chris Mason39279cc2007-06-12 06:35:45 -04003786 if (dentry->d_name.len > BTRFS_NAME_LEN)
3787 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003788
Chris Mason39279cc2007-06-12 06:35:45 -04003789 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003790
Chris Mason39279cc2007-06-12 06:35:45 -04003791 if (ret < 0)
3792 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003793
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003794 if (location.objectid == 0)
3795 return NULL;
3796
3797 if (location.type == BTRFS_INODE_ITEM_KEY) {
3798 inode = btrfs_iget(dir->i_sb, &location, root);
3799 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003800 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003801
3802 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3803
Yan, Zheng76dda932009-09-21 16:00:26 -04003804 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003805 ret = fixup_tree_root_location(root, dir, dentry,
3806 &location, &sub_root);
3807 if (ret < 0) {
3808 if (ret != -ENOENT)
3809 inode = ERR_PTR(ret);
3810 else
3811 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3812 } else {
Chris Mason39279cc2007-06-12 06:35:45 -04003813 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04003814 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003815 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3816
Yan, Zhengc71bf092009-11-12 09:34:40 +00003817 if (root != sub_root) {
3818 down_read(&root->fs_info->cleanup_work_sem);
3819 if (!(inode->i_sb->s_flags & MS_RDONLY))
3820 btrfs_orphan_cleanup(sub_root);
3821 up_read(&root->fs_info->cleanup_work_sem);
3822 }
3823
Chris Mason3de45862008-11-17 21:02:50 -05003824 return inode;
3825}
3826
Yan, Zheng76dda932009-09-21 16:00:26 -04003827static int btrfs_dentry_delete(struct dentry *dentry)
3828{
3829 struct btrfs_root *root;
3830
Yan, Zhengefefb142009-10-09 09:25:16 -04003831 if (!dentry->d_inode && !IS_ROOT(dentry))
3832 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04003833
Yan, Zhengefefb142009-10-09 09:25:16 -04003834 if (dentry->d_inode) {
3835 root = BTRFS_I(dentry->d_inode)->root;
3836 if (btrfs_root_refs(&root->root_item) == 0)
3837 return 1;
3838 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003839 return 0;
3840}
3841
Chris Mason3de45862008-11-17 21:02:50 -05003842static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3843 struct nameidata *nd)
3844{
3845 struct inode *inode;
3846
Chris Mason3de45862008-11-17 21:02:50 -05003847 inode = btrfs_lookup_dentry(dir, dentry);
3848 if (IS_ERR(inode))
3849 return ERR_CAST(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003850
Chris Mason39279cc2007-06-12 06:35:45 -04003851 return d_splice_alias(inode, dentry);
3852}
3853
3854static unsigned char btrfs_filetype_table[] = {
3855 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3856};
3857
David Woodhousecbdf5a22008-08-06 19:42:33 +01003858static int btrfs_real_readdir(struct file *filp, void *dirent,
3859 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003860{
3861 struct inode *inode = filp->f_dentry->d_inode;
3862 struct btrfs_root *root = BTRFS_I(inode)->root;
3863 struct btrfs_item *item;
3864 struct btrfs_dir_item *di;
3865 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003866 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003867 struct btrfs_path *path;
3868 int ret;
3869 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003870 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003871 int slot;
3872 int advance;
3873 unsigned char d_type;
3874 int over = 0;
3875 u32 di_cur;
3876 u32 di_total;
3877 u32 di_len;
3878 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003879 char tmp_name[32];
3880 char *name_ptr;
3881 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003882
3883 /* FIXME, use a real flag for deciding about the key type */
3884 if (root->fs_info->tree_root == root)
3885 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003886
Chris Mason39544012007-12-12 14:38:19 -05003887 /* special case for "." */
3888 if (filp->f_pos == 0) {
3889 over = filldir(dirent, ".", 1,
3890 1, inode->i_ino,
3891 DT_DIR);
3892 if (over)
3893 return 0;
3894 filp->f_pos = 1;
3895 }
Chris Mason39544012007-12-12 14:38:19 -05003896 /* special case for .., just use the back ref */
3897 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003898 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003899 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003900 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003901 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003902 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003903 filp->f_pos = 2;
3904 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003905 path = btrfs_alloc_path();
3906 path->reada = 2;
3907
Chris Mason39279cc2007-06-12 06:35:45 -04003908 btrfs_set_key_type(&key, key_type);
3909 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003910 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003911
Chris Mason39279cc2007-06-12 06:35:45 -04003912 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3913 if (ret < 0)
3914 goto err;
3915 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003916
3917 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003918 leaf = path->nodes[0];
3919 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003920 slot = path->slots[0];
3921 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003922 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003923 ret = btrfs_next_leaf(root, path);
3924 if (ret)
3925 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003926 leaf = path->nodes[0];
3927 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003928 slot = path->slots[0];
3929 } else {
3930 slot++;
3931 path->slots[0]++;
3932 }
3933 }
Chris Mason3de45862008-11-17 21:02:50 -05003934
Chris Mason39279cc2007-06-12 06:35:45 -04003935 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003936 item = btrfs_item_nr(leaf, slot);
3937 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3938
3939 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003940 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003941 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003942 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003943 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003944 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003945
3946 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003947
Chris Mason39279cc2007-06-12 06:35:45 -04003948 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3949 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003950 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003951
3952 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003953 struct btrfs_key location;
3954
3955 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003956 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003957 name_ptr = tmp_name;
3958 } else {
3959 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003960 if (!name_ptr) {
3961 ret = -ENOMEM;
3962 goto err;
3963 }
Chris Mason5f39d392007-10-15 16:14:19 -04003964 }
3965 read_extent_buffer(leaf, name_ptr,
3966 (unsigned long)(di + 1), name_len);
3967
3968 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3969 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003970
3971 /* is this a reference to our own snapshot? If so
3972 * skip it
3973 */
3974 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3975 location.objectid == root->root_key.objectid) {
3976 over = 0;
3977 goto skip;
3978 }
Chris Mason5f39d392007-10-15 16:14:19 -04003979 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003980 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003981 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003982
Chris Mason3de45862008-11-17 21:02:50 -05003983skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003984 if (name_ptr != tmp_name)
3985 kfree(name_ptr);
3986
Chris Mason39279cc2007-06-12 06:35:45 -04003987 if (over)
3988 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003989 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003990 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003991 di_cur += di_len;
3992 di = (struct btrfs_dir_item *)((char *)di + di_len);
3993 }
3994 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003995
3996 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003997 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00003998 /*
3999 * 32-bit glibc will use getdents64, but then strtol -
4000 * so the last number we can serve is this.
4001 */
4002 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004003 else
4004 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004005nopos:
4006 ret = 0;
4007err:
Chris Mason39279cc2007-06-12 06:35:45 -04004008 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004009 return ret;
4010}
4011
4012int btrfs_write_inode(struct inode *inode, int wait)
4013{
4014 struct btrfs_root *root = BTRFS_I(inode)->root;
4015 struct btrfs_trans_handle *trans;
4016 int ret = 0;
4017
Yan Zhengc146afa2008-11-12 14:34:12 -05004018 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004019 return 0;
4020
Chris Mason39279cc2007-06-12 06:35:45 -04004021 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04004022 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004023 btrfs_set_trans_block_group(trans, inode);
4024 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004025 }
4026 return ret;
4027}
4028
4029/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004030 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004031 * inode changes. But, it is most likely to find the inode in cache.
4032 * FIXME, needs more benchmarking...there are no reasons other than performance
4033 * to keep or drop this code.
4034 */
4035void btrfs_dirty_inode(struct inode *inode)
4036{
4037 struct btrfs_root *root = BTRFS_I(inode)->root;
4038 struct btrfs_trans_handle *trans;
4039
Chris Masonf9295742008-07-17 12:54:14 -04004040 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004041 btrfs_set_trans_block_group(trans, inode);
4042 btrfs_update_inode(trans, root, inode);
4043 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004044}
4045
Chris Masond352ac62008-09-29 15:18:18 -04004046/*
4047 * find the highest existing sequence number in a directory
4048 * and then set the in-memory index_cnt variable to reflect
4049 * free sequence numbers
4050 */
Josef Bacikaec74772008-07-24 12:12:38 -04004051static int btrfs_set_inode_index_count(struct inode *inode)
4052{
4053 struct btrfs_root *root = BTRFS_I(inode)->root;
4054 struct btrfs_key key, found_key;
4055 struct btrfs_path *path;
4056 struct extent_buffer *leaf;
4057 int ret;
4058
4059 key.objectid = inode->i_ino;
4060 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4061 key.offset = (u64)-1;
4062
4063 path = btrfs_alloc_path();
4064 if (!path)
4065 return -ENOMEM;
4066
4067 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4068 if (ret < 0)
4069 goto out;
4070 /* FIXME: we should be able to handle this */
4071 if (ret == 0)
4072 goto out;
4073 ret = 0;
4074
4075 /*
4076 * MAGIC NUMBER EXPLANATION:
4077 * since we search a directory based on f_pos we have to start at 2
4078 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4079 * else has to start at 2
4080 */
4081 if (path->slots[0] == 0) {
4082 BTRFS_I(inode)->index_cnt = 2;
4083 goto out;
4084 }
4085
4086 path->slots[0]--;
4087
4088 leaf = path->nodes[0];
4089 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4090
4091 if (found_key.objectid != inode->i_ino ||
4092 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4093 BTRFS_I(inode)->index_cnt = 2;
4094 goto out;
4095 }
4096
4097 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4098out:
4099 btrfs_free_path(path);
4100 return ret;
4101}
4102
Chris Masond352ac62008-09-29 15:18:18 -04004103/*
4104 * helper to find a free sequence number in a given directory. This current
4105 * code is very simple, later versions will do smarter things in the btree
4106 */
Chris Mason3de45862008-11-17 21:02:50 -05004107int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004108{
4109 int ret = 0;
4110
4111 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4112 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004113 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004114 return ret;
4115 }
4116
Chris Mason00e4e6b2008-08-05 11:18:09 -04004117 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004118 BTRFS_I(dir)->index_cnt++;
4119
4120 return ret;
4121}
4122
Chris Mason39279cc2007-06-12 06:35:45 -04004123static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4124 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004125 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004126 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004127 u64 ref_objectid, u64 objectid,
4128 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004129{
4130 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004131 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004132 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004133 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004134 struct btrfs_inode_ref *ref;
4135 struct btrfs_key key[2];
4136 u32 sizes[2];
4137 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004138 int ret;
4139 int owner;
4140
Chris Mason5f39d392007-10-15 16:14:19 -04004141 path = btrfs_alloc_path();
4142 BUG_ON(!path);
4143
Chris Mason39279cc2007-06-12 06:35:45 -04004144 inode = new_inode(root->fs_info->sb);
4145 if (!inode)
4146 return ERR_PTR(-ENOMEM);
4147
Josef Bacikaec74772008-07-24 12:12:38 -04004148 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004149 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004150 if (ret) {
4151 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004152 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004153 }
Josef Bacikaec74772008-07-24 12:12:38 -04004154 }
4155 /*
4156 * index_cnt is ignored for everything but a dir,
4157 * btrfs_get_inode_index_count has an explanation for the magic
4158 * number
4159 */
Chris Masone02119d2008-09-05 16:13:11 -04004160 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004161 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004162 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004163 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05004164 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004165
Chris Mason39279cc2007-06-12 06:35:45 -04004166 if (mode & S_IFDIR)
4167 owner = 0;
4168 else
4169 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004170 BTRFS_I(inode)->block_group =
4171 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004172
4173 key[0].objectid = objectid;
4174 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4175 key[0].offset = 0;
4176
4177 key[1].objectid = objectid;
4178 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4179 key[1].offset = ref_objectid;
4180
4181 sizes[0] = sizeof(struct btrfs_inode_item);
4182 sizes[1] = name_len + sizeof(*ref);
4183
Chris Masonb9473432009-03-13 11:00:37 -04004184 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004185 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4186 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004187 goto fail;
4188
Chris Mason79683f22008-11-19 22:00:53 -05004189 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05004190
Chris Mason42f15d72009-02-06 11:35:57 -05004191 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05004192 inode->i_gid = dir->i_gid;
4193 if (S_ISDIR(mode))
4194 mode |= S_ISGID;
4195 } else
4196 inode->i_gid = current_fsgid();
4197
Chris Mason39279cc2007-06-12 06:35:45 -04004198 inode->i_mode = mode;
4199 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004200 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004201 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004202 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4203 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004204 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004205
4206 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4207 struct btrfs_inode_ref);
4208 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004209 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004210 ptr = (unsigned long)(ref + 1);
4211 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4212
Chris Mason5f39d392007-10-15 16:14:19 -04004213 btrfs_mark_buffer_dirty(path->nodes[0]);
4214 btrfs_free_path(path);
4215
Chris Mason39279cc2007-06-12 06:35:45 -04004216 location = &BTRFS_I(inode)->location;
4217 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004218 location->offset = 0;
4219 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4220
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004221 btrfs_inherit_iflags(inode, dir);
4222
Chris Mason94272162009-07-02 12:26:06 -04004223 if ((mode & S_IFREG)) {
4224 if (btrfs_test_opt(root, NODATASUM))
4225 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4226 if (btrfs_test_opt(root, NODATACOW))
4227 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4228 }
4229
Chris Mason39279cc2007-06-12 06:35:45 -04004230 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004231 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004232 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004233fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004234 if (dir)
4235 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004236 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004237 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004238 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004239}
4240
4241static inline u8 btrfs_inode_type(struct inode *inode)
4242{
4243 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4244}
4245
Chris Masond352ac62008-09-29 15:18:18 -04004246/*
4247 * utility function to add 'inode' into 'parent_inode' with
4248 * a give name and a given sequence number.
4249 * if 'add_backref' is true, also insert a backref from the
4250 * inode to the parent directory.
4251 */
Chris Masone02119d2008-09-05 16:13:11 -04004252int btrfs_add_link(struct btrfs_trans_handle *trans,
4253 struct inode *parent_inode, struct inode *inode,
4254 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004255{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004256 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004257 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004258 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004259
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004260 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4261 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4262 } else {
4263 key.objectid = inode->i_ino;
4264 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4265 key.offset = 0;
4266 }
Chris Mason39279cc2007-06-12 06:35:45 -04004267
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004268 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4269 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4270 key.objectid, root->root_key.objectid,
4271 parent_inode->i_ino,
4272 index, name, name_len);
4273 } else if (add_backref) {
4274 ret = btrfs_insert_inode_ref(trans, root,
4275 name, name_len, inode->i_ino,
4276 parent_inode->i_ino, index);
4277 }
4278
Chris Mason39279cc2007-06-12 06:35:45 -04004279 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004280 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4281 parent_inode->i_ino, &key,
4282 btrfs_inode_type(inode), index);
4283 BUG_ON(ret);
4284
Chris Masondbe674a2008-07-17 12:54:05 -04004285 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004286 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004287 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004288 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004289 }
4290 return ret;
4291}
4292
4293static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004294 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004295 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004296{
Chris Masone02119d2008-09-05 16:13:11 -04004297 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4298 inode, dentry->d_name.name,
4299 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004300 if (!err) {
4301 d_instantiate(dentry, inode);
4302 return 0;
4303 }
4304 if (err > 0)
4305 err = -EEXIST;
4306 return err;
4307}
4308
Josef Bacik618e21d2007-07-11 10:18:17 -04004309static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4310 int mode, dev_t rdev)
4311{
4312 struct btrfs_trans_handle *trans;
4313 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004314 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004315 int err;
4316 int drop_inode = 0;
4317 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004318 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004319 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004320
4321 if (!new_valid_dev(rdev))
4322 return -EINVAL;
4323
Josef Bacik9ed74f22009-09-11 16:12:44 -04004324 /*
4325 * 2 for inode item and ref
4326 * 2 for dir items
4327 * 1 for xattr if selinux is on
4328 */
4329 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004330 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004331 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004332
Josef Bacik618e21d2007-07-11 10:18:17 -04004333 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004334 if (!trans)
4335 goto fail;
Josef Bacik618e21d2007-07-11 10:18:17 -04004336 btrfs_set_trans_block_group(trans, dir);
4337
4338 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4339 if (err) {
4340 err = -ENOSPC;
4341 goto out_unlock;
4342 }
4343
Josef Bacikaec74772008-07-24 12:12:38 -04004344 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004345 dentry->d_name.len,
4346 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004347 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004348 err = PTR_ERR(inode);
4349 if (IS_ERR(inode))
4350 goto out_unlock;
4351
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004352 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004353 if (err) {
4354 drop_inode = 1;
4355 goto out_unlock;
4356 }
4357
Josef Bacik618e21d2007-07-11 10:18:17 -04004358 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004359 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004360 if (err)
4361 drop_inode = 1;
4362 else {
4363 inode->i_op = &btrfs_special_inode_operations;
4364 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004365 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004366 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004367 btrfs_update_inode_block_group(trans, inode);
4368 btrfs_update_inode_block_group(trans, dir);
4369out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004370 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004371 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004372fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004373 btrfs_unreserve_metadata_space(root, 5);
Josef Bacik618e21d2007-07-11 10:18:17 -04004374 if (drop_inode) {
4375 inode_dec_link_count(inode);
4376 iput(inode);
4377 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004378 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004379 return err;
4380}
4381
Chris Mason39279cc2007-06-12 06:35:45 -04004382static int btrfs_create(struct inode *dir, struct dentry *dentry,
4383 int mode, struct nameidata *nd)
4384{
4385 struct btrfs_trans_handle *trans;
4386 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004387 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004388 int err;
4389 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05004390 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004391 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004392 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004393
Josef Bacik9ed74f22009-09-11 16:12:44 -04004394 /*
4395 * 2 for inode item and ref
4396 * 2 for dir items
4397 * 1 for xattr if selinux is on
4398 */
4399 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004400 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004401 return err;
4402
Chris Mason39279cc2007-06-12 06:35:45 -04004403 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004404 if (!trans)
4405 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04004406 btrfs_set_trans_block_group(trans, dir);
4407
4408 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4409 if (err) {
4410 err = -ENOSPC;
4411 goto out_unlock;
4412 }
4413
Josef Bacikaec74772008-07-24 12:12:38 -04004414 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004415 dentry->d_name.len,
4416 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004417 objectid, BTRFS_I(dir)->block_group, mode,
4418 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004419 err = PTR_ERR(inode);
4420 if (IS_ERR(inode))
4421 goto out_unlock;
4422
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004423 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004424 if (err) {
4425 drop_inode = 1;
4426 goto out_unlock;
4427 }
4428
Chris Mason39279cc2007-06-12 06:35:45 -04004429 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004430 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004431 if (err)
4432 drop_inode = 1;
4433 else {
4434 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004435 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004436 inode->i_fop = &btrfs_file_operations;
4437 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004438 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004439 }
Chris Mason39279cc2007-06-12 06:35:45 -04004440 btrfs_update_inode_block_group(trans, inode);
4441 btrfs_update_inode_block_group(trans, dir);
4442out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004443 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004444 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004445fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004446 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004447 if (drop_inode) {
4448 inode_dec_link_count(inode);
4449 iput(inode);
4450 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004451 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004452 return err;
4453}
4454
4455static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4456 struct dentry *dentry)
4457{
4458 struct btrfs_trans_handle *trans;
4459 struct btrfs_root *root = BTRFS_I(dir)->root;
4460 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004461 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004462 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004463 int err;
4464 int drop_inode = 0;
4465
4466 if (inode->i_nlink == 0)
4467 return -ENOENT;
4468
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004469 /* do not allow sys_link's with other subvols of the same device */
4470 if (root->objectid != BTRFS_I(inode)->root->objectid)
4471 return -EPERM;
4472
Josef Bacik9ed74f22009-09-11 16:12:44 -04004473 /*
4474 * 1 item for inode ref
4475 * 2 items for dir items
4476 */
4477 err = btrfs_reserve_metadata_space(root, 3);
Chris Mason1832a6d2007-12-21 16:27:21 -05004478 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004479 return err;
4480
4481 btrfs_inc_nlink(inode);
4482
Chris Mason3de45862008-11-17 21:02:50 -05004483 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004484 if (err)
4485 goto fail;
4486
Chris Mason39279cc2007-06-12 06:35:45 -04004487 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004488
Chris Mason39279cc2007-06-12 06:35:45 -04004489 btrfs_set_trans_block_group(trans, dir);
4490 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004491
Chris Mason00e4e6b2008-08-05 11:18:09 -04004492 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004493
Yan, Zhenga5719522009-09-24 09:17:31 -04004494 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004495 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004496 } else {
4497 btrfs_update_inode_block_group(trans, dir);
4498 err = btrfs_update_inode(trans, root, inode);
4499 BUG_ON(err);
4500 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4501 }
Chris Mason39279cc2007-06-12 06:35:45 -04004502
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004503 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004504 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004505fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004506 btrfs_unreserve_metadata_space(root, 3);
Chris Mason39279cc2007-06-12 06:35:45 -04004507 if (drop_inode) {
4508 inode_dec_link_count(inode);
4509 iput(inode);
4510 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004511 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004512 return err;
4513}
4514
Chris Mason39279cc2007-06-12 06:35:45 -04004515static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4516{
Chris Masonb9d86662008-05-02 16:13:49 -04004517 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004518 struct btrfs_trans_handle *trans;
4519 struct btrfs_root *root = BTRFS_I(dir)->root;
4520 int err = 0;
4521 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004522 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004523 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004524 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004525
Josef Bacik9ed74f22009-09-11 16:12:44 -04004526 /*
4527 * 2 items for inode and ref
4528 * 2 items for dir items
4529 * 1 for xattr if selinux is on
4530 */
4531 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004532 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004533 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004534
Chris Mason39279cc2007-06-12 06:35:45 -04004535 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004536 if (!trans) {
4537 err = -ENOMEM;
Chris Mason39279cc2007-06-12 06:35:45 -04004538 goto out_unlock;
4539 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004540 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004541
4542 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4543 if (err) {
4544 err = -ENOSPC;
4545 goto out_unlock;
4546 }
4547
Josef Bacikaec74772008-07-24 12:12:38 -04004548 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004549 dentry->d_name.len,
4550 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004551 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4552 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004553 if (IS_ERR(inode)) {
4554 err = PTR_ERR(inode);
4555 goto out_fail;
4556 }
Chris Mason5f39d392007-10-15 16:14:19 -04004557
Chris Mason39279cc2007-06-12 06:35:45 -04004558 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004559
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004560 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004561 if (err)
4562 goto out_fail;
4563
Chris Mason39279cc2007-06-12 06:35:45 -04004564 inode->i_op = &btrfs_dir_inode_operations;
4565 inode->i_fop = &btrfs_dir_file_operations;
4566 btrfs_set_trans_block_group(trans, inode);
4567
Chris Masondbe674a2008-07-17 12:54:05 -04004568 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004569 err = btrfs_update_inode(trans, root, inode);
4570 if (err)
4571 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004572
Chris Masone02119d2008-09-05 16:13:11 -04004573 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4574 inode, dentry->d_name.name,
4575 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004576 if (err)
4577 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004578
Chris Mason39279cc2007-06-12 06:35:45 -04004579 d_instantiate(dentry, inode);
4580 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004581 btrfs_update_inode_block_group(trans, inode);
4582 btrfs_update_inode_block_group(trans, dir);
4583
4584out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004585 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004586 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04004587
Chris Mason39279cc2007-06-12 06:35:45 -04004588out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004589 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004590 if (drop_on_err)
4591 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004592 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004593 return err;
4594}
4595
Chris Masond352ac62008-09-29 15:18:18 -04004596/* helper for btfs_get_extent. Given an existing extent in the tree,
4597 * and an extent that you want to insert, deal with overlap and insert
4598 * the new extent into the tree.
4599 */
Chris Mason3b951512008-04-17 11:29:12 -04004600static int merge_extent_mapping(struct extent_map_tree *em_tree,
4601 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004602 struct extent_map *em,
4603 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004604{
4605 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004606
Chris Masone6dcd2d2008-07-17 12:53:50 -04004607 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4608 start_diff = map_start - em->start;
4609 em->start = map_start;
4610 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004611 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4612 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004613 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004614 em->block_len -= start_diff;
4615 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004616 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004617}
4618
Chris Masonc8b97812008-10-29 14:49:59 -04004619static noinline int uncompress_inline(struct btrfs_path *path,
4620 struct inode *inode, struct page *page,
4621 size_t pg_offset, u64 extent_offset,
4622 struct btrfs_file_extent_item *item)
4623{
4624 int ret;
4625 struct extent_buffer *leaf = path->nodes[0];
4626 char *tmp;
4627 size_t max_size;
4628 unsigned long inline_size;
4629 unsigned long ptr;
4630
4631 WARN_ON(pg_offset != 0);
4632 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4633 inline_size = btrfs_file_extent_inline_item_len(leaf,
4634 btrfs_item_nr(leaf, path->slots[0]));
4635 tmp = kmalloc(inline_size, GFP_NOFS);
4636 ptr = btrfs_file_extent_inline_start(item);
4637
4638 read_extent_buffer(leaf, tmp, ptr, inline_size);
4639
Chris Mason5b050f02008-11-11 09:34:41 -05004640 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004641 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4642 inline_size, max_size);
4643 if (ret) {
4644 char *kaddr = kmap_atomic(page, KM_USER0);
4645 unsigned long copy_size = min_t(u64,
4646 PAGE_CACHE_SIZE - pg_offset,
4647 max_size - extent_offset);
4648 memset(kaddr + pg_offset, 0, copy_size);
4649 kunmap_atomic(kaddr, KM_USER0);
4650 }
4651 kfree(tmp);
4652 return 0;
4653}
4654
Chris Masond352ac62008-09-29 15:18:18 -04004655/*
4656 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004657 * the ugly parts come from merging extents from the disk with the in-ram
4658 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004659 * where the in-ram extents might be locked pending data=ordered completion.
4660 *
4661 * This also copies inline extents directly into the page.
4662 */
Chris Masond3977122009-01-05 21:25:51 -05004663
Chris Masona52d9a82007-08-27 16:49:44 -04004664struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004665 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004666 int create)
4667{
4668 int ret;
4669 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004670 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004671 u64 extent_start = 0;
4672 u64 extent_end = 0;
4673 u64 objectid = inode->i_ino;
4674 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004675 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004676 struct btrfs_root *root = BTRFS_I(inode)->root;
4677 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004678 struct extent_buffer *leaf;
4679 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004680 struct extent_map *em = NULL;
4681 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004682 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004683 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004684 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004685
Chris Masona52d9a82007-08-27 16:49:44 -04004686again:
Chris Mason890871b2009-09-02 16:24:52 -04004687 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004688 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004689 if (em)
4690 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004691 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004692
Chris Masona52d9a82007-08-27 16:49:44 -04004693 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004694 if (em->start > start || em->start + em->len <= start)
4695 free_extent_map(em);
4696 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004697 free_extent_map(em);
4698 else
4699 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004700 }
Chris Masond1310b22008-01-24 16:13:08 -05004701 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004702 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004703 err = -ENOMEM;
4704 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004705 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004706 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004707 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004708 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004709 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004710 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004711
4712 if (!path) {
4713 path = btrfs_alloc_path();
4714 BUG_ON(!path);
4715 }
4716
Chris Mason179e29e2007-11-01 11:28:41 -04004717 ret = btrfs_lookup_file_extent(trans, root, path,
4718 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004719 if (ret < 0) {
4720 err = ret;
4721 goto out;
4722 }
4723
4724 if (ret != 0) {
4725 if (path->slots[0] == 0)
4726 goto not_found;
4727 path->slots[0]--;
4728 }
4729
Chris Mason5f39d392007-10-15 16:14:19 -04004730 leaf = path->nodes[0];
4731 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004732 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004733 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004734 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4735 found_type = btrfs_key_type(&found_key);
4736 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004737 found_type != BTRFS_EXTENT_DATA_KEY) {
4738 goto not_found;
4739 }
4740
Chris Mason5f39d392007-10-15 16:14:19 -04004741 found_type = btrfs_file_extent_type(leaf, item);
4742 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004743 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004744 if (found_type == BTRFS_FILE_EXTENT_REG ||
4745 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004746 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004747 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004748 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4749 size_t size;
4750 size = btrfs_file_extent_inline_len(leaf, item);
4751 extent_end = (extent_start + size + root->sectorsize - 1) &
4752 ~((u64)root->sectorsize - 1);
4753 }
4754
4755 if (start >= extent_end) {
4756 path->slots[0]++;
4757 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4758 ret = btrfs_next_leaf(root, path);
4759 if (ret < 0) {
4760 err = ret;
4761 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004762 }
Yan Zheng9036c102008-10-30 14:19:41 -04004763 if (ret > 0)
4764 goto not_found;
4765 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004766 }
Yan Zheng9036c102008-10-30 14:19:41 -04004767 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4768 if (found_key.objectid != objectid ||
4769 found_key.type != BTRFS_EXTENT_DATA_KEY)
4770 goto not_found;
4771 if (start + len <= found_key.offset)
4772 goto not_found;
4773 em->start = start;
4774 em->len = found_key.offset - start;
4775 goto not_found_em;
4776 }
4777
Yan Zhengd899e052008-10-30 14:25:28 -04004778 if (found_type == BTRFS_FILE_EXTENT_REG ||
4779 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004780 em->start = extent_start;
4781 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004782 em->orig_start = extent_start -
4783 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004784 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4785 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004786 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004787 goto insert;
4788 }
Chris Masonc8b97812008-10-29 14:49:59 -04004789 if (compressed) {
4790 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4791 em->block_start = bytenr;
4792 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4793 item);
4794 } else {
4795 bytenr += btrfs_file_extent_offset(leaf, item);
4796 em->block_start = bytenr;
4797 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004798 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4799 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004800 }
Chris Masona52d9a82007-08-27 16:49:44 -04004801 goto insert;
4802 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004803 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004804 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004805 size_t size;
4806 size_t extent_offset;
4807 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004808
Chris Masona52d9a82007-08-27 16:49:44 -04004809 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004810 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004811 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004812 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004813 goto out;
4814 }
4815
Yan Zheng9036c102008-10-30 14:19:41 -04004816 size = btrfs_file_extent_inline_len(leaf, item);
4817 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004818 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004819 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004820 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004821 em->len = (copy_size + root->sectorsize - 1) &
4822 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004823 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004824 if (compressed)
4825 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004826 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004827 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004828 if (btrfs_file_extent_compression(leaf, item) ==
4829 BTRFS_COMPRESS_ZLIB) {
4830 ret = uncompress_inline(path, inode, page,
4831 pg_offset,
4832 extent_offset, item);
4833 BUG_ON(ret);
4834 } else {
4835 map = kmap(page);
4836 read_extent_buffer(leaf, map + pg_offset, ptr,
4837 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04004838 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4839 memset(map + pg_offset + copy_size, 0,
4840 PAGE_CACHE_SIZE - pg_offset -
4841 copy_size);
4842 }
Chris Masonc8b97812008-10-29 14:49:59 -04004843 kunmap(page);
4844 }
Chris Mason179e29e2007-11-01 11:28:41 -04004845 flush_dcache_page(page);
4846 } else if (create && PageUptodate(page)) {
4847 if (!trans) {
4848 kunmap(page);
4849 free_extent_map(em);
4850 em = NULL;
4851 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004852 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004853 goto again;
4854 }
Chris Masonc8b97812008-10-29 14:49:59 -04004855 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004856 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004857 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004858 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004859 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004860 }
Chris Masond1310b22008-01-24 16:13:08 -05004861 set_extent_uptodate(io_tree, em->start,
4862 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004863 goto insert;
4864 } else {
Chris Masond3977122009-01-05 21:25:51 -05004865 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004866 WARN_ON(1);
4867 }
4868not_found:
4869 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004870 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004871not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004872 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004873 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004874insert:
4875 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004876 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004877 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4878 "[%llu %llu]\n", (unsigned long long)em->start,
4879 (unsigned long long)em->len,
4880 (unsigned long long)start,
4881 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004882 err = -EIO;
4883 goto out;
4884 }
Chris Masond1310b22008-01-24 16:13:08 -05004885
4886 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04004887 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004888 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004889 /* it is possible that someone inserted the extent into the tree
4890 * while we had the lock dropped. It is also possible that
4891 * an overlapping map exists in the tree
4892 */
Chris Masona52d9a82007-08-27 16:49:44 -04004893 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004894 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004895
4896 ret = 0;
4897
Chris Mason3b951512008-04-17 11:29:12 -04004898 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004899 if (existing && (existing->start > start ||
4900 existing->start + existing->len <= start)) {
4901 free_extent_map(existing);
4902 existing = NULL;
4903 }
Chris Mason3b951512008-04-17 11:29:12 -04004904 if (!existing) {
4905 existing = lookup_extent_mapping(em_tree, em->start,
4906 em->len);
4907 if (existing) {
4908 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004909 em, start,
4910 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004911 free_extent_map(existing);
4912 if (err) {
4913 free_extent_map(em);
4914 em = NULL;
4915 }
4916 } else {
4917 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004918 free_extent_map(em);
4919 em = NULL;
4920 }
4921 } else {
4922 free_extent_map(em);
4923 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004924 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004925 }
Chris Masona52d9a82007-08-27 16:49:44 -04004926 }
Chris Mason890871b2009-09-02 16:24:52 -04004927 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004928out:
Chris Masonf4219502008-07-22 11:18:09 -04004929 if (path)
4930 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004931 if (trans) {
4932 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004933 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004934 err = ret;
4935 }
Chris Masona52d9a82007-08-27 16:49:44 -04004936 if (err) {
4937 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04004938 return ERR_PTR(err);
4939 }
4940 return em;
4941}
4942
Chris Mason16432982008-04-10 10:23:21 -04004943static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4944 const struct iovec *iov, loff_t offset,
4945 unsigned long nr_segs)
4946{
Chris Masone1c4b742008-04-22 13:26:46 -04004947 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004948}
4949
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004950static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4951 __u64 start, __u64 len)
4952{
4953 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4954}
4955
Chris Mason9ebefb182007-06-15 13:50:00 -04004956int btrfs_readpage(struct file *file, struct page *page)
4957{
Chris Masond1310b22008-01-24 16:13:08 -05004958 struct extent_io_tree *tree;
4959 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004960 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004961}
Chris Mason1832a6d2007-12-21 16:27:21 -05004962
Chris Mason39279cc2007-06-12 06:35:45 -04004963static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4964{
Chris Masond1310b22008-01-24 16:13:08 -05004965 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004966
4967
4968 if (current->flags & PF_MEMALLOC) {
4969 redirty_page_for_writepage(wbc, page);
4970 unlock_page(page);
4971 return 0;
4972 }
Chris Masond1310b22008-01-24 16:13:08 -05004973 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004974 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4975}
Chris Mason39279cc2007-06-12 06:35:45 -04004976
Chris Masonf4219502008-07-22 11:18:09 -04004977int btrfs_writepages(struct address_space *mapping,
4978 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004979{
Chris Masond1310b22008-01-24 16:13:08 -05004980 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004981
Chris Masond1310b22008-01-24 16:13:08 -05004982 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004983 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4984}
4985
Chris Mason3ab2fb52007-11-08 10:59:22 -05004986static int
4987btrfs_readpages(struct file *file, struct address_space *mapping,
4988 struct list_head *pages, unsigned nr_pages)
4989{
Chris Masond1310b22008-01-24 16:13:08 -05004990 struct extent_io_tree *tree;
4991 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004992 return extent_readpages(tree, mapping, pages, nr_pages,
4993 btrfs_get_extent);
4994}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004995static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004996{
Chris Masond1310b22008-01-24 16:13:08 -05004997 struct extent_io_tree *tree;
4998 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004999 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005000
Chris Masond1310b22008-01-24 16:13:08 -05005001 tree = &BTRFS_I(page->mapping->host)->io_tree;
5002 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05005003 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005004 if (ret == 1) {
5005 ClearPagePrivate(page);
5006 set_page_private(page, 0);
5007 page_cache_release(page);
5008 }
5009 return ret;
5010}
Chris Mason39279cc2007-06-12 06:35:45 -04005011
Chris Masone6dcd2d2008-07-17 12:53:50 -04005012static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
5013{
Chris Mason98509cf2008-09-11 15:51:43 -04005014 if (PageWriteback(page) || PageDirty(page))
5015 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05005016 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005017}
5018
Chris Masona52d9a82007-08-27 16:49:44 -04005019static void btrfs_invalidatepage(struct page *page, unsigned long offset)
5020{
Chris Masond1310b22008-01-24 16:13:08 -05005021 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005022 struct btrfs_ordered_extent *ordered;
5023 u64 page_start = page_offset(page);
5024 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005025
Chris Mason8b62b722009-09-02 16:53:46 -04005026
5027 /*
5028 * we have the page locked, so new writeback can't start,
5029 * and the dirty bit won't be cleared while we are here.
5030 *
5031 * Wait for IO on this page so that we can safely clear
5032 * the PagePrivate2 bit and do ordered accounting
5033 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005034 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04005035
Chris Masond1310b22008-01-24 16:13:08 -05005036 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005037 if (offset) {
5038 btrfs_releasepage(page, GFP_NOFS);
5039 return;
5040 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005041 lock_extent(tree, page_start, page_end, GFP_NOFS);
5042 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
5043 page_offset(page));
5044 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04005045 /*
5046 * IO on this page will never be started, so we need
5047 * to account for any ordered extents now
5048 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005049 clear_extent_bit(tree, page_start, page_end,
5050 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04005051 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
5052 NULL, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04005053 /*
5054 * whoever cleared the private bit is responsible
5055 * for the finish_ordered_io
5056 */
5057 if (TestClearPagePrivate2(page)) {
5058 btrfs_finish_ordered_io(page->mapping->host,
5059 page_start, page_end);
5060 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005061 btrfs_put_ordered_extent(ordered);
5062 lock_extent(tree, page_start, page_end, GFP_NOFS);
5063 }
5064 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005065 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
5066 EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005067 __btrfs_releasepage(page, GFP_NOFS);
5068
Chris Mason4a096752008-07-21 10:29:44 -04005069 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005070 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005071 ClearPagePrivate(page);
5072 set_page_private(page, 0);
5073 page_cache_release(page);
5074 }
Chris Mason39279cc2007-06-12 06:35:45 -04005075}
5076
Chris Mason9ebefb182007-06-15 13:50:00 -04005077/*
5078 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
5079 * called from a page fault handler when a page is first dirtied. Hence we must
5080 * be careful to check for EOF conditions here. We set the page up correctly
5081 * for a written page which means we get ENOSPC checking when writing into
5082 * holes and correct delalloc and unwritten extent mapping on filesystems that
5083 * support these features.
5084 *
5085 * We are not allowed to take the i_mutex here so we have to play games to
5086 * protect against truncate races as the page could now be beyond EOF. Because
5087 * vmtruncate() writes the inode size before removing pages, once we have the
5088 * page lock we can determine safely if the page is beyond EOF. If it is not
5089 * beyond EOF, then the page is guaranteed safe against truncation until we
5090 * unlock the page.
5091 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07005092int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04005093{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005094 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05005095 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05005096 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005097 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5098 struct btrfs_ordered_extent *ordered;
5099 char *kaddr;
5100 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04005101 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05005102 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04005103 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005104 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04005105
Josef Bacik6a632092009-02-20 11:00:09 -05005106 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07005107 if (ret) {
5108 if (ret == -ENOMEM)
5109 ret = VM_FAULT_OOM;
5110 else /* -ENOSPC, -EIO, etc */
5111 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05005112 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07005113 }
Chris Mason1832a6d2007-12-21 16:27:21 -05005114
Josef Bacik9ed74f22009-09-11 16:12:44 -04005115 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
5116 if (ret) {
5117 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5118 ret = VM_FAULT_SIGBUS;
5119 goto out;
5120 }
5121
Nick Piggin56a76f82009-03-31 15:23:23 -07005122 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005123again:
Chris Mason9ebefb182007-06-15 13:50:00 -04005124 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04005125 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005126 page_start = page_offset(page);
5127 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005128
Chris Mason9ebefb182007-06-15 13:50:00 -04005129 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04005130 (page_start >= size)) {
Josef Bacik6a632092009-02-20 11:00:09 -05005131 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason9ebefb182007-06-15 13:50:00 -04005132 /* page got truncated out from underneath us */
5133 goto out_unlock;
5134 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005135 wait_on_page_writeback(page);
5136
5137 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
5138 set_page_extent_mapped(page);
5139
Chris Masoneb84ae02008-07-17 13:53:27 -04005140 /*
5141 * we can't set the delalloc bits if there are pending ordered
5142 * extents. Drop our locks and wait for them to finish
5143 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005144 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5145 if (ordered) {
5146 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5147 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005148 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005149 btrfs_put_ordered_extent(ordered);
5150 goto again;
5151 }
5152
Josef Bacikfbf19082009-10-01 17:10:23 -04005153 /*
5154 * XXX - page_mkwrite gets called every time the page is dirtied, even
5155 * if it was already dirty, so for space accounting reasons we need to
5156 * clear any delalloc bits for the range we are fixing to save. There
5157 * is probably a better way to do this, but for now keep consistent with
5158 * prepare_pages in the normal write path.
5159 */
5160 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005161 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
5162 GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04005163
Josef Bacik9ed74f22009-09-11 16:12:44 -04005164 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
5165 if (ret) {
5166 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5167 ret = VM_FAULT_SIGBUS;
Josef Bacikfbf19082009-10-01 17:10:23 -04005168 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005169 goto out_unlock;
5170 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005171 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04005172
5173 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04005174 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005175 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04005176 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04005177 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04005178
Chris Masone6dcd2d2008-07-17 12:53:50 -04005179 if (zero_start != PAGE_CACHE_SIZE) {
5180 kaddr = kmap(page);
5181 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5182 flush_dcache_page(page);
5183 kunmap(page);
5184 }
Chris Mason247e7432008-07-17 12:53:51 -04005185 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005186 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04005187 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005188
Chris Mason257c62e2009-10-13 13:21:08 -04005189 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5190 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5191
Chris Masone6dcd2d2008-07-17 12:53:50 -04005192 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04005193
5194out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005195 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason50a9b212009-09-11 12:33:12 -04005196 if (!ret)
5197 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04005198 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05005199out:
Chris Mason9ebefb182007-06-15 13:50:00 -04005200 return ret;
5201}
5202
Chris Mason39279cc2007-06-12 06:35:45 -04005203static void btrfs_truncate(struct inode *inode)
5204{
5205 struct btrfs_root *root = BTRFS_I(inode)->root;
5206 int ret;
5207 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005208 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04005209 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005210
Yan, Zheng80825102009-11-12 09:35:36 +00005211 if (!S_ISREG(inode->i_mode)) {
5212 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04005213 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005214 }
Chris Mason39279cc2007-06-12 06:35:45 -04005215
Josef Bacik5d5e1032009-10-13 16:46:49 -04005216 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5217 if (ret)
5218 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005219
Chris Mason4a096752008-07-21 10:29:44 -04005220 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00005221 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005222
Chris Mason39279cc2007-06-12 06:35:45 -04005223 trans = btrfs_start_transaction(root, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00005224 btrfs_set_trans_block_group(trans, inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005225
5226 /*
5227 * setattr is responsible for setting the ordered_data_close flag,
5228 * but that is only tested during the last file release. That
5229 * could happen well after the next commit, leaving a great big
5230 * window where new writes may get lost if someone chooses to write
5231 * to this file after truncating to zero
5232 *
5233 * The inode doesn't have any dirty data here, and so if we commit
5234 * this is a noop. If someone immediately starts writing to the inode
5235 * it is very likely we'll catch some of their writes in this
5236 * transaction, and the commit will find this file on the ordered
5237 * data list with good things to send down.
5238 *
5239 * This is a best effort solution, there is still a window where
5240 * using truncate to replace the contents of the file will
5241 * end up with a zero length file after a crash.
5242 */
5243 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5244 btrfs_add_ordered_operation(trans, root, inode);
5245
Yan, Zheng80825102009-11-12 09:35:36 +00005246 while (1) {
5247 ret = btrfs_truncate_inode_items(trans, root, inode,
5248 inode->i_size,
5249 BTRFS_EXTENT_DATA_KEY);
5250 if (ret != -EAGAIN)
5251 break;
Chris Mason39279cc2007-06-12 06:35:45 -04005252
Yan, Zheng80825102009-11-12 09:35:36 +00005253 ret = btrfs_update_inode(trans, root, inode);
5254 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005255
Yan, Zheng80825102009-11-12 09:35:36 +00005256 nr = trans->blocks_used;
5257 btrfs_end_transaction(trans, root);
5258 btrfs_btree_balance_dirty(root, nr);
5259
5260 trans = btrfs_start_transaction(root, 1);
5261 btrfs_set_trans_block_group(trans, inode);
5262 }
5263
5264 if (ret == 0 && inode->i_nlink > 0) {
5265 ret = btrfs_orphan_del(trans, inode);
5266 BUG_ON(ret);
5267 }
5268
5269 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04005270 BUG_ON(ret);
5271
Josef Bacik7b128762008-07-24 12:17:14 -04005272 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04005273 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005274 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005275 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005276}
5277
Sven Wegener3b963622008-06-09 21:57:42 -04005278/*
Chris Masond352ac62008-09-29 15:18:18 -04005279 * create a new subvolume directory/inode (helper for the ioctl).
5280 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05005281int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04005282 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005283 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04005284{
Chris Mason39279cc2007-06-12 06:35:45 -04005285 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005286 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005287 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005288
Josef Bacikaec74772008-07-24 12:12:38 -04005289 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005290 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04005291 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005292 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005293 inode->i_op = &btrfs_dir_inode_operations;
5294 inode->i_fop = &btrfs_dir_file_operations;
5295
Chris Mason39279cc2007-06-12 06:35:45 -04005296 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04005297 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04005298
Yan, Zheng76dda932009-09-21 16:00:26 -04005299 err = btrfs_update_inode(trans, new_root, inode);
5300 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005301
Yan, Zheng76dda932009-09-21 16:00:26 -04005302 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005303 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005304}
5305
Chris Masond352ac62008-09-29 15:18:18 -04005306/* helper function for file defrag and space balancing. This
5307 * forces readahead on a given range of bytes in an inode
5308 */
Chris Masonedbd8d42007-12-21 16:27:24 -05005309unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04005310 struct file_ra_state *ra, struct file *file,
5311 pgoff_t offset, pgoff_t last_index)
5312{
Chris Mason8e7bf942008-04-28 09:02:36 -04005313 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04005314
Chris Mason86479a02007-09-10 19:58:16 -04005315 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5316 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04005317}
5318
Chris Mason39279cc2007-06-12 06:35:45 -04005319struct inode *btrfs_alloc_inode(struct super_block *sb)
5320{
5321 struct btrfs_inode *ei;
5322
5323 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5324 if (!ei)
5325 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04005326 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04005327 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005328 ei->logged_trans = 0;
Josef Bacik32c00af2009-10-08 13:34:05 -04005329 ei->outstanding_extents = 0;
5330 ei->reserved_extents = 0;
Josef Bacika6dbd422009-11-11 15:53:34 -05005331 ei->root = NULL;
Josef Bacik32c00af2009-10-08 13:34:05 -04005332 spin_lock_init(&ei->accounting_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005333 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04005334 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005335 INIT_LIST_HEAD(&ei->ordered_operations);
Chris Mason39279cc2007-06-12 06:35:45 -04005336 return &ei->vfs_inode;
5337}
5338
5339void btrfs_destroy_inode(struct inode *inode)
5340{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005341 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005342 struct btrfs_root *root = BTRFS_I(inode)->root;
5343
Chris Mason39279cc2007-06-12 06:35:45 -04005344 WARN_ON(!list_empty(&inode->i_dentry));
5345 WARN_ON(inode->i_data.nrpages);
5346
Chris Mason5a3f23d2009-03-31 13:27:11 -04005347 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05005348 * This can happen where we create an inode, but somebody else also
5349 * created the same inode and we need to destroy the one we already
5350 * created.
5351 */
5352 if (!root)
5353 goto free;
5354
5355 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005356 * Make sure we're properly removed from the ordered operation
5357 * lists.
5358 */
5359 smp_mb();
5360 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5361 spin_lock(&root->fs_info->ordered_extent_lock);
5362 list_del_init(&BTRFS_I(inode)->ordered_operations);
5363 spin_unlock(&root->fs_info->ordered_extent_lock);
5364 }
5365
5366 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005367 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00005368 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
5369 inode->i_ino);
5370 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04005371 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04005372 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005373
Chris Masond3977122009-01-05 21:25:51 -05005374 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005375 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5376 if (!ordered)
5377 break;
5378 else {
Chris Masond3977122009-01-05 21:25:51 -05005379 printk(KERN_ERR "btrfs found ordered "
5380 "extent %llu %llu on inode cleanup\n",
5381 (unsigned long long)ordered->file_offset,
5382 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005383 btrfs_remove_ordered_extent(inode, ordered);
5384 btrfs_put_ordered_extent(ordered);
5385 btrfs_put_ordered_extent(ordered);
5386 }
5387 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005388 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005389 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05005390free:
Chris Mason39279cc2007-06-12 06:35:45 -04005391 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5392}
5393
Yan, Zheng76dda932009-09-21 16:00:26 -04005394void btrfs_drop_inode(struct inode *inode)
5395{
5396 struct btrfs_root *root = BTRFS_I(inode)->root;
5397
5398 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5399 generic_delete_inode(inode);
5400 else
5401 generic_drop_inode(inode);
5402}
5403
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005404static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04005405{
5406 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5407
5408 inode_init_once(&ei->vfs_inode);
5409}
5410
5411void btrfs_destroy_cachep(void)
5412{
5413 if (btrfs_inode_cachep)
5414 kmem_cache_destroy(btrfs_inode_cachep);
5415 if (btrfs_trans_handle_cachep)
5416 kmem_cache_destroy(btrfs_trans_handle_cachep);
5417 if (btrfs_transaction_cachep)
5418 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04005419 if (btrfs_path_cachep)
5420 kmem_cache_destroy(btrfs_path_cachep);
5421}
5422
5423int btrfs_init_cachep(void)
5424{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005425 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5426 sizeof(struct btrfs_inode), 0,
5427 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04005428 if (!btrfs_inode_cachep)
5429 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005430
5431 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5432 sizeof(struct btrfs_trans_handle), 0,
5433 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005434 if (!btrfs_trans_handle_cachep)
5435 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005436
5437 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5438 sizeof(struct btrfs_transaction), 0,
5439 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005440 if (!btrfs_transaction_cachep)
5441 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005442
5443 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5444 sizeof(struct btrfs_path), 0,
5445 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005446 if (!btrfs_path_cachep)
5447 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005448
Chris Mason39279cc2007-06-12 06:35:45 -04005449 return 0;
5450fail:
5451 btrfs_destroy_cachep();
5452 return -ENOMEM;
5453}
5454
5455static int btrfs_getattr(struct vfsmount *mnt,
5456 struct dentry *dentry, struct kstat *stat)
5457{
5458 struct inode *inode = dentry->d_inode;
5459 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05005460 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05005461 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005462 stat->blocks = (inode_get_bytes(inode) +
5463 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04005464 return 0;
5465}
5466
Chris Masond3977122009-01-05 21:25:51 -05005467static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5468 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005469{
5470 struct btrfs_trans_handle *trans;
5471 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005472 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005473 struct inode *new_inode = new_dentry->d_inode;
5474 struct inode *old_inode = old_dentry->d_inode;
5475 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005476 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005477 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005478 int ret;
5479
Yan, Zhengf679a842009-09-24 09:17:31 -04005480 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5481 return -EPERM;
5482
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005483 /* we only allow rename subvolume link between subvolumes */
5484 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05005485 return -EXDEV;
5486
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005487 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5488 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5489 return -ENOTEMPTY;
5490
Chris Mason39279cc2007-06-12 06:35:45 -04005491 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005492 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04005493 return -ENOTEMPTY;
Chris Mason0660b5a2008-11-17 20:37:39 -05005494
Josef Bacik9ed74f22009-09-11 16:12:44 -04005495 /*
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005496 * We want to reserve the absolute worst case amount of items. So if
5497 * both inodes are subvols and we need to unlink them then that would
5498 * require 4 item modifications, but if they are both normal inodes it
5499 * would require 5 item modifications, so we'll assume their normal
5500 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5501 * should cover the worst case number of items we'll modify.
Josef Bacik9ed74f22009-09-11 16:12:44 -04005502 */
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005503 ret = btrfs_reserve_metadata_space(root, 11);
Chris Mason1832a6d2007-12-21 16:27:21 -05005504 if (ret)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005505 return ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05005506
Chris Mason5a3f23d2009-03-31 13:27:11 -04005507 /*
5508 * we're using rename to replace one file with another.
5509 * and the replacement file is large. Start IO on it now so
5510 * we don't add too much work to the end of the transaction
5511 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04005512 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04005513 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5514 filemap_flush(old_inode->i_mapping);
5515
Yan, Zheng76dda932009-09-21 16:00:26 -04005516 /* close the racy window with snapshot create/destroy ioctl */
5517 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5518 down_read(&root->fs_info->subvol_sem);
Chris Mason5f39d392007-10-15 16:14:19 -04005519
Chris Mason39279cc2007-06-12 06:35:45 -04005520 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga5719522009-09-24 09:17:31 -04005521 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04005522
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005523 if (dest != root)
5524 btrfs_record_root_in_trans(trans, dest);
5525
Yan, Zhenga5719522009-09-24 09:17:31 -04005526 ret = btrfs_set_inode_index(new_dir, &index);
5527 if (ret)
5528 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005529
Yan, Zhenga5719522009-09-24 09:17:31 -04005530 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005531 /* force full log commit if subvolume involved. */
5532 root->fs_info->last_trans_log_full_commit = trans->transid;
5533 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04005534 ret = btrfs_insert_inode_ref(trans, dest,
5535 new_dentry->d_name.name,
5536 new_dentry->d_name.len,
5537 old_inode->i_ino,
5538 new_dir->i_ino, index);
5539 if (ret)
5540 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541 /*
5542 * this is an ugly little race, but the rename is required
5543 * to make sure that if we crash, the inode is either at the
5544 * old name or the new one. pinning the log transaction lets
5545 * us make sure we don't allow a log commit to come in after
5546 * we unlink the name but before we add the new name back in.
5547 */
5548 btrfs_pin_log_trans(root);
5549 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005550 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005551 * make sure the inode gets flushed if it is replacing
5552 * something.
5553 */
5554 if (new_inode && new_inode->i_size &&
5555 old_inode && S_ISREG(old_inode->i_mode)) {
5556 btrfs_add_ordered_operation(trans, root, old_inode);
5557 }
5558
Chris Mason39279cc2007-06-12 06:35:45 -04005559 old_dir->i_ctime = old_dir->i_mtime = ctime;
5560 new_dir->i_ctime = new_dir->i_mtime = ctime;
5561 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04005562
Chris Mason12fcfd22009-03-24 10:24:20 -04005563 if (old_dentry->d_parent != new_dentry->d_parent)
5564 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5565
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005566 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5567 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5568 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5569 old_dentry->d_name.name,
5570 old_dentry->d_name.len);
5571 } else {
5572 btrfs_inc_nlink(old_dentry->d_inode);
5573 ret = btrfs_unlink_inode(trans, root, old_dir,
5574 old_dentry->d_inode,
5575 old_dentry->d_name.name,
5576 old_dentry->d_name.len);
5577 }
5578 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005579
5580 if (new_inode) {
5581 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005582 if (unlikely(new_inode->i_ino ==
5583 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5584 root_objectid = BTRFS_I(new_inode)->location.objectid;
5585 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5586 root_objectid,
5587 new_dentry->d_name.name,
5588 new_dentry->d_name.len);
5589 BUG_ON(new_inode->i_nlink == 0);
5590 } else {
5591 ret = btrfs_unlink_inode(trans, dest, new_dir,
5592 new_dentry->d_inode,
5593 new_dentry->d_name.name,
5594 new_dentry->d_name.len);
5595 }
5596 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005597 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04005598 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005599 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005600 }
Chris Mason39279cc2007-06-12 06:35:45 -04005601 }
Josef Bacikaec74772008-07-24 12:12:38 -04005602
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005603 ret = btrfs_add_link(trans, new_dir, old_inode,
5604 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04005605 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005606 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005607
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005608 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5609 btrfs_log_new_name(trans, old_inode, old_dir,
5610 new_dentry->d_parent);
5611 btrfs_end_log_trans(root);
5612 }
Chris Mason39279cc2007-06-12 06:35:45 -04005613out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04005614 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005615
Yan, Zheng76dda932009-09-21 16:00:26 -04005616 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5617 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005618
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005619 btrfs_unreserve_metadata_space(root, 11);
Chris Mason39279cc2007-06-12 06:35:45 -04005620 return ret;
5621}
5622
Chris Masond352ac62008-09-29 15:18:18 -04005623/*
5624 * some fairly slow code that needs optimization. This walks the list
5625 * of all the inodes with pending delalloc and forces them to disk.
5626 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005627int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04005628{
5629 struct list_head *head = &root->fs_info->delalloc_inodes;
5630 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005631 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04005632
Yan Zhengc146afa2008-11-12 14:34:12 -05005633 if (root->fs_info->sb->s_flags & MS_RDONLY)
5634 return -EROFS;
5635
Chris Mason75eff682008-12-15 15:54:40 -05005636 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05005637 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04005638 binode = list_entry(head->next, struct btrfs_inode,
5639 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005640 inode = igrab(&binode->vfs_inode);
5641 if (!inode)
5642 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05005643 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005644 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005645 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005646 if (delay_iput)
5647 btrfs_add_delayed_iput(inode);
5648 else
5649 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005650 }
5651 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05005652 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04005653 }
Chris Mason75eff682008-12-15 15:54:40 -05005654 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04005655
5656 /* the filemap_flush will queue IO into the worker threads, but
5657 * we have to make sure the IO is actually started and that
5658 * ordered extents get created before we return
5659 */
5660 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05005661 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05005662 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005663 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05005664 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5665 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04005666 }
5667 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04005668 return 0;
5669}
5670
Chris Mason39279cc2007-06-12 06:35:45 -04005671static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5672 const char *symname)
5673{
5674 struct btrfs_trans_handle *trans;
5675 struct btrfs_root *root = BTRFS_I(dir)->root;
5676 struct btrfs_path *path;
5677 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05005678 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005679 int err;
5680 int drop_inode = 0;
5681 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005682 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04005683 int name_len;
5684 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04005685 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005686 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04005687 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05005688 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005689
5690 name_len = strlen(symname) + 1;
5691 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5692 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05005693
Josef Bacik9ed74f22009-09-11 16:12:44 -04005694 /*
5695 * 2 items for inode item and ref
5696 * 2 items for dir items
5697 * 1 item for xattr if selinux is on
5698 */
5699 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05005700 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04005701 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05005702
Chris Mason39279cc2007-06-12 06:35:45 -04005703 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005704 if (!trans)
5705 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04005706 btrfs_set_trans_block_group(trans, dir);
5707
5708 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5709 if (err) {
5710 err = -ENOSPC;
5711 goto out_unlock;
5712 }
5713
Josef Bacikaec74772008-07-24 12:12:38 -04005714 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05005715 dentry->d_name.len,
5716 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04005717 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5718 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005719 err = PTR_ERR(inode);
5720 if (IS_ERR(inode))
5721 goto out_unlock;
5722
Yan, Zhengf34f57a2009-11-12 09:35:27 +00005723 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04005724 if (err) {
5725 drop_inode = 1;
5726 goto out_unlock;
5727 }
5728
Chris Mason39279cc2007-06-12 06:35:45 -04005729 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005730 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005731 if (err)
5732 drop_inode = 1;
5733 else {
5734 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005735 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04005736 inode->i_fop = &btrfs_file_operations;
5737 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05005738 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04005739 }
Chris Mason39279cc2007-06-12 06:35:45 -04005740 btrfs_update_inode_block_group(trans, inode);
5741 btrfs_update_inode_block_group(trans, dir);
5742 if (drop_inode)
5743 goto out_unlock;
5744
5745 path = btrfs_alloc_path();
5746 BUG_ON(!path);
5747 key.objectid = inode->i_ino;
5748 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005749 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5750 datasize = btrfs_file_extent_calc_inline_size(name_len);
5751 err = btrfs_insert_empty_item(trans, root, path, &key,
5752 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005753 if (err) {
5754 drop_inode = 1;
5755 goto out_unlock;
5756 }
Chris Mason5f39d392007-10-15 16:14:19 -04005757 leaf = path->nodes[0];
5758 ei = btrfs_item_ptr(leaf, path->slots[0],
5759 struct btrfs_file_extent_item);
5760 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5761 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04005762 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04005763 btrfs_set_file_extent_encryption(leaf, ei, 0);
5764 btrfs_set_file_extent_compression(leaf, ei, 0);
5765 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5766 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5767
Chris Mason39279cc2007-06-12 06:35:45 -04005768 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04005769 write_extent_buffer(leaf, symname, ptr, name_len);
5770 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04005771 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04005772
Chris Mason39279cc2007-06-12 06:35:45 -04005773 inode->i_op = &btrfs_symlink_inode_operations;
5774 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04005775 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04005776 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04005777 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005778 err = btrfs_update_inode(trans, root, inode);
5779 if (err)
5780 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005781
5782out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005783 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04005784 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005785out_fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005786 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04005787 if (drop_inode) {
5788 inode_dec_link_count(inode);
5789 iput(inode);
5790 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005791 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005792 return err;
5793}
Chris Mason16432982008-04-10 10:23:21 -04005794
Yan, Zheng5a303d52009-11-12 09:34:52 +00005795static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00005796 u64 alloc_hint, int mode)
Yan Zhengd899e052008-10-30 14:25:28 -04005797{
Yan, Zheng5a303d52009-11-12 09:34:52 +00005798 struct btrfs_trans_handle *trans;
Yan Zhengd899e052008-10-30 14:25:28 -04005799 struct btrfs_root *root = BTRFS_I(inode)->root;
5800 struct btrfs_key ins;
5801 u64 alloc_size;
5802 u64 cur_offset = start;
5803 u64 num_bytes = end - start;
5804 int ret = 0;
5805
Yan Zhengd899e052008-10-30 14:25:28 -04005806 while (num_bytes > 0) {
5807 alloc_size = min(num_bytes, root->fs_info->max_extent);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005808
Chris Mason3a1abec2009-12-17 15:47:17 -05005809 trans = btrfs_start_transaction(root, 1);
5810
Yan Zhengd899e052008-10-30 14:25:28 -04005811 ret = btrfs_reserve_extent(trans, root, alloc_size,
5812 root->sectorsize, 0, alloc_hint,
5813 (u64)-1, &ins, 1);
5814 if (ret) {
5815 WARN_ON(1);
Chris Mason3a1abec2009-12-17 15:47:17 -05005816 goto stop_trans;
Yan Zhengd899e052008-10-30 14:25:28 -04005817 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00005818
5819 ret = btrfs_reserve_metadata_space(root, 3);
5820 if (ret) {
5821 btrfs_free_reserved_extent(root, ins.objectid,
5822 ins.offset);
Chris Mason3a1abec2009-12-17 15:47:17 -05005823 goto stop_trans;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005824 }
5825
Yan Zhengd899e052008-10-30 14:25:28 -04005826 ret = insert_reserved_file_extent(trans, inode,
5827 cur_offset, ins.objectid,
5828 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00005829 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04005830 BTRFS_FILE_EXTENT_PREALLOC);
5831 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04005832 btrfs_drop_extent_cache(inode, cur_offset,
5833 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005834
Yan Zhengd899e052008-10-30 14:25:28 -04005835 num_bytes -= ins.offset;
5836 cur_offset += ins.offset;
5837 alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005838
Yan Zhengd899e052008-10-30 14:25:28 -04005839 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005840 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04005841 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zheng5a303d52009-11-12 09:34:52 +00005842 cur_offset > inode->i_size) {
5843 i_size_write(inode, cur_offset);
5844 btrfs_ordered_update_i_size(inode, cur_offset, NULL);
5845 }
5846
Yan Zhengd899e052008-10-30 14:25:28 -04005847 ret = btrfs_update_inode(trans, root, inode);
5848 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04005849
Yan, Zheng5a303d52009-11-12 09:34:52 +00005850 btrfs_end_transaction(trans, root);
5851 btrfs_unreserve_metadata_space(root, 3);
5852 }
Yan Zhengd899e052008-10-30 14:25:28 -04005853 return ret;
Chris Mason3a1abec2009-12-17 15:47:17 -05005854
5855stop_trans:
5856 btrfs_end_transaction(trans, root);
5857 return ret;
5858
Yan Zhengd899e052008-10-30 14:25:28 -04005859}
5860
5861static long btrfs_fallocate(struct inode *inode, int mode,
5862 loff_t offset, loff_t len)
5863{
5864 u64 cur_offset;
5865 u64 last_byte;
5866 u64 alloc_start;
5867 u64 alloc_end;
5868 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04005869 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04005870 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5871 struct extent_map *em;
5872 int ret;
5873
5874 alloc_start = offset & ~mask;
5875 alloc_end = (offset + len + mask) & ~mask;
5876
Chris Mason546888d2009-04-21 11:53:38 -04005877 /*
5878 * wait for ordered IO before we have any locks. We'll loop again
5879 * below with the locks held.
5880 */
5881 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5882
Yan Zhengd899e052008-10-30 14:25:28 -04005883 mutex_lock(&inode->i_mutex);
5884 if (alloc_start > inode->i_size) {
5885 ret = btrfs_cont_expand(inode, alloc_start);
5886 if (ret)
5887 goto out;
5888 }
5889
Yan, Zheng5a303d52009-11-12 09:34:52 +00005890 ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode,
Josef Bacika970b0a2009-06-27 21:07:34 -04005891 alloc_end - alloc_start);
5892 if (ret)
5893 goto out;
5894
Chris Masone980b502009-04-24 14:39:24 -04005895 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04005896 while (1) {
5897 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04005898
Chris Mason546888d2009-04-21 11:53:38 -04005899 /* the extent lock is ordered inside the running
5900 * transaction
5901 */
Chris Masone980b502009-04-24 14:39:24 -04005902 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5903 GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04005904 ordered = btrfs_lookup_first_ordered_extent(inode,
5905 alloc_end - 1);
5906 if (ordered &&
5907 ordered->file_offset + ordered->len > alloc_start &&
5908 ordered->file_offset < alloc_end) {
5909 btrfs_put_ordered_extent(ordered);
5910 unlock_extent(&BTRFS_I(inode)->io_tree,
Chris Masone980b502009-04-24 14:39:24 -04005911 alloc_start, locked_end, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005912 /*
5913 * we can't wait on the range with the transaction
5914 * running or with the extent lock held
5915 */
Yan Zhengd899e052008-10-30 14:25:28 -04005916 btrfs_wait_ordered_range(inode, alloc_start,
5917 alloc_end - alloc_start);
5918 } else {
5919 if (ordered)
5920 btrfs_put_ordered_extent(ordered);
5921 break;
5922 }
5923 }
5924
5925 cur_offset = alloc_start;
5926 while (1) {
5927 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5928 alloc_end - cur_offset, 0);
5929 BUG_ON(IS_ERR(em) || !em);
5930 last_byte = min(extent_map_end(em), alloc_end);
5931 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005932 if (em->block_start == EXTENT_MAP_HOLE ||
5933 (cur_offset >= inode->i_size &&
5934 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5935 ret = prealloc_file_range(inode,
5936 cur_offset, last_byte,
5937 alloc_hint, mode);
Yan Zhengd899e052008-10-30 14:25:28 -04005938 if (ret < 0) {
5939 free_extent_map(em);
5940 break;
5941 }
5942 }
5943 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5944 alloc_hint = em->block_start;
5945 free_extent_map(em);
5946
5947 cur_offset = last_byte;
5948 if (cur_offset >= alloc_end) {
5949 ret = 0;
5950 break;
5951 }
5952 }
Chris Masone980b502009-04-24 14:39:24 -04005953 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04005954 GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005955
Yan, Zheng5a303d52009-11-12 09:34:52 +00005956 btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode,
5957 alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04005958out:
5959 mutex_unlock(&inode->i_mutex);
5960 return ret;
5961}
5962
Chris Masone6dcd2d2008-07-17 12:53:50 -04005963static int btrfs_set_page_dirty(struct page *page)
5964{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005965 return __set_page_dirty_nobuffers(page);
5966}
5967
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005968static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05005969{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005970 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05005971 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04005972 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05005973}
Chris Mason39279cc2007-06-12 06:35:45 -04005974
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005975static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05005976 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005977 .lookup = btrfs_lookup,
5978 .create = btrfs_create,
5979 .unlink = btrfs_unlink,
5980 .link = btrfs_link,
5981 .mkdir = btrfs_mkdir,
5982 .rmdir = btrfs_rmdir,
5983 .rename = btrfs_rename,
5984 .symlink = btrfs_symlink,
5985 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005986 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005987 .setxattr = btrfs_setxattr,
5988 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005989 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005990 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005991 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005992};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005993static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005994 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05005995 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005996};
Yan, Zheng76dda932009-09-21 16:00:26 -04005997
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005998static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005999 .llseek = generic_file_llseek,
6000 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01006001 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04006002 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04006003#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04006004 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04006005#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04006006 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04006007 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04006008};
6009
Chris Masond1310b22008-01-24 16:13:08 -05006010static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04006011 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05006012 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04006013 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04006014 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006015 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04006016 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04006017 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05006018 .set_bit_hook = btrfs_set_bit_hook,
6019 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006020 .merge_extent_hook = btrfs_merge_extent_hook,
6021 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04006022};
6023
Chris Mason35054392009-01-21 13:11:13 -05006024/*
6025 * btrfs doesn't support the bmap operation because swapfiles
6026 * use bmap to make a mapping of extents in the file. They assume
6027 * these extents won't change over the life of the file and they
6028 * use the bmap result to do IO directly to the drive.
6029 *
6030 * the btrfs bmap call would return logical addresses that aren't
6031 * suitable for IO and they also will change frequently as COW
6032 * operations happen. So, swapfile + btrfs == corruption.
6033 *
6034 * For now we're avoiding this by dropping bmap.
6035 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006036static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006037 .readpage = btrfs_readpage,
6038 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04006039 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05006040 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04006041 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04006042 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04006043 .invalidatepage = btrfs_invalidatepage,
6044 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006045 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02006046 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04006047};
6048
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006049static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006050 .readpage = btrfs_readpage,
6051 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04006052 .invalidatepage = btrfs_invalidatepage,
6053 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04006054};
6055
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006056static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006057 .truncate = btrfs_truncate,
6058 .getattr = btrfs_getattr,
6059 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006060 .setxattr = btrfs_setxattr,
6061 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05006062 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006063 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05006064 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04006065 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006066 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04006067};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006068static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04006069 .getattr = btrfs_getattr,
6070 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05006071 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006072 .setxattr = btrfs_setxattr,
6073 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04006074 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006075 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04006076};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006077static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006078 .readlink = generic_readlink,
6079 .follow_link = page_follow_link_light,
6080 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05006081 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05006082 .setxattr = btrfs_setxattr,
6083 .getxattr = btrfs_getxattr,
6084 .listxattr = btrfs_listxattr,
6085 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04006086};
Yan, Zheng76dda932009-09-21 16:00:26 -04006087
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04006088const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04006089 .d_delete = btrfs_dentry_delete,
6090};