blob: ef250be49cdb3c73016b1c074e70639c154fbb88 [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
Jim Owens0279b4c2009-02-04 09:29:13 -050091static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
92{
93 int err;
94
95 err = btrfs_init_acl(inode, dir);
96 if (!err)
97 err = btrfs_xattr_security_init(inode, dir);
98 return err;
99}
100
Chris Masond352ac62008-09-29 15:18:18 -0400101/*
Chris Masonc8b97812008-10-29 14:49:59 -0400102 * this does all the hard work for inserting an inline extent into
103 * the btree. The caller should have done a btrfs_drop_extents so that
104 * no overlapping inline items exist in the btree
105 */
Chris Masond3977122009-01-05 21:25:51 -0500106static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400107 struct btrfs_root *root, struct inode *inode,
108 u64 start, size_t size, size_t compressed_size,
109 struct page **compressed_pages)
110{
111 struct btrfs_key key;
112 struct btrfs_path *path;
113 struct extent_buffer *leaf;
114 struct page *page = NULL;
115 char *kaddr;
116 unsigned long ptr;
117 struct btrfs_file_extent_item *ei;
118 int err = 0;
119 int ret;
120 size_t cur_size = size;
121 size_t datasize;
122 unsigned long offset;
123 int use_compress = 0;
124
125 if (compressed_size && compressed_pages) {
126 use_compress = 1;
127 cur_size = compressed_size;
128 }
129
Chris Masond3977122009-01-05 21:25:51 -0500130 path = btrfs_alloc_path();
131 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400132 return -ENOMEM;
133
Chris Masonb9473432009-03-13 11:00:37 -0400134 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400135 btrfs_set_trans_block_group(trans, inode);
136
137 key.objectid = inode->i_ino;
138 key.offset = start;
139 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400140 datasize = btrfs_file_extent_calc_inline_size(cur_size);
141
142 inode_add_bytes(inode, size);
143 ret = btrfs_insert_empty_item(trans, root, path, &key,
144 datasize);
145 BUG_ON(ret);
146 if (ret) {
147 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400148 goto fail;
149 }
150 leaf = path->nodes[0];
151 ei = btrfs_item_ptr(leaf, path->slots[0],
152 struct btrfs_file_extent_item);
153 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
154 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
155 btrfs_set_file_extent_encryption(leaf, ei, 0);
156 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
157 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
158 ptr = btrfs_file_extent_inline_start(ei);
159
160 if (use_compress) {
161 struct page *cpage;
162 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500163 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400164 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500165 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400166 PAGE_CACHE_SIZE);
167
Chris Masonb9473432009-03-13 11:00:37 -0400168 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400169 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400170 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400171
172 i++;
173 ptr += cur_size;
174 compressed_size -= cur_size;
175 }
176 btrfs_set_file_extent_compression(leaf, ei,
177 BTRFS_COMPRESS_ZLIB);
178 } else {
179 page = find_get_page(inode->i_mapping,
180 start >> PAGE_CACHE_SHIFT);
181 btrfs_set_file_extent_compression(leaf, ei, 0);
182 kaddr = kmap_atomic(page, KM_USER0);
183 offset = start & (PAGE_CACHE_SIZE - 1);
184 write_extent_buffer(leaf, kaddr + offset, ptr, size);
185 kunmap_atomic(kaddr, KM_USER0);
186 page_cache_release(page);
187 }
188 btrfs_mark_buffer_dirty(leaf);
189 btrfs_free_path(path);
190
191 BTRFS_I(inode)->disk_i_size = inode->i_size;
192 btrfs_update_inode(trans, root, inode);
193 return 0;
194fail:
195 btrfs_free_path(path);
196 return err;
197}
198
199
200/*
201 * conditionally insert an inline extent into the file. This
202 * does the checks required to make sure the data is small enough
203 * to fit as an inline extent.
204 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400205static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400206 struct btrfs_root *root,
207 struct inode *inode, u64 start, u64 end,
208 size_t compressed_size,
209 struct page **compressed_pages)
210{
211 u64 isize = i_size_read(inode);
212 u64 actual_end = min(end + 1, isize);
213 u64 inline_len = actual_end - start;
214 u64 aligned_end = (end + root->sectorsize - 1) &
215 ~((u64)root->sectorsize - 1);
216 u64 hint_byte;
217 u64 data_len = inline_len;
218 int ret;
219
220 if (compressed_size)
221 data_len = compressed_size;
222
223 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400224 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400225 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
226 (!compressed_size &&
227 (actual_end & (root->sectorsize - 1)) == 0) ||
228 end + 1 < isize ||
229 data_len > root->fs_info->max_inline) {
230 return 1;
231 }
232
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000233 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400234 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 BUG_ON(ret);
236
237 if (isize > actual_end)
238 inline_len = min_t(u64, isize, actual_end);
239 ret = insert_inline_extent(trans, root, inode, start,
240 inline_len, compressed_size,
241 compressed_pages);
242 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -0400243 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 return 0;
245}
246
Chris Mason771ed682008-11-06 22:02:51 -0500247struct async_extent {
248 u64 start;
249 u64 ram_size;
250 u64 compressed_size;
251 struct page **pages;
252 unsigned long nr_pages;
253 struct list_head list;
254};
255
256struct async_cow {
257 struct inode *inode;
258 struct btrfs_root *root;
259 struct page *locked_page;
260 u64 start;
261 u64 end;
262 struct list_head extents;
263 struct btrfs_work work;
264};
265
266static noinline int add_async_extent(struct async_cow *cow,
267 u64 start, u64 ram_size,
268 u64 compressed_size,
269 struct page **pages,
270 unsigned long nr_pages)
271{
272 struct async_extent *async_extent;
273
274 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
275 async_extent->start = start;
276 async_extent->ram_size = ram_size;
277 async_extent->compressed_size = compressed_size;
278 async_extent->pages = pages;
279 async_extent->nr_pages = nr_pages;
280 list_add_tail(&async_extent->list, &cow->extents);
281 return 0;
282}
283
Chris Masonc8b97812008-10-29 14:49:59 -0400284/*
Chris Mason771ed682008-11-06 22:02:51 -0500285 * we create compressed extents in two phases. The first
286 * phase compresses a range of pages that have already been
287 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400288 *
Chris Mason771ed682008-11-06 22:02:51 -0500289 * This is done inside an ordered work queue, and the compression
290 * is spread across many cpus. The actual IO submission is step
291 * two, and the ordered work queue takes care of making sure that
292 * happens in the same order things were put onto the queue by
293 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400294 *
Chris Mason771ed682008-11-06 22:02:51 -0500295 * If this code finds it can't get good compression, it puts an
296 * entry onto the work queue to write the uncompressed bytes. This
297 * makes sure that both compressed inodes and uncompressed inodes
298 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400299 */
Chris Mason771ed682008-11-06 22:02:51 -0500300static noinline int compress_file_range(struct inode *inode,
301 struct page *locked_page,
302 u64 start, u64 end,
303 struct async_cow *async_cow,
304 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400305{
306 struct btrfs_root *root = BTRFS_I(inode)->root;
307 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400308 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400309 u64 orig_start;
310 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400311 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400312 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500313 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400314 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400315 struct page **pages = NULL;
316 unsigned long nr_pages;
317 unsigned long nr_pages_ret = 0;
318 unsigned long total_compressed = 0;
319 unsigned long total_in = 0;
320 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500321 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400322 int i;
323 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400324
Chris Masonc8b97812008-10-29 14:49:59 -0400325 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500326
Chris Mason42dc7ba2008-12-15 11:44:56 -0500327 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400328again:
329 will_compress = 0;
330 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
331 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
332
Chris Masonf03d9301f2009-02-04 09:31:06 -0500333 /*
334 * we don't want to send crud past the end of i_size through
335 * compression, that's just a waste of CPU time. So, if the
336 * end of the file is before the start of our current
337 * requested range of bytes, we bail out to the uncompressed
338 * cleanup code that can deal with all of this.
339 *
340 * It isn't really the fastest way to fix things, but this is a
341 * very uncommon corner.
342 */
343 if (actual_end <= start)
344 goto cleanup_and_bail_uncompressed;
345
Chris Masonc8b97812008-10-29 14:49:59 -0400346 total_compressed = actual_end - start;
347
348 /* we want to make sure that amount of ram required to uncompress
349 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500350 * of a compressed extent to 128k. This is a crucial number
351 * because it also controls how easily we can spread reads across
352 * cpus for decompression.
353 *
354 * We also want to make sure the amount of IO required to do
355 * a random read is reasonably small, so we limit the size of
356 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400357 */
358 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400359 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500360 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400361 disk_num_bytes = num_bytes;
362 total_in = 0;
363 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400364
Chris Mason771ed682008-11-06 22:02:51 -0500365 /*
366 * we do compression for mount -o compress and when the
367 * inode has not been flagged as nocompress. This flag can
368 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400369 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200370 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Masonc8b97812008-10-29 14:49:59 -0400371 btrfs_test_opt(root, COMPRESS)) {
372 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400373 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400374
Chris Masonc8b97812008-10-29 14:49:59 -0400375 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
376 total_compressed, pages,
377 nr_pages, &nr_pages_ret,
378 &total_in,
379 &total_compressed,
380 max_compressed);
381
382 if (!ret) {
383 unsigned long offset = total_compressed &
384 (PAGE_CACHE_SIZE - 1);
385 struct page *page = pages[nr_pages_ret - 1];
386 char *kaddr;
387
388 /* zero the tail end of the last page, we might be
389 * sending it down to disk
390 */
391 if (offset) {
392 kaddr = kmap_atomic(page, KM_USER0);
393 memset(kaddr + offset, 0,
394 PAGE_CACHE_SIZE - offset);
395 kunmap_atomic(kaddr, KM_USER0);
396 }
397 will_compress = 1;
398 }
399 }
400 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500401 trans = btrfs_join_transaction(root, 1);
402 BUG_ON(!trans);
403 btrfs_set_trans_block_group(trans, inode);
404
Chris Masonc8b97812008-10-29 14:49:59 -0400405 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500406 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400407 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500408 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400409 */
410 ret = cow_file_range_inline(trans, root, inode,
411 start, end, 0, NULL);
412 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500413 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400414 ret = cow_file_range_inline(trans, root, inode,
415 start, end,
416 total_compressed, pages);
417 }
Chris Mason771ed682008-11-06 22:02:51 -0500418 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400419 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500420 /*
421 * inline extent creation worked, we don't need
422 * to create any more async work items. Unlock
423 * and free up our temp pages.
424 */
Chris Masonc8b97812008-10-29 14:49:59 -0400425 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400426 &BTRFS_I(inode)->io_tree,
427 start, end, NULL,
428 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400429 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400430 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400431 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400432 ret = 0;
433 goto free_pages_out;
434 }
435 }
436
437 if (will_compress) {
438 /*
439 * we aren't doing an inline extent round the compressed size
440 * up to a block size boundary so the allocator does sane
441 * things
442 */
443 total_compressed = (total_compressed + blocksize - 1) &
444 ~(blocksize - 1);
445
446 /*
447 * one last check to make sure the compression is really a
448 * win, compare the page count read with the blocks on disk
449 */
450 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
451 ~(PAGE_CACHE_SIZE - 1);
452 if (total_compressed >= total_in) {
453 will_compress = 0;
454 } else {
455 disk_num_bytes = total_compressed;
456 num_bytes = total_in;
457 }
458 }
459 if (!will_compress && pages) {
460 /*
461 * the compression code ran but failed to make things smaller,
462 * free any pages it allocated and our page pointer array
463 */
464 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400465 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400466 page_cache_release(pages[i]);
467 }
468 kfree(pages);
469 pages = NULL;
470 total_compressed = 0;
471 nr_pages_ret = 0;
472
473 /* flag the file so we don't compress in the future */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200474 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Masonc8b97812008-10-29 14:49:59 -0400475 }
Chris Mason771ed682008-11-06 22:02:51 -0500476 if (will_compress) {
477 *num_added += 1;
478
479 /* the async work queues will take care of doing actual
480 * allocation on disk for these compressed pages,
481 * and will submit them to the elevator.
482 */
483 add_async_extent(async_cow, start, num_bytes,
484 total_compressed, pages, nr_pages_ret);
485
Chris Mason42dc7ba2008-12-15 11:44:56 -0500486 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500487 start += num_bytes;
488 pages = NULL;
489 cond_resched();
490 goto again;
491 }
492 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500493cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500494 /*
495 * No compression, but we still need to write the pages in
496 * the file we've been given so far. redirty the locked
497 * page if it corresponds to our extent and set things up
498 * for the async work queue to run cow_file_range to do
499 * the normal delalloc dance
500 */
501 if (page_offset(locked_page) >= start &&
502 page_offset(locked_page) <= end) {
503 __set_page_dirty_nobuffers(locked_page);
504 /* unlocked later on in the async handlers */
505 }
506 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
507 *num_added += 1;
508 }
509
510out:
511 return 0;
512
513free_pages_out:
514 for (i = 0; i < nr_pages_ret; i++) {
515 WARN_ON(pages[i]->mapping);
516 page_cache_release(pages[i]);
517 }
Chris Masond3977122009-01-05 21:25:51 -0500518 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500519
520 goto out;
521}
522
523/*
524 * phase two of compressed writeback. This is the ordered portion
525 * of the code, which only gets called in the order the work was
526 * queued. We walk all the async extents created by compress_file_range
527 * and send them down to the disk.
528 */
529static noinline int submit_compressed_extents(struct inode *inode,
530 struct async_cow *async_cow)
531{
532 struct async_extent *async_extent;
533 u64 alloc_hint = 0;
534 struct btrfs_trans_handle *trans;
535 struct btrfs_key ins;
536 struct extent_map *em;
537 struct btrfs_root *root = BTRFS_I(inode)->root;
538 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
539 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500540 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500541
542 if (list_empty(&async_cow->extents))
543 return 0;
544
545 trans = btrfs_join_transaction(root, 1);
546
Chris Masond3977122009-01-05 21:25:51 -0500547 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500548 async_extent = list_entry(async_cow->extents.next,
549 struct async_extent, list);
550 list_del(&async_extent->list);
551
552 io_tree = &BTRFS_I(inode)->io_tree;
553
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500554retry:
Chris Mason771ed682008-11-06 22:02:51 -0500555 /* did the compression code fall back to uncompressed IO? */
556 if (!async_extent->pages) {
557 int page_started = 0;
558 unsigned long nr_written = 0;
559
560 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500561 async_extent->start +
562 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500563
564 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500565 ret = cow_file_range(inode, async_cow->locked_page,
566 async_extent->start,
567 async_extent->start +
568 async_extent->ram_size - 1,
569 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500570
571 /*
572 * if page_started, cow_file_range inserted an
573 * inline extent and took care of all the unlocking
574 * and IO for us. Otherwise, we need to submit
575 * all those pages down to the drive.
576 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500577 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500578 extent_write_locked_range(io_tree,
579 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500580 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500581 async_extent->ram_size - 1,
582 btrfs_get_extent,
583 WB_SYNC_ALL);
584 kfree(async_extent);
585 cond_resched();
586 continue;
587 }
588
589 lock_extent(io_tree, async_extent->start,
590 async_extent->start + async_extent->ram_size - 1,
591 GFP_NOFS);
592 /*
593 * here we're doing allocation and writeback of the
594 * compressed pages
595 */
596 btrfs_drop_extent_cache(inode, async_extent->start,
597 async_extent->start +
598 async_extent->ram_size - 1, 0);
599
600 ret = btrfs_reserve_extent(trans, root,
601 async_extent->compressed_size,
602 async_extent->compressed_size,
603 0, alloc_hint,
604 (u64)-1, &ins, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500605 if (ret) {
606 int i;
607 for (i = 0; i < async_extent->nr_pages; i++) {
608 WARN_ON(async_extent->pages[i]->mapping);
609 page_cache_release(async_extent->pages[i]);
610 }
611 kfree(async_extent->pages);
612 async_extent->nr_pages = 0;
613 async_extent->pages = NULL;
614 unlock_extent(io_tree, async_extent->start,
615 async_extent->start +
616 async_extent->ram_size - 1, GFP_NOFS);
617 goto retry;
618 }
619
Chris Mason771ed682008-11-06 22:02:51 -0500620 em = alloc_extent_map(GFP_NOFS);
621 em->start = async_extent->start;
622 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500623 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500624
625 em->block_start = ins.objectid;
626 em->block_len = ins.offset;
627 em->bdev = root->fs_info->fs_devices->latest_bdev;
628 set_bit(EXTENT_FLAG_PINNED, &em->flags);
629 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
630
Chris Masond3977122009-01-05 21:25:51 -0500631 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400632 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500633 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400634 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500635 if (ret != -EEXIST) {
636 free_extent_map(em);
637 break;
638 }
639 btrfs_drop_extent_cache(inode, async_extent->start,
640 async_extent->start +
641 async_extent->ram_size - 1, 0);
642 }
643
644 ret = btrfs_add_ordered_extent(inode, async_extent->start,
645 ins.objectid,
646 async_extent->ram_size,
647 ins.offset,
648 BTRFS_ORDERED_COMPRESSED);
649 BUG_ON(ret);
650
651 btrfs_end_transaction(trans, root);
652
653 /*
654 * clear dirty, set writeback and unlock the pages.
655 */
656 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400657 &BTRFS_I(inode)->io_tree,
658 async_extent->start,
659 async_extent->start +
660 async_extent->ram_size - 1,
661 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
662 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400663 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400664 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500665
666 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500667 async_extent->start,
668 async_extent->ram_size,
669 ins.objectid,
670 ins.offset, async_extent->pages,
671 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500672
673 BUG_ON(ret);
674 trans = btrfs_join_transaction(root, 1);
675 alloc_hint = ins.objectid + ins.offset;
676 kfree(async_extent);
677 cond_resched();
678 }
679
680 btrfs_end_transaction(trans, root);
681 return 0;
682}
683
684/*
685 * when extent_io.c finds a delayed allocation range in the file,
686 * the call backs end up in this code. The basic idea is to
687 * allocate extents on disk for the range, and create ordered data structs
688 * in ram to track those extents.
689 *
690 * locked_page is the page that writepage had locked already. We use
691 * it to make sure we don't do extra locks or unlocks.
692 *
693 * *page_started is set to one if we unlock locked_page and do everything
694 * required to start IO on it. It may be clean and already done with
695 * IO when we return.
696 */
697static noinline int cow_file_range(struct inode *inode,
698 struct page *locked_page,
699 u64 start, u64 end, int *page_started,
700 unsigned long *nr_written,
701 int unlock)
702{
703 struct btrfs_root *root = BTRFS_I(inode)->root;
704 struct btrfs_trans_handle *trans;
705 u64 alloc_hint = 0;
706 u64 num_bytes;
707 unsigned long ram_size;
708 u64 disk_num_bytes;
709 u64 cur_alloc_size;
710 u64 blocksize = root->sectorsize;
711 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500712 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500713 struct btrfs_key ins;
714 struct extent_map *em;
715 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
716 int ret = 0;
717
718 trans = btrfs_join_transaction(root, 1);
719 BUG_ON(!trans);
720 btrfs_set_trans_block_group(trans, inode);
721
Chris Mason42dc7ba2008-12-15 11:44:56 -0500722 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500723
724 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
725 num_bytes = max(blocksize, num_bytes);
726 disk_num_bytes = num_bytes;
727 ret = 0;
728
729 if (start == 0) {
730 /* lets try to make an inline extent */
731 ret = cow_file_range_inline(trans, root, inode,
732 start, end, 0, NULL);
733 if (ret == 0) {
734 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400735 &BTRFS_I(inode)->io_tree,
736 start, end, NULL,
737 EXTENT_CLEAR_UNLOCK_PAGE |
738 EXTENT_CLEAR_UNLOCK |
739 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400740 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400741 EXTENT_CLEAR_DIRTY |
742 EXTENT_SET_WRITEBACK |
743 EXTENT_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500744 *nr_written = *nr_written +
745 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
746 *page_started = 1;
747 ret = 0;
748 goto out;
749 }
750 }
Chris Masonc8b97812008-10-29 14:49:59 -0400751
752 BUG_ON(disk_num_bytes >
753 btrfs_super_total_bytes(&root->fs_info->super_copy));
754
Chris Masonb917b7c2009-09-18 16:07:03 -0400755
756 read_lock(&BTRFS_I(inode)->extent_tree.lock);
757 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
758 start, num_bytes);
759 if (em) {
Josef Bacik6346c932009-11-10 21:23:47 -0500760 /*
761 * if block start isn't an actual block number then find the
762 * first block in this inode and use that as a hint. If that
763 * block is also bogus then just don't worry about it.
764 */
765 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
766 free_extent_map(em);
767 em = search_extent_mapping(em_tree, 0, 0);
768 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
769 alloc_hint = em->block_start;
770 if (em)
771 free_extent_map(em);
772 } else {
773 alloc_hint = em->block_start;
774 free_extent_map(em);
775 }
Chris Masonb917b7c2009-09-18 16:07:03 -0400776 }
777 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400778 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400779
Chris Masond3977122009-01-05 21:25:51 -0500780 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400781 unsigned long op;
782
Chris Masonc8b97812008-10-29 14:49:59 -0400783 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400784 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500785 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400786 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500787 BUG_ON(ret);
788
Chris Masone6dcd2d2008-07-17 12:53:50 -0400789 em = alloc_extent_map(GFP_NOFS);
790 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500791 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500792 ram_size = ins.offset;
793 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400794
Chris Masone6dcd2d2008-07-17 12:53:50 -0400795 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400796 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400797 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400798 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400799
Chris Masond3977122009-01-05 21:25:51 -0500800 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400801 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400802 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400803 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400804 if (ret != -EEXIST) {
805 free_extent_map(em);
806 break;
807 }
808 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400809 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400810 }
811
Chris Mason98d20f62008-04-14 09:46:10 -0400812 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400813 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500814 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400815 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400816
Yan Zheng17d217f2008-12-12 10:03:38 -0500817 if (root->root_key.objectid ==
818 BTRFS_DATA_RELOC_TREE_OBJECTID) {
819 ret = btrfs_reloc_clone_csums(inode, start,
820 cur_alloc_size);
821 BUG_ON(ret);
822 }
823
Chris Masond3977122009-01-05 21:25:51 -0500824 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400825 break;
Chris Masond3977122009-01-05 21:25:51 -0500826
Chris Masonc8b97812008-10-29 14:49:59 -0400827 /* we're not doing compressed IO, don't unlock the first
828 * page (which the caller expects to stay locked), don't
829 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400830 *
831 * Do set the Private2 bit so we know this page was properly
832 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400833 */
Chris Masona791e352009-10-08 11:27:10 -0400834 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
835 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
836 EXTENT_SET_PRIVATE2;
837
Chris Masonc8b97812008-10-29 14:49:59 -0400838 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
839 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400840 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400841 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500842 num_bytes -= cur_alloc_size;
843 alloc_hint = ins.objectid + ins.offset;
844 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400845 }
Chris Masonb888db22007-08-27 16:49:44 -0400846out:
Chris Mason771ed682008-11-06 22:02:51 -0500847 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400848 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400849
Chris Masonbe20aa92007-12-17 20:14:01 -0500850 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500851}
Chris Masonc8b97812008-10-29 14:49:59 -0400852
Chris Mason771ed682008-11-06 22:02:51 -0500853/*
854 * work queue call back to started compression on a file and pages
855 */
856static noinline void async_cow_start(struct btrfs_work *work)
857{
858 struct async_cow *async_cow;
859 int num_added = 0;
860 async_cow = container_of(work, struct async_cow, work);
861
862 compress_file_range(async_cow->inode, async_cow->locked_page,
863 async_cow->start, async_cow->end, async_cow,
864 &num_added);
865 if (num_added == 0)
866 async_cow->inode = NULL;
867}
868
869/*
870 * work queue call back to submit previously compressed pages
871 */
872static noinline void async_cow_submit(struct btrfs_work *work)
873{
874 struct async_cow *async_cow;
875 struct btrfs_root *root;
876 unsigned long nr_pages;
877
878 async_cow = container_of(work, struct async_cow, work);
879
880 root = async_cow->root;
881 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
882 PAGE_CACHE_SHIFT;
883
884 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
885
886 if (atomic_read(&root->fs_info->async_delalloc_pages) <
887 5 * 1042 * 1024 &&
888 waitqueue_active(&root->fs_info->async_submit_wait))
889 wake_up(&root->fs_info->async_submit_wait);
890
Chris Masond3977122009-01-05 21:25:51 -0500891 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500892 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500893}
Chris Masonc8b97812008-10-29 14:49:59 -0400894
Chris Mason771ed682008-11-06 22:02:51 -0500895static noinline void async_cow_free(struct btrfs_work *work)
896{
897 struct async_cow *async_cow;
898 async_cow = container_of(work, struct async_cow, work);
899 kfree(async_cow);
900}
901
902static int cow_file_range_async(struct inode *inode, struct page *locked_page,
903 u64 start, u64 end, int *page_started,
904 unsigned long *nr_written)
905{
906 struct async_cow *async_cow;
907 struct btrfs_root *root = BTRFS_I(inode)->root;
908 unsigned long nr_pages;
909 u64 cur_end;
910 int limit = 10 * 1024 * 1042;
911
Chris Masona3429ab2009-10-08 12:30:20 -0400912 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
913 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500914 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500915 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
916 async_cow->inode = inode;
917 async_cow->root = root;
918 async_cow->locked_page = locked_page;
919 async_cow->start = start;
920
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200921 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500922 cur_end = end;
923 else
924 cur_end = min(end, start + 512 * 1024 - 1);
925
926 async_cow->end = cur_end;
927 INIT_LIST_HEAD(&async_cow->extents);
928
929 async_cow->work.func = async_cow_start;
930 async_cow->work.ordered_func = async_cow_submit;
931 async_cow->work.ordered_free = async_cow_free;
932 async_cow->work.flags = 0;
933
Chris Mason771ed682008-11-06 22:02:51 -0500934 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
935 PAGE_CACHE_SHIFT;
936 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
937
938 btrfs_queue_worker(&root->fs_info->delalloc_workers,
939 &async_cow->work);
940
941 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
942 wait_event(root->fs_info->async_submit_wait,
943 (atomic_read(&root->fs_info->async_delalloc_pages) <
944 limit));
945 }
946
Chris Masond3977122009-01-05 21:25:51 -0500947 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500948 atomic_read(&root->fs_info->async_delalloc_pages)) {
949 wait_event(root->fs_info->async_submit_wait,
950 (atomic_read(&root->fs_info->async_delalloc_pages) ==
951 0));
952 }
953
954 *nr_written += nr_pages;
955 start = cur_end + 1;
956 }
957 *page_started = 1;
958 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500959}
960
Chris Masond3977122009-01-05 21:25:51 -0500961static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500962 u64 bytenr, u64 num_bytes)
963{
964 int ret;
965 struct btrfs_ordered_sum *sums;
966 LIST_HEAD(list);
967
Yan Zheng07d400a2009-01-06 11:42:00 -0500968 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
969 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500970 if (ret == 0 && list_empty(&list))
971 return 0;
972
973 while (!list_empty(&list)) {
974 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
975 list_del(&sums->list);
976 kfree(sums);
977 }
978 return 1;
979}
980
Chris Masond352ac62008-09-29 15:18:18 -0400981/*
982 * when nowcow writeback call back. This checks for snapshots or COW copies
983 * of the extents that exist in the file, and COWs the file as required.
984 *
985 * If no cow copies or snapshots exist, we write directly to the existing
986 * blocks on disk
987 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400988static noinline int run_delalloc_nocow(struct inode *inode,
989 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -0500990 u64 start, u64 end, int *page_started, int force,
991 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -0500992{
Chris Masonbe20aa92007-12-17 20:14:01 -0500993 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400994 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500995 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500996 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400997 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500998 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400999 u64 cow_start;
1000 u64 cur_offset;
1001 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001003 u64 disk_bytenr;
1004 u64 num_bytes;
1005 int extent_type;
1006 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001007 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001008 int nocow;
1009 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -05001010
1011 path = btrfs_alloc_path();
1012 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001013 trans = btrfs_join_transaction(root, 1);
1014 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -05001015
Yan Zheng80ff3852008-10-30 14:20:02 -04001016 cow_start = (u64)-1;
1017 cur_offset = start;
1018 while (1) {
1019 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1020 cur_offset, 0);
1021 BUG_ON(ret < 0);
1022 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1023 leaf = path->nodes[0];
1024 btrfs_item_key_to_cpu(leaf, &found_key,
1025 path->slots[0] - 1);
1026 if (found_key.objectid == inode->i_ino &&
1027 found_key.type == BTRFS_EXTENT_DATA_KEY)
1028 path->slots[0]--;
1029 }
1030 check_prev = 0;
1031next_slot:
1032 leaf = path->nodes[0];
1033 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1034 ret = btrfs_next_leaf(root, path);
1035 if (ret < 0)
1036 BUG_ON(1);
1037 if (ret > 0)
1038 break;
1039 leaf = path->nodes[0];
1040 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001041
Yan Zheng80ff3852008-10-30 14:20:02 -04001042 nocow = 0;
1043 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001044 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001045 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001046
Yan Zheng80ff3852008-10-30 14:20:02 -04001047 if (found_key.objectid > inode->i_ino ||
1048 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1049 found_key.offset > end)
1050 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001051
Yan Zheng80ff3852008-10-30 14:20:02 -04001052 if (found_key.offset > cur_offset) {
1053 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001054 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001055 goto out_check;
1056 }
Chris Masonc31f8832008-01-08 15:46:31 -05001057
Yan Zheng80ff3852008-10-30 14:20:02 -04001058 fi = btrfs_item_ptr(leaf, path->slots[0],
1059 struct btrfs_file_extent_item);
1060 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001061
Yan Zhengd899e052008-10-30 14:25:28 -04001062 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1063 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001064 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001065 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001066 extent_end = found_key.offset +
1067 btrfs_file_extent_num_bytes(leaf, fi);
1068 if (extent_end <= start) {
1069 path->slots[0]++;
1070 goto next_slot;
1071 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001072 if (disk_bytenr == 0)
1073 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001074 if (btrfs_file_extent_compression(leaf, fi) ||
1075 btrfs_file_extent_encryption(leaf, fi) ||
1076 btrfs_file_extent_other_encoding(leaf, fi))
1077 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001078 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1079 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001080 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001081 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001082 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001083 found_key.offset -
1084 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001085 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001086 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001087 disk_bytenr += cur_offset - found_key.offset;
1088 num_bytes = min(end + 1, extent_end) - cur_offset;
1089 /*
1090 * force cow if csum exists in the range.
1091 * this ensure that csum for a given extent are
1092 * either valid or do not exist.
1093 */
1094 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1095 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001096 nocow = 1;
1097 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1098 extent_end = found_key.offset +
1099 btrfs_file_extent_inline_len(leaf, fi);
1100 extent_end = ALIGN(extent_end, root->sectorsize);
1101 } else {
1102 BUG_ON(1);
1103 }
1104out_check:
1105 if (extent_end <= start) {
1106 path->slots[0]++;
1107 goto next_slot;
1108 }
1109 if (!nocow) {
1110 if (cow_start == (u64)-1)
1111 cow_start = cur_offset;
1112 cur_offset = extent_end;
1113 if (cur_offset > end)
1114 break;
1115 path->slots[0]++;
1116 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001117 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001118
Yan Zheng7ea394f2008-08-05 13:05:02 -04001119 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001120 if (cow_start != (u64)-1) {
1121 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001122 found_key.offset - 1, page_started,
1123 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001124 BUG_ON(ret);
1125 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001126 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001127
Yan Zhengd899e052008-10-30 14:25:28 -04001128 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1129 struct extent_map *em;
1130 struct extent_map_tree *em_tree;
1131 em_tree = &BTRFS_I(inode)->extent_tree;
1132 em = alloc_extent_map(GFP_NOFS);
1133 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001134 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001135 em->len = num_bytes;
1136 em->block_len = num_bytes;
1137 em->block_start = disk_bytenr;
1138 em->bdev = root->fs_info->fs_devices->latest_bdev;
1139 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1140 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001141 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001142 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001143 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001144 if (ret != -EEXIST) {
1145 free_extent_map(em);
1146 break;
1147 }
1148 btrfs_drop_extent_cache(inode, em->start,
1149 em->start + em->len - 1, 0);
1150 }
1151 type = BTRFS_ORDERED_PREALLOC;
1152 } else {
1153 type = BTRFS_ORDERED_NOCOW;
1154 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001155
1156 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001157 num_bytes, num_bytes, type);
1158 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001159
Yan Zhengd899e052008-10-30 14:25:28 -04001160 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001161 cur_offset, cur_offset + num_bytes - 1,
1162 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1163 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1164 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001165 cur_offset = extent_end;
1166 if (cur_offset > end)
1167 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001168 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001169 btrfs_release_path(root, path);
1170
1171 if (cur_offset <= end && cow_start == (u64)-1)
1172 cow_start = cur_offset;
1173 if (cow_start != (u64)-1) {
1174 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001175 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001176 BUG_ON(ret);
1177 }
1178
1179 ret = btrfs_end_transaction(trans, root);
1180 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001181 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001182 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001183}
1184
Chris Masond352ac62008-09-29 15:18:18 -04001185/*
1186 * extent_io.c call back to do delayed allocation processing
1187 */
Chris Masonc8b97812008-10-29 14:49:59 -04001188static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001189 u64 start, u64 end, int *page_started,
1190 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001191{
Chris Masonbe20aa92007-12-17 20:14:01 -05001192 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001193 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001194
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001195 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001196 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001197 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001198 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001199 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001200 page_started, 0, nr_written);
Chris Mason7f366cf2009-03-12 20:12:45 -04001201 else if (!btrfs_test_opt(root, COMPRESS))
1202 ret = cow_file_range(inode, locked_page, start, end,
1203 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001204 else
Chris Mason771ed682008-11-06 22:02:51 -05001205 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001206 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001207 return ret;
1208}
1209
Josef Bacik9ed74f22009-09-11 16:12:44 -04001210static int btrfs_split_extent_hook(struct inode *inode,
1211 struct extent_state *orig, u64 split)
1212{
1213 struct btrfs_root *root = BTRFS_I(inode)->root;
1214 u64 size;
1215
1216 if (!(orig->state & EXTENT_DELALLOC))
1217 return 0;
1218
1219 size = orig->end - orig->start + 1;
1220 if (size > root->fs_info->max_extent) {
1221 u64 num_extents;
1222 u64 new_size;
1223
1224 new_size = orig->end - split + 1;
1225 num_extents = div64_u64(size + root->fs_info->max_extent - 1,
1226 root->fs_info->max_extent);
1227
1228 /*
Josef Bacik32c00af2009-10-08 13:34:05 -04001229 * if we break a large extent up then leave oustanding_extents
1230 * be, since we've already accounted for the large extent.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001231 */
1232 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1233 root->fs_info->max_extent) < num_extents)
1234 return 0;
1235 }
1236
Josef Bacik32c00af2009-10-08 13:34:05 -04001237 spin_lock(&BTRFS_I(inode)->accounting_lock);
1238 BTRFS_I(inode)->outstanding_extents++;
1239 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001240
1241 return 0;
1242}
1243
1244/*
1245 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1246 * extents so we can keep track of new extents that are just merged onto old
1247 * extents, such as when we are doing sequential writes, so we can properly
1248 * account for the metadata space we'll need.
1249 */
1250static int btrfs_merge_extent_hook(struct inode *inode,
1251 struct extent_state *new,
1252 struct extent_state *other)
1253{
1254 struct btrfs_root *root = BTRFS_I(inode)->root;
1255 u64 new_size, old_size;
1256 u64 num_extents;
1257
1258 /* not delalloc, ignore it */
1259 if (!(other->state & EXTENT_DELALLOC))
1260 return 0;
1261
1262 old_size = other->end - other->start + 1;
1263 if (new->start < other->start)
1264 new_size = other->end - new->start + 1;
1265 else
1266 new_size = new->end - other->start + 1;
1267
1268 /* we're not bigger than the max, unreserve the space and go */
1269 if (new_size <= root->fs_info->max_extent) {
Josef Bacik32c00af2009-10-08 13:34:05 -04001270 spin_lock(&BTRFS_I(inode)->accounting_lock);
1271 BTRFS_I(inode)->outstanding_extents--;
1272 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001273 return 0;
1274 }
1275
1276 /*
1277 * If we grew by another max_extent, just return, we want to keep that
1278 * reserved amount.
1279 */
1280 num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
1281 root->fs_info->max_extent);
1282 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1283 root->fs_info->max_extent) > num_extents)
1284 return 0;
1285
Josef Bacik32c00af2009-10-08 13:34:05 -04001286 spin_lock(&BTRFS_I(inode)->accounting_lock);
1287 BTRFS_I(inode)->outstanding_extents--;
1288 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001289
1290 return 0;
1291}
1292
Chris Masond352ac62008-09-29 15:18:18 -04001293/*
1294 * extent_io.c set_bit_hook, used to track delayed allocation
1295 * bytes in this file, and to maintain the list of inodes that
1296 * have pending delalloc work to be done.
1297 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001298static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001299 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001300{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001301
Chris Mason75eff682008-12-15 15:54:40 -05001302 /*
1303 * set_bit and clear bit hooks normally require _irqsave/restore
1304 * but in this case, we are only testeing for the DELALLOC
1305 * bit, which is only set or cleared with irqs on
1306 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001307 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001308 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001309
Josef Bacik32c00af2009-10-08 13:34:05 -04001310 spin_lock(&BTRFS_I(inode)->accounting_lock);
1311 BTRFS_I(inode)->outstanding_extents++;
1312 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik6a632092009-02-20 11:00:09 -05001313 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
Chris Mason75eff682008-12-15 15:54:40 -05001314 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001315 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001316 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001317 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1318 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1319 &root->fs_info->delalloc_inodes);
1320 }
Chris Mason75eff682008-12-15 15:54:40 -05001321 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001322 }
1323 return 0;
1324}
1325
Chris Masond352ac62008-09-29 15:18:18 -04001326/*
1327 * extent_io.c clear_bit_hook, see set_bit_hook for why
1328 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001329static int btrfs_clear_bit_hook(struct inode *inode,
1330 struct extent_state *state, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001331{
Chris Mason75eff682008-12-15 15:54:40 -05001332 /*
1333 * set_bit and clear bit hooks normally require _irqsave/restore
1334 * but in this case, we are only testeing for the DELALLOC
1335 * bit, which is only set or cleared with irqs on
1336 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001337 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001338 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001339
Josef Bacik32c00af2009-10-08 13:34:05 -04001340 if (bits & EXTENT_DO_ACCOUNTING) {
1341 spin_lock(&BTRFS_I(inode)->accounting_lock);
1342 BTRFS_I(inode)->outstanding_extents--;
1343 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1344 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1345 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04001346
Chris Mason75eff682008-12-15 15:54:40 -05001347 spin_lock(&root->fs_info->delalloc_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001348 if (state->end - state->start + 1 >
1349 root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001350 printk(KERN_INFO "btrfs warning: delalloc account "
1351 "%llu %llu\n",
Josef Bacik9ed74f22009-09-11 16:12:44 -04001352 (unsigned long long)
1353 state->end - state->start + 1,
Chris Masond3977122009-01-05 21:25:51 -05001354 (unsigned long long)
1355 root->fs_info->delalloc_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001356 btrfs_delalloc_free_space(root, inode, (u64)-1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001357 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001358 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001359 } else {
Josef Bacik6a632092009-02-20 11:00:09 -05001360 btrfs_delalloc_free_space(root, inode,
Josef Bacik9ed74f22009-09-11 16:12:44 -04001361 state->end -
1362 state->start + 1);
1363 root->fs_info->delalloc_bytes -= state->end -
1364 state->start + 1;
1365 BTRFS_I(inode)->delalloc_bytes -= state->end -
1366 state->start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001367 }
Chris Masonea8c2812008-08-04 23:17:27 -04001368 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1369 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1370 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1371 }
Chris Mason75eff682008-12-15 15:54:40 -05001372 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001373 }
1374 return 0;
1375}
1376
Chris Masond352ac62008-09-29 15:18:18 -04001377/*
1378 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1379 * we don't create bios that span stripes or chunks
1380 */
Chris Mason239b14b2008-03-24 15:02:07 -04001381int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001382 size_t size, struct bio *bio,
1383 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001384{
1385 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1386 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001387 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001388 u64 length = 0;
1389 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001390 int ret;
1391
Chris Mason771ed682008-11-06 22:02:51 -05001392 if (bio_flags & EXTENT_BIO_COMPRESSED)
1393 return 0;
1394
Chris Masonf2d8d742008-04-21 10:03:05 -04001395 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001396 map_tree = &root->fs_info->mapping_tree;
1397 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001398 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001399 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001400
Chris Masond3977122009-01-05 21:25:51 -05001401 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001402 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001403 return 0;
1404}
1405
Chris Masond352ac62008-09-29 15:18:18 -04001406/*
1407 * in order to insert checksums into the metadata in large chunks,
1408 * we wait until bio submission time. All the pages in the bio are
1409 * checksummed and sums are attached onto the ordered extent record.
1410 *
1411 * At IO completion time the cums attached on the ordered extent record
1412 * are inserted into the btree
1413 */
Chris Masond3977122009-01-05 21:25:51 -05001414static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1415 struct bio *bio, int mirror_num,
1416 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001417{
Chris Mason065631f2008-02-20 12:07:25 -05001418 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001419 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001420
Chris Masond20f7042008-12-08 16:58:54 -05001421 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001422 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001423 return 0;
1424}
Chris Masone0156402008-04-16 11:15:20 -04001425
Chris Mason4a69a412008-11-06 22:03:00 -05001426/*
1427 * in order to insert checksums into the metadata in large chunks,
1428 * we wait until bio submission time. All the pages in the bio are
1429 * checksummed and sums are attached onto the ordered extent record.
1430 *
1431 * At IO completion time the cums attached on the ordered extent record
1432 * are inserted into the btree
1433 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001434static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001435 int mirror_num, unsigned long bio_flags)
1436{
1437 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001438 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001439}
1440
Chris Masond352ac62008-09-29 15:18:18 -04001441/*
Chris Masoncad321a2008-12-17 14:51:42 -05001442 * extent_io.c submission hook. This does the right thing for csum calculation
1443 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001444 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001445static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001446 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001447{
1448 struct btrfs_root *root = BTRFS_I(inode)->root;
1449 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001450 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001451
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001452 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001453
Chris Masone6dcd2d2008-07-17 12:53:50 -04001454 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1455 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001456
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001457 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001458 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001459 return btrfs_submit_compressed_read(inode, bio,
1460 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001461 } else if (!skip_sum)
1462 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001463 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001464 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001465 /* csum items have already been cloned */
1466 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1467 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001468 /* we're doing a write, do the async checksumming */
1469 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001470 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001471 bio_flags, __btrfs_submit_bio_start,
1472 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001473 }
1474
Chris Mason0b86a832008-03-24 15:01:56 -04001475mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001476 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001477}
Chris Mason6885f302008-02-20 16:11:05 -05001478
Chris Masond352ac62008-09-29 15:18:18 -04001479/*
1480 * given a list of ordered sums record them in the inode. This happens
1481 * at IO completion time based on sums calculated at bio submission time.
1482 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001483static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001484 struct inode *inode, u64 file_offset,
1485 struct list_head *list)
1486{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001487 struct btrfs_ordered_sum *sum;
1488
1489 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001490
1491 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001492 btrfs_csum_file_blocks(trans,
1493 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001494 }
1495 return 0;
1496}
1497
Chris Masonea8c2812008-08-04 23:17:27 -04001498int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1499{
Chris Masond3977122009-01-05 21:25:51 -05001500 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001501 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001502 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1503 GFP_NOFS);
1504}
1505
Chris Masond352ac62008-09-29 15:18:18 -04001506/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001507struct btrfs_writepage_fixup {
1508 struct page *page;
1509 struct btrfs_work work;
1510};
1511
Christoph Hellwigb2950862008-12-02 09:54:17 -05001512static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001513{
1514 struct btrfs_writepage_fixup *fixup;
1515 struct btrfs_ordered_extent *ordered;
1516 struct page *page;
1517 struct inode *inode;
1518 u64 page_start;
1519 u64 page_end;
1520
1521 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1522 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001523again:
Chris Mason247e7432008-07-17 12:53:51 -04001524 lock_page(page);
1525 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1526 ClearPageChecked(page);
1527 goto out_page;
1528 }
1529
1530 inode = page->mapping->host;
1531 page_start = page_offset(page);
1532 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1533
1534 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001535
1536 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001537 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001538 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001539
1540 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1541 if (ordered) {
1542 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1543 page_end, GFP_NOFS);
1544 unlock_page(page);
1545 btrfs_start_ordered_extent(inode, ordered, 1);
1546 goto again;
1547 }
Chris Mason247e7432008-07-17 12:53:51 -04001548
Chris Masonea8c2812008-08-04 23:17:27 -04001549 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001550 ClearPageChecked(page);
1551out:
1552 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1553out_page:
1554 unlock_page(page);
1555 page_cache_release(page);
1556}
1557
1558/*
1559 * There are a few paths in the higher layers of the kernel that directly
1560 * set the page dirty bit without asking the filesystem if it is a
1561 * good idea. This causes problems because we want to make sure COW
1562 * properly happens and the data=ordered rules are followed.
1563 *
Chris Masonc8b97812008-10-29 14:49:59 -04001564 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001565 * hasn't been properly setup for IO. We kick off an async process
1566 * to fix it up. The async helper will wait for ordered extents, set
1567 * the delalloc bit and make it safe to write the page.
1568 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001569static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001570{
1571 struct inode *inode = page->mapping->host;
1572 struct btrfs_writepage_fixup *fixup;
1573 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001574
Chris Mason8b62b722009-09-02 16:53:46 -04001575 /* this page is properly in the ordered list */
1576 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001577 return 0;
1578
1579 if (PageChecked(page))
1580 return -EAGAIN;
1581
1582 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1583 if (!fixup)
1584 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001585
Chris Mason247e7432008-07-17 12:53:51 -04001586 SetPageChecked(page);
1587 page_cache_get(page);
1588 fixup->work.func = btrfs_writepage_fixup_worker;
1589 fixup->page = page;
1590 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1591 return -EAGAIN;
1592}
1593
Yan Zhengd899e052008-10-30 14:25:28 -04001594static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1595 struct inode *inode, u64 file_pos,
1596 u64 disk_bytenr, u64 disk_num_bytes,
1597 u64 num_bytes, u64 ram_bytes,
1598 u8 compression, u8 encryption,
1599 u16 other_encoding, int extent_type)
1600{
1601 struct btrfs_root *root = BTRFS_I(inode)->root;
1602 struct btrfs_file_extent_item *fi;
1603 struct btrfs_path *path;
1604 struct extent_buffer *leaf;
1605 struct btrfs_key ins;
1606 u64 hint;
1607 int ret;
1608
1609 path = btrfs_alloc_path();
1610 BUG_ON(!path);
1611
Chris Masonb9473432009-03-13 11:00:37 -04001612 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001613
1614 /*
1615 * we may be replacing one extent in the tree with another.
1616 * The new extent is pinned in the extent map, and we don't want
1617 * to drop it from the cache until it is completely in the btree.
1618 *
1619 * So, tell btrfs_drop_extents to leave this extent in the cache.
1620 * the caller is expected to unpin it and allow it to be merged
1621 * with the others.
1622 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001623 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1624 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001625 BUG_ON(ret);
1626
1627 ins.objectid = inode->i_ino;
1628 ins.offset = file_pos;
1629 ins.type = BTRFS_EXTENT_DATA_KEY;
1630 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1631 BUG_ON(ret);
1632 leaf = path->nodes[0];
1633 fi = btrfs_item_ptr(leaf, path->slots[0],
1634 struct btrfs_file_extent_item);
1635 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1636 btrfs_set_file_extent_type(leaf, fi, extent_type);
1637 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1638 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1639 btrfs_set_file_extent_offset(leaf, fi, 0);
1640 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1641 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1642 btrfs_set_file_extent_compression(leaf, fi, compression);
1643 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1644 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001645
1646 btrfs_unlock_up_safe(path, 1);
1647 btrfs_set_lock_blocking(leaf);
1648
Yan Zhengd899e052008-10-30 14:25:28 -04001649 btrfs_mark_buffer_dirty(leaf);
1650
1651 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001652
1653 ins.objectid = disk_bytenr;
1654 ins.offset = disk_num_bytes;
1655 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001656 ret = btrfs_alloc_reserved_file_extent(trans, root,
1657 root->root_key.objectid,
1658 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001659 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001660 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001661
Yan Zhengd899e052008-10-30 14:25:28 -04001662 return 0;
1663}
1664
Chris Mason5d13a982009-03-13 11:41:46 -04001665/*
1666 * helper function for btrfs_finish_ordered_io, this
1667 * just reads in some of the csum leaves to prime them into ram
1668 * before we start the transaction. It limits the amount of btree
1669 * reads required while inside the transaction.
1670 */
1671static noinline void reada_csum(struct btrfs_root *root,
1672 struct btrfs_path *path,
1673 struct btrfs_ordered_extent *ordered_extent)
1674{
1675 struct btrfs_ordered_sum *sum;
1676 u64 bytenr;
1677
1678 sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
1679 list);
1680 bytenr = sum->sums[0].bytenr;
1681
1682 /*
1683 * we don't care about the results, the point of this search is
1684 * just to get the btree leaves into ram
1685 */
1686 btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
1687}
1688
Chris Masond352ac62008-09-29 15:18:18 -04001689/* as ordered data IO finishes, this gets called so we can finish
1690 * an ordered extent if the range of bytes in the file it covers are
1691 * fully written.
1692 */
Chris Mason211f90e2008-07-18 11:56:15 -04001693static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001694{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001695 struct btrfs_root *root = BTRFS_I(inode)->root;
1696 struct btrfs_trans_handle *trans;
Chris Mason5d13a982009-03-13 11:41:46 -04001697 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001698 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001699 struct btrfs_path *path;
Yan Zhengd899e052008-10-30 14:25:28 -04001700 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001701 int ret;
1702
1703 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001704 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001705 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001706
Chris Masonb7ec40d2009-03-12 20:12:45 -04001707 /*
1708 * before we join the transaction, try to do some of our IO.
1709 * This will limit the amount of IO that we have to do with
1710 * the transaction running. We're unlikely to need to do any
1711 * IO if the file extents are new, the disk_i_size checks
1712 * covers the most common case.
1713 */
1714 if (start < BTRFS_I(inode)->disk_i_size) {
1715 path = btrfs_alloc_path();
1716 if (path) {
1717 ret = btrfs_lookup_file_extent(NULL, root, path,
1718 inode->i_ino,
1719 start, 0);
Chris Mason5d13a982009-03-13 11:41:46 -04001720 ordered_extent = btrfs_lookup_ordered_extent(inode,
1721 start);
1722 if (!list_empty(&ordered_extent->list)) {
1723 btrfs_release_path(root, path);
1724 reada_csum(root, path, ordered_extent);
1725 }
Chris Masonb7ec40d2009-03-12 20:12:45 -04001726 btrfs_free_path(path);
1727 }
1728 }
1729
Chris Masonf9295742008-07-17 12:54:14 -04001730 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001731
Chris Mason5d13a982009-03-13 11:41:46 -04001732 if (!ordered_extent)
1733 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001734 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001735 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1736 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001737
1738 lock_extent(io_tree, ordered_extent->file_offset,
1739 ordered_extent->file_offset + ordered_extent->len - 1,
1740 GFP_NOFS);
1741
Chris Masonc8b97812008-10-29 14:49:59 -04001742 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001743 compressed = 1;
1744 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1745 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001746 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001747 ordered_extent->file_offset,
1748 ordered_extent->file_offset +
1749 ordered_extent->len);
1750 BUG_ON(ret);
1751 } else {
1752 ret = insert_reserved_file_extent(trans, inode,
1753 ordered_extent->file_offset,
1754 ordered_extent->start,
1755 ordered_extent->disk_len,
1756 ordered_extent->len,
1757 ordered_extent->len,
1758 compressed, 0, 0,
1759 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001760 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1761 ordered_extent->file_offset,
1762 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001763 BUG_ON(ret);
1764 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001765 unlock_extent(io_tree, ordered_extent->file_offset,
1766 ordered_extent->file_offset + ordered_extent->len - 1,
1767 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001768nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001769 add_pending_csums(trans, inode, ordered_extent->file_offset,
1770 &ordered_extent->list);
1771
Chris Mason34353022008-09-23 20:19:49 -04001772 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001773 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001774 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001775 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001776 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001777
Chris Masone6dcd2d2008-07-17 12:53:50 -04001778 /* once for us */
1779 btrfs_put_ordered_extent(ordered_extent);
1780 /* once for the tree */
1781 btrfs_put_ordered_extent(ordered_extent);
1782
Chris Masone6dcd2d2008-07-17 12:53:50 -04001783 btrfs_end_transaction(trans, root);
1784 return 0;
1785}
1786
Christoph Hellwigb2950862008-12-02 09:54:17 -05001787static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001788 struct extent_state *state, int uptodate)
1789{
Chris Mason8b62b722009-09-02 16:53:46 -04001790 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001791 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1792}
1793
Chris Masond352ac62008-09-29 15:18:18 -04001794/*
1795 * When IO fails, either with EIO or csum verification fails, we
1796 * try other mirrors that might have a good copy of the data. This
1797 * io_failure_record is used to record state as we go through all the
1798 * mirrors. If another mirror has good data, the page is set up to date
1799 * and things continue. If a good mirror can't be found, the original
1800 * bio end_io callback is called to indicate things have failed.
1801 */
Chris Mason7e383262008-04-09 16:28:12 -04001802struct io_failure_record {
1803 struct page *page;
1804 u64 start;
1805 u64 len;
1806 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001807 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001808 int last_mirror;
1809};
1810
Christoph Hellwigb2950862008-12-02 09:54:17 -05001811static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001812 struct page *page, u64 start, u64 end,
1813 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001814{
1815 struct io_failure_record *failrec = NULL;
1816 u64 private;
1817 struct extent_map *em;
1818 struct inode *inode = page->mapping->host;
1819 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001820 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001821 struct bio *bio;
1822 int num_copies;
1823 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001824 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001825 u64 logical;
1826
1827 ret = get_state_private(failure_tree, start, &private);
1828 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001829 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1830 if (!failrec)
1831 return -ENOMEM;
1832 failrec->start = start;
1833 failrec->len = end - start + 1;
1834 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001835 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001836
Chris Mason890871b2009-09-02 16:24:52 -04001837 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001838 em = lookup_extent_mapping(em_tree, start, failrec->len);
1839 if (em->start > start || em->start + em->len < start) {
1840 free_extent_map(em);
1841 em = NULL;
1842 }
Chris Mason890871b2009-09-02 16:24:52 -04001843 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001844
1845 if (!em || IS_ERR(em)) {
1846 kfree(failrec);
1847 return -EIO;
1848 }
1849 logical = start - em->start;
1850 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001851 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1852 logical = em->block_start;
1853 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1854 }
Chris Mason7e383262008-04-09 16:28:12 -04001855 failrec->logical = logical;
1856 free_extent_map(em);
1857 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1858 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001859 set_state_private(failure_tree, start,
1860 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001861 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001862 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001863 }
1864 num_copies = btrfs_num_copies(
1865 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1866 failrec->logical, failrec->len);
1867 failrec->last_mirror++;
1868 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001869 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001870 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1871 failrec->start,
1872 EXTENT_LOCKED);
1873 if (state && state->start != failrec->start)
1874 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001875 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001876 }
1877 if (!state || failrec->last_mirror > num_copies) {
1878 set_state_private(failure_tree, failrec->start, 0);
1879 clear_extent_bits(failure_tree, failrec->start,
1880 failrec->start + failrec->len - 1,
1881 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1882 kfree(failrec);
1883 return -EIO;
1884 }
1885 bio = bio_alloc(GFP_NOFS, 1);
1886 bio->bi_private = state;
1887 bio->bi_end_io = failed_bio->bi_end_io;
1888 bio->bi_sector = failrec->logical >> 9;
1889 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001890 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001891
Chris Mason7e383262008-04-09 16:28:12 -04001892 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001893 if (failed_bio->bi_rw & (1 << BIO_RW))
1894 rw = WRITE;
1895 else
1896 rw = READ;
1897
1898 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001899 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001900 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001901 return 0;
1902}
1903
Chris Masond352ac62008-09-29 15:18:18 -04001904/*
1905 * each time an IO finishes, we do a fast check in the IO failure tree
1906 * to see if we need to process or clean up an io_failure_record
1907 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001908static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001909{
1910 u64 private;
1911 u64 private_failure;
1912 struct io_failure_record *failure;
1913 int ret;
1914
1915 private = 0;
1916 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1917 (u64)-1, 1, EXTENT_DIRTY)) {
1918 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1919 start, &private_failure);
1920 if (ret == 0) {
1921 failure = (struct io_failure_record *)(unsigned long)
1922 private_failure;
1923 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1924 failure->start, 0);
1925 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1926 failure->start,
1927 failure->start + failure->len - 1,
1928 EXTENT_DIRTY | EXTENT_LOCKED,
1929 GFP_NOFS);
1930 kfree(failure);
1931 }
1932 }
Chris Mason7e383262008-04-09 16:28:12 -04001933 return 0;
1934}
1935
Chris Masond352ac62008-09-29 15:18:18 -04001936/*
1937 * when reads are done, we need to check csums to verify the data is correct
1938 * if there's a match, we allow the bio to finish. If not, we go through
1939 * the io_failure_record routines to find good copies
1940 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001941static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001942 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001943{
Chris Mason35ebb932007-10-30 16:56:53 -04001944 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001945 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001946 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001947 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001948 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001949 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001950 struct btrfs_root *root = BTRFS_I(inode)->root;
1951 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001952
Chris Masond20f7042008-12-08 16:58:54 -05001953 if (PageChecked(page)) {
1954 ClearPageChecked(page);
1955 goto good;
1956 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001957
1958 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001959 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001960
Yan Zheng17d217f2008-12-12 10:03:38 -05001961 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001962 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001963 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1964 GFP_NOFS);
1965 return 0;
1966 }
1967
Yanc2e639f2008-02-04 08:57:25 -05001968 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001969 private = state->private;
1970 ret = 0;
1971 } else {
1972 ret = get_state_private(io_tree, start, &private);
1973 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001974 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001975 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001976 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001977
Chris Masonff79f812007-10-15 16:22:25 -04001978 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1979 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001980 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001981 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001982
Chris Mason9ab86c82009-01-07 09:48:51 -05001983 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001984good:
Chris Mason7e383262008-04-09 16:28:12 -04001985 /* if the io failure tree for this inode is non-empty,
1986 * check to see if we've recovered from a failed IO
1987 */
Chris Mason1259ab72008-05-12 13:39:03 -04001988 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001989 return 0;
1990
1991zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001992 if (printk_ratelimit()) {
1993 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1994 "private %llu\n", page->mapping->host->i_ino,
1995 (unsigned long long)start, csum,
1996 (unsigned long long)private);
1997 }
Chris Masondb945352007-10-15 16:15:53 -04001998 memset(kaddr + offset, 1, end - start + 1);
1999 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05002000 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04002001 if (private == 0)
2002 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002003 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002004}
Chris Masonb888db22007-08-27 16:49:44 -04002005
Josef Bacik7b128762008-07-24 12:17:14 -04002006/*
2007 * This creates an orphan entry for the given inode in case something goes
2008 * wrong in the middle of an unlink/truncate.
2009 */
2010int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2011{
2012 struct btrfs_root *root = BTRFS_I(inode)->root;
2013 int ret = 0;
2014
Yanbcc63ab2008-07-30 16:29:20 -04002015 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002016
2017 /* already on the orphan list, we're good */
2018 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002019 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002020 return 0;
2021 }
2022
2023 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2024
Yanbcc63ab2008-07-30 16:29:20 -04002025 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002026
2027 /*
2028 * insert an orphan item to track this unlinked/truncated file
2029 */
2030 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2031
2032 return ret;
2033}
2034
2035/*
2036 * We have done the truncate/delete so we can go ahead and remove the orphan
2037 * item for this particular inode.
2038 */
2039int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2040{
2041 struct btrfs_root *root = BTRFS_I(inode)->root;
2042 int ret = 0;
2043
Yanbcc63ab2008-07-30 16:29:20 -04002044 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002045
2046 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002047 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002048 return 0;
2049 }
2050
2051 list_del_init(&BTRFS_I(inode)->i_orphan);
2052 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04002053 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002054 return 0;
2055 }
2056
Yanbcc63ab2008-07-30 16:29:20 -04002057 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002058
2059 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2060
2061 return ret;
2062}
2063
2064/*
2065 * this cleans up any orphans that may be left on the list from the last use
2066 * of this root.
2067 */
2068void btrfs_orphan_cleanup(struct btrfs_root *root)
2069{
2070 struct btrfs_path *path;
2071 struct extent_buffer *leaf;
2072 struct btrfs_item *item;
2073 struct btrfs_key key, found_key;
2074 struct btrfs_trans_handle *trans;
2075 struct inode *inode;
2076 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2077
Josef Bacik7b128762008-07-24 12:17:14 -04002078 path = btrfs_alloc_path();
2079 if (!path)
2080 return;
2081 path->reada = -1;
2082
2083 key.objectid = BTRFS_ORPHAN_OBJECTID;
2084 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2085 key.offset = (u64)-1;
2086
Josef Bacik7b128762008-07-24 12:17:14 -04002087
2088 while (1) {
2089 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2090 if (ret < 0) {
2091 printk(KERN_ERR "Error searching slot for orphan: %d"
2092 "\n", ret);
2093 break;
2094 }
2095
2096 /*
2097 * if ret == 0 means we found what we were searching for, which
2098 * is weird, but possible, so only screw with path if we didnt
2099 * find the key and see if we have stuff that matches
2100 */
2101 if (ret > 0) {
2102 if (path->slots[0] == 0)
2103 break;
2104 path->slots[0]--;
2105 }
2106
2107 /* pull out the item */
2108 leaf = path->nodes[0];
2109 item = btrfs_item_nr(leaf, path->slots[0]);
2110 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2111
2112 /* make sure the item matches what we want */
2113 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2114 break;
2115 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2116 break;
2117
2118 /* release the path since we're done with it */
2119 btrfs_release_path(root, path);
2120
2121 /*
2122 * this is where we are basically btrfs_lookup, without the
2123 * crossing root thing. we store the inode number in the
2124 * offset of the orphan item.
2125 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002126 found_key.objectid = found_key.offset;
2127 found_key.type = BTRFS_INODE_ITEM_KEY;
2128 found_key.offset = 0;
2129 inode = btrfs_iget(root->fs_info->sb, &found_key, root);
2130 if (IS_ERR(inode))
Josef Bacik7b128762008-07-24 12:17:14 -04002131 break;
2132
Josef Bacik7b128762008-07-24 12:17:14 -04002133 /*
2134 * add this inode to the orphan list so btrfs_orphan_del does
2135 * the proper thing when we hit it
2136 */
Yanbcc63ab2008-07-30 16:29:20 -04002137 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002138 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04002139 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002140
2141 /*
2142 * if this is a bad inode, means we actually succeeded in
2143 * removing the inode, but not the orphan record, which means
2144 * we need to manually delete the orphan since iput will just
2145 * do a destroy_inode
2146 */
2147 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002148 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04002149 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002150 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002151 iput(inode);
2152 continue;
2153 }
2154
2155 /* if we have links, this was a truncate, lets do that */
2156 if (inode->i_nlink) {
2157 nr_truncate++;
2158 btrfs_truncate(inode);
2159 } else {
2160 nr_unlink++;
2161 }
2162
2163 /* this will do delete_inode and everything for us */
2164 iput(inode);
2165 }
2166
2167 if (nr_unlink)
2168 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2169 if (nr_truncate)
2170 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2171
2172 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002173}
2174
Chris Masond352ac62008-09-29 15:18:18 -04002175/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002176 * very simple check to peek ahead in the leaf looking for xattrs. If we
2177 * don't find any xattrs, we know there can't be any acls.
2178 *
2179 * slot is the slot the inode is in, objectid is the objectid of the inode
2180 */
2181static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2182 int slot, u64 objectid)
2183{
2184 u32 nritems = btrfs_header_nritems(leaf);
2185 struct btrfs_key found_key;
2186 int scanned = 0;
2187
2188 slot++;
2189 while (slot < nritems) {
2190 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2191
2192 /* we found a different objectid, there must not be acls */
2193 if (found_key.objectid != objectid)
2194 return 0;
2195
2196 /* we found an xattr, assume we've got an acl */
2197 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2198 return 1;
2199
2200 /*
2201 * we found a key greater than an xattr key, there can't
2202 * be any acls later on
2203 */
2204 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2205 return 0;
2206
2207 slot++;
2208 scanned++;
2209
2210 /*
2211 * it goes inode, inode backrefs, xattrs, extents,
2212 * so if there are a ton of hard links to an inode there can
2213 * be a lot of backrefs. Don't waste time searching too hard,
2214 * this is just an optimization
2215 */
2216 if (scanned >= 8)
2217 break;
2218 }
2219 /* we hit the end of the leaf before we found an xattr or
2220 * something larger than an xattr. We have to assume the inode
2221 * has acls
2222 */
2223 return 1;
2224}
2225
2226/*
Chris Masond352ac62008-09-29 15:18:18 -04002227 * read an inode from the btree into the in-memory inode
2228 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002229static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002230{
2231 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002232 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002233 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002234 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002235 struct btrfs_root *root = BTRFS_I(inode)->root;
2236 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002237 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002238 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002239 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002240 int ret;
2241
2242 path = btrfs_alloc_path();
2243 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002244 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002245
Chris Mason39279cc2007-06-12 06:35:45 -04002246 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002247 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002248 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002249
Chris Mason5f39d392007-10-15 16:14:19 -04002250 leaf = path->nodes[0];
2251 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2252 struct btrfs_inode_item);
2253
2254 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2255 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2256 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2257 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002258 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002259
2260 tspec = btrfs_inode_atime(inode_item);
2261 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2262 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2263
2264 tspec = btrfs_inode_mtime(inode_item);
2265 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2266 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2267
2268 tspec = btrfs_inode_ctime(inode_item);
2269 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2270 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2271
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002272 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002273 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002274 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002275 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002276 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002277 rdev = btrfs_inode_rdev(leaf, inode_item);
2278
Josef Bacikaec74772008-07-24 12:12:38 -04002279 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002280 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002281
Chris Mason5f39d392007-10-15 16:14:19 -04002282 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002283
Chris Mason46a53cc2009-04-27 11:47:50 -04002284 /*
2285 * try to precache a NULL acl entry for files that don't have
2286 * any xattrs or acls
2287 */
2288 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002289 if (!maybe_acls)
2290 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002291
Yan Zhengd2fb3432008-12-11 16:30:39 -05002292 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2293 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002294 btrfs_free_path(path);
2295 inode_item = NULL;
2296
Chris Mason39279cc2007-06-12 06:35:45 -04002297 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002298 case S_IFREG:
2299 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002300 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002301 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002302 inode->i_fop = &btrfs_file_operations;
2303 inode->i_op = &btrfs_file_inode_operations;
2304 break;
2305 case S_IFDIR:
2306 inode->i_fop = &btrfs_dir_file_operations;
2307 if (root == root->fs_info->tree_root)
2308 inode->i_op = &btrfs_dir_ro_inode_operations;
2309 else
2310 inode->i_op = &btrfs_dir_inode_operations;
2311 break;
2312 case S_IFLNK:
2313 inode->i_op = &btrfs_symlink_inode_operations;
2314 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002315 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002316 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002317 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002318 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002319 init_special_inode(inode, inode->i_mode, rdev);
2320 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002321 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002322
2323 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002324 return;
2325
2326make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002327 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002328 make_bad_inode(inode);
2329}
2330
Chris Masond352ac62008-09-29 15:18:18 -04002331/*
2332 * given a leaf and an inode, copy the inode fields into the leaf
2333 */
Chris Masone02119d2008-09-05 16:13:11 -04002334static void fill_inode_item(struct btrfs_trans_handle *trans,
2335 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002336 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002337 struct inode *inode)
2338{
Chris Mason5f39d392007-10-15 16:14:19 -04002339 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2340 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002341 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002342 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2343 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2344
2345 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2346 inode->i_atime.tv_sec);
2347 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2348 inode->i_atime.tv_nsec);
2349
2350 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2351 inode->i_mtime.tv_sec);
2352 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2353 inode->i_mtime.tv_nsec);
2354
2355 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2356 inode->i_ctime.tv_sec);
2357 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2358 inode->i_ctime.tv_nsec);
2359
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002360 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002361 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002362 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002363 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002364 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002365 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002366 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002367}
2368
Chris Masond352ac62008-09-29 15:18:18 -04002369/*
2370 * copy everything in the in-memory inode into the btree.
2371 */
Chris Masond3977122009-01-05 21:25:51 -05002372noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2373 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002374{
2375 struct btrfs_inode_item *inode_item;
2376 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002377 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002378 int ret;
2379
2380 path = btrfs_alloc_path();
2381 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002382 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002383 ret = btrfs_lookup_inode(trans, root, path,
2384 &BTRFS_I(inode)->location, 1);
2385 if (ret) {
2386 if (ret > 0)
2387 ret = -ENOENT;
2388 goto failed;
2389 }
2390
Chris Masonb4ce94d2009-02-04 09:25:08 -05002391 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002392 leaf = path->nodes[0];
2393 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002394 struct btrfs_inode_item);
2395
Chris Masone02119d2008-09-05 16:13:11 -04002396 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002397 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002398 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002399 ret = 0;
2400failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002401 btrfs_free_path(path);
2402 return ret;
2403}
2404
2405
Chris Masond352ac62008-09-29 15:18:18 -04002406/*
2407 * unlink helper that gets used here in inode.c and in the tree logging
2408 * recovery code. It remove a link in a directory with a given name, and
2409 * also drops the back refs in the inode to the directory
2410 */
Chris Masone02119d2008-09-05 16:13:11 -04002411int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2412 struct btrfs_root *root,
2413 struct inode *dir, struct inode *inode,
2414 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002415{
2416 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002417 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002418 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002419 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002420 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002421 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002422
2423 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002424 if (!path) {
2425 ret = -ENOMEM;
2426 goto err;
2427 }
2428
Chris Masonb9473432009-03-13 11:00:37 -04002429 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002430 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2431 name, name_len, -1);
2432 if (IS_ERR(di)) {
2433 ret = PTR_ERR(di);
2434 goto err;
2435 }
2436 if (!di) {
2437 ret = -ENOENT;
2438 goto err;
2439 }
Chris Mason5f39d392007-10-15 16:14:19 -04002440 leaf = path->nodes[0];
2441 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002442 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002443 if (ret)
2444 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002445 btrfs_release_path(root, path);
2446
Josef Bacikaec74772008-07-24 12:12:38 -04002447 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002448 inode->i_ino,
2449 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002450 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002451 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002452 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002453 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002454 goto err;
2455 }
2456
Chris Mason39279cc2007-06-12 06:35:45 -04002457 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002458 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002459 if (IS_ERR(di)) {
2460 ret = PTR_ERR(di);
2461 goto err;
2462 }
2463 if (!di) {
2464 ret = -ENOENT;
2465 goto err;
2466 }
2467 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002468 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002469
Chris Masone02119d2008-09-05 16:13:11 -04002470 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2471 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002472 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002473
2474 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2475 dir, index);
2476 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002477err:
2478 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002479 if (ret)
2480 goto out;
2481
2482 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2483 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2484 btrfs_update_inode(trans, root, dir);
2485 btrfs_drop_nlink(inode);
2486 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002487out:
Chris Mason39279cc2007-06-12 06:35:45 -04002488 return ret;
2489}
2490
2491static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2492{
2493 struct btrfs_root *root;
2494 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002495 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002496 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002497 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002498
2499 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002500
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002501 /*
2502 * 5 items for unlink inode
2503 * 1 for orphan
2504 */
2505 ret = btrfs_reserve_metadata_space(root, 6);
2506 if (ret)
2507 return ret;
2508
Chris Mason39279cc2007-06-12 06:35:45 -04002509 trans = btrfs_start_transaction(root, 1);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002510 if (IS_ERR(trans)) {
2511 btrfs_unreserve_metadata_space(root, 6);
2512 return PTR_ERR(trans);
2513 }
Chris Mason5f39d392007-10-15 16:14:19 -04002514
Chris Mason39279cc2007-06-12 06:35:45 -04002515 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002516
2517 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2518
Chris Masone02119d2008-09-05 16:13:11 -04002519 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2520 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002521
2522 if (inode->i_nlink == 0)
2523 ret = btrfs_orphan_add(trans, inode);
2524
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002525 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002526
Chris Mason89ce8a62008-06-25 16:01:31 -04002527 btrfs_end_transaction_throttle(trans, root);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002528 btrfs_unreserve_metadata_space(root, 6);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002529 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002530 return ret;
2531}
2532
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002533int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2534 struct btrfs_root *root,
2535 struct inode *dir, u64 objectid,
2536 const char *name, int name_len)
2537{
2538 struct btrfs_path *path;
2539 struct extent_buffer *leaf;
2540 struct btrfs_dir_item *di;
2541 struct btrfs_key key;
2542 u64 index;
2543 int ret;
2544
2545 path = btrfs_alloc_path();
2546 if (!path)
2547 return -ENOMEM;
2548
2549 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2550 name, name_len, -1);
2551 BUG_ON(!di || IS_ERR(di));
2552
2553 leaf = path->nodes[0];
2554 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2555 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2556 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2557 BUG_ON(ret);
2558 btrfs_release_path(root, path);
2559
2560 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2561 objectid, root->root_key.objectid,
2562 dir->i_ino, &index, name, name_len);
2563 if (ret < 0) {
2564 BUG_ON(ret != -ENOENT);
2565 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2566 name, name_len);
2567 BUG_ON(!di || IS_ERR(di));
2568
2569 leaf = path->nodes[0];
2570 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2571 btrfs_release_path(root, path);
2572 index = key.offset;
2573 }
2574
2575 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2576 index, name, name_len, -1);
2577 BUG_ON(!di || IS_ERR(di));
2578
2579 leaf = path->nodes[0];
2580 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2581 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2582 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2583 BUG_ON(ret);
2584 btrfs_release_path(root, path);
2585
2586 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2587 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2588 ret = btrfs_update_inode(trans, root, dir);
2589 BUG_ON(ret);
2590 dir->i_sb->s_dirt = 1;
2591
2592 btrfs_free_path(path);
2593 return 0;
2594}
2595
Chris Mason39279cc2007-06-12 06:35:45 -04002596static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2597{
2598 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002599 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002600 int ret;
2601 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002602 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002603 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002604
Chris Mason3394e162008-11-17 20:42:26 -05002605 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002606 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002607 return -ENOTEMPTY;
2608
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002609 ret = btrfs_reserve_metadata_space(root, 5);
2610 if (ret)
2611 return ret;
2612
Chris Mason39279cc2007-06-12 06:35:45 -04002613 trans = btrfs_start_transaction(root, 1);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002614 if (IS_ERR(trans)) {
2615 btrfs_unreserve_metadata_space(root, 5);
2616 return PTR_ERR(trans);
2617 }
2618
Chris Mason39279cc2007-06-12 06:35:45 -04002619 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002620
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002621 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2622 err = btrfs_unlink_subvol(trans, root, dir,
2623 BTRFS_I(inode)->location.objectid,
2624 dentry->d_name.name,
2625 dentry->d_name.len);
2626 goto out;
2627 }
2628
Josef Bacik7b128762008-07-24 12:17:14 -04002629 err = btrfs_orphan_add(trans, inode);
2630 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002631 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002632
Chris Mason39279cc2007-06-12 06:35:45 -04002633 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002634 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2635 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002636 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002637 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002638out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002639 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002640 ret = btrfs_end_transaction_throttle(trans, root);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002641 btrfs_unreserve_metadata_space(root, 5);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002642 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002643
Chris Mason39279cc2007-06-12 06:35:45 -04002644 if (ret && !err)
2645 err = ret;
2646 return err;
2647}
2648
Chris Masond20f7042008-12-08 16:58:54 -05002649#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002650/*
Chris Mason323ac952008-10-01 19:05:46 -04002651 * when truncating bytes in a file, it is possible to avoid reading
2652 * the leaves that contain only checksum items. This can be the
2653 * majority of the IO required to delete a large file, but it must
2654 * be done carefully.
2655 *
2656 * The keys in the level just above the leaves are checked to make sure
2657 * the lowest key in a given leaf is a csum key, and starts at an offset
2658 * after the new size.
2659 *
2660 * Then the key for the next leaf is checked to make sure it also has
2661 * a checksum item for the same file. If it does, we know our target leaf
2662 * contains only checksum items, and it can be safely freed without reading
2663 * it.
2664 *
2665 * This is just an optimization targeted at large files. It may do
2666 * nothing. It will return 0 unless things went badly.
2667 */
2668static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2669 struct btrfs_root *root,
2670 struct btrfs_path *path,
2671 struct inode *inode, u64 new_size)
2672{
2673 struct btrfs_key key;
2674 int ret;
2675 int nritems;
2676 struct btrfs_key found_key;
2677 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002678 struct btrfs_leaf_ref *ref;
2679 u64 leaf_gen;
2680 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002681
2682 path->lowest_level = 1;
2683 key.objectid = inode->i_ino;
2684 key.type = BTRFS_CSUM_ITEM_KEY;
2685 key.offset = new_size;
2686again:
2687 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2688 if (ret < 0)
2689 goto out;
2690
2691 if (path->nodes[1] == NULL) {
2692 ret = 0;
2693 goto out;
2694 }
2695 ret = 0;
2696 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2697 nritems = btrfs_header_nritems(path->nodes[1]);
2698
2699 if (!nritems)
2700 goto out;
2701
2702 if (path->slots[1] >= nritems)
2703 goto next_node;
2704
2705 /* did we find a key greater than anything we want to delete? */
2706 if (found_key.objectid > inode->i_ino ||
2707 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2708 goto out;
2709
2710 /* we check the next key in the node to make sure the leave contains
2711 * only checksum items. This comparison doesn't work if our
2712 * leaf is the last one in the node
2713 */
2714 if (path->slots[1] + 1 >= nritems) {
2715next_node:
2716 /* search forward from the last key in the node, this
2717 * will bring us into the next node in the tree
2718 */
2719 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2720
2721 /* unlikely, but we inc below, so check to be safe */
2722 if (found_key.offset == (u64)-1)
2723 goto out;
2724
2725 /* search_forward needs a path with locks held, do the
2726 * search again for the original key. It is possible
2727 * this will race with a balance and return a path that
2728 * we could modify, but this drop is just an optimization
2729 * and is allowed to miss some leaves.
2730 */
2731 btrfs_release_path(root, path);
2732 found_key.offset++;
2733
2734 /* setup a max key for search_forward */
2735 other_key.offset = (u64)-1;
2736 other_key.type = key.type;
2737 other_key.objectid = key.objectid;
2738
2739 path->keep_locks = 1;
2740 ret = btrfs_search_forward(root, &found_key, &other_key,
2741 path, 0, 0);
2742 path->keep_locks = 0;
2743 if (ret || found_key.objectid != key.objectid ||
2744 found_key.type != key.type) {
2745 ret = 0;
2746 goto out;
2747 }
2748
2749 key.offset = found_key.offset;
2750 btrfs_release_path(root, path);
2751 cond_resched();
2752 goto again;
2753 }
2754
2755 /* we know there's one more slot after us in the tree,
2756 * read that key so we can verify it is also a checksum item
2757 */
2758 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2759
2760 if (found_key.objectid < inode->i_ino)
2761 goto next_key;
2762
2763 if (found_key.type != key.type || found_key.offset < new_size)
2764 goto next_key;
2765
2766 /*
2767 * if the key for the next leaf isn't a csum key from this objectid,
2768 * we can't be sure there aren't good items inside this leaf.
2769 * Bail out
2770 */
2771 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2772 goto out;
2773
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002774 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2775 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002776 /*
2777 * it is safe to delete this leaf, it contains only
2778 * csum items from this inode at an offset >= new_size
2779 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002780 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002781 BUG_ON(ret);
2782
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002783 if (root->ref_cows && leaf_gen < trans->transid) {
2784 ref = btrfs_alloc_leaf_ref(root, 0);
2785 if (ref) {
2786 ref->root_gen = root->root_key.offset;
2787 ref->bytenr = leaf_start;
2788 ref->owner = 0;
2789 ref->generation = leaf_gen;
2790 ref->nritems = 0;
2791
Chris Masonbd56b302009-02-04 09:27:02 -05002792 btrfs_sort_leaf_ref(ref);
2793
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002794 ret = btrfs_add_leaf_ref(root, ref, 0);
2795 WARN_ON(ret);
2796 btrfs_free_leaf_ref(root, ref);
2797 } else {
2798 WARN_ON(1);
2799 }
2800 }
Chris Mason323ac952008-10-01 19:05:46 -04002801next_key:
2802 btrfs_release_path(root, path);
2803
2804 if (other_key.objectid == inode->i_ino &&
2805 other_key.type == key.type && other_key.offset > key.offset) {
2806 key.offset = other_key.offset;
2807 cond_resched();
2808 goto again;
2809 }
2810 ret = 0;
2811out:
2812 /* fixup any changes we've made to the path */
2813 path->lowest_level = 0;
2814 path->keep_locks = 0;
2815 btrfs_release_path(root, path);
2816 return ret;
2817}
2818
Chris Masond20f7042008-12-08 16:58:54 -05002819#endif
2820
Chris Mason323ac952008-10-01 19:05:46 -04002821/*
Chris Mason39279cc2007-06-12 06:35:45 -04002822 * this can truncate away extent items, csum items and directory items.
2823 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002824 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002825 *
2826 * csum items that cross the new i_size are truncated to the new size
2827 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002828 *
2829 * min_type is the minimum key type to truncate down to. If set to 0, this
2830 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002831 */
Chris Masone02119d2008-09-05 16:13:11 -04002832noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2833 struct btrfs_root *root,
2834 struct inode *inode,
2835 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002836{
2837 int ret;
2838 struct btrfs_path *path;
2839 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002840 struct btrfs_key found_key;
Chris Mason06d9a8d2009-02-04 09:30:58 -05002841 u32 found_type = (u8)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002842 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002843 struct btrfs_file_extent_item *fi;
2844 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002845 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002846 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002847 u64 item_end = 0;
2848 int found_extent;
2849 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002850 int pending_del_nr = 0;
2851 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002852 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002853 int encoding;
Chris Mason3b951512008-04-17 11:29:12 -04002854 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002855
Chris Masone02119d2008-09-05 16:13:11 -04002856 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002857 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002858 path = btrfs_alloc_path();
2859 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04002860 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04002861
Chris Mason39279cc2007-06-12 06:35:45 -04002862 /* FIXME, add redo link to tree so we don't leak on crash */
2863 key.objectid = inode->i_ino;
2864 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002865 key.type = (u8)-1;
2866
Chris Mason85e21ba2008-01-29 15:11:36 -05002867search_again:
Chris Masonb9473432009-03-13 11:00:37 -04002868 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05002869 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002870 if (ret < 0)
Chris Mason85e21ba2008-01-29 15:11:36 -05002871 goto error;
Chris Masond3977122009-01-05 21:25:51 -05002872
Chris Mason85e21ba2008-01-29 15:11:36 -05002873 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002874 /* there are no items in the tree for us to truncate, we're
2875 * done
2876 */
2877 if (path->slots[0] == 0) {
2878 ret = 0;
2879 goto error;
2880 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002881 path->slots[0]--;
2882 }
2883
Chris Masond3977122009-01-05 21:25:51 -05002884 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002885 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002886 leaf = path->nodes[0];
2887 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2888 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002889 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002890
Chris Mason5f39d392007-10-15 16:14:19 -04002891 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002892 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002893
Chris Mason85e21ba2008-01-29 15:11:36 -05002894 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002895 break;
2896
Chris Mason5f39d392007-10-15 16:14:19 -04002897 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002898 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002899 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002900 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002901 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002902 encoding = btrfs_file_extent_compression(leaf, fi);
2903 encoding |= btrfs_file_extent_encryption(leaf, fi);
2904 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2905
Chris Mason179e29e2007-11-01 11:28:41 -04002906 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002907 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002908 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002909 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002910 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002911 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002912 }
Yan008630c2007-11-07 13:31:09 -05002913 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002914 }
Chris Masone02119d2008-09-05 16:13:11 -04002915 if (item_end < new_size) {
Chris Masond3977122009-01-05 21:25:51 -05002916 if (found_type == BTRFS_DIR_ITEM_KEY)
Chris Masonb888db22007-08-27 16:49:44 -04002917 found_type = BTRFS_INODE_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002918 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
Chris Masond20f7042008-12-08 16:58:54 -05002919 found_type = BTRFS_EXTENT_DATA_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002920 else if (found_type == BTRFS_EXTENT_DATA_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002921 found_type = BTRFS_XATTR_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002922 else if (found_type == BTRFS_XATTR_ITEM_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002923 found_type = BTRFS_INODE_REF_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002924 else if (found_type)
Chris Masonb888db22007-08-27 16:49:44 -04002925 found_type--;
Chris Masond3977122009-01-05 21:25:51 -05002926 else
Chris Masonb888db22007-08-27 16:49:44 -04002927 break;
Yana61721d2007-09-17 11:08:38 -04002928 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002929 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002930 }
Chris Masone02119d2008-09-05 16:13:11 -04002931 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002932 del_item = 1;
2933 else
2934 del_item = 0;
2935 found_extent = 0;
2936
2937 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002938 if (found_type != BTRFS_EXTENT_DATA_KEY)
2939 goto delete;
2940
2941 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002942 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002943 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002944 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002945 u64 orig_num_bytes =
2946 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002947 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002948 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002949 extent_num_bytes = extent_num_bytes &
2950 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002951 btrfs_set_file_extent_num_bytes(leaf, fi,
2952 extent_num_bytes);
2953 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002954 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002955 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002956 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002957 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002958 } else {
Chris Masondb945352007-10-15 16:15:53 -04002959 extent_num_bytes =
2960 btrfs_file_extent_disk_num_bytes(leaf,
2961 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002962 extent_offset = found_key.offset -
2963 btrfs_file_extent_offset(leaf, fi);
2964
Chris Mason39279cc2007-06-12 06:35:45 -04002965 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002966 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002967 if (extent_start != 0) {
2968 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002969 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002970 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002971 }
2972 }
Chris Mason90692182008-02-08 13:49:28 -05002973 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002974 /*
2975 * we can't truncate inline items that have had
2976 * special encodings
2977 */
2978 if (!del_item &&
2979 btrfs_file_extent_compression(leaf, fi) == 0 &&
2980 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2981 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002982 u32 size = new_size - found_key.offset;
2983
2984 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002985 inode_sub_bytes(inode, item_end + 1 -
2986 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002987 }
2988 size =
2989 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002990 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002991 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002992 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002993 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002994 inode_sub_bytes(inode, item_end + 1 -
2995 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002996 }
Chris Mason39279cc2007-06-12 06:35:45 -04002997 }
Chris Mason179e29e2007-11-01 11:28:41 -04002998delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002999 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003000 if (!pending_del_nr) {
3001 /* no pending yet, add ourselves */
3002 pending_del_slot = path->slots[0];
3003 pending_del_nr = 1;
3004 } else if (pending_del_nr &&
3005 path->slots[0] + 1 == pending_del_slot) {
3006 /* hop on the pending chunk */
3007 pending_del_nr++;
3008 pending_del_slot = path->slots[0];
3009 } else {
Chris Masond3977122009-01-05 21:25:51 -05003010 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003011 }
Chris Mason39279cc2007-06-12 06:35:45 -04003012 } else {
3013 break;
3014 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003015 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04003016 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003017 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003018 extent_num_bytes, 0,
3019 btrfs_header_owner(leaf),
3020 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003021 BUG_ON(ret);
3022 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003023next:
3024 if (path->slots[0] == 0) {
3025 if (pending_del_nr)
3026 goto del_pending;
3027 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05003028 if (found_type == BTRFS_INODE_ITEM_KEY)
3029 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05003030 goto search_again;
3031 }
3032
3033 path->slots[0]--;
3034 if (pending_del_nr &&
3035 path->slots[0] + 1 != pending_del_slot) {
3036 struct btrfs_key debug;
3037del_pending:
3038 btrfs_item_key_to_cpu(path->nodes[0], &debug,
3039 pending_del_slot);
3040 ret = btrfs_del_items(trans, root, path,
3041 pending_del_slot,
3042 pending_del_nr);
3043 BUG_ON(ret);
3044 pending_del_nr = 0;
3045 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05003046 if (found_type == BTRFS_INODE_ITEM_KEY)
3047 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05003048 goto search_again;
3049 }
Chris Mason39279cc2007-06-12 06:35:45 -04003050 }
3051 ret = 0;
3052error:
Chris Mason85e21ba2008-01-29 15:11:36 -05003053 if (pending_del_nr) {
3054 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3055 pending_del_nr);
3056 }
Chris Mason39279cc2007-06-12 06:35:45 -04003057 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003058 return ret;
3059}
3060
Chris Masona52d9a82007-08-27 16:49:44 -04003061/*
3062 * taken from block_truncate_page, but does cow as it zeros out
3063 * any bytes left in the last page in the file.
3064 */
3065static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3066{
3067 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003068 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003069 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3070 struct btrfs_ordered_extent *ordered;
3071 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003072 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003073 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3074 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3075 struct page *page;
3076 int ret = 0;
3077 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003078 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003079
3080 if ((offset & (blocksize - 1)) == 0)
3081 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003082 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
3083 if (ret)
3084 goto out;
3085
3086 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
3087 if (ret)
3088 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003089
3090 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003091again:
Chris Masona52d9a82007-08-27 16:49:44 -04003092 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003093 if (!page) {
3094 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3095 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04003096 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003097 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003098
3099 page_start = page_offset(page);
3100 page_end = page_start + PAGE_CACHE_SIZE - 1;
3101
Chris Masona52d9a82007-08-27 16:49:44 -04003102 if (!PageUptodate(page)) {
3103 ret = btrfs_readpage(NULL, page);
3104 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003105 if (page->mapping != mapping) {
3106 unlock_page(page);
3107 page_cache_release(page);
3108 goto again;
3109 }
Chris Masona52d9a82007-08-27 16:49:44 -04003110 if (!PageUptodate(page)) {
3111 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003112 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003113 }
3114 }
Chris Mason211c17f2008-05-15 09:13:45 -04003115 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003116
3117 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3118 set_page_extent_mapped(page);
3119
3120 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3121 if (ordered) {
3122 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3123 unlock_page(page);
3124 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003125 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003126 btrfs_put_ordered_extent(ordered);
3127 goto again;
3128 }
3129
Josef Bacik5d5e1032009-10-13 16:46:49 -04003130 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
3131 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3132 GFP_NOFS);
3133
Josef Bacik9ed74f22009-09-11 16:12:44 -04003134 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3135 if (ret) {
3136 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3137 goto out_unlock;
3138 }
3139
Chris Masone6dcd2d2008-07-17 12:53:50 -04003140 ret = 0;
3141 if (offset != PAGE_CACHE_SIZE) {
3142 kaddr = kmap(page);
3143 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3144 flush_dcache_page(page);
3145 kunmap(page);
3146 }
Chris Mason247e7432008-07-17 12:53:51 -04003147 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003148 set_page_dirty(page);
3149 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003150
Chris Mason89642222008-07-24 09:41:53 -04003151out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003152 if (ret)
3153 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3154 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003155 unlock_page(page);
3156 page_cache_release(page);
3157out:
3158 return ret;
3159}
3160
Yan Zheng9036c102008-10-30 14:19:41 -04003161int btrfs_cont_expand(struct inode *inode, loff_t size)
3162{
3163 struct btrfs_trans_handle *trans;
3164 struct btrfs_root *root = BTRFS_I(inode)->root;
3165 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3166 struct extent_map *em;
3167 u64 mask = root->sectorsize - 1;
3168 u64 hole_start = (inode->i_size + mask) & ~mask;
3169 u64 block_end = (size + mask) & ~mask;
3170 u64 last_byte;
3171 u64 cur_offset;
3172 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003173 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003174
3175 if (size <= hole_start)
3176 return 0;
3177
Josef Bacik5d5e1032009-10-13 16:46:49 -04003178 err = btrfs_truncate_page(inode->i_mapping, inode->i_size);
3179 if (err)
3180 return err;
Yan Zheng9036c102008-10-30 14:19:41 -04003181
3182 while (1) {
3183 struct btrfs_ordered_extent *ordered;
3184 btrfs_wait_ordered_range(inode, hole_start,
3185 block_end - hole_start);
3186 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3187 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3188 if (!ordered)
3189 break;
3190 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3191 btrfs_put_ordered_extent(ordered);
3192 }
3193
3194 trans = btrfs_start_transaction(root, 1);
3195 btrfs_set_trans_block_group(trans, inode);
3196
3197 cur_offset = hole_start;
3198 while (1) {
3199 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3200 block_end - cur_offset, 0);
3201 BUG_ON(IS_ERR(em) || !em);
3202 last_byte = min(extent_map_end(em), block_end);
3203 last_byte = (last_byte + mask) & ~mask;
3204 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003205 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003206 hole_size = last_byte - cur_offset;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00003207 err = btrfs_drop_extents(trans, inode, cur_offset,
Chris Mason771ed682008-11-06 22:02:51 -05003208 cur_offset + hole_size,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00003209 &hint_byte, 1);
Chris Mason771ed682008-11-06 22:02:51 -05003210 if (err)
3211 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003212
3213 err = btrfs_reserve_metadata_space(root, 1);
3214 if (err)
3215 break;
3216
Yan Zheng9036c102008-10-30 14:19:41 -04003217 err = btrfs_insert_file_extent(trans, root,
3218 inode->i_ino, cur_offset, 0,
3219 0, hole_size, 0, hole_size,
3220 0, 0, 0);
3221 btrfs_drop_extent_cache(inode, hole_start,
3222 last_byte - 1, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003223 btrfs_unreserve_metadata_space(root, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04003224 }
3225 free_extent_map(em);
3226 cur_offset = last_byte;
3227 if (err || cur_offset >= block_end)
3228 break;
3229 }
3230
3231 btrfs_end_transaction(trans, root);
3232 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3233 return err;
3234}
3235
Chris Mason39279cc2007-06-12 06:35:45 -04003236static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3237{
3238 struct inode *inode = dentry->d_inode;
3239 int err;
3240
3241 err = inode_change_ok(inode, attr);
3242 if (err)
3243 return err;
3244
Chris Mason5a3f23d2009-03-31 13:27:11 -04003245 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3246 if (attr->ia_size > inode->i_size) {
3247 err = btrfs_cont_expand(inode, attr->ia_size);
3248 if (err)
3249 return err;
3250 } else if (inode->i_size > 0 &&
3251 attr->ia_size == 0) {
3252
3253 /* we're truncating a file that used to have good
3254 * data down to zero. Make sure it gets into
3255 * the ordered flush list so that any new writes
3256 * get down to disk quickly.
3257 */
3258 BTRFS_I(inode)->ordered_data_close = 1;
3259 }
Chris Mason39279cc2007-06-12 06:35:45 -04003260 }
Yan Zheng9036c102008-10-30 14:19:41 -04003261
Chris Mason39279cc2007-06-12 06:35:45 -04003262 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003263
3264 if (!err && ((attr->ia_valid & ATTR_MODE)))
3265 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003266 return err;
3267}
Chris Mason61295eb2008-01-14 16:24:38 -05003268
Chris Mason39279cc2007-06-12 06:35:45 -04003269void btrfs_delete_inode(struct inode *inode)
3270{
3271 struct btrfs_trans_handle *trans;
3272 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003273 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003274 int ret;
3275
3276 truncate_inode_pages(&inode->i_data, 0);
3277 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003278 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003279 goto no_delete;
3280 }
Chris Mason4a096752008-07-21 10:29:44 -04003281 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003282
Yan, Zheng76dda932009-09-21 16:00:26 -04003283 if (inode->i_nlink > 0) {
3284 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3285 goto no_delete;
3286 }
3287
Chris Masondbe674a2008-07-17 12:54:05 -04003288 btrfs_i_size_write(inode, 0);
Yan Zheng180591b2009-01-06 09:58:06 -05003289 trans = btrfs_join_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003290
Chris Mason39279cc2007-06-12 06:35:45 -04003291 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04003292 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04003293 if (ret) {
3294 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04003295 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04003296 }
3297
3298 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05003299
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003300 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05003301 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003302
Chris Mason39279cc2007-06-12 06:35:45 -04003303 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003304 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003305 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04003306
3307no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003308 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003309 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003310 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003311no_delete:
3312 clear_inode(inode);
3313}
3314
3315/*
3316 * this returns the key found in the dir entry in the location pointer.
3317 * If no dir entries were found, location->objectid is 0.
3318 */
3319static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3320 struct btrfs_key *location)
3321{
3322 const char *name = dentry->d_name.name;
3323 int namelen = dentry->d_name.len;
3324 struct btrfs_dir_item *di;
3325 struct btrfs_path *path;
3326 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003327 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003328
3329 path = btrfs_alloc_path();
3330 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003331
Chris Mason39279cc2007-06-12 06:35:45 -04003332 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3333 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003334 if (IS_ERR(di))
3335 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003336
3337 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003338 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003339
Chris Mason5f39d392007-10-15 16:14:19 -04003340 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003341out:
Chris Mason39279cc2007-06-12 06:35:45 -04003342 btrfs_free_path(path);
3343 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003344out_err:
3345 location->objectid = 0;
3346 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003347}
3348
3349/*
3350 * when we hit a tree root in a directory, the btrfs part of the inode
3351 * needs to be changed to reflect the root directory of the tree root. This
3352 * is kind of like crossing a mount point.
3353 */
3354static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003355 struct inode *dir,
3356 struct dentry *dentry,
3357 struct btrfs_key *location,
3358 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003359{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003360 struct btrfs_path *path;
3361 struct btrfs_root *new_root;
3362 struct btrfs_root_ref *ref;
3363 struct extent_buffer *leaf;
3364 int ret;
3365 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003366
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003367 path = btrfs_alloc_path();
3368 if (!path) {
3369 err = -ENOMEM;
3370 goto out;
3371 }
Chris Mason39279cc2007-06-12 06:35:45 -04003372
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003373 err = -ENOENT;
3374 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3375 BTRFS_I(dir)->root->root_key.objectid,
3376 location->objectid);
3377 if (ret) {
3378 if (ret < 0)
3379 err = ret;
3380 goto out;
3381 }
Chris Mason39279cc2007-06-12 06:35:45 -04003382
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003383 leaf = path->nodes[0];
3384 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3385 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3386 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3387 goto out;
3388
3389 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3390 (unsigned long)(ref + 1),
3391 dentry->d_name.len);
3392 if (ret)
3393 goto out;
3394
3395 btrfs_release_path(root->fs_info->tree_root, path);
3396
3397 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3398 if (IS_ERR(new_root)) {
3399 err = PTR_ERR(new_root);
3400 goto out;
3401 }
3402
3403 if (btrfs_root_refs(&new_root->root_item) == 0) {
3404 err = -ENOENT;
3405 goto out;
3406 }
3407
3408 *sub_root = new_root;
3409 location->objectid = btrfs_root_dirid(&new_root->root_item);
3410 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003411 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003412 err = 0;
3413out:
3414 btrfs_free_path(path);
3415 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003416}
3417
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003418static void inode_tree_add(struct inode *inode)
3419{
3420 struct btrfs_root *root = BTRFS_I(inode)->root;
3421 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003422 struct rb_node **p;
3423 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003424again:
3425 p = &root->inode_tree.rb_node;
3426 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003427
Yan, Zheng76dda932009-09-21 16:00:26 -04003428 if (hlist_unhashed(&inode->i_hash))
3429 return;
3430
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003431 spin_lock(&root->inode_lock);
3432 while (*p) {
3433 parent = *p;
3434 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3435
3436 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003437 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003438 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003439 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003440 else {
3441 WARN_ON(!(entry->vfs_inode.i_state &
3442 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003443 rb_erase(parent, &root->inode_tree);
3444 RB_CLEAR_NODE(parent);
3445 spin_unlock(&root->inode_lock);
3446 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003447 }
3448 }
3449 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3450 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3451 spin_unlock(&root->inode_lock);
3452}
3453
3454static void inode_tree_del(struct inode *inode)
3455{
3456 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003457 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003458
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003459 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003460 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003461 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003462 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003463 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003464 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003465 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003466
3467 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3468 synchronize_srcu(&root->fs_info->subvol_srcu);
3469 spin_lock(&root->inode_lock);
3470 empty = RB_EMPTY_ROOT(&root->inode_tree);
3471 spin_unlock(&root->inode_lock);
3472 if (empty)
3473 btrfs_add_dead_root(root);
3474 }
3475}
3476
3477int btrfs_invalidate_inodes(struct btrfs_root *root)
3478{
3479 struct rb_node *node;
3480 struct rb_node *prev;
3481 struct btrfs_inode *entry;
3482 struct inode *inode;
3483 u64 objectid = 0;
3484
3485 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3486
3487 spin_lock(&root->inode_lock);
3488again:
3489 node = root->inode_tree.rb_node;
3490 prev = NULL;
3491 while (node) {
3492 prev = node;
3493 entry = rb_entry(node, struct btrfs_inode, rb_node);
3494
3495 if (objectid < entry->vfs_inode.i_ino)
3496 node = node->rb_left;
3497 else if (objectid > entry->vfs_inode.i_ino)
3498 node = node->rb_right;
3499 else
3500 break;
3501 }
3502 if (!node) {
3503 while (prev) {
3504 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3505 if (objectid <= entry->vfs_inode.i_ino) {
3506 node = prev;
3507 break;
3508 }
3509 prev = rb_next(prev);
3510 }
3511 }
3512 while (node) {
3513 entry = rb_entry(node, struct btrfs_inode, rb_node);
3514 objectid = entry->vfs_inode.i_ino + 1;
3515 inode = igrab(&entry->vfs_inode);
3516 if (inode) {
3517 spin_unlock(&root->inode_lock);
3518 if (atomic_read(&inode->i_count) > 1)
3519 d_prune_aliases(inode);
3520 /*
3521 * btrfs_drop_inode will remove it from
3522 * the inode cache when its usage count
3523 * hits zero.
3524 */
3525 iput(inode);
3526 cond_resched();
3527 spin_lock(&root->inode_lock);
3528 goto again;
3529 }
3530
3531 if (cond_resched_lock(&root->inode_lock))
3532 goto again;
3533
3534 node = rb_next(node);
3535 }
3536 spin_unlock(&root->inode_lock);
3537 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003538}
3539
Chris Masone02119d2008-09-05 16:13:11 -04003540static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003541{
Chris Masone02119d2008-09-05 16:13:11 -04003542 struct btrfs_inode *bi = BTRFS_I(inode);
3543
Chris Masone02119d2008-09-05 16:13:11 -04003544 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05003545 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003546 bi->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04003547 bi->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003548 bi->logged_trans = 0;
3549 bi->delalloc_bytes = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003550 bi->reserved_bytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003551 bi->disk_i_size = 0;
3552 bi->flags = 0;
3553 bi->index_cnt = (u64)-1;
Chris Mason12fcfd22009-03-24 10:24:20 -04003554 bi->last_unlink_trans = 0;
Chris Mason27574952009-05-14 13:10:02 -04003555 bi->ordered_data_close = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003556 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3557 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04003558 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003559 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3560 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003561 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04003562 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003563 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Chris Masonba1da2f2008-07-17 12:54:15 -04003564 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonee6e6502008-07-17 12:54:40 -04003565 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003566 mutex_init(&BTRFS_I(inode)->log_mutex);
3567}
3568
3569static int btrfs_init_locked_inode(struct inode *inode, void *p)
3570{
3571 struct btrfs_iget_args *args = p;
3572 inode->i_ino = args->ino;
3573 init_btrfs_i(inode);
3574 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003575 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003576 return 0;
3577}
3578
3579static int btrfs_find_actor(struct inode *inode, void *opaque)
3580{
3581 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003582 return args->ino == inode->i_ino &&
3583 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003584}
3585
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003586static struct inode *btrfs_iget_locked(struct super_block *s,
3587 u64 objectid,
3588 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003589{
3590 struct inode *inode;
3591 struct btrfs_iget_args args;
3592 args.ino = objectid;
3593 args.root = root;
3594
3595 inode = iget5_locked(s, objectid, btrfs_find_actor,
3596 btrfs_init_locked_inode,
3597 (void *)&args);
3598 return inode;
3599}
3600
Balaji Rao1a54ef82008-07-21 02:01:04 +05303601/* Get an inode object given its location and corresponding root.
3602 * Returns in *is_new if the inode was read from disk
3603 */
3604struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003605 struct btrfs_root *root)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303606{
3607 struct inode *inode;
3608
3609 inode = btrfs_iget_locked(s, location->objectid, root);
3610 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003611 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303612
3613 if (inode->i_state & I_NEW) {
3614 BTRFS_I(inode)->root = root;
3615 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3616 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003617
3618 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303619 unlock_new_inode(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303620 }
3621
3622 return inode;
3623}
3624
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003625static struct inode *new_simple_dir(struct super_block *s,
3626 struct btrfs_key *key,
3627 struct btrfs_root *root)
3628{
3629 struct inode *inode = new_inode(s);
3630
3631 if (!inode)
3632 return ERR_PTR(-ENOMEM);
3633
3634 init_btrfs_i(inode);
3635
3636 BTRFS_I(inode)->root = root;
3637 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3638 BTRFS_I(inode)->dummy_inode = 1;
3639
3640 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3641 inode->i_op = &simple_dir_inode_operations;
3642 inode->i_fop = &simple_dir_operations;
3643 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3644 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3645
3646 return inode;
3647}
3648
Chris Mason3de45862008-11-17 21:02:50 -05003649struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003650{
Chris Masond3977122009-01-05 21:25:51 -05003651 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003652 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003653 struct btrfs_root *sub_root = root;
3654 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003655 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003656 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003657
Yan, Zheng76dda932009-09-21 16:00:26 -04003658 dentry->d_op = &btrfs_dentry_operations;
3659
Chris Mason39279cc2007-06-12 06:35:45 -04003660 if (dentry->d_name.len > BTRFS_NAME_LEN)
3661 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003662
Chris Mason39279cc2007-06-12 06:35:45 -04003663 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003664
Chris Mason39279cc2007-06-12 06:35:45 -04003665 if (ret < 0)
3666 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003667
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003668 if (location.objectid == 0)
3669 return NULL;
3670
3671 if (location.type == BTRFS_INODE_ITEM_KEY) {
3672 inode = btrfs_iget(dir->i_sb, &location, root);
3673 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003674 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003675
3676 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3677
Yan, Zheng76dda932009-09-21 16:00:26 -04003678 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003679 ret = fixup_tree_root_location(root, dir, dentry,
3680 &location, &sub_root);
3681 if (ret < 0) {
3682 if (ret != -ENOENT)
3683 inode = ERR_PTR(ret);
3684 else
3685 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3686 } else {
Chris Mason39279cc2007-06-12 06:35:45 -04003687 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04003688 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003689 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3690
Chris Mason3de45862008-11-17 21:02:50 -05003691 return inode;
3692}
3693
Yan, Zheng76dda932009-09-21 16:00:26 -04003694static int btrfs_dentry_delete(struct dentry *dentry)
3695{
3696 struct btrfs_root *root;
3697
Yan, Zhengefefb142009-10-09 09:25:16 -04003698 if (!dentry->d_inode && !IS_ROOT(dentry))
3699 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04003700
Yan, Zhengefefb142009-10-09 09:25:16 -04003701 if (dentry->d_inode) {
3702 root = BTRFS_I(dentry->d_inode)->root;
3703 if (btrfs_root_refs(&root->root_item) == 0)
3704 return 1;
3705 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003706 return 0;
3707}
3708
Chris Mason3de45862008-11-17 21:02:50 -05003709static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3710 struct nameidata *nd)
3711{
3712 struct inode *inode;
3713
Chris Mason3de45862008-11-17 21:02:50 -05003714 inode = btrfs_lookup_dentry(dir, dentry);
3715 if (IS_ERR(inode))
3716 return ERR_CAST(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003717
Chris Mason39279cc2007-06-12 06:35:45 -04003718 return d_splice_alias(inode, dentry);
3719}
3720
3721static unsigned char btrfs_filetype_table[] = {
3722 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3723};
3724
David Woodhousecbdf5a22008-08-06 19:42:33 +01003725static int btrfs_real_readdir(struct file *filp, void *dirent,
3726 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003727{
3728 struct inode *inode = filp->f_dentry->d_inode;
3729 struct btrfs_root *root = BTRFS_I(inode)->root;
3730 struct btrfs_item *item;
3731 struct btrfs_dir_item *di;
3732 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003733 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003734 struct btrfs_path *path;
3735 int ret;
3736 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003737 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003738 int slot;
3739 int advance;
3740 unsigned char d_type;
3741 int over = 0;
3742 u32 di_cur;
3743 u32 di_total;
3744 u32 di_len;
3745 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003746 char tmp_name[32];
3747 char *name_ptr;
3748 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003749
3750 /* FIXME, use a real flag for deciding about the key type */
3751 if (root->fs_info->tree_root == root)
3752 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003753
Chris Mason39544012007-12-12 14:38:19 -05003754 /* special case for "." */
3755 if (filp->f_pos == 0) {
3756 over = filldir(dirent, ".", 1,
3757 1, inode->i_ino,
3758 DT_DIR);
3759 if (over)
3760 return 0;
3761 filp->f_pos = 1;
3762 }
Chris Mason39544012007-12-12 14:38:19 -05003763 /* special case for .., just use the back ref */
3764 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003765 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003766 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003767 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003768 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003769 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003770 filp->f_pos = 2;
3771 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003772 path = btrfs_alloc_path();
3773 path->reada = 2;
3774
Chris Mason39279cc2007-06-12 06:35:45 -04003775 btrfs_set_key_type(&key, key_type);
3776 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003777 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003778
Chris Mason39279cc2007-06-12 06:35:45 -04003779 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3780 if (ret < 0)
3781 goto err;
3782 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003783
3784 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003785 leaf = path->nodes[0];
3786 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003787 slot = path->slots[0];
3788 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003789 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003790 ret = btrfs_next_leaf(root, path);
3791 if (ret)
3792 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003793 leaf = path->nodes[0];
3794 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003795 slot = path->slots[0];
3796 } else {
3797 slot++;
3798 path->slots[0]++;
3799 }
3800 }
Chris Mason3de45862008-11-17 21:02:50 -05003801
Chris Mason39279cc2007-06-12 06:35:45 -04003802 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003803 item = btrfs_item_nr(leaf, slot);
3804 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3805
3806 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003807 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003808 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003809 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003810 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003811 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003812
3813 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003814
Chris Mason39279cc2007-06-12 06:35:45 -04003815 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3816 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003817 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003818
3819 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003820 struct btrfs_key location;
3821
3822 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003823 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003824 name_ptr = tmp_name;
3825 } else {
3826 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003827 if (!name_ptr) {
3828 ret = -ENOMEM;
3829 goto err;
3830 }
Chris Mason5f39d392007-10-15 16:14:19 -04003831 }
3832 read_extent_buffer(leaf, name_ptr,
3833 (unsigned long)(di + 1), name_len);
3834
3835 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3836 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003837
3838 /* is this a reference to our own snapshot? If so
3839 * skip it
3840 */
3841 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3842 location.objectid == root->root_key.objectid) {
3843 over = 0;
3844 goto skip;
3845 }
Chris Mason5f39d392007-10-15 16:14:19 -04003846 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003847 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003848 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003849
Chris Mason3de45862008-11-17 21:02:50 -05003850skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003851 if (name_ptr != tmp_name)
3852 kfree(name_ptr);
3853
Chris Mason39279cc2007-06-12 06:35:45 -04003854 if (over)
3855 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003856 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003857 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003858 di_cur += di_len;
3859 di = (struct btrfs_dir_item *)((char *)di + di_len);
3860 }
3861 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003862
3863 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003864 if (key_type == BTRFS_DIR_INDEX_KEY)
Chris Mason89f135d2009-01-28 15:34:27 -05003865 filp->f_pos = INT_LIMIT(off_t);
Yan Zheng5e591a02008-02-19 11:41:02 -05003866 else
3867 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003868nopos:
3869 ret = 0;
3870err:
Chris Mason39279cc2007-06-12 06:35:45 -04003871 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003872 return ret;
3873}
3874
3875int btrfs_write_inode(struct inode *inode, int wait)
3876{
3877 struct btrfs_root *root = BTRFS_I(inode)->root;
3878 struct btrfs_trans_handle *trans;
3879 int ret = 0;
3880
Yan Zhengc146afa2008-11-12 14:34:12 -05003881 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003882 return 0;
3883
Chris Mason39279cc2007-06-12 06:35:45 -04003884 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003885 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003886 btrfs_set_trans_block_group(trans, inode);
3887 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003888 }
3889 return ret;
3890}
3891
3892/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003893 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003894 * inode changes. But, it is most likely to find the inode in cache.
3895 * FIXME, needs more benchmarking...there are no reasons other than performance
3896 * to keep or drop this code.
3897 */
3898void btrfs_dirty_inode(struct inode *inode)
3899{
3900 struct btrfs_root *root = BTRFS_I(inode)->root;
3901 struct btrfs_trans_handle *trans;
3902
Chris Masonf9295742008-07-17 12:54:14 -04003903 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003904 btrfs_set_trans_block_group(trans, inode);
3905 btrfs_update_inode(trans, root, inode);
3906 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003907}
3908
Chris Masond352ac62008-09-29 15:18:18 -04003909/*
3910 * find the highest existing sequence number in a directory
3911 * and then set the in-memory index_cnt variable to reflect
3912 * free sequence numbers
3913 */
Josef Bacikaec74772008-07-24 12:12:38 -04003914static int btrfs_set_inode_index_count(struct inode *inode)
3915{
3916 struct btrfs_root *root = BTRFS_I(inode)->root;
3917 struct btrfs_key key, found_key;
3918 struct btrfs_path *path;
3919 struct extent_buffer *leaf;
3920 int ret;
3921
3922 key.objectid = inode->i_ino;
3923 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3924 key.offset = (u64)-1;
3925
3926 path = btrfs_alloc_path();
3927 if (!path)
3928 return -ENOMEM;
3929
3930 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3931 if (ret < 0)
3932 goto out;
3933 /* FIXME: we should be able to handle this */
3934 if (ret == 0)
3935 goto out;
3936 ret = 0;
3937
3938 /*
3939 * MAGIC NUMBER EXPLANATION:
3940 * since we search a directory based on f_pos we have to start at 2
3941 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3942 * else has to start at 2
3943 */
3944 if (path->slots[0] == 0) {
3945 BTRFS_I(inode)->index_cnt = 2;
3946 goto out;
3947 }
3948
3949 path->slots[0]--;
3950
3951 leaf = path->nodes[0];
3952 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3953
3954 if (found_key.objectid != inode->i_ino ||
3955 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3956 BTRFS_I(inode)->index_cnt = 2;
3957 goto out;
3958 }
3959
3960 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3961out:
3962 btrfs_free_path(path);
3963 return ret;
3964}
3965
Chris Masond352ac62008-09-29 15:18:18 -04003966/*
3967 * helper to find a free sequence number in a given directory. This current
3968 * code is very simple, later versions will do smarter things in the btree
3969 */
Chris Mason3de45862008-11-17 21:02:50 -05003970int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04003971{
3972 int ret = 0;
3973
3974 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3975 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05003976 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04003977 return ret;
3978 }
3979
Chris Mason00e4e6b2008-08-05 11:18:09 -04003980 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04003981 BTRFS_I(dir)->index_cnt++;
3982
3983 return ret;
3984}
3985
Chris Mason39279cc2007-06-12 06:35:45 -04003986static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3987 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04003988 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05003989 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05003990 u64 ref_objectid, u64 objectid,
3991 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04003992{
3993 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003994 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04003995 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04003996 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05003997 struct btrfs_inode_ref *ref;
3998 struct btrfs_key key[2];
3999 u32 sizes[2];
4000 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004001 int ret;
4002 int owner;
4003
Chris Mason5f39d392007-10-15 16:14:19 -04004004 path = btrfs_alloc_path();
4005 BUG_ON(!path);
4006
Chris Mason39279cc2007-06-12 06:35:45 -04004007 inode = new_inode(root->fs_info->sb);
4008 if (!inode)
4009 return ERR_PTR(-ENOMEM);
4010
Josef Bacikaec74772008-07-24 12:12:38 -04004011 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004012 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004013 if (ret) {
4014 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004015 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004016 }
Josef Bacikaec74772008-07-24 12:12:38 -04004017 }
4018 /*
4019 * index_cnt is ignored for everything but a dir,
4020 * btrfs_get_inode_index_count has an explanation for the magic
4021 * number
4022 */
Chris Masone02119d2008-09-05 16:13:11 -04004023 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004024 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004025 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004026 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05004027 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004028
Chris Mason39279cc2007-06-12 06:35:45 -04004029 if (mode & S_IFDIR)
4030 owner = 0;
4031 else
4032 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004033 BTRFS_I(inode)->block_group =
4034 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004035
4036 key[0].objectid = objectid;
4037 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4038 key[0].offset = 0;
4039
4040 key[1].objectid = objectid;
4041 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4042 key[1].offset = ref_objectid;
4043
4044 sizes[0] = sizeof(struct btrfs_inode_item);
4045 sizes[1] = name_len + sizeof(*ref);
4046
Chris Masonb9473432009-03-13 11:00:37 -04004047 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004048 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4049 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004050 goto fail;
4051
Chris Mason79683f22008-11-19 22:00:53 -05004052 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05004053
Chris Mason42f15d72009-02-06 11:35:57 -05004054 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05004055 inode->i_gid = dir->i_gid;
4056 if (S_ISDIR(mode))
4057 mode |= S_ISGID;
4058 } else
4059 inode->i_gid = current_fsgid();
4060
Chris Mason39279cc2007-06-12 06:35:45 -04004061 inode->i_mode = mode;
4062 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004063 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004064 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004065 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4066 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004067 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004068
4069 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4070 struct btrfs_inode_ref);
4071 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004072 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004073 ptr = (unsigned long)(ref + 1);
4074 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4075
Chris Mason5f39d392007-10-15 16:14:19 -04004076 btrfs_mark_buffer_dirty(path->nodes[0]);
4077 btrfs_free_path(path);
4078
Chris Mason39279cc2007-06-12 06:35:45 -04004079 location = &BTRFS_I(inode)->location;
4080 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004081 location->offset = 0;
4082 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4083
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004084 btrfs_inherit_iflags(inode, dir);
4085
Chris Mason94272162009-07-02 12:26:06 -04004086 if ((mode & S_IFREG)) {
4087 if (btrfs_test_opt(root, NODATASUM))
4088 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4089 if (btrfs_test_opt(root, NODATACOW))
4090 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4091 }
4092
Chris Mason39279cc2007-06-12 06:35:45 -04004093 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004094 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004095 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004096fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004097 if (dir)
4098 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004099 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004100 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004101 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004102}
4103
4104static inline u8 btrfs_inode_type(struct inode *inode)
4105{
4106 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4107}
4108
Chris Masond352ac62008-09-29 15:18:18 -04004109/*
4110 * utility function to add 'inode' into 'parent_inode' with
4111 * a give name and a given sequence number.
4112 * if 'add_backref' is true, also insert a backref from the
4113 * inode to the parent directory.
4114 */
Chris Masone02119d2008-09-05 16:13:11 -04004115int btrfs_add_link(struct btrfs_trans_handle *trans,
4116 struct inode *parent_inode, struct inode *inode,
4117 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004118{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004119 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004120 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004121 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004122
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004123 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4124 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4125 } else {
4126 key.objectid = inode->i_ino;
4127 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4128 key.offset = 0;
4129 }
Chris Mason39279cc2007-06-12 06:35:45 -04004130
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004131 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4132 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4133 key.objectid, root->root_key.objectid,
4134 parent_inode->i_ino,
4135 index, name, name_len);
4136 } else if (add_backref) {
4137 ret = btrfs_insert_inode_ref(trans, root,
4138 name, name_len, inode->i_ino,
4139 parent_inode->i_ino, index);
4140 }
4141
Chris Mason39279cc2007-06-12 06:35:45 -04004142 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004143 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4144 parent_inode->i_ino, &key,
4145 btrfs_inode_type(inode), index);
4146 BUG_ON(ret);
4147
Chris Masondbe674a2008-07-17 12:54:05 -04004148 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004149 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004150 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004151 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004152 }
4153 return ret;
4154}
4155
4156static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004157 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004158 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004159{
Chris Masone02119d2008-09-05 16:13:11 -04004160 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4161 inode, dentry->d_name.name,
4162 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004163 if (!err) {
4164 d_instantiate(dentry, inode);
4165 return 0;
4166 }
4167 if (err > 0)
4168 err = -EEXIST;
4169 return err;
4170}
4171
Josef Bacik618e21d2007-07-11 10:18:17 -04004172static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4173 int mode, dev_t rdev)
4174{
4175 struct btrfs_trans_handle *trans;
4176 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004177 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004178 int err;
4179 int drop_inode = 0;
4180 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004181 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004182 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004183
4184 if (!new_valid_dev(rdev))
4185 return -EINVAL;
4186
Josef Bacik9ed74f22009-09-11 16:12:44 -04004187 /*
4188 * 2 for inode item and ref
4189 * 2 for dir items
4190 * 1 for xattr if selinux is on
4191 */
4192 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004193 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004194 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004195
Josef Bacik618e21d2007-07-11 10:18:17 -04004196 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004197 if (!trans)
4198 goto fail;
Josef Bacik618e21d2007-07-11 10:18:17 -04004199 btrfs_set_trans_block_group(trans, dir);
4200
4201 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4202 if (err) {
4203 err = -ENOSPC;
4204 goto out_unlock;
4205 }
4206
Josef Bacikaec74772008-07-24 12:12:38 -04004207 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004208 dentry->d_name.len,
4209 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004210 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004211 err = PTR_ERR(inode);
4212 if (IS_ERR(inode))
4213 goto out_unlock;
4214
Jim Owens0279b4c2009-02-04 09:29:13 -05004215 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004216 if (err) {
4217 drop_inode = 1;
4218 goto out_unlock;
4219 }
4220
Josef Bacik618e21d2007-07-11 10:18:17 -04004221 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004222 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004223 if (err)
4224 drop_inode = 1;
4225 else {
4226 inode->i_op = &btrfs_special_inode_operations;
4227 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004228 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004229 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004230 btrfs_update_inode_block_group(trans, inode);
4231 btrfs_update_inode_block_group(trans, dir);
4232out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004233 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004234 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004235fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004236 btrfs_unreserve_metadata_space(root, 5);
Josef Bacik618e21d2007-07-11 10:18:17 -04004237 if (drop_inode) {
4238 inode_dec_link_count(inode);
4239 iput(inode);
4240 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004241 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004242 return err;
4243}
4244
Chris Mason39279cc2007-06-12 06:35:45 -04004245static int btrfs_create(struct inode *dir, struct dentry *dentry,
4246 int mode, struct nameidata *nd)
4247{
4248 struct btrfs_trans_handle *trans;
4249 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004250 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004251 int err;
4252 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05004253 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004254 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004255 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004256
Josef Bacik9ed74f22009-09-11 16:12:44 -04004257 /*
4258 * 2 for inode item and ref
4259 * 2 for dir items
4260 * 1 for xattr if selinux is on
4261 */
4262 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004263 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004264 return err;
4265
Chris Mason39279cc2007-06-12 06:35:45 -04004266 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004267 if (!trans)
4268 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04004269 btrfs_set_trans_block_group(trans, dir);
4270
4271 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4272 if (err) {
4273 err = -ENOSPC;
4274 goto out_unlock;
4275 }
4276
Josef Bacikaec74772008-07-24 12:12:38 -04004277 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004278 dentry->d_name.len,
4279 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004280 objectid, BTRFS_I(dir)->block_group, mode,
4281 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004282 err = PTR_ERR(inode);
4283 if (IS_ERR(inode))
4284 goto out_unlock;
4285
Jim Owens0279b4c2009-02-04 09:29:13 -05004286 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004287 if (err) {
4288 drop_inode = 1;
4289 goto out_unlock;
4290 }
4291
Chris Mason39279cc2007-06-12 06:35:45 -04004292 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004293 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004294 if (err)
4295 drop_inode = 1;
4296 else {
4297 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004298 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004299 inode->i_fop = &btrfs_file_operations;
4300 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004301 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004302 }
Chris Mason39279cc2007-06-12 06:35:45 -04004303 btrfs_update_inode_block_group(trans, inode);
4304 btrfs_update_inode_block_group(trans, dir);
4305out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004306 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004307 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004308fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004309 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004310 if (drop_inode) {
4311 inode_dec_link_count(inode);
4312 iput(inode);
4313 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004314 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004315 return err;
4316}
4317
4318static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4319 struct dentry *dentry)
4320{
4321 struct btrfs_trans_handle *trans;
4322 struct btrfs_root *root = BTRFS_I(dir)->root;
4323 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004324 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004325 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004326 int err;
4327 int drop_inode = 0;
4328
4329 if (inode->i_nlink == 0)
4330 return -ENOENT;
4331
Josef Bacik9ed74f22009-09-11 16:12:44 -04004332 /*
4333 * 1 item for inode ref
4334 * 2 items for dir items
4335 */
4336 err = btrfs_reserve_metadata_space(root, 3);
Chris Mason1832a6d2007-12-21 16:27:21 -05004337 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004338 return err;
4339
4340 btrfs_inc_nlink(inode);
4341
Chris Mason3de45862008-11-17 21:02:50 -05004342 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004343 if (err)
4344 goto fail;
4345
Chris Mason39279cc2007-06-12 06:35:45 -04004346 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004347
Chris Mason39279cc2007-06-12 06:35:45 -04004348 btrfs_set_trans_block_group(trans, dir);
4349 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004350
Chris Mason00e4e6b2008-08-05 11:18:09 -04004351 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004352
Yan, Zhenga5719522009-09-24 09:17:31 -04004353 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004354 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004355 } else {
4356 btrfs_update_inode_block_group(trans, dir);
4357 err = btrfs_update_inode(trans, root, inode);
4358 BUG_ON(err);
4359 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4360 }
Chris Mason39279cc2007-06-12 06:35:45 -04004361
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004362 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004363 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004364fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004365 btrfs_unreserve_metadata_space(root, 3);
Chris Mason39279cc2007-06-12 06:35:45 -04004366 if (drop_inode) {
4367 inode_dec_link_count(inode);
4368 iput(inode);
4369 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004370 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004371 return err;
4372}
4373
Chris Mason39279cc2007-06-12 06:35:45 -04004374static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4375{
Chris Masonb9d86662008-05-02 16:13:49 -04004376 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004377 struct btrfs_trans_handle *trans;
4378 struct btrfs_root *root = BTRFS_I(dir)->root;
4379 int err = 0;
4380 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004381 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004382 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004383 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004384
Josef Bacik9ed74f22009-09-11 16:12:44 -04004385 /*
4386 * 2 items for inode and ref
4387 * 2 items for dir items
4388 * 1 for xattr if selinux is on
4389 */
4390 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004391 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004392 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004393
Chris Mason39279cc2007-06-12 06:35:45 -04004394 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004395 if (!trans) {
4396 err = -ENOMEM;
Chris Mason39279cc2007-06-12 06:35:45 -04004397 goto out_unlock;
4398 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004399 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004400
4401 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4402 if (err) {
4403 err = -ENOSPC;
4404 goto out_unlock;
4405 }
4406
Josef Bacikaec74772008-07-24 12:12:38 -04004407 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004408 dentry->d_name.len,
4409 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004410 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4411 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004412 if (IS_ERR(inode)) {
4413 err = PTR_ERR(inode);
4414 goto out_fail;
4415 }
Chris Mason5f39d392007-10-15 16:14:19 -04004416
Chris Mason39279cc2007-06-12 06:35:45 -04004417 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004418
Jim Owens0279b4c2009-02-04 09:29:13 -05004419 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004420 if (err)
4421 goto out_fail;
4422
Chris Mason39279cc2007-06-12 06:35:45 -04004423 inode->i_op = &btrfs_dir_inode_operations;
4424 inode->i_fop = &btrfs_dir_file_operations;
4425 btrfs_set_trans_block_group(trans, inode);
4426
Chris Masondbe674a2008-07-17 12:54:05 -04004427 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004428 err = btrfs_update_inode(trans, root, inode);
4429 if (err)
4430 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004431
Chris Masone02119d2008-09-05 16:13:11 -04004432 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4433 inode, dentry->d_name.name,
4434 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004435 if (err)
4436 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004437
Chris Mason39279cc2007-06-12 06:35:45 -04004438 d_instantiate(dentry, inode);
4439 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004440 btrfs_update_inode_block_group(trans, inode);
4441 btrfs_update_inode_block_group(trans, dir);
4442
4443out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004444 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004445 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04004446
Chris Mason39279cc2007-06-12 06:35:45 -04004447out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004448 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004449 if (drop_on_err)
4450 iput(inode);
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
Chris Masond352ac62008-09-29 15:18:18 -04004455/* helper for btfs_get_extent. Given an existing extent in the tree,
4456 * and an extent that you want to insert, deal with overlap and insert
4457 * the new extent into the tree.
4458 */
Chris Mason3b951512008-04-17 11:29:12 -04004459static int merge_extent_mapping(struct extent_map_tree *em_tree,
4460 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004461 struct extent_map *em,
4462 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004463{
4464 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004465
Chris Masone6dcd2d2008-07-17 12:53:50 -04004466 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4467 start_diff = map_start - em->start;
4468 em->start = map_start;
4469 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004470 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4471 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004472 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004473 em->block_len -= start_diff;
4474 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004475 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004476}
4477
Chris Masonc8b97812008-10-29 14:49:59 -04004478static noinline int uncompress_inline(struct btrfs_path *path,
4479 struct inode *inode, struct page *page,
4480 size_t pg_offset, u64 extent_offset,
4481 struct btrfs_file_extent_item *item)
4482{
4483 int ret;
4484 struct extent_buffer *leaf = path->nodes[0];
4485 char *tmp;
4486 size_t max_size;
4487 unsigned long inline_size;
4488 unsigned long ptr;
4489
4490 WARN_ON(pg_offset != 0);
4491 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4492 inline_size = btrfs_file_extent_inline_item_len(leaf,
4493 btrfs_item_nr(leaf, path->slots[0]));
4494 tmp = kmalloc(inline_size, GFP_NOFS);
4495 ptr = btrfs_file_extent_inline_start(item);
4496
4497 read_extent_buffer(leaf, tmp, ptr, inline_size);
4498
Chris Mason5b050f02008-11-11 09:34:41 -05004499 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004500 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4501 inline_size, max_size);
4502 if (ret) {
4503 char *kaddr = kmap_atomic(page, KM_USER0);
4504 unsigned long copy_size = min_t(u64,
4505 PAGE_CACHE_SIZE - pg_offset,
4506 max_size - extent_offset);
4507 memset(kaddr + pg_offset, 0, copy_size);
4508 kunmap_atomic(kaddr, KM_USER0);
4509 }
4510 kfree(tmp);
4511 return 0;
4512}
4513
Chris Masond352ac62008-09-29 15:18:18 -04004514/*
4515 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004516 * the ugly parts come from merging extents from the disk with the in-ram
4517 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004518 * where the in-ram extents might be locked pending data=ordered completion.
4519 *
4520 * This also copies inline extents directly into the page.
4521 */
Chris Masond3977122009-01-05 21:25:51 -05004522
Chris Masona52d9a82007-08-27 16:49:44 -04004523struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004524 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004525 int create)
4526{
4527 int ret;
4528 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004529 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004530 u64 extent_start = 0;
4531 u64 extent_end = 0;
4532 u64 objectid = inode->i_ino;
4533 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004534 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004535 struct btrfs_root *root = BTRFS_I(inode)->root;
4536 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004537 struct extent_buffer *leaf;
4538 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004539 struct extent_map *em = NULL;
4540 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004541 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004542 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004543 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004544
Chris Masona52d9a82007-08-27 16:49:44 -04004545again:
Chris Mason890871b2009-09-02 16:24:52 -04004546 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004547 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004548 if (em)
4549 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004550 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004551
Chris Masona52d9a82007-08-27 16:49:44 -04004552 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004553 if (em->start > start || em->start + em->len <= start)
4554 free_extent_map(em);
4555 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004556 free_extent_map(em);
4557 else
4558 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004559 }
Chris Masond1310b22008-01-24 16:13:08 -05004560 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004561 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004562 err = -ENOMEM;
4563 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004564 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004565 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004566 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004567 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004568 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004569 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004570
4571 if (!path) {
4572 path = btrfs_alloc_path();
4573 BUG_ON(!path);
4574 }
4575
Chris Mason179e29e2007-11-01 11:28:41 -04004576 ret = btrfs_lookup_file_extent(trans, root, path,
4577 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004578 if (ret < 0) {
4579 err = ret;
4580 goto out;
4581 }
4582
4583 if (ret != 0) {
4584 if (path->slots[0] == 0)
4585 goto not_found;
4586 path->slots[0]--;
4587 }
4588
Chris Mason5f39d392007-10-15 16:14:19 -04004589 leaf = path->nodes[0];
4590 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004591 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004592 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004593 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4594 found_type = btrfs_key_type(&found_key);
4595 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004596 found_type != BTRFS_EXTENT_DATA_KEY) {
4597 goto not_found;
4598 }
4599
Chris Mason5f39d392007-10-15 16:14:19 -04004600 found_type = btrfs_file_extent_type(leaf, item);
4601 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004602 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004603 if (found_type == BTRFS_FILE_EXTENT_REG ||
4604 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004605 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004606 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004607 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4608 size_t size;
4609 size = btrfs_file_extent_inline_len(leaf, item);
4610 extent_end = (extent_start + size + root->sectorsize - 1) &
4611 ~((u64)root->sectorsize - 1);
4612 }
4613
4614 if (start >= extent_end) {
4615 path->slots[0]++;
4616 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4617 ret = btrfs_next_leaf(root, path);
4618 if (ret < 0) {
4619 err = ret;
4620 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004621 }
Yan Zheng9036c102008-10-30 14:19:41 -04004622 if (ret > 0)
4623 goto not_found;
4624 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004625 }
Yan Zheng9036c102008-10-30 14:19:41 -04004626 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4627 if (found_key.objectid != objectid ||
4628 found_key.type != BTRFS_EXTENT_DATA_KEY)
4629 goto not_found;
4630 if (start + len <= found_key.offset)
4631 goto not_found;
4632 em->start = start;
4633 em->len = found_key.offset - start;
4634 goto not_found_em;
4635 }
4636
Yan Zhengd899e052008-10-30 14:25:28 -04004637 if (found_type == BTRFS_FILE_EXTENT_REG ||
4638 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004639 em->start = extent_start;
4640 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004641 em->orig_start = extent_start -
4642 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004643 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4644 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004645 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004646 goto insert;
4647 }
Chris Masonc8b97812008-10-29 14:49:59 -04004648 if (compressed) {
4649 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4650 em->block_start = bytenr;
4651 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4652 item);
4653 } else {
4654 bytenr += btrfs_file_extent_offset(leaf, item);
4655 em->block_start = bytenr;
4656 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004657 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4658 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004659 }
Chris Masona52d9a82007-08-27 16:49:44 -04004660 goto insert;
4661 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004662 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004663 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004664 size_t size;
4665 size_t extent_offset;
4666 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004667
Chris Masona52d9a82007-08-27 16:49:44 -04004668 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004669 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004670 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004671 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004672 goto out;
4673 }
4674
Yan Zheng9036c102008-10-30 14:19:41 -04004675 size = btrfs_file_extent_inline_len(leaf, item);
4676 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004677 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004678 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004679 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004680 em->len = (copy_size + root->sectorsize - 1) &
4681 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004682 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004683 if (compressed)
4684 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004685 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004686 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004687 if (btrfs_file_extent_compression(leaf, item) ==
4688 BTRFS_COMPRESS_ZLIB) {
4689 ret = uncompress_inline(path, inode, page,
4690 pg_offset,
4691 extent_offset, item);
4692 BUG_ON(ret);
4693 } else {
4694 map = kmap(page);
4695 read_extent_buffer(leaf, map + pg_offset, ptr,
4696 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04004697 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4698 memset(map + pg_offset + copy_size, 0,
4699 PAGE_CACHE_SIZE - pg_offset -
4700 copy_size);
4701 }
Chris Masonc8b97812008-10-29 14:49:59 -04004702 kunmap(page);
4703 }
Chris Mason179e29e2007-11-01 11:28:41 -04004704 flush_dcache_page(page);
4705 } else if (create && PageUptodate(page)) {
4706 if (!trans) {
4707 kunmap(page);
4708 free_extent_map(em);
4709 em = NULL;
4710 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004711 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004712 goto again;
4713 }
Chris Masonc8b97812008-10-29 14:49:59 -04004714 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004715 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004716 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004717 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004718 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004719 }
Chris Masond1310b22008-01-24 16:13:08 -05004720 set_extent_uptodate(io_tree, em->start,
4721 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004722 goto insert;
4723 } else {
Chris Masond3977122009-01-05 21:25:51 -05004724 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004725 WARN_ON(1);
4726 }
4727not_found:
4728 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004729 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004730not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004731 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004732 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004733insert:
4734 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004735 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004736 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4737 "[%llu %llu]\n", (unsigned long long)em->start,
4738 (unsigned long long)em->len,
4739 (unsigned long long)start,
4740 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004741 err = -EIO;
4742 goto out;
4743 }
Chris Masond1310b22008-01-24 16:13:08 -05004744
4745 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04004746 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004747 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004748 /* it is possible that someone inserted the extent into the tree
4749 * while we had the lock dropped. It is also possible that
4750 * an overlapping map exists in the tree
4751 */
Chris Masona52d9a82007-08-27 16:49:44 -04004752 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004753 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004754
4755 ret = 0;
4756
Chris Mason3b951512008-04-17 11:29:12 -04004757 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004758 if (existing && (existing->start > start ||
4759 existing->start + existing->len <= start)) {
4760 free_extent_map(existing);
4761 existing = NULL;
4762 }
Chris Mason3b951512008-04-17 11:29:12 -04004763 if (!existing) {
4764 existing = lookup_extent_mapping(em_tree, em->start,
4765 em->len);
4766 if (existing) {
4767 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004768 em, start,
4769 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004770 free_extent_map(existing);
4771 if (err) {
4772 free_extent_map(em);
4773 em = NULL;
4774 }
4775 } else {
4776 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004777 free_extent_map(em);
4778 em = NULL;
4779 }
4780 } else {
4781 free_extent_map(em);
4782 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004783 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004784 }
Chris Masona52d9a82007-08-27 16:49:44 -04004785 }
Chris Mason890871b2009-09-02 16:24:52 -04004786 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004787out:
Chris Masonf4219502008-07-22 11:18:09 -04004788 if (path)
4789 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004790 if (trans) {
4791 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004792 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004793 err = ret;
4794 }
Chris Masona52d9a82007-08-27 16:49:44 -04004795 if (err) {
4796 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04004797 return ERR_PTR(err);
4798 }
4799 return em;
4800}
4801
Chris Mason16432982008-04-10 10:23:21 -04004802static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4803 const struct iovec *iov, loff_t offset,
4804 unsigned long nr_segs)
4805{
Chris Masone1c4b742008-04-22 13:26:46 -04004806 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004807}
4808
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004809static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4810 __u64 start, __u64 len)
4811{
4812 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4813}
4814
Chris Mason9ebefb182007-06-15 13:50:00 -04004815int btrfs_readpage(struct file *file, struct page *page)
4816{
Chris Masond1310b22008-01-24 16:13:08 -05004817 struct extent_io_tree *tree;
4818 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004819 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004820}
Chris Mason1832a6d2007-12-21 16:27:21 -05004821
Chris Mason39279cc2007-06-12 06:35:45 -04004822static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4823{
Chris Masond1310b22008-01-24 16:13:08 -05004824 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004825
4826
4827 if (current->flags & PF_MEMALLOC) {
4828 redirty_page_for_writepage(wbc, page);
4829 unlock_page(page);
4830 return 0;
4831 }
Chris Masond1310b22008-01-24 16:13:08 -05004832 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004833 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4834}
Chris Mason39279cc2007-06-12 06:35:45 -04004835
Chris Masonf4219502008-07-22 11:18:09 -04004836int btrfs_writepages(struct address_space *mapping,
4837 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004838{
Chris Masond1310b22008-01-24 16:13:08 -05004839 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004840
Chris Masond1310b22008-01-24 16:13:08 -05004841 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004842 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4843}
4844
Chris Mason3ab2fb52007-11-08 10:59:22 -05004845static int
4846btrfs_readpages(struct file *file, struct address_space *mapping,
4847 struct list_head *pages, unsigned nr_pages)
4848{
Chris Masond1310b22008-01-24 16:13:08 -05004849 struct extent_io_tree *tree;
4850 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004851 return extent_readpages(tree, mapping, pages, nr_pages,
4852 btrfs_get_extent);
4853}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004854static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004855{
Chris Masond1310b22008-01-24 16:13:08 -05004856 struct extent_io_tree *tree;
4857 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004858 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004859
Chris Masond1310b22008-01-24 16:13:08 -05004860 tree = &BTRFS_I(page->mapping->host)->io_tree;
4861 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004862 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004863 if (ret == 1) {
4864 ClearPagePrivate(page);
4865 set_page_private(page, 0);
4866 page_cache_release(page);
4867 }
4868 return ret;
4869}
Chris Mason39279cc2007-06-12 06:35:45 -04004870
Chris Masone6dcd2d2008-07-17 12:53:50 -04004871static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4872{
Chris Mason98509cf2008-09-11 15:51:43 -04004873 if (PageWriteback(page) || PageDirty(page))
4874 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05004875 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004876}
4877
Chris Masona52d9a82007-08-27 16:49:44 -04004878static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4879{
Chris Masond1310b22008-01-24 16:13:08 -05004880 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004881 struct btrfs_ordered_extent *ordered;
4882 u64 page_start = page_offset(page);
4883 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004884
Chris Mason8b62b722009-09-02 16:53:46 -04004885
4886 /*
4887 * we have the page locked, so new writeback can't start,
4888 * and the dirty bit won't be cleared while we are here.
4889 *
4890 * Wait for IO on this page so that we can safely clear
4891 * the PagePrivate2 bit and do ordered accounting
4892 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004893 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04004894
Chris Masond1310b22008-01-24 16:13:08 -05004895 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004896 if (offset) {
4897 btrfs_releasepage(page, GFP_NOFS);
4898 return;
4899 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004900 lock_extent(tree, page_start, page_end, GFP_NOFS);
4901 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4902 page_offset(page));
4903 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004904 /*
4905 * IO on this page will never be started, so we need
4906 * to account for any ordered extents now
4907 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004908 clear_extent_bit(tree, page_start, page_end,
4909 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04004910 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
4911 NULL, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04004912 /*
4913 * whoever cleared the private bit is responsible
4914 * for the finish_ordered_io
4915 */
4916 if (TestClearPagePrivate2(page)) {
4917 btrfs_finish_ordered_io(page->mapping->host,
4918 page_start, page_end);
4919 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004920 btrfs_put_ordered_extent(ordered);
4921 lock_extent(tree, page_start, page_end, GFP_NOFS);
4922 }
4923 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004924 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4925 EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004926 __btrfs_releasepage(page, GFP_NOFS);
4927
Chris Mason4a096752008-07-21 10:29:44 -04004928 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004929 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004930 ClearPagePrivate(page);
4931 set_page_private(page, 0);
4932 page_cache_release(page);
4933 }
Chris Mason39279cc2007-06-12 06:35:45 -04004934}
4935
Chris Mason9ebefb182007-06-15 13:50:00 -04004936/*
4937 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4938 * called from a page fault handler when a page is first dirtied. Hence we must
4939 * be careful to check for EOF conditions here. We set the page up correctly
4940 * for a written page which means we get ENOSPC checking when writing into
4941 * holes and correct delalloc and unwritten extent mapping on filesystems that
4942 * support these features.
4943 *
4944 * We are not allowed to take the i_mutex here so we have to play games to
4945 * protect against truncate races as the page could now be beyond EOF. Because
4946 * vmtruncate() writes the inode size before removing pages, once we have the
4947 * page lock we can determine safely if the page is beyond EOF. If it is not
4948 * beyond EOF, then the page is guaranteed safe against truncation until we
4949 * unlock the page.
4950 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07004951int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04004952{
Nick Pigginc2ec1752009-03-31 15:23:21 -07004953 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05004954 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004955 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004956 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4957 struct btrfs_ordered_extent *ordered;
4958 char *kaddr;
4959 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04004960 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05004961 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04004962 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004963 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04004964
Josef Bacik6a632092009-02-20 11:00:09 -05004965 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07004966 if (ret) {
4967 if (ret == -ENOMEM)
4968 ret = VM_FAULT_OOM;
4969 else /* -ENOSPC, -EIO, etc */
4970 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05004971 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07004972 }
Chris Mason1832a6d2007-12-21 16:27:21 -05004973
Josef Bacik9ed74f22009-09-11 16:12:44 -04004974 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
4975 if (ret) {
4976 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
4977 ret = VM_FAULT_SIGBUS;
4978 goto out;
4979 }
4980
Nick Piggin56a76f82009-03-31 15:23:23 -07004981 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004982again:
Chris Mason9ebefb182007-06-15 13:50:00 -04004983 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04004984 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004985 page_start = page_offset(page);
4986 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004987
Chris Mason9ebefb182007-06-15 13:50:00 -04004988 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04004989 (page_start >= size)) {
Josef Bacik6a632092009-02-20 11:00:09 -05004990 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason9ebefb182007-06-15 13:50:00 -04004991 /* page got truncated out from underneath us */
4992 goto out_unlock;
4993 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004994 wait_on_page_writeback(page);
4995
4996 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4997 set_page_extent_mapped(page);
4998
Chris Masoneb84ae02008-07-17 13:53:27 -04004999 /*
5000 * we can't set the delalloc bits if there are pending ordered
5001 * extents. Drop our locks and wait for them to finish
5002 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005003 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5004 if (ordered) {
5005 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5006 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005007 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005008 btrfs_put_ordered_extent(ordered);
5009 goto again;
5010 }
5011
Josef Bacikfbf19082009-10-01 17:10:23 -04005012 /*
5013 * XXX - page_mkwrite gets called every time the page is dirtied, even
5014 * if it was already dirty, so for space accounting reasons we need to
5015 * clear any delalloc bits for the range we are fixing to save. There
5016 * is probably a better way to do this, but for now keep consistent with
5017 * prepare_pages in the normal write path.
5018 */
5019 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005020 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
5021 GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04005022
Josef Bacik9ed74f22009-09-11 16:12:44 -04005023 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
5024 if (ret) {
5025 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5026 ret = VM_FAULT_SIGBUS;
Josef Bacikfbf19082009-10-01 17:10:23 -04005027 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005028 goto out_unlock;
5029 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005030 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04005031
5032 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04005033 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005034 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04005035 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04005036 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04005037
Chris Masone6dcd2d2008-07-17 12:53:50 -04005038 if (zero_start != PAGE_CACHE_SIZE) {
5039 kaddr = kmap(page);
5040 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5041 flush_dcache_page(page);
5042 kunmap(page);
5043 }
Chris Mason247e7432008-07-17 12:53:51 -04005044 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005045 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04005046 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005047
Chris Mason257c62e2009-10-13 13:21:08 -04005048 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5049 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5050
Chris Masone6dcd2d2008-07-17 12:53:50 -04005051 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04005052
5053out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005054 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason50a9b212009-09-11 12:33:12 -04005055 if (!ret)
5056 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04005057 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05005058out:
Chris Mason9ebefb182007-06-15 13:50:00 -04005059 return ret;
5060}
5061
Chris Mason39279cc2007-06-12 06:35:45 -04005062static void btrfs_truncate(struct inode *inode)
5063{
5064 struct btrfs_root *root = BTRFS_I(inode)->root;
5065 int ret;
5066 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005067 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04005068 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005069
5070 if (!S_ISREG(inode->i_mode))
5071 return;
5072 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
5073 return;
5074
Josef Bacik5d5e1032009-10-13 16:46:49 -04005075 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5076 if (ret)
5077 return;
Chris Mason4a096752008-07-21 10:29:44 -04005078 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04005079
Chris Mason39279cc2007-06-12 06:35:45 -04005080 trans = btrfs_start_transaction(root, 1);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005081
5082 /*
5083 * setattr is responsible for setting the ordered_data_close flag,
5084 * but that is only tested during the last file release. That
5085 * could happen well after the next commit, leaving a great big
5086 * window where new writes may get lost if someone chooses to write
5087 * to this file after truncating to zero
5088 *
5089 * The inode doesn't have any dirty data here, and so if we commit
5090 * this is a noop. If someone immediately starts writing to the inode
5091 * it is very likely we'll catch some of their writes in this
5092 * transaction, and the commit will find this file on the ordered
5093 * data list with good things to send down.
5094 *
5095 * This is a best effort solution, there is still a window where
5096 * using truncate to replace the contents of the file will
5097 * end up with a zero length file after a crash.
5098 */
5099 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5100 btrfs_add_ordered_operation(trans, root, inode);
5101
Chris Mason39279cc2007-06-12 06:35:45 -04005102 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04005103 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04005104
Josef Bacik7b128762008-07-24 12:17:14 -04005105 ret = btrfs_orphan_add(trans, inode);
5106 if (ret)
5107 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005108 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04005109 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05005110 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04005111 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005112
Josef Bacik7b128762008-07-24 12:17:14 -04005113 ret = btrfs_orphan_del(trans, inode);
5114 BUG_ON(ret);
5115
5116out:
5117 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04005118 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005119 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005120 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005121}
5122
Sven Wegener3b963622008-06-09 21:57:42 -04005123/*
Chris Masond352ac62008-09-29 15:18:18 -04005124 * create a new subvolume directory/inode (helper for the ioctl).
5125 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05005126int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04005127 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005128 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04005129{
Chris Mason39279cc2007-06-12 06:35:45 -04005130 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005131 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005132 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005133
Josef Bacikaec74772008-07-24 12:12:38 -04005134 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005135 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04005136 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005137 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005138 inode->i_op = &btrfs_dir_inode_operations;
5139 inode->i_fop = &btrfs_dir_file_operations;
5140
Chris Mason39279cc2007-06-12 06:35:45 -04005141 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04005142 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04005143
Yan, Zheng76dda932009-09-21 16:00:26 -04005144 err = btrfs_update_inode(trans, new_root, inode);
5145 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005146
Yan, Zheng76dda932009-09-21 16:00:26 -04005147 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005148 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005149}
5150
Chris Masond352ac62008-09-29 15:18:18 -04005151/* helper function for file defrag and space balancing. This
5152 * forces readahead on a given range of bytes in an inode
5153 */
Chris Masonedbd8d42007-12-21 16:27:24 -05005154unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04005155 struct file_ra_state *ra, struct file *file,
5156 pgoff_t offset, pgoff_t last_index)
5157{
Chris Mason8e7bf942008-04-28 09:02:36 -04005158 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04005159
Chris Mason86479a02007-09-10 19:58:16 -04005160 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5161 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04005162}
5163
Chris Mason39279cc2007-06-12 06:35:45 -04005164struct inode *btrfs_alloc_inode(struct super_block *sb)
5165{
5166 struct btrfs_inode *ei;
5167
5168 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5169 if (!ei)
5170 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04005171 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04005172 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005173 ei->logged_trans = 0;
Josef Bacik32c00af2009-10-08 13:34:05 -04005174 ei->outstanding_extents = 0;
5175 ei->reserved_extents = 0;
Josef Bacika6dbd422009-11-11 15:53:34 -05005176 ei->root = NULL;
Josef Bacik32c00af2009-10-08 13:34:05 -04005177 spin_lock_init(&ei->accounting_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005178 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04005179 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005180 INIT_LIST_HEAD(&ei->ordered_operations);
Chris Mason39279cc2007-06-12 06:35:45 -04005181 return &ei->vfs_inode;
5182}
5183
5184void btrfs_destroy_inode(struct inode *inode)
5185{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005186 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005187 struct btrfs_root *root = BTRFS_I(inode)->root;
5188
Chris Mason39279cc2007-06-12 06:35:45 -04005189 WARN_ON(!list_empty(&inode->i_dentry));
5190 WARN_ON(inode->i_data.nrpages);
5191
Chris Mason5a3f23d2009-03-31 13:27:11 -04005192 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05005193 * This can happen where we create an inode, but somebody else also
5194 * created the same inode and we need to destroy the one we already
5195 * created.
5196 */
5197 if (!root)
5198 goto free;
5199
5200 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005201 * Make sure we're properly removed from the ordered operation
5202 * lists.
5203 */
5204 smp_mb();
5205 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5206 spin_lock(&root->fs_info->ordered_extent_lock);
5207 list_del_init(&BTRFS_I(inode)->ordered_operations);
5208 spin_unlock(&root->fs_info->ordered_extent_lock);
5209 }
5210
5211 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005212 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
5213 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
5214 " list\n", inode->i_ino);
5215 dump_stack();
5216 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04005217 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005218
Chris Masond3977122009-01-05 21:25:51 -05005219 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005220 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5221 if (!ordered)
5222 break;
5223 else {
Chris Masond3977122009-01-05 21:25:51 -05005224 printk(KERN_ERR "btrfs found ordered "
5225 "extent %llu %llu on inode cleanup\n",
5226 (unsigned long long)ordered->file_offset,
5227 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005228 btrfs_remove_ordered_extent(inode, ordered);
5229 btrfs_put_ordered_extent(ordered);
5230 btrfs_put_ordered_extent(ordered);
5231 }
5232 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005233 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005234 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05005235free:
Chris Mason39279cc2007-06-12 06:35:45 -04005236 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5237}
5238
Yan, Zheng76dda932009-09-21 16:00:26 -04005239void btrfs_drop_inode(struct inode *inode)
5240{
5241 struct btrfs_root *root = BTRFS_I(inode)->root;
5242
5243 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5244 generic_delete_inode(inode);
5245 else
5246 generic_drop_inode(inode);
5247}
5248
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005249static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04005250{
5251 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5252
5253 inode_init_once(&ei->vfs_inode);
5254}
5255
5256void btrfs_destroy_cachep(void)
5257{
5258 if (btrfs_inode_cachep)
5259 kmem_cache_destroy(btrfs_inode_cachep);
5260 if (btrfs_trans_handle_cachep)
5261 kmem_cache_destroy(btrfs_trans_handle_cachep);
5262 if (btrfs_transaction_cachep)
5263 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04005264 if (btrfs_path_cachep)
5265 kmem_cache_destroy(btrfs_path_cachep);
5266}
5267
5268int btrfs_init_cachep(void)
5269{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005270 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5271 sizeof(struct btrfs_inode), 0,
5272 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04005273 if (!btrfs_inode_cachep)
5274 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005275
5276 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5277 sizeof(struct btrfs_trans_handle), 0,
5278 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005279 if (!btrfs_trans_handle_cachep)
5280 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005281
5282 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5283 sizeof(struct btrfs_transaction), 0,
5284 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005285 if (!btrfs_transaction_cachep)
5286 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005287
5288 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5289 sizeof(struct btrfs_path), 0,
5290 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005291 if (!btrfs_path_cachep)
5292 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005293
Chris Mason39279cc2007-06-12 06:35:45 -04005294 return 0;
5295fail:
5296 btrfs_destroy_cachep();
5297 return -ENOMEM;
5298}
5299
5300static int btrfs_getattr(struct vfsmount *mnt,
5301 struct dentry *dentry, struct kstat *stat)
5302{
5303 struct inode *inode = dentry->d_inode;
5304 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05005305 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05005306 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005307 stat->blocks = (inode_get_bytes(inode) +
5308 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04005309 return 0;
5310}
5311
Chris Masond3977122009-01-05 21:25:51 -05005312static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5313 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005314{
5315 struct btrfs_trans_handle *trans;
5316 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005317 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005318 struct inode *new_inode = new_dentry->d_inode;
5319 struct inode *old_inode = old_dentry->d_inode;
5320 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005321 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005322 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005323 int ret;
5324
Yan, Zhengf679a842009-09-24 09:17:31 -04005325 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5326 return -EPERM;
5327
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005328 /* we only allow rename subvolume link between subvolumes */
5329 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05005330 return -EXDEV;
5331
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005332 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5333 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5334 return -ENOTEMPTY;
5335
Chris Mason39279cc2007-06-12 06:35:45 -04005336 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005337 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04005338 return -ENOTEMPTY;
Chris Mason0660b5a2008-11-17 20:37:39 -05005339
Josef Bacik9ed74f22009-09-11 16:12:44 -04005340 /*
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005341 * We want to reserve the absolute worst case amount of items. So if
5342 * both inodes are subvols and we need to unlink them then that would
5343 * require 4 item modifications, but if they are both normal inodes it
5344 * would require 5 item modifications, so we'll assume their normal
5345 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5346 * should cover the worst case number of items we'll modify.
Josef Bacik9ed74f22009-09-11 16:12:44 -04005347 */
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005348 ret = btrfs_reserve_metadata_space(root, 11);
Chris Mason1832a6d2007-12-21 16:27:21 -05005349 if (ret)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005350 return ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05005351
Chris Mason5a3f23d2009-03-31 13:27:11 -04005352 /*
5353 * we're using rename to replace one file with another.
5354 * and the replacement file is large. Start IO on it now so
5355 * we don't add too much work to the end of the transaction
5356 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04005357 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04005358 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5359 filemap_flush(old_inode->i_mapping);
5360
Yan, Zheng76dda932009-09-21 16:00:26 -04005361 /* close the racy window with snapshot create/destroy ioctl */
5362 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5363 down_read(&root->fs_info->subvol_sem);
Chris Mason5f39d392007-10-15 16:14:19 -04005364
Chris Mason39279cc2007-06-12 06:35:45 -04005365 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga5719522009-09-24 09:17:31 -04005366 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04005367
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005368 if (dest != root)
5369 btrfs_record_root_in_trans(trans, dest);
5370
Yan, Zhenga5719522009-09-24 09:17:31 -04005371 ret = btrfs_set_inode_index(new_dir, &index);
5372 if (ret)
5373 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005374
Yan, Zhenga5719522009-09-24 09:17:31 -04005375 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005376 /* force full log commit if subvolume involved. */
5377 root->fs_info->last_trans_log_full_commit = trans->transid;
5378 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04005379 ret = btrfs_insert_inode_ref(trans, dest,
5380 new_dentry->d_name.name,
5381 new_dentry->d_name.len,
5382 old_inode->i_ino,
5383 new_dir->i_ino, index);
5384 if (ret)
5385 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005386 /*
5387 * this is an ugly little race, but the rename is required
5388 * to make sure that if we crash, the inode is either at the
5389 * old name or the new one. pinning the log transaction lets
5390 * us make sure we don't allow a log commit to come in after
5391 * we unlink the name but before we add the new name back in.
5392 */
5393 btrfs_pin_log_trans(root);
5394 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005395 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005396 * make sure the inode gets flushed if it is replacing
5397 * something.
5398 */
5399 if (new_inode && new_inode->i_size &&
5400 old_inode && S_ISREG(old_inode->i_mode)) {
5401 btrfs_add_ordered_operation(trans, root, old_inode);
5402 }
5403
Chris Mason39279cc2007-06-12 06:35:45 -04005404 old_dir->i_ctime = old_dir->i_mtime = ctime;
5405 new_dir->i_ctime = new_dir->i_mtime = ctime;
5406 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04005407
Chris Mason12fcfd22009-03-24 10:24:20 -04005408 if (old_dentry->d_parent != new_dentry->d_parent)
5409 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5410
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005411 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5412 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5413 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5414 old_dentry->d_name.name,
5415 old_dentry->d_name.len);
5416 } else {
5417 btrfs_inc_nlink(old_dentry->d_inode);
5418 ret = btrfs_unlink_inode(trans, root, old_dir,
5419 old_dentry->d_inode,
5420 old_dentry->d_name.name,
5421 old_dentry->d_name.len);
5422 }
5423 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005424
5425 if (new_inode) {
5426 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005427 if (unlikely(new_inode->i_ino ==
5428 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5429 root_objectid = BTRFS_I(new_inode)->location.objectid;
5430 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5431 root_objectid,
5432 new_dentry->d_name.name,
5433 new_dentry->d_name.len);
5434 BUG_ON(new_inode->i_nlink == 0);
5435 } else {
5436 ret = btrfs_unlink_inode(trans, dest, new_dir,
5437 new_dentry->d_inode,
5438 new_dentry->d_name.name,
5439 new_dentry->d_name.len);
5440 }
5441 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005442 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04005443 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005444 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005445 }
Chris Mason39279cc2007-06-12 06:35:45 -04005446 }
Josef Bacikaec74772008-07-24 12:12:38 -04005447
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005448 ret = btrfs_add_link(trans, new_dir, old_inode,
5449 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04005450 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005451 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005452
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005453 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5454 btrfs_log_new_name(trans, old_inode, old_dir,
5455 new_dentry->d_parent);
5456 btrfs_end_log_trans(root);
5457 }
Chris Mason39279cc2007-06-12 06:35:45 -04005458out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04005459 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005460
Yan, Zheng76dda932009-09-21 16:00:26 -04005461 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5462 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005463
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005464 btrfs_unreserve_metadata_space(root, 11);
Chris Mason39279cc2007-06-12 06:35:45 -04005465 return ret;
5466}
5467
Chris Masond352ac62008-09-29 15:18:18 -04005468/*
5469 * some fairly slow code that needs optimization. This walks the list
5470 * of all the inodes with pending delalloc and forces them to disk.
5471 */
Chris Masonea8c2812008-08-04 23:17:27 -04005472int btrfs_start_delalloc_inodes(struct btrfs_root *root)
5473{
5474 struct list_head *head = &root->fs_info->delalloc_inodes;
5475 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005476 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04005477
Yan Zhengc146afa2008-11-12 14:34:12 -05005478 if (root->fs_info->sb->s_flags & MS_RDONLY)
5479 return -EROFS;
5480
Chris Mason75eff682008-12-15 15:54:40 -05005481 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05005482 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04005483 binode = list_entry(head->next, struct btrfs_inode,
5484 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005485 inode = igrab(&binode->vfs_inode);
5486 if (!inode)
5487 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05005488 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005489 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005490 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005491 iput(inode);
5492 }
5493 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05005494 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04005495 }
Chris Mason75eff682008-12-15 15:54:40 -05005496 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04005497
5498 /* the filemap_flush will queue IO into the worker threads, but
5499 * we have to make sure the IO is actually started and that
5500 * ordered extents get created before we return
5501 */
5502 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05005503 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05005504 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005505 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05005506 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5507 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04005508 }
5509 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04005510 return 0;
5511}
5512
Chris Mason39279cc2007-06-12 06:35:45 -04005513static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5514 const char *symname)
5515{
5516 struct btrfs_trans_handle *trans;
5517 struct btrfs_root *root = BTRFS_I(dir)->root;
5518 struct btrfs_path *path;
5519 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05005520 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005521 int err;
5522 int drop_inode = 0;
5523 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005524 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04005525 int name_len;
5526 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04005527 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005528 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04005529 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05005530 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005531
5532 name_len = strlen(symname) + 1;
5533 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5534 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05005535
Josef Bacik9ed74f22009-09-11 16:12:44 -04005536 /*
5537 * 2 items for inode item and ref
5538 * 2 items for dir items
5539 * 1 item for xattr if selinux is on
5540 */
5541 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05005542 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04005543 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05005544
Chris Mason39279cc2007-06-12 06:35:45 -04005545 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005546 if (!trans)
5547 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04005548 btrfs_set_trans_block_group(trans, dir);
5549
5550 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5551 if (err) {
5552 err = -ENOSPC;
5553 goto out_unlock;
5554 }
5555
Josef Bacikaec74772008-07-24 12:12:38 -04005556 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05005557 dentry->d_name.len,
5558 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04005559 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5560 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005561 err = PTR_ERR(inode);
5562 if (IS_ERR(inode))
5563 goto out_unlock;
5564
Jim Owens0279b4c2009-02-04 09:29:13 -05005565 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04005566 if (err) {
5567 drop_inode = 1;
5568 goto out_unlock;
5569 }
5570
Chris Mason39279cc2007-06-12 06:35:45 -04005571 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005572 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005573 if (err)
5574 drop_inode = 1;
5575 else {
5576 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005577 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04005578 inode->i_fop = &btrfs_file_operations;
5579 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05005580 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04005581 }
Chris Mason39279cc2007-06-12 06:35:45 -04005582 btrfs_update_inode_block_group(trans, inode);
5583 btrfs_update_inode_block_group(trans, dir);
5584 if (drop_inode)
5585 goto out_unlock;
5586
5587 path = btrfs_alloc_path();
5588 BUG_ON(!path);
5589 key.objectid = inode->i_ino;
5590 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005591 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5592 datasize = btrfs_file_extent_calc_inline_size(name_len);
5593 err = btrfs_insert_empty_item(trans, root, path, &key,
5594 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005595 if (err) {
5596 drop_inode = 1;
5597 goto out_unlock;
5598 }
Chris Mason5f39d392007-10-15 16:14:19 -04005599 leaf = path->nodes[0];
5600 ei = btrfs_item_ptr(leaf, path->slots[0],
5601 struct btrfs_file_extent_item);
5602 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5603 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04005604 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04005605 btrfs_set_file_extent_encryption(leaf, ei, 0);
5606 btrfs_set_file_extent_compression(leaf, ei, 0);
5607 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5608 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5609
Chris Mason39279cc2007-06-12 06:35:45 -04005610 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04005611 write_extent_buffer(leaf, symname, ptr, name_len);
5612 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04005613 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04005614
Chris Mason39279cc2007-06-12 06:35:45 -04005615 inode->i_op = &btrfs_symlink_inode_operations;
5616 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04005617 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04005618 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04005619 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005620 err = btrfs_update_inode(trans, root, inode);
5621 if (err)
5622 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005623
5624out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005625 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04005626 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005627out_fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005628 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04005629 if (drop_inode) {
5630 inode_dec_link_count(inode);
5631 iput(inode);
5632 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005633 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005634 return err;
5635}
Chris Mason16432982008-04-10 10:23:21 -04005636
Chris Mason546888d2009-04-21 11:53:38 -04005637static int prealloc_file_range(struct btrfs_trans_handle *trans,
5638 struct inode *inode, u64 start, u64 end,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00005639 u64 alloc_hint, int mode)
Yan Zhengd899e052008-10-30 14:25:28 -04005640{
Yan Zhengd899e052008-10-30 14:25:28 -04005641 struct btrfs_root *root = BTRFS_I(inode)->root;
5642 struct btrfs_key ins;
5643 u64 alloc_size;
5644 u64 cur_offset = start;
5645 u64 num_bytes = end - start;
5646 int ret = 0;
5647
Yan Zhengd899e052008-10-30 14:25:28 -04005648 while (num_bytes > 0) {
5649 alloc_size = min(num_bytes, root->fs_info->max_extent);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005650
5651 ret = btrfs_reserve_metadata_space(root, 1);
5652 if (ret)
5653 goto out;
5654
Yan Zhengd899e052008-10-30 14:25:28 -04005655 ret = btrfs_reserve_extent(trans, root, alloc_size,
5656 root->sectorsize, 0, alloc_hint,
5657 (u64)-1, &ins, 1);
5658 if (ret) {
5659 WARN_ON(1);
5660 goto out;
5661 }
5662 ret = insert_reserved_file_extent(trans, inode,
5663 cur_offset, ins.objectid,
5664 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00005665 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04005666 BTRFS_FILE_EXTENT_PREALLOC);
5667 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04005668 btrfs_drop_extent_cache(inode, cur_offset,
5669 cur_offset + ins.offset -1, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005670 num_bytes -= ins.offset;
5671 cur_offset += ins.offset;
5672 alloc_hint = ins.objectid + ins.offset;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005673 btrfs_unreserve_metadata_space(root, 1);
Yan Zhengd899e052008-10-30 14:25:28 -04005674 }
5675out:
5676 if (cur_offset > start) {
5677 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005678 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04005679 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
5680 cur_offset > i_size_read(inode))
5681 btrfs_i_size_write(inode, cur_offset);
5682 ret = btrfs_update_inode(trans, root, inode);
5683 BUG_ON(ret);
5684 }
5685
Yan Zhengd899e052008-10-30 14:25:28 -04005686 return ret;
5687}
5688
5689static long btrfs_fallocate(struct inode *inode, int mode,
5690 loff_t offset, loff_t len)
5691{
5692 u64 cur_offset;
5693 u64 last_byte;
5694 u64 alloc_start;
5695 u64 alloc_end;
5696 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04005697 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04005698 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5699 struct extent_map *em;
Chris Mason546888d2009-04-21 11:53:38 -04005700 struct btrfs_trans_handle *trans;
Josef Bacika970b0a2009-06-27 21:07:34 -04005701 struct btrfs_root *root;
Yan Zhengd899e052008-10-30 14:25:28 -04005702 int ret;
5703
5704 alloc_start = offset & ~mask;
5705 alloc_end = (offset + len + mask) & ~mask;
5706
Chris Mason546888d2009-04-21 11:53:38 -04005707 /*
5708 * wait for ordered IO before we have any locks. We'll loop again
5709 * below with the locks held.
5710 */
5711 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5712
Yan Zhengd899e052008-10-30 14:25:28 -04005713 mutex_lock(&inode->i_mutex);
5714 if (alloc_start > inode->i_size) {
5715 ret = btrfs_cont_expand(inode, alloc_start);
5716 if (ret)
5717 goto out;
5718 }
5719
Josef Bacika970b0a2009-06-27 21:07:34 -04005720 root = BTRFS_I(inode)->root;
5721
5722 ret = btrfs_check_data_free_space(root, inode,
5723 alloc_end - alloc_start);
5724 if (ret)
5725 goto out;
5726
Chris Masone980b502009-04-24 14:39:24 -04005727 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04005728 while (1) {
5729 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04005730
5731 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
5732 if (!trans) {
5733 ret = -EIO;
Josef Bacika970b0a2009-06-27 21:07:34 -04005734 goto out_free;
Chris Mason546888d2009-04-21 11:53:38 -04005735 }
5736
5737 /* the extent lock is ordered inside the running
5738 * transaction
5739 */
Chris Masone980b502009-04-24 14:39:24 -04005740 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5741 GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04005742 ordered = btrfs_lookup_first_ordered_extent(inode,
5743 alloc_end - 1);
5744 if (ordered &&
5745 ordered->file_offset + ordered->len > alloc_start &&
5746 ordered->file_offset < alloc_end) {
5747 btrfs_put_ordered_extent(ordered);
5748 unlock_extent(&BTRFS_I(inode)->io_tree,
Chris Masone980b502009-04-24 14:39:24 -04005749 alloc_start, locked_end, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005750 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
5751
5752 /*
5753 * we can't wait on the range with the transaction
5754 * running or with the extent lock held
5755 */
Yan Zhengd899e052008-10-30 14:25:28 -04005756 btrfs_wait_ordered_range(inode, alloc_start,
5757 alloc_end - alloc_start);
5758 } else {
5759 if (ordered)
5760 btrfs_put_ordered_extent(ordered);
5761 break;
5762 }
5763 }
5764
5765 cur_offset = alloc_start;
5766 while (1) {
5767 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5768 alloc_end - cur_offset, 0);
5769 BUG_ON(IS_ERR(em) || !em);
5770 last_byte = min(extent_map_end(em), alloc_end);
5771 last_byte = (last_byte + mask) & ~mask;
5772 if (em->block_start == EXTENT_MAP_HOLE) {
Chris Mason546888d2009-04-21 11:53:38 -04005773 ret = prealloc_file_range(trans, inode, cur_offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00005774 last_byte, alloc_hint, mode);
Yan Zhengd899e052008-10-30 14:25:28 -04005775 if (ret < 0) {
5776 free_extent_map(em);
5777 break;
5778 }
5779 }
5780 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5781 alloc_hint = em->block_start;
5782 free_extent_map(em);
5783
5784 cur_offset = last_byte;
5785 if (cur_offset >= alloc_end) {
5786 ret = 0;
5787 break;
5788 }
5789 }
Chris Masone980b502009-04-24 14:39:24 -04005790 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04005791 GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005792
5793 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
Josef Bacika970b0a2009-06-27 21:07:34 -04005794out_free:
5795 btrfs_free_reserved_data_space(root, inode, alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04005796out:
5797 mutex_unlock(&inode->i_mutex);
5798 return ret;
5799}
5800
Chris Masone6dcd2d2008-07-17 12:53:50 -04005801static int btrfs_set_page_dirty(struct page *page)
5802{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005803 return __set_page_dirty_nobuffers(page);
5804}
5805
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005806static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05005807{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005808 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05005809 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04005810 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05005811}
Chris Mason39279cc2007-06-12 06:35:45 -04005812
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005813static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05005814 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005815 .lookup = btrfs_lookup,
5816 .create = btrfs_create,
5817 .unlink = btrfs_unlink,
5818 .link = btrfs_link,
5819 .mkdir = btrfs_mkdir,
5820 .rmdir = btrfs_rmdir,
5821 .rename = btrfs_rename,
5822 .symlink = btrfs_symlink,
5823 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005824 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005825 .setxattr = btrfs_setxattr,
5826 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005827 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005828 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005829 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005830};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005831static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005832 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05005833 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005834};
Yan, Zheng76dda932009-09-21 16:00:26 -04005835
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005836static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005837 .llseek = generic_file_llseek,
5838 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01005839 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005840 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005841#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005842 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005843#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04005844 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04005845 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04005846};
5847
Chris Masond1310b22008-01-24 16:13:08 -05005848static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04005849 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05005850 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04005851 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005852 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005853 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04005854 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04005855 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05005856 .set_bit_hook = btrfs_set_bit_hook,
5857 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005858 .merge_extent_hook = btrfs_merge_extent_hook,
5859 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005860};
5861
Chris Mason35054392009-01-21 13:11:13 -05005862/*
5863 * btrfs doesn't support the bmap operation because swapfiles
5864 * use bmap to make a mapping of extents in the file. They assume
5865 * these extents won't change over the life of the file and they
5866 * use the bmap result to do IO directly to the drive.
5867 *
5868 * the btrfs bmap call would return logical addresses that aren't
5869 * suitable for IO and they also will change frequently as COW
5870 * operations happen. So, swapfile + btrfs == corruption.
5871 *
5872 * For now we're avoiding this by dropping bmap.
5873 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07005874static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04005875 .readpage = btrfs_readpage,
5876 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04005877 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05005878 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005879 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04005880 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005881 .invalidatepage = btrfs_invalidatepage,
5882 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005883 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02005884 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04005885};
5886
Alexey Dobriyan7f094102009-09-21 17:01:10 -07005887static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04005888 .readpage = btrfs_readpage,
5889 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04005890 .invalidatepage = btrfs_invalidatepage,
5891 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04005892};
5893
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005894static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005895 .truncate = btrfs_truncate,
5896 .getattr = btrfs_getattr,
5897 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005898 .setxattr = btrfs_setxattr,
5899 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005900 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005901 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005902 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04005903 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005904 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04005905};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005906static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04005907 .getattr = btrfs_getattr,
5908 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05005909 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005910 .setxattr = btrfs_setxattr,
5911 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04005912 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005913 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005914};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005915static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005916 .readlink = generic_readlink,
5917 .follow_link = page_follow_link_light,
5918 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05005919 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05005920 .setxattr = btrfs_setxattr,
5921 .getxattr = btrfs_getxattr,
5922 .listxattr = btrfs_listxattr,
5923 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005924};
Yan, Zheng76dda932009-09-21 16:00:26 -04005925
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04005926const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04005927 .d_delete = btrfs_dentry_delete,
5928};