blob: 8d8baaa61504bdc745e1a39986fca2ba32fad769 [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
Yan, Zhengc2167752009-11-12 09:34:21 +0000191 /*
192 * we're an inline extent, so nobody can
193 * extend the file past i_size without locking
194 * a page we already have locked.
195 *
196 * We must do any isize and inode updates
197 * before we unlock the pages. Otherwise we
198 * could end up racing with unlink.
199 */
Chris Masonc8b97812008-10-29 14:49:59 -0400200 BTRFS_I(inode)->disk_i_size = inode->i_size;
201 btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000202
Chris Masonc8b97812008-10-29 14:49:59 -0400203 return 0;
204fail:
205 btrfs_free_path(path);
206 return err;
207}
208
209
210/*
211 * conditionally insert an inline extent into the file. This
212 * does the checks required to make sure the data is small enough
213 * to fit as an inline extent.
214 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400215static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400216 struct btrfs_root *root,
217 struct inode *inode, u64 start, u64 end,
218 size_t compressed_size,
219 struct page **compressed_pages)
220{
221 u64 isize = i_size_read(inode);
222 u64 actual_end = min(end + 1, isize);
223 u64 inline_len = actual_end - start;
224 u64 aligned_end = (end + root->sectorsize - 1) &
225 ~((u64)root->sectorsize - 1);
226 u64 hint_byte;
227 u64 data_len = inline_len;
228 int ret;
229
230 if (compressed_size)
231 data_len = compressed_size;
232
233 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400234 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400235 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
236 (!compressed_size &&
237 (actual_end & (root->sectorsize - 1)) == 0) ||
238 end + 1 < isize ||
239 data_len > root->fs_info->max_inline) {
240 return 1;
241 }
242
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000243 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400244 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400245 BUG_ON(ret);
246
247 if (isize > actual_end)
248 inline_len = min_t(u64, isize, actual_end);
249 ret = insert_inline_extent(trans, root, inode, start,
250 inline_len, compressed_size,
251 compressed_pages);
252 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -0400253 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400254 return 0;
255}
256
Chris Mason771ed682008-11-06 22:02:51 -0500257struct async_extent {
258 u64 start;
259 u64 ram_size;
260 u64 compressed_size;
261 struct page **pages;
262 unsigned long nr_pages;
263 struct list_head list;
264};
265
266struct async_cow {
267 struct inode *inode;
268 struct btrfs_root *root;
269 struct page *locked_page;
270 u64 start;
271 u64 end;
272 struct list_head extents;
273 struct btrfs_work work;
274};
275
276static noinline int add_async_extent(struct async_cow *cow,
277 u64 start, u64 ram_size,
278 u64 compressed_size,
279 struct page **pages,
280 unsigned long nr_pages)
281{
282 struct async_extent *async_extent;
283
284 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
285 async_extent->start = start;
286 async_extent->ram_size = ram_size;
287 async_extent->compressed_size = compressed_size;
288 async_extent->pages = pages;
289 async_extent->nr_pages = nr_pages;
290 list_add_tail(&async_extent->list, &cow->extents);
291 return 0;
292}
293
Chris Masonc8b97812008-10-29 14:49:59 -0400294/*
Chris Mason771ed682008-11-06 22:02:51 -0500295 * we create compressed extents in two phases. The first
296 * phase compresses a range of pages that have already been
297 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400298 *
Chris Mason771ed682008-11-06 22:02:51 -0500299 * This is done inside an ordered work queue, and the compression
300 * is spread across many cpus. The actual IO submission is step
301 * two, and the ordered work queue takes care of making sure that
302 * happens in the same order things were put onto the queue by
303 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400304 *
Chris Mason771ed682008-11-06 22:02:51 -0500305 * If this code finds it can't get good compression, it puts an
306 * entry onto the work queue to write the uncompressed bytes. This
307 * makes sure that both compressed inodes and uncompressed inodes
308 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400309 */
Chris Mason771ed682008-11-06 22:02:51 -0500310static noinline int compress_file_range(struct inode *inode,
311 struct page *locked_page,
312 u64 start, u64 end,
313 struct async_cow *async_cow,
314 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400315{
316 struct btrfs_root *root = BTRFS_I(inode)->root;
317 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400318 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400319 u64 orig_start;
320 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400321 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400322 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500323 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400324 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400325 struct page **pages = NULL;
326 unsigned long nr_pages;
327 unsigned long nr_pages_ret = 0;
328 unsigned long total_compressed = 0;
329 unsigned long total_in = 0;
330 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500331 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400332 int i;
333 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400334
Chris Masonc8b97812008-10-29 14:49:59 -0400335 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500336
Chris Mason42dc7ba2008-12-15 11:44:56 -0500337 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400338again:
339 will_compress = 0;
340 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
341 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
342
Chris Masonf03d9302009-02-04 09:31:06 -0500343 /*
344 * we don't want to send crud past the end of i_size through
345 * compression, that's just a waste of CPU time. So, if the
346 * end of the file is before the start of our current
347 * requested range of bytes, we bail out to the uncompressed
348 * cleanup code that can deal with all of this.
349 *
350 * It isn't really the fastest way to fix things, but this is a
351 * very uncommon corner.
352 */
353 if (actual_end <= start)
354 goto cleanup_and_bail_uncompressed;
355
Chris Masonc8b97812008-10-29 14:49:59 -0400356 total_compressed = actual_end - start;
357
358 /* we want to make sure that amount of ram required to uncompress
359 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500360 * of a compressed extent to 128k. This is a crucial number
361 * because it also controls how easily we can spread reads across
362 * cpus for decompression.
363 *
364 * We also want to make sure the amount of IO required to do
365 * a random read is reasonably small, so we limit the size of
366 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400367 */
368 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400369 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500370 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400371 disk_num_bytes = num_bytes;
372 total_in = 0;
373 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400374
Chris Mason771ed682008-11-06 22:02:51 -0500375 /*
376 * we do compression for mount -o compress and when the
377 * inode has not been flagged as nocompress. This flag can
378 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400379 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200380 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Masonc8b97812008-10-29 14:49:59 -0400381 btrfs_test_opt(root, COMPRESS)) {
382 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400383 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400384
Chris Masonc8b97812008-10-29 14:49:59 -0400385 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
386 total_compressed, pages,
387 nr_pages, &nr_pages_ret,
388 &total_in,
389 &total_compressed,
390 max_compressed);
391
392 if (!ret) {
393 unsigned long offset = total_compressed &
394 (PAGE_CACHE_SIZE - 1);
395 struct page *page = pages[nr_pages_ret - 1];
396 char *kaddr;
397
398 /* zero the tail end of the last page, we might be
399 * sending it down to disk
400 */
401 if (offset) {
402 kaddr = kmap_atomic(page, KM_USER0);
403 memset(kaddr + offset, 0,
404 PAGE_CACHE_SIZE - offset);
405 kunmap_atomic(kaddr, KM_USER0);
406 }
407 will_compress = 1;
408 }
409 }
410 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500411 trans = btrfs_join_transaction(root, 1);
412 BUG_ON(!trans);
413 btrfs_set_trans_block_group(trans, inode);
414
Chris Masonc8b97812008-10-29 14:49:59 -0400415 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500416 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400417 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500418 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400419 */
420 ret = cow_file_range_inline(trans, root, inode,
421 start, end, 0, NULL);
422 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500423 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400424 ret = cow_file_range_inline(trans, root, inode,
425 start, end,
426 total_compressed, pages);
427 }
428 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500429 /*
430 * inline extent creation worked, we don't need
431 * to create any more async work items. Unlock
432 * and free up our temp pages.
433 */
Chris Masonc8b97812008-10-29 14:49:59 -0400434 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400435 &BTRFS_I(inode)->io_tree,
436 start, end, NULL,
437 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400438 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400439 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400440 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000441
442 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400443 goto free_pages_out;
444 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000445 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400446 }
447
448 if (will_compress) {
449 /*
450 * we aren't doing an inline extent round the compressed size
451 * up to a block size boundary so the allocator does sane
452 * things
453 */
454 total_compressed = (total_compressed + blocksize - 1) &
455 ~(blocksize - 1);
456
457 /*
458 * one last check to make sure the compression is really a
459 * win, compare the page count read with the blocks on disk
460 */
461 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
462 ~(PAGE_CACHE_SIZE - 1);
463 if (total_compressed >= total_in) {
464 will_compress = 0;
465 } else {
466 disk_num_bytes = total_compressed;
467 num_bytes = total_in;
468 }
469 }
470 if (!will_compress && pages) {
471 /*
472 * the compression code ran but failed to make things smaller,
473 * free any pages it allocated and our page pointer array
474 */
475 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400476 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400477 page_cache_release(pages[i]);
478 }
479 kfree(pages);
480 pages = NULL;
481 total_compressed = 0;
482 nr_pages_ret = 0;
483
484 /* flag the file so we don't compress in the future */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200485 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Masonc8b97812008-10-29 14:49:59 -0400486 }
Chris Mason771ed682008-11-06 22:02:51 -0500487 if (will_compress) {
488 *num_added += 1;
489
490 /* the async work queues will take care of doing actual
491 * allocation on disk for these compressed pages,
492 * and will submit them to the elevator.
493 */
494 add_async_extent(async_cow, start, num_bytes,
495 total_compressed, pages, nr_pages_ret);
496
Chris Mason42dc7ba2008-12-15 11:44:56 -0500497 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500498 start += num_bytes;
499 pages = NULL;
500 cond_resched();
501 goto again;
502 }
503 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500504cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500505 /*
506 * No compression, but we still need to write the pages in
507 * the file we've been given so far. redirty the locked
508 * page if it corresponds to our extent and set things up
509 * for the async work queue to run cow_file_range to do
510 * the normal delalloc dance
511 */
512 if (page_offset(locked_page) >= start &&
513 page_offset(locked_page) <= end) {
514 __set_page_dirty_nobuffers(locked_page);
515 /* unlocked later on in the async handlers */
516 }
517 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
518 *num_added += 1;
519 }
520
521out:
522 return 0;
523
524free_pages_out:
525 for (i = 0; i < nr_pages_ret; i++) {
526 WARN_ON(pages[i]->mapping);
527 page_cache_release(pages[i]);
528 }
Chris Masond3977122009-01-05 21:25:51 -0500529 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500530
531 goto out;
532}
533
534/*
535 * phase two of compressed writeback. This is the ordered portion
536 * of the code, which only gets called in the order the work was
537 * queued. We walk all the async extents created by compress_file_range
538 * and send them down to the disk.
539 */
540static noinline int submit_compressed_extents(struct inode *inode,
541 struct async_cow *async_cow)
542{
543 struct async_extent *async_extent;
544 u64 alloc_hint = 0;
545 struct btrfs_trans_handle *trans;
546 struct btrfs_key ins;
547 struct extent_map *em;
548 struct btrfs_root *root = BTRFS_I(inode)->root;
549 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
550 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500551 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500552
553 if (list_empty(&async_cow->extents))
554 return 0;
555
Chris Mason771ed682008-11-06 22:02:51 -0500556
Chris Masond3977122009-01-05 21:25:51 -0500557 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500558 async_extent = list_entry(async_cow->extents.next,
559 struct async_extent, list);
560 list_del(&async_extent->list);
561
562 io_tree = &BTRFS_I(inode)->io_tree;
563
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500564retry:
Chris Mason771ed682008-11-06 22:02:51 -0500565 /* did the compression code fall back to uncompressed IO? */
566 if (!async_extent->pages) {
567 int page_started = 0;
568 unsigned long nr_written = 0;
569
570 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500571 async_extent->start +
572 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500573
574 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500575 ret = cow_file_range(inode, async_cow->locked_page,
576 async_extent->start,
577 async_extent->start +
578 async_extent->ram_size - 1,
579 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500580
581 /*
582 * if page_started, cow_file_range inserted an
583 * inline extent and took care of all the unlocking
584 * and IO for us. Otherwise, we need to submit
585 * all those pages down to the drive.
586 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500587 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500588 extent_write_locked_range(io_tree,
589 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500590 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500591 async_extent->ram_size - 1,
592 btrfs_get_extent,
593 WB_SYNC_ALL);
594 kfree(async_extent);
595 cond_resched();
596 continue;
597 }
598
599 lock_extent(io_tree, async_extent->start,
600 async_extent->start + async_extent->ram_size - 1,
601 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500602
Yan, Zhengc2167752009-11-12 09:34:21 +0000603 trans = btrfs_join_transaction(root, 1);
Chris Mason771ed682008-11-06 22:02:51 -0500604 ret = btrfs_reserve_extent(trans, root,
605 async_extent->compressed_size,
606 async_extent->compressed_size,
607 0, alloc_hint,
608 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000609 btrfs_end_transaction(trans, root);
610
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500611 if (ret) {
612 int i;
613 for (i = 0; i < async_extent->nr_pages; i++) {
614 WARN_ON(async_extent->pages[i]->mapping);
615 page_cache_release(async_extent->pages[i]);
616 }
617 kfree(async_extent->pages);
618 async_extent->nr_pages = 0;
619 async_extent->pages = NULL;
620 unlock_extent(io_tree, async_extent->start,
621 async_extent->start +
622 async_extent->ram_size - 1, GFP_NOFS);
623 goto retry;
624 }
625
Yan, Zhengc2167752009-11-12 09:34:21 +0000626 /*
627 * here we're doing allocation and writeback of the
628 * compressed pages
629 */
630 btrfs_drop_extent_cache(inode, async_extent->start,
631 async_extent->start +
632 async_extent->ram_size - 1, 0);
633
Chris Mason771ed682008-11-06 22:02:51 -0500634 em = alloc_extent_map(GFP_NOFS);
635 em->start = async_extent->start;
636 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500637 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500638
639 em->block_start = ins.objectid;
640 em->block_len = ins.offset;
641 em->bdev = root->fs_info->fs_devices->latest_bdev;
642 set_bit(EXTENT_FLAG_PINNED, &em->flags);
643 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
644
Chris Masond3977122009-01-05 21:25:51 -0500645 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400646 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500647 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400648 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500649 if (ret != -EEXIST) {
650 free_extent_map(em);
651 break;
652 }
653 btrfs_drop_extent_cache(inode, async_extent->start,
654 async_extent->start +
655 async_extent->ram_size - 1, 0);
656 }
657
658 ret = btrfs_add_ordered_extent(inode, async_extent->start,
659 ins.objectid,
660 async_extent->ram_size,
661 ins.offset,
662 BTRFS_ORDERED_COMPRESSED);
663 BUG_ON(ret);
664
Chris Mason771ed682008-11-06 22:02:51 -0500665 /*
666 * clear dirty, set writeback and unlock the pages.
667 */
668 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400669 &BTRFS_I(inode)->io_tree,
670 async_extent->start,
671 async_extent->start +
672 async_extent->ram_size - 1,
673 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
674 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400675 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400676 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500677
678 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500679 async_extent->start,
680 async_extent->ram_size,
681 ins.objectid,
682 ins.offset, async_extent->pages,
683 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500684
685 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500686 alloc_hint = ins.objectid + ins.offset;
687 kfree(async_extent);
688 cond_resched();
689 }
690
Chris Mason771ed682008-11-06 22:02:51 -0500691 return 0;
692}
693
694/*
695 * when extent_io.c finds a delayed allocation range in the file,
696 * the call backs end up in this code. The basic idea is to
697 * allocate extents on disk for the range, and create ordered data structs
698 * in ram to track those extents.
699 *
700 * locked_page is the page that writepage had locked already. We use
701 * it to make sure we don't do extra locks or unlocks.
702 *
703 * *page_started is set to one if we unlock locked_page and do everything
704 * required to start IO on it. It may be clean and already done with
705 * IO when we return.
706 */
707static noinline int cow_file_range(struct inode *inode,
708 struct page *locked_page,
709 u64 start, u64 end, int *page_started,
710 unsigned long *nr_written,
711 int unlock)
712{
713 struct btrfs_root *root = BTRFS_I(inode)->root;
714 struct btrfs_trans_handle *trans;
715 u64 alloc_hint = 0;
716 u64 num_bytes;
717 unsigned long ram_size;
718 u64 disk_num_bytes;
719 u64 cur_alloc_size;
720 u64 blocksize = root->sectorsize;
721 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500722 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500723 struct btrfs_key ins;
724 struct extent_map *em;
725 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
726 int ret = 0;
727
728 trans = btrfs_join_transaction(root, 1);
729 BUG_ON(!trans);
730 btrfs_set_trans_block_group(trans, inode);
731
Chris Mason42dc7ba2008-12-15 11:44:56 -0500732 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500733
734 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
735 num_bytes = max(blocksize, num_bytes);
736 disk_num_bytes = num_bytes;
737 ret = 0;
738
739 if (start == 0) {
740 /* lets try to make an inline extent */
741 ret = cow_file_range_inline(trans, root, inode,
742 start, end, 0, NULL);
743 if (ret == 0) {
744 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400745 &BTRFS_I(inode)->io_tree,
746 start, end, NULL,
747 EXTENT_CLEAR_UNLOCK_PAGE |
748 EXTENT_CLEAR_UNLOCK |
749 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400750 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400751 EXTENT_CLEAR_DIRTY |
752 EXTENT_SET_WRITEBACK |
753 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000754
Chris Mason771ed682008-11-06 22:02:51 -0500755 *nr_written = *nr_written +
756 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
757 *page_started = 1;
758 ret = 0;
759 goto out;
760 }
761 }
Chris Masonc8b97812008-10-29 14:49:59 -0400762
763 BUG_ON(disk_num_bytes >
764 btrfs_super_total_bytes(&root->fs_info->super_copy));
765
Chris Masonb917b7c2009-09-18 16:07:03 -0400766
767 read_lock(&BTRFS_I(inode)->extent_tree.lock);
768 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
769 start, num_bytes);
770 if (em) {
Josef Bacik6346c932009-11-10 21:23:47 -0500771 /*
772 * if block start isn't an actual block number then find the
773 * first block in this inode and use that as a hint. If that
774 * block is also bogus then just don't worry about it.
775 */
776 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
777 free_extent_map(em);
778 em = search_extent_mapping(em_tree, 0, 0);
779 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
780 alloc_hint = em->block_start;
781 if (em)
782 free_extent_map(em);
783 } else {
784 alloc_hint = em->block_start;
785 free_extent_map(em);
786 }
Chris Masonb917b7c2009-09-18 16:07:03 -0400787 }
788 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400789 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400790
Chris Masond3977122009-01-05 21:25:51 -0500791 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400792 unsigned long op;
793
Chris Masonc8b97812008-10-29 14:49:59 -0400794 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400795 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500796 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400797 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500798 BUG_ON(ret);
799
Chris Masone6dcd2d2008-07-17 12:53:50 -0400800 em = alloc_extent_map(GFP_NOFS);
801 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500802 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500803 ram_size = ins.offset;
804 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400805
Chris Masone6dcd2d2008-07-17 12:53:50 -0400806 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400807 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400808 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400809 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400810
Chris Masond3977122009-01-05 21:25:51 -0500811 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400812 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400813 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400814 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400815 if (ret != -EEXIST) {
816 free_extent_map(em);
817 break;
818 }
819 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400820 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400821 }
822
Chris Mason98d20f62008-04-14 09:46:10 -0400823 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400824 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500825 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400826 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400827
Yan Zheng17d217f2008-12-12 10:03:38 -0500828 if (root->root_key.objectid ==
829 BTRFS_DATA_RELOC_TREE_OBJECTID) {
830 ret = btrfs_reloc_clone_csums(inode, start,
831 cur_alloc_size);
832 BUG_ON(ret);
833 }
834
Chris Masond3977122009-01-05 21:25:51 -0500835 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400836 break;
Chris Masond3977122009-01-05 21:25:51 -0500837
Chris Masonc8b97812008-10-29 14:49:59 -0400838 /* we're not doing compressed IO, don't unlock the first
839 * page (which the caller expects to stay locked), don't
840 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400841 *
842 * Do set the Private2 bit so we know this page was properly
843 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400844 */
Chris Masona791e352009-10-08 11:27:10 -0400845 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
846 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
847 EXTENT_SET_PRIVATE2;
848
Chris Masonc8b97812008-10-29 14:49:59 -0400849 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
850 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400851 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400852 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500853 num_bytes -= cur_alloc_size;
854 alloc_hint = ins.objectid + ins.offset;
855 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400856 }
Chris Masonb888db22007-08-27 16:49:44 -0400857out:
Chris Mason771ed682008-11-06 22:02:51 -0500858 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400859 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400860
Chris Masonbe20aa92007-12-17 20:14:01 -0500861 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500862}
Chris Masonc8b97812008-10-29 14:49:59 -0400863
Chris Mason771ed682008-11-06 22:02:51 -0500864/*
865 * work queue call back to started compression on a file and pages
866 */
867static noinline void async_cow_start(struct btrfs_work *work)
868{
869 struct async_cow *async_cow;
870 int num_added = 0;
871 async_cow = container_of(work, struct async_cow, work);
872
873 compress_file_range(async_cow->inode, async_cow->locked_page,
874 async_cow->start, async_cow->end, async_cow,
875 &num_added);
876 if (num_added == 0)
877 async_cow->inode = NULL;
878}
879
880/*
881 * work queue call back to submit previously compressed pages
882 */
883static noinline void async_cow_submit(struct btrfs_work *work)
884{
885 struct async_cow *async_cow;
886 struct btrfs_root *root;
887 unsigned long nr_pages;
888
889 async_cow = container_of(work, struct async_cow, work);
890
891 root = async_cow->root;
892 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
893 PAGE_CACHE_SHIFT;
894
895 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
896
897 if (atomic_read(&root->fs_info->async_delalloc_pages) <
898 5 * 1042 * 1024 &&
899 waitqueue_active(&root->fs_info->async_submit_wait))
900 wake_up(&root->fs_info->async_submit_wait);
901
Chris Masond3977122009-01-05 21:25:51 -0500902 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500903 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500904}
Chris Masonc8b97812008-10-29 14:49:59 -0400905
Chris Mason771ed682008-11-06 22:02:51 -0500906static noinline void async_cow_free(struct btrfs_work *work)
907{
908 struct async_cow *async_cow;
909 async_cow = container_of(work, struct async_cow, work);
910 kfree(async_cow);
911}
912
913static int cow_file_range_async(struct inode *inode, struct page *locked_page,
914 u64 start, u64 end, int *page_started,
915 unsigned long *nr_written)
916{
917 struct async_cow *async_cow;
918 struct btrfs_root *root = BTRFS_I(inode)->root;
919 unsigned long nr_pages;
920 u64 cur_end;
921 int limit = 10 * 1024 * 1042;
922
Chris Masona3429ab2009-10-08 12:30:20 -0400923 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
924 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500925 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500926 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
927 async_cow->inode = inode;
928 async_cow->root = root;
929 async_cow->locked_page = locked_page;
930 async_cow->start = start;
931
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200932 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500933 cur_end = end;
934 else
935 cur_end = min(end, start + 512 * 1024 - 1);
936
937 async_cow->end = cur_end;
938 INIT_LIST_HEAD(&async_cow->extents);
939
940 async_cow->work.func = async_cow_start;
941 async_cow->work.ordered_func = async_cow_submit;
942 async_cow->work.ordered_free = async_cow_free;
943 async_cow->work.flags = 0;
944
Chris Mason771ed682008-11-06 22:02:51 -0500945 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
946 PAGE_CACHE_SHIFT;
947 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
948
949 btrfs_queue_worker(&root->fs_info->delalloc_workers,
950 &async_cow->work);
951
952 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
953 wait_event(root->fs_info->async_submit_wait,
954 (atomic_read(&root->fs_info->async_delalloc_pages) <
955 limit));
956 }
957
Chris Masond3977122009-01-05 21:25:51 -0500958 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500959 atomic_read(&root->fs_info->async_delalloc_pages)) {
960 wait_event(root->fs_info->async_submit_wait,
961 (atomic_read(&root->fs_info->async_delalloc_pages) ==
962 0));
963 }
964
965 *nr_written += nr_pages;
966 start = cur_end + 1;
967 }
968 *page_started = 1;
969 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500970}
971
Chris Masond3977122009-01-05 21:25:51 -0500972static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500973 u64 bytenr, u64 num_bytes)
974{
975 int ret;
976 struct btrfs_ordered_sum *sums;
977 LIST_HEAD(list);
978
Yan Zheng07d400a2009-01-06 11:42:00 -0500979 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
980 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500981 if (ret == 0 && list_empty(&list))
982 return 0;
983
984 while (!list_empty(&list)) {
985 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
986 list_del(&sums->list);
987 kfree(sums);
988 }
989 return 1;
990}
991
Chris Masond352ac62008-09-29 15:18:18 -0400992/*
993 * when nowcow writeback call back. This checks for snapshots or COW copies
994 * of the extents that exist in the file, and COWs the file as required.
995 *
996 * If no cow copies or snapshots exist, we write directly to the existing
997 * blocks on disk
998 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400999static noinline int run_delalloc_nocow(struct inode *inode,
1000 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001001 u64 start, u64 end, int *page_started, int force,
1002 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001003{
Chris Masonbe20aa92007-12-17 20:14:01 -05001004 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001005 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001006 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001007 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001008 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001009 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001010 u64 cow_start;
1011 u64 cur_offset;
1012 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001013 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001014 u64 disk_bytenr;
1015 u64 num_bytes;
1016 int extent_type;
1017 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001018 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001019 int nocow;
1020 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -05001021
1022 path = btrfs_alloc_path();
1023 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001024 trans = btrfs_join_transaction(root, 1);
1025 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -05001026
Yan Zheng80ff3852008-10-30 14:20:02 -04001027 cow_start = (u64)-1;
1028 cur_offset = start;
1029 while (1) {
1030 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1031 cur_offset, 0);
1032 BUG_ON(ret < 0);
1033 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1034 leaf = path->nodes[0];
1035 btrfs_item_key_to_cpu(leaf, &found_key,
1036 path->slots[0] - 1);
1037 if (found_key.objectid == inode->i_ino &&
1038 found_key.type == BTRFS_EXTENT_DATA_KEY)
1039 path->slots[0]--;
1040 }
1041 check_prev = 0;
1042next_slot:
1043 leaf = path->nodes[0];
1044 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1045 ret = btrfs_next_leaf(root, path);
1046 if (ret < 0)
1047 BUG_ON(1);
1048 if (ret > 0)
1049 break;
1050 leaf = path->nodes[0];
1051 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001052
Yan Zheng80ff3852008-10-30 14:20:02 -04001053 nocow = 0;
1054 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001055 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001056 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001057
Yan Zheng80ff3852008-10-30 14:20:02 -04001058 if (found_key.objectid > inode->i_ino ||
1059 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1060 found_key.offset > end)
1061 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001062
Yan Zheng80ff3852008-10-30 14:20:02 -04001063 if (found_key.offset > cur_offset) {
1064 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001065 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001066 goto out_check;
1067 }
Chris Masonc31f8832008-01-08 15:46:31 -05001068
Yan Zheng80ff3852008-10-30 14:20:02 -04001069 fi = btrfs_item_ptr(leaf, path->slots[0],
1070 struct btrfs_file_extent_item);
1071 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001072
Yan Zhengd899e052008-10-30 14:25:28 -04001073 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1074 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001075 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001076 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001077 extent_end = found_key.offset +
1078 btrfs_file_extent_num_bytes(leaf, fi);
1079 if (extent_end <= start) {
1080 path->slots[0]++;
1081 goto next_slot;
1082 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001083 if (disk_bytenr == 0)
1084 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001085 if (btrfs_file_extent_compression(leaf, fi) ||
1086 btrfs_file_extent_encryption(leaf, fi) ||
1087 btrfs_file_extent_other_encoding(leaf, fi))
1088 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001089 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1090 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001091 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001092 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001093 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001094 found_key.offset -
1095 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001096 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001097 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001098 disk_bytenr += cur_offset - found_key.offset;
1099 num_bytes = min(end + 1, extent_end) - cur_offset;
1100 /*
1101 * force cow if csum exists in the range.
1102 * this ensure that csum for a given extent are
1103 * either valid or do not exist.
1104 */
1105 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1106 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001107 nocow = 1;
1108 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1109 extent_end = found_key.offset +
1110 btrfs_file_extent_inline_len(leaf, fi);
1111 extent_end = ALIGN(extent_end, root->sectorsize);
1112 } else {
1113 BUG_ON(1);
1114 }
1115out_check:
1116 if (extent_end <= start) {
1117 path->slots[0]++;
1118 goto next_slot;
1119 }
1120 if (!nocow) {
1121 if (cow_start == (u64)-1)
1122 cow_start = cur_offset;
1123 cur_offset = extent_end;
1124 if (cur_offset > end)
1125 break;
1126 path->slots[0]++;
1127 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001128 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001129
Yan Zheng7ea394f2008-08-05 13:05:02 -04001130 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001131 if (cow_start != (u64)-1) {
1132 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001133 found_key.offset - 1, page_started,
1134 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001135 BUG_ON(ret);
1136 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001137 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001138
Yan Zhengd899e052008-10-30 14:25:28 -04001139 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1140 struct extent_map *em;
1141 struct extent_map_tree *em_tree;
1142 em_tree = &BTRFS_I(inode)->extent_tree;
1143 em = alloc_extent_map(GFP_NOFS);
1144 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001145 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001146 em->len = num_bytes;
1147 em->block_len = num_bytes;
1148 em->block_start = disk_bytenr;
1149 em->bdev = root->fs_info->fs_devices->latest_bdev;
1150 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1151 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001152 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001153 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001154 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001155 if (ret != -EEXIST) {
1156 free_extent_map(em);
1157 break;
1158 }
1159 btrfs_drop_extent_cache(inode, em->start,
1160 em->start + em->len - 1, 0);
1161 }
1162 type = BTRFS_ORDERED_PREALLOC;
1163 } else {
1164 type = BTRFS_ORDERED_NOCOW;
1165 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001166
1167 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001168 num_bytes, num_bytes, type);
1169 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001170
Yan Zhengd899e052008-10-30 14:25:28 -04001171 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001172 cur_offset, cur_offset + num_bytes - 1,
1173 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1174 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1175 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001176 cur_offset = extent_end;
1177 if (cur_offset > end)
1178 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001179 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001180 btrfs_release_path(root, path);
1181
1182 if (cur_offset <= end && cow_start == (u64)-1)
1183 cow_start = cur_offset;
1184 if (cow_start != (u64)-1) {
1185 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001186 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001187 BUG_ON(ret);
1188 }
1189
1190 ret = btrfs_end_transaction(trans, root);
1191 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001192 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001193 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001194}
1195
Chris Masond352ac62008-09-29 15:18:18 -04001196/*
1197 * extent_io.c call back to do delayed allocation processing
1198 */
Chris Masonc8b97812008-10-29 14:49:59 -04001199static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001200 u64 start, u64 end, int *page_started,
1201 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001202{
Chris Masonbe20aa92007-12-17 20:14:01 -05001203 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001204 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001205
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001206 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001207 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001208 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001209 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001210 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001211 page_started, 0, nr_written);
Chris Mason7f366cf2009-03-12 20:12:45 -04001212 else if (!btrfs_test_opt(root, COMPRESS))
1213 ret = cow_file_range(inode, locked_page, start, end,
1214 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001215 else
Chris Mason771ed682008-11-06 22:02:51 -05001216 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001217 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001218 return ret;
1219}
1220
Josef Bacik9ed74f22009-09-11 16:12:44 -04001221static int btrfs_split_extent_hook(struct inode *inode,
1222 struct extent_state *orig, u64 split)
1223{
1224 struct btrfs_root *root = BTRFS_I(inode)->root;
1225 u64 size;
1226
1227 if (!(orig->state & EXTENT_DELALLOC))
1228 return 0;
1229
1230 size = orig->end - orig->start + 1;
1231 if (size > root->fs_info->max_extent) {
1232 u64 num_extents;
1233 u64 new_size;
1234
1235 new_size = orig->end - split + 1;
1236 num_extents = div64_u64(size + root->fs_info->max_extent - 1,
1237 root->fs_info->max_extent);
1238
1239 /*
Josef Bacik32c00af2009-10-08 13:34:05 -04001240 * if we break a large extent up then leave oustanding_extents
1241 * be, since we've already accounted for the large extent.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001242 */
1243 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1244 root->fs_info->max_extent) < num_extents)
1245 return 0;
1246 }
1247
Josef Bacik32c00af2009-10-08 13:34:05 -04001248 spin_lock(&BTRFS_I(inode)->accounting_lock);
1249 BTRFS_I(inode)->outstanding_extents++;
1250 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001251
1252 return 0;
1253}
1254
1255/*
1256 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1257 * extents so we can keep track of new extents that are just merged onto old
1258 * extents, such as when we are doing sequential writes, so we can properly
1259 * account for the metadata space we'll need.
1260 */
1261static int btrfs_merge_extent_hook(struct inode *inode,
1262 struct extent_state *new,
1263 struct extent_state *other)
1264{
1265 struct btrfs_root *root = BTRFS_I(inode)->root;
1266 u64 new_size, old_size;
1267 u64 num_extents;
1268
1269 /* not delalloc, ignore it */
1270 if (!(other->state & EXTENT_DELALLOC))
1271 return 0;
1272
1273 old_size = other->end - other->start + 1;
1274 if (new->start < other->start)
1275 new_size = other->end - new->start + 1;
1276 else
1277 new_size = new->end - other->start + 1;
1278
1279 /* we're not bigger than the max, unreserve the space and go */
1280 if (new_size <= root->fs_info->max_extent) {
Josef Bacik32c00af2009-10-08 13:34:05 -04001281 spin_lock(&BTRFS_I(inode)->accounting_lock);
1282 BTRFS_I(inode)->outstanding_extents--;
1283 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001284 return 0;
1285 }
1286
1287 /*
1288 * If we grew by another max_extent, just return, we want to keep that
1289 * reserved amount.
1290 */
1291 num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
1292 root->fs_info->max_extent);
1293 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1294 root->fs_info->max_extent) > num_extents)
1295 return 0;
1296
Josef Bacik32c00af2009-10-08 13:34:05 -04001297 spin_lock(&BTRFS_I(inode)->accounting_lock);
1298 BTRFS_I(inode)->outstanding_extents--;
1299 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001300
1301 return 0;
1302}
1303
Chris Masond352ac62008-09-29 15:18:18 -04001304/*
1305 * extent_io.c set_bit_hook, used to track delayed allocation
1306 * bytes in this file, and to maintain the list of inodes that
1307 * have pending delalloc work to be done.
1308 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001309static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001310 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001311{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001312
Chris Mason75eff682008-12-15 15:54:40 -05001313 /*
1314 * set_bit and clear bit hooks normally require _irqsave/restore
1315 * but in this case, we are only testeing for the DELALLOC
1316 * bit, which is only set or cleared with irqs on
1317 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001318 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001319 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001320
Josef Bacik32c00af2009-10-08 13:34:05 -04001321 spin_lock(&BTRFS_I(inode)->accounting_lock);
1322 BTRFS_I(inode)->outstanding_extents++;
1323 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik6a632092009-02-20 11:00:09 -05001324 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
Chris Mason75eff682008-12-15 15:54:40 -05001325 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001326 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001327 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001328 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1329 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1330 &root->fs_info->delalloc_inodes);
1331 }
Chris Mason75eff682008-12-15 15:54:40 -05001332 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001333 }
1334 return 0;
1335}
1336
Chris Masond352ac62008-09-29 15:18:18 -04001337/*
1338 * extent_io.c clear_bit_hook, see set_bit_hook for why
1339 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001340static int btrfs_clear_bit_hook(struct inode *inode,
1341 struct extent_state *state, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001342{
Chris Mason75eff682008-12-15 15:54:40 -05001343 /*
1344 * set_bit and clear bit hooks normally require _irqsave/restore
1345 * but in this case, we are only testeing for the DELALLOC
1346 * bit, which is only set or cleared with irqs on
1347 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001348 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001349 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001350
Josef Bacik32c00af2009-10-08 13:34:05 -04001351 if (bits & EXTENT_DO_ACCOUNTING) {
1352 spin_lock(&BTRFS_I(inode)->accounting_lock);
1353 BTRFS_I(inode)->outstanding_extents--;
1354 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1355 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1356 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04001357
Chris Mason75eff682008-12-15 15:54:40 -05001358 spin_lock(&root->fs_info->delalloc_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001359 if (state->end - state->start + 1 >
1360 root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001361 printk(KERN_INFO "btrfs warning: delalloc account "
1362 "%llu %llu\n",
Josef Bacik9ed74f22009-09-11 16:12:44 -04001363 (unsigned long long)
1364 state->end - state->start + 1,
Chris Masond3977122009-01-05 21:25:51 -05001365 (unsigned long long)
1366 root->fs_info->delalloc_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001367 btrfs_delalloc_free_space(root, inode, (u64)-1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001368 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001369 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001370 } else {
Josef Bacik6a632092009-02-20 11:00:09 -05001371 btrfs_delalloc_free_space(root, inode,
Josef Bacik9ed74f22009-09-11 16:12:44 -04001372 state->end -
1373 state->start + 1);
1374 root->fs_info->delalloc_bytes -= state->end -
1375 state->start + 1;
1376 BTRFS_I(inode)->delalloc_bytes -= state->end -
1377 state->start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001378 }
Chris Masonea8c2812008-08-04 23:17:27 -04001379 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1380 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1381 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1382 }
Chris Mason75eff682008-12-15 15:54:40 -05001383 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001384 }
1385 return 0;
1386}
1387
Chris Masond352ac62008-09-29 15:18:18 -04001388/*
1389 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1390 * we don't create bios that span stripes or chunks
1391 */
Chris Mason239b14b2008-03-24 15:02:07 -04001392int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001393 size_t size, struct bio *bio,
1394 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001395{
1396 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1397 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001398 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001399 u64 length = 0;
1400 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001401 int ret;
1402
Chris Mason771ed682008-11-06 22:02:51 -05001403 if (bio_flags & EXTENT_BIO_COMPRESSED)
1404 return 0;
1405
Chris Masonf2d8d742008-04-21 10:03:05 -04001406 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001407 map_tree = &root->fs_info->mapping_tree;
1408 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001409 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001410 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001411
Chris Masond3977122009-01-05 21:25:51 -05001412 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001413 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001414 return 0;
1415}
1416
Chris Masond352ac62008-09-29 15:18:18 -04001417/*
1418 * in order to insert checksums into the metadata in large chunks,
1419 * we wait until bio submission time. All the pages in the bio are
1420 * checksummed and sums are attached onto the ordered extent record.
1421 *
1422 * At IO completion time the cums attached on the ordered extent record
1423 * are inserted into the btree
1424 */
Chris Masond3977122009-01-05 21:25:51 -05001425static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1426 struct bio *bio, int mirror_num,
1427 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001428{
Chris Mason065631f2008-02-20 12:07:25 -05001429 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001430 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001431
Chris Masond20f7042008-12-08 16:58:54 -05001432 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001433 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001434 return 0;
1435}
Chris Masone0156402008-04-16 11:15:20 -04001436
Chris Mason4a69a412008-11-06 22:03:00 -05001437/*
1438 * in order to insert checksums into the metadata in large chunks,
1439 * we wait until bio submission time. All the pages in the bio are
1440 * checksummed and sums are attached onto the ordered extent record.
1441 *
1442 * At IO completion time the cums attached on the ordered extent record
1443 * are inserted into the btree
1444 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001445static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001446 int mirror_num, unsigned long bio_flags)
1447{
1448 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001449 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001450}
1451
Chris Masond352ac62008-09-29 15:18:18 -04001452/*
Chris Masoncad321a2008-12-17 14:51:42 -05001453 * extent_io.c submission hook. This does the right thing for csum calculation
1454 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001455 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001456static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001457 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001458{
1459 struct btrfs_root *root = BTRFS_I(inode)->root;
1460 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001461 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001462
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001463 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001464
Chris Masone6dcd2d2008-07-17 12:53:50 -04001465 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1466 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001467
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001468 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001469 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001470 return btrfs_submit_compressed_read(inode, bio,
1471 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001472 } else if (!skip_sum)
1473 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001474 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001475 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001476 /* csum items have already been cloned */
1477 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1478 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001479 /* we're doing a write, do the async checksumming */
1480 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001481 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001482 bio_flags, __btrfs_submit_bio_start,
1483 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001484 }
1485
Chris Mason0b86a832008-03-24 15:01:56 -04001486mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001487 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001488}
Chris Mason6885f302008-02-20 16:11:05 -05001489
Chris Masond352ac62008-09-29 15:18:18 -04001490/*
1491 * given a list of ordered sums record them in the inode. This happens
1492 * at IO completion time based on sums calculated at bio submission time.
1493 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001494static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001495 struct inode *inode, u64 file_offset,
1496 struct list_head *list)
1497{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001498 struct btrfs_ordered_sum *sum;
1499
1500 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001501
1502 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001503 btrfs_csum_file_blocks(trans,
1504 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001505 }
1506 return 0;
1507}
1508
Chris Masonea8c2812008-08-04 23:17:27 -04001509int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1510{
Chris Masond3977122009-01-05 21:25:51 -05001511 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001512 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001513 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1514 GFP_NOFS);
1515}
1516
Chris Masond352ac62008-09-29 15:18:18 -04001517/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001518struct btrfs_writepage_fixup {
1519 struct page *page;
1520 struct btrfs_work work;
1521};
1522
Christoph Hellwigb2950862008-12-02 09:54:17 -05001523static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001524{
1525 struct btrfs_writepage_fixup *fixup;
1526 struct btrfs_ordered_extent *ordered;
1527 struct page *page;
1528 struct inode *inode;
1529 u64 page_start;
1530 u64 page_end;
1531
1532 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1533 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001534again:
Chris Mason247e7432008-07-17 12:53:51 -04001535 lock_page(page);
1536 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1537 ClearPageChecked(page);
1538 goto out_page;
1539 }
1540
1541 inode = page->mapping->host;
1542 page_start = page_offset(page);
1543 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1544
1545 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001546
1547 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001548 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001549 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001550
1551 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1552 if (ordered) {
1553 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1554 page_end, GFP_NOFS);
1555 unlock_page(page);
1556 btrfs_start_ordered_extent(inode, ordered, 1);
1557 goto again;
1558 }
Chris Mason247e7432008-07-17 12:53:51 -04001559
Chris Masonea8c2812008-08-04 23:17:27 -04001560 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001561 ClearPageChecked(page);
1562out:
1563 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1564out_page:
1565 unlock_page(page);
1566 page_cache_release(page);
1567}
1568
1569/*
1570 * There are a few paths in the higher layers of the kernel that directly
1571 * set the page dirty bit without asking the filesystem if it is a
1572 * good idea. This causes problems because we want to make sure COW
1573 * properly happens and the data=ordered rules are followed.
1574 *
Chris Masonc8b97812008-10-29 14:49:59 -04001575 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001576 * hasn't been properly setup for IO. We kick off an async process
1577 * to fix it up. The async helper will wait for ordered extents, set
1578 * the delalloc bit and make it safe to write the page.
1579 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001580static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001581{
1582 struct inode *inode = page->mapping->host;
1583 struct btrfs_writepage_fixup *fixup;
1584 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001585
Chris Mason8b62b722009-09-02 16:53:46 -04001586 /* this page is properly in the ordered list */
1587 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001588 return 0;
1589
1590 if (PageChecked(page))
1591 return -EAGAIN;
1592
1593 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1594 if (!fixup)
1595 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001596
Chris Mason247e7432008-07-17 12:53:51 -04001597 SetPageChecked(page);
1598 page_cache_get(page);
1599 fixup->work.func = btrfs_writepage_fixup_worker;
1600 fixup->page = page;
1601 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1602 return -EAGAIN;
1603}
1604
Yan Zhengd899e052008-10-30 14:25:28 -04001605static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1606 struct inode *inode, u64 file_pos,
1607 u64 disk_bytenr, u64 disk_num_bytes,
1608 u64 num_bytes, u64 ram_bytes,
1609 u8 compression, u8 encryption,
1610 u16 other_encoding, int extent_type)
1611{
1612 struct btrfs_root *root = BTRFS_I(inode)->root;
1613 struct btrfs_file_extent_item *fi;
1614 struct btrfs_path *path;
1615 struct extent_buffer *leaf;
1616 struct btrfs_key ins;
1617 u64 hint;
1618 int ret;
1619
1620 path = btrfs_alloc_path();
1621 BUG_ON(!path);
1622
Chris Masonb9473432009-03-13 11:00:37 -04001623 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001624
1625 /*
1626 * we may be replacing one extent in the tree with another.
1627 * The new extent is pinned in the extent map, and we don't want
1628 * to drop it from the cache until it is completely in the btree.
1629 *
1630 * So, tell btrfs_drop_extents to leave this extent in the cache.
1631 * the caller is expected to unpin it and allow it to be merged
1632 * with the others.
1633 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001634 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1635 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001636 BUG_ON(ret);
1637
1638 ins.objectid = inode->i_ino;
1639 ins.offset = file_pos;
1640 ins.type = BTRFS_EXTENT_DATA_KEY;
1641 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1642 BUG_ON(ret);
1643 leaf = path->nodes[0];
1644 fi = btrfs_item_ptr(leaf, path->slots[0],
1645 struct btrfs_file_extent_item);
1646 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1647 btrfs_set_file_extent_type(leaf, fi, extent_type);
1648 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1649 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1650 btrfs_set_file_extent_offset(leaf, fi, 0);
1651 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1652 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1653 btrfs_set_file_extent_compression(leaf, fi, compression);
1654 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1655 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001656
1657 btrfs_unlock_up_safe(path, 1);
1658 btrfs_set_lock_blocking(leaf);
1659
Yan Zhengd899e052008-10-30 14:25:28 -04001660 btrfs_mark_buffer_dirty(leaf);
1661
1662 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001663
1664 ins.objectid = disk_bytenr;
1665 ins.offset = disk_num_bytes;
1666 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001667 ret = btrfs_alloc_reserved_file_extent(trans, root,
1668 root->root_key.objectid,
1669 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001670 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001671 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001672
Yan Zhengd899e052008-10-30 14:25:28 -04001673 return 0;
1674}
1675
Chris Mason5d13a982009-03-13 11:41:46 -04001676/*
1677 * helper function for btrfs_finish_ordered_io, this
1678 * just reads in some of the csum leaves to prime them into ram
1679 * before we start the transaction. It limits the amount of btree
1680 * reads required while inside the transaction.
1681 */
1682static noinline void reada_csum(struct btrfs_root *root,
1683 struct btrfs_path *path,
1684 struct btrfs_ordered_extent *ordered_extent)
1685{
1686 struct btrfs_ordered_sum *sum;
1687 u64 bytenr;
1688
1689 sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
1690 list);
1691 bytenr = sum->sums[0].bytenr;
1692
1693 /*
1694 * we don't care about the results, the point of this search is
1695 * just to get the btree leaves into ram
1696 */
1697 btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
1698}
1699
Chris Masond352ac62008-09-29 15:18:18 -04001700/* as ordered data IO finishes, this gets called so we can finish
1701 * an ordered extent if the range of bytes in the file it covers are
1702 * fully written.
1703 */
Chris Mason211f90e2008-07-18 11:56:15 -04001704static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001705{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001706 struct btrfs_root *root = BTRFS_I(inode)->root;
1707 struct btrfs_trans_handle *trans;
Chris Mason5d13a982009-03-13 11:41:46 -04001708 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001709 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonb7ec40d2009-03-12 20:12:45 -04001710 struct btrfs_path *path;
Yan Zhengd899e052008-10-30 14:25:28 -04001711 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001712 int ret;
1713
1714 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001715 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001716 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001717
Chris Masonb7ec40d2009-03-12 20:12:45 -04001718 /*
1719 * before we join the transaction, try to do some of our IO.
1720 * This will limit the amount of IO that we have to do with
1721 * the transaction running. We're unlikely to need to do any
1722 * IO if the file extents are new, the disk_i_size checks
1723 * covers the most common case.
1724 */
1725 if (start < BTRFS_I(inode)->disk_i_size) {
1726 path = btrfs_alloc_path();
1727 if (path) {
1728 ret = btrfs_lookup_file_extent(NULL, root, path,
1729 inode->i_ino,
1730 start, 0);
Chris Mason5d13a982009-03-13 11:41:46 -04001731 ordered_extent = btrfs_lookup_ordered_extent(inode,
1732 start);
1733 if (!list_empty(&ordered_extent->list)) {
1734 btrfs_release_path(root, path);
1735 reada_csum(root, path, ordered_extent);
1736 }
Chris Masonb7ec40d2009-03-12 20:12:45 -04001737 btrfs_free_path(path);
1738 }
1739 }
1740
Chris Mason5d13a982009-03-13 11:41:46 -04001741 if (!ordered_extent)
1742 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001743 BUG_ON(!ordered_extent);
Yan, Zhengc2167752009-11-12 09:34:21 +00001744 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1745 BUG_ON(!list_empty(&ordered_extent->list));
1746 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1747 if (!ret) {
1748 trans = btrfs_join_transaction(root, 1);
1749 ret = btrfs_update_inode(trans, root, inode);
1750 BUG_ON(ret);
1751 btrfs_end_transaction(trans, root);
1752 }
1753 goto out;
1754 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001755
1756 lock_extent(io_tree, ordered_extent->file_offset,
1757 ordered_extent->file_offset + ordered_extent->len - 1,
1758 GFP_NOFS);
1759
Yan, Zhengc2167752009-11-12 09:34:21 +00001760 trans = btrfs_join_transaction(root, 1);
1761
Chris Masonc8b97812008-10-29 14:49:59 -04001762 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001763 compressed = 1;
1764 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1765 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001766 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001767 ordered_extent->file_offset,
1768 ordered_extent->file_offset +
1769 ordered_extent->len);
1770 BUG_ON(ret);
1771 } else {
1772 ret = insert_reserved_file_extent(trans, inode,
1773 ordered_extent->file_offset,
1774 ordered_extent->start,
1775 ordered_extent->disk_len,
1776 ordered_extent->len,
1777 ordered_extent->len,
1778 compressed, 0, 0,
1779 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001780 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1781 ordered_extent->file_offset,
1782 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001783 BUG_ON(ret);
1784 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001785 unlock_extent(io_tree, ordered_extent->file_offset,
1786 ordered_extent->file_offset + ordered_extent->len - 1,
1787 GFP_NOFS);
1788 add_pending_csums(trans, inode, ordered_extent->file_offset,
1789 &ordered_extent->list);
1790
Yan, Zhengc2167752009-11-12 09:34:21 +00001791 /* this also removes the ordered extent from the tree */
1792 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1793 ret = btrfs_update_inode(trans, root, inode);
1794 BUG_ON(ret);
1795 btrfs_end_transaction(trans, root);
1796out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001797 /* once for us */
1798 btrfs_put_ordered_extent(ordered_extent);
1799 /* once for the tree */
1800 btrfs_put_ordered_extent(ordered_extent);
1801
Chris Masone6dcd2d2008-07-17 12:53:50 -04001802 return 0;
1803}
1804
Christoph Hellwigb2950862008-12-02 09:54:17 -05001805static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001806 struct extent_state *state, int uptodate)
1807{
Chris Mason8b62b722009-09-02 16:53:46 -04001808 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001809 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1810}
1811
Chris Masond352ac62008-09-29 15:18:18 -04001812/*
1813 * When IO fails, either with EIO or csum verification fails, we
1814 * try other mirrors that might have a good copy of the data. This
1815 * io_failure_record is used to record state as we go through all the
1816 * mirrors. If another mirror has good data, the page is set up to date
1817 * and things continue. If a good mirror can't be found, the original
1818 * bio end_io callback is called to indicate things have failed.
1819 */
Chris Mason7e383262008-04-09 16:28:12 -04001820struct io_failure_record {
1821 struct page *page;
1822 u64 start;
1823 u64 len;
1824 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001825 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001826 int last_mirror;
1827};
1828
Christoph Hellwigb2950862008-12-02 09:54:17 -05001829static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001830 struct page *page, u64 start, u64 end,
1831 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001832{
1833 struct io_failure_record *failrec = NULL;
1834 u64 private;
1835 struct extent_map *em;
1836 struct inode *inode = page->mapping->host;
1837 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001838 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001839 struct bio *bio;
1840 int num_copies;
1841 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001842 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001843 u64 logical;
1844
1845 ret = get_state_private(failure_tree, start, &private);
1846 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001847 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1848 if (!failrec)
1849 return -ENOMEM;
1850 failrec->start = start;
1851 failrec->len = end - start + 1;
1852 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001853 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001854
Chris Mason890871b2009-09-02 16:24:52 -04001855 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001856 em = lookup_extent_mapping(em_tree, start, failrec->len);
1857 if (em->start > start || em->start + em->len < start) {
1858 free_extent_map(em);
1859 em = NULL;
1860 }
Chris Mason890871b2009-09-02 16:24:52 -04001861 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001862
1863 if (!em || IS_ERR(em)) {
1864 kfree(failrec);
1865 return -EIO;
1866 }
1867 logical = start - em->start;
1868 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001869 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1870 logical = em->block_start;
1871 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1872 }
Chris Mason7e383262008-04-09 16:28:12 -04001873 failrec->logical = logical;
1874 free_extent_map(em);
1875 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1876 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001877 set_state_private(failure_tree, start,
1878 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001879 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001880 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001881 }
1882 num_copies = btrfs_num_copies(
1883 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1884 failrec->logical, failrec->len);
1885 failrec->last_mirror++;
1886 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001887 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001888 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1889 failrec->start,
1890 EXTENT_LOCKED);
1891 if (state && state->start != failrec->start)
1892 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001893 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001894 }
1895 if (!state || failrec->last_mirror > num_copies) {
1896 set_state_private(failure_tree, failrec->start, 0);
1897 clear_extent_bits(failure_tree, failrec->start,
1898 failrec->start + failrec->len - 1,
1899 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1900 kfree(failrec);
1901 return -EIO;
1902 }
1903 bio = bio_alloc(GFP_NOFS, 1);
1904 bio->bi_private = state;
1905 bio->bi_end_io = failed_bio->bi_end_io;
1906 bio->bi_sector = failrec->logical >> 9;
1907 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001908 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001909
Chris Mason7e383262008-04-09 16:28:12 -04001910 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001911 if (failed_bio->bi_rw & (1 << BIO_RW))
1912 rw = WRITE;
1913 else
1914 rw = READ;
1915
1916 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001917 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001918 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001919 return 0;
1920}
1921
Chris Masond352ac62008-09-29 15:18:18 -04001922/*
1923 * each time an IO finishes, we do a fast check in the IO failure tree
1924 * to see if we need to process or clean up an io_failure_record
1925 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001926static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001927{
1928 u64 private;
1929 u64 private_failure;
1930 struct io_failure_record *failure;
1931 int ret;
1932
1933 private = 0;
1934 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1935 (u64)-1, 1, EXTENT_DIRTY)) {
1936 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1937 start, &private_failure);
1938 if (ret == 0) {
1939 failure = (struct io_failure_record *)(unsigned long)
1940 private_failure;
1941 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1942 failure->start, 0);
1943 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1944 failure->start,
1945 failure->start + failure->len - 1,
1946 EXTENT_DIRTY | EXTENT_LOCKED,
1947 GFP_NOFS);
1948 kfree(failure);
1949 }
1950 }
Chris Mason7e383262008-04-09 16:28:12 -04001951 return 0;
1952}
1953
Chris Masond352ac62008-09-29 15:18:18 -04001954/*
1955 * when reads are done, we need to check csums to verify the data is correct
1956 * if there's a match, we allow the bio to finish. If not, we go through
1957 * the io_failure_record routines to find good copies
1958 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001959static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001960 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001961{
Chris Mason35ebb932007-10-30 16:56:53 -04001962 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001963 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001964 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001965 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001966 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001967 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001968 struct btrfs_root *root = BTRFS_I(inode)->root;
1969 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001970
Chris Masond20f7042008-12-08 16:58:54 -05001971 if (PageChecked(page)) {
1972 ClearPageChecked(page);
1973 goto good;
1974 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001975
1976 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001977 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001978
Yan Zheng17d217f2008-12-12 10:03:38 -05001979 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001980 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001981 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1982 GFP_NOFS);
1983 return 0;
1984 }
1985
Yanc2e639f2008-02-04 08:57:25 -05001986 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001987 private = state->private;
1988 ret = 0;
1989 } else {
1990 ret = get_state_private(io_tree, start, &private);
1991 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001992 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001993 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001994 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001995
Chris Masonff79f812007-10-15 16:22:25 -04001996 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1997 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001998 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001999 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05002000
Chris Mason9ab86c82009-01-07 09:48:51 -05002001 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05002002good:
Chris Mason7e383262008-04-09 16:28:12 -04002003 /* if the io failure tree for this inode is non-empty,
2004 * check to see if we've recovered from a failed IO
2005 */
Chris Mason1259ab72008-05-12 13:39:03 -04002006 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04002007 return 0;
2008
2009zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04002010 if (printk_ratelimit()) {
2011 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
2012 "private %llu\n", page->mapping->host->i_ino,
2013 (unsigned long long)start, csum,
2014 (unsigned long long)private);
2015 }
Chris Masondb945352007-10-15 16:15:53 -04002016 memset(kaddr + offset, 1, end - start + 1);
2017 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05002018 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04002019 if (private == 0)
2020 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04002021 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002022}
Chris Masonb888db22007-08-27 16:49:44 -04002023
Josef Bacik7b128762008-07-24 12:17:14 -04002024/*
2025 * This creates an orphan entry for the given inode in case something goes
2026 * wrong in the middle of an unlink/truncate.
2027 */
2028int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2029{
2030 struct btrfs_root *root = BTRFS_I(inode)->root;
2031 int ret = 0;
2032
Yanbcc63ab2008-07-30 16:29:20 -04002033 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002034
2035 /* already on the orphan list, we're good */
2036 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002037 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002038 return 0;
2039 }
2040
2041 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2042
Yanbcc63ab2008-07-30 16:29:20 -04002043 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002044
2045 /*
2046 * insert an orphan item to track this unlinked/truncated file
2047 */
2048 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2049
2050 return ret;
2051}
2052
2053/*
2054 * We have done the truncate/delete so we can go ahead and remove the orphan
2055 * item for this particular inode.
2056 */
2057int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2058{
2059 struct btrfs_root *root = BTRFS_I(inode)->root;
2060 int ret = 0;
2061
Yanbcc63ab2008-07-30 16:29:20 -04002062 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002063
2064 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002065 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002066 return 0;
2067 }
2068
2069 list_del_init(&BTRFS_I(inode)->i_orphan);
2070 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04002071 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002072 return 0;
2073 }
2074
Yanbcc63ab2008-07-30 16:29:20 -04002075 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002076
2077 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2078
2079 return ret;
2080}
2081
2082/*
2083 * this cleans up any orphans that may be left on the list from the last use
2084 * of this root.
2085 */
2086void btrfs_orphan_cleanup(struct btrfs_root *root)
2087{
2088 struct btrfs_path *path;
2089 struct extent_buffer *leaf;
2090 struct btrfs_item *item;
2091 struct btrfs_key key, found_key;
2092 struct btrfs_trans_handle *trans;
2093 struct inode *inode;
2094 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2095
Yan, Zhengc71bf092009-11-12 09:34:40 +00002096 if (!xchg(&root->clean_orphans, 0))
Josef Bacik7b128762008-07-24 12:17:14 -04002097 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002098
2099 path = btrfs_alloc_path();
2100 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002101 path->reada = -1;
2102
2103 key.objectid = BTRFS_ORPHAN_OBJECTID;
2104 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2105 key.offset = (u64)-1;
2106
Josef Bacik7b128762008-07-24 12:17:14 -04002107 while (1) {
2108 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2109 if (ret < 0) {
2110 printk(KERN_ERR "Error searching slot for orphan: %d"
2111 "\n", ret);
2112 break;
2113 }
2114
2115 /*
2116 * if ret == 0 means we found what we were searching for, which
2117 * is weird, but possible, so only screw with path if we didnt
2118 * find the key and see if we have stuff that matches
2119 */
2120 if (ret > 0) {
2121 if (path->slots[0] == 0)
2122 break;
2123 path->slots[0]--;
2124 }
2125
2126 /* pull out the item */
2127 leaf = path->nodes[0];
2128 item = btrfs_item_nr(leaf, path->slots[0]);
2129 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2130
2131 /* make sure the item matches what we want */
2132 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2133 break;
2134 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2135 break;
2136
2137 /* release the path since we're done with it */
2138 btrfs_release_path(root, path);
2139
2140 /*
2141 * this is where we are basically btrfs_lookup, without the
2142 * crossing root thing. we store the inode number in the
2143 * offset of the orphan item.
2144 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002145 found_key.objectid = found_key.offset;
2146 found_key.type = BTRFS_INODE_ITEM_KEY;
2147 found_key.offset = 0;
2148 inode = btrfs_iget(root->fs_info->sb, &found_key, root);
2149 if (IS_ERR(inode))
Josef Bacik7b128762008-07-24 12:17:14 -04002150 break;
2151
Josef Bacik7b128762008-07-24 12:17:14 -04002152 /*
2153 * add this inode to the orphan list so btrfs_orphan_del does
2154 * the proper thing when we hit it
2155 */
Yanbcc63ab2008-07-30 16:29:20 -04002156 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002157 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04002158 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002159
2160 /*
2161 * if this is a bad inode, means we actually succeeded in
2162 * removing the inode, but not the orphan record, which means
2163 * we need to manually delete the orphan since iput will just
2164 * do a destroy_inode
2165 */
2166 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002167 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04002168 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002169 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002170 iput(inode);
2171 continue;
2172 }
2173
2174 /* if we have links, this was a truncate, lets do that */
2175 if (inode->i_nlink) {
2176 nr_truncate++;
2177 btrfs_truncate(inode);
2178 } else {
2179 nr_unlink++;
2180 }
2181
2182 /* this will do delete_inode and everything for us */
2183 iput(inode);
2184 }
2185
2186 if (nr_unlink)
2187 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2188 if (nr_truncate)
2189 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2190
2191 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002192}
2193
Chris Masond352ac62008-09-29 15:18:18 -04002194/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002195 * very simple check to peek ahead in the leaf looking for xattrs. If we
2196 * don't find any xattrs, we know there can't be any acls.
2197 *
2198 * slot is the slot the inode is in, objectid is the objectid of the inode
2199 */
2200static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2201 int slot, u64 objectid)
2202{
2203 u32 nritems = btrfs_header_nritems(leaf);
2204 struct btrfs_key found_key;
2205 int scanned = 0;
2206
2207 slot++;
2208 while (slot < nritems) {
2209 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2210
2211 /* we found a different objectid, there must not be acls */
2212 if (found_key.objectid != objectid)
2213 return 0;
2214
2215 /* we found an xattr, assume we've got an acl */
2216 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2217 return 1;
2218
2219 /*
2220 * we found a key greater than an xattr key, there can't
2221 * be any acls later on
2222 */
2223 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2224 return 0;
2225
2226 slot++;
2227 scanned++;
2228
2229 /*
2230 * it goes inode, inode backrefs, xattrs, extents,
2231 * so if there are a ton of hard links to an inode there can
2232 * be a lot of backrefs. Don't waste time searching too hard,
2233 * this is just an optimization
2234 */
2235 if (scanned >= 8)
2236 break;
2237 }
2238 /* we hit the end of the leaf before we found an xattr or
2239 * something larger than an xattr. We have to assume the inode
2240 * has acls
2241 */
2242 return 1;
2243}
2244
2245/*
Chris Masond352ac62008-09-29 15:18:18 -04002246 * read an inode from the btree into the in-memory inode
2247 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002248static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002249{
2250 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002251 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002252 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002253 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002254 struct btrfs_root *root = BTRFS_I(inode)->root;
2255 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002256 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002257 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002258 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002259 int ret;
2260
2261 path = btrfs_alloc_path();
2262 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002263 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002264
Chris Mason39279cc2007-06-12 06:35:45 -04002265 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002266 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002267 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002268
Chris Mason5f39d392007-10-15 16:14:19 -04002269 leaf = path->nodes[0];
2270 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2271 struct btrfs_inode_item);
2272
2273 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2274 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2275 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2276 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002277 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002278
2279 tspec = btrfs_inode_atime(inode_item);
2280 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2281 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2282
2283 tspec = btrfs_inode_mtime(inode_item);
2284 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2285 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2286
2287 tspec = btrfs_inode_ctime(inode_item);
2288 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2289 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2290
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002291 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002292 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002293 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002294 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002295 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002296 rdev = btrfs_inode_rdev(leaf, inode_item);
2297
Josef Bacikaec74772008-07-24 12:12:38 -04002298 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002299 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002300
Chris Mason5f39d392007-10-15 16:14:19 -04002301 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002302
Chris Mason46a53cc2009-04-27 11:47:50 -04002303 /*
2304 * try to precache a NULL acl entry for files that don't have
2305 * any xattrs or acls
2306 */
2307 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002308 if (!maybe_acls)
2309 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002310
Yan Zhengd2fb3432008-12-11 16:30:39 -05002311 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2312 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002313 btrfs_free_path(path);
2314 inode_item = NULL;
2315
Chris Mason39279cc2007-06-12 06:35:45 -04002316 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002317 case S_IFREG:
2318 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002319 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002320 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002321 inode->i_fop = &btrfs_file_operations;
2322 inode->i_op = &btrfs_file_inode_operations;
2323 break;
2324 case S_IFDIR:
2325 inode->i_fop = &btrfs_dir_file_operations;
2326 if (root == root->fs_info->tree_root)
2327 inode->i_op = &btrfs_dir_ro_inode_operations;
2328 else
2329 inode->i_op = &btrfs_dir_inode_operations;
2330 break;
2331 case S_IFLNK:
2332 inode->i_op = &btrfs_symlink_inode_operations;
2333 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002334 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002335 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002336 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002337 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002338 init_special_inode(inode, inode->i_mode, rdev);
2339 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002340 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002341
2342 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002343 return;
2344
2345make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002346 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002347 make_bad_inode(inode);
2348}
2349
Chris Masond352ac62008-09-29 15:18:18 -04002350/*
2351 * given a leaf and an inode, copy the inode fields into the leaf
2352 */
Chris Masone02119d2008-09-05 16:13:11 -04002353static void fill_inode_item(struct btrfs_trans_handle *trans,
2354 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002355 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002356 struct inode *inode)
2357{
Chris Mason5f39d392007-10-15 16:14:19 -04002358 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2359 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002360 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002361 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2362 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2363
2364 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2365 inode->i_atime.tv_sec);
2366 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2367 inode->i_atime.tv_nsec);
2368
2369 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2370 inode->i_mtime.tv_sec);
2371 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2372 inode->i_mtime.tv_nsec);
2373
2374 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2375 inode->i_ctime.tv_sec);
2376 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2377 inode->i_ctime.tv_nsec);
2378
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002379 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002380 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002381 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002382 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002383 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002384 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002385 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002386}
2387
Chris Masond352ac62008-09-29 15:18:18 -04002388/*
2389 * copy everything in the in-memory inode into the btree.
2390 */
Chris Masond3977122009-01-05 21:25:51 -05002391noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2392 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002393{
2394 struct btrfs_inode_item *inode_item;
2395 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002396 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002397 int ret;
2398
2399 path = btrfs_alloc_path();
2400 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002401 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002402 ret = btrfs_lookup_inode(trans, root, path,
2403 &BTRFS_I(inode)->location, 1);
2404 if (ret) {
2405 if (ret > 0)
2406 ret = -ENOENT;
2407 goto failed;
2408 }
2409
Chris Masonb4ce94d2009-02-04 09:25:08 -05002410 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002411 leaf = path->nodes[0];
2412 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002413 struct btrfs_inode_item);
2414
Chris Masone02119d2008-09-05 16:13:11 -04002415 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002416 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002417 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002418 ret = 0;
2419failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002420 btrfs_free_path(path);
2421 return ret;
2422}
2423
2424
Chris Masond352ac62008-09-29 15:18:18 -04002425/*
2426 * unlink helper that gets used here in inode.c and in the tree logging
2427 * recovery code. It remove a link in a directory with a given name, and
2428 * also drops the back refs in the inode to the directory
2429 */
Chris Masone02119d2008-09-05 16:13:11 -04002430int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2431 struct btrfs_root *root,
2432 struct inode *dir, struct inode *inode,
2433 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002434{
2435 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002436 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002437 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002438 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002439 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002440 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002441
2442 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002443 if (!path) {
2444 ret = -ENOMEM;
2445 goto err;
2446 }
2447
Chris Masonb9473432009-03-13 11:00:37 -04002448 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002449 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2450 name, name_len, -1);
2451 if (IS_ERR(di)) {
2452 ret = PTR_ERR(di);
2453 goto err;
2454 }
2455 if (!di) {
2456 ret = -ENOENT;
2457 goto err;
2458 }
Chris Mason5f39d392007-10-15 16:14:19 -04002459 leaf = path->nodes[0];
2460 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002461 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002462 if (ret)
2463 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002464 btrfs_release_path(root, path);
2465
Josef Bacikaec74772008-07-24 12:12:38 -04002466 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002467 inode->i_ino,
2468 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002469 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002470 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002471 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002472 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002473 goto err;
2474 }
2475
Chris Mason39279cc2007-06-12 06:35:45 -04002476 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002477 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002478 if (IS_ERR(di)) {
2479 ret = PTR_ERR(di);
2480 goto err;
2481 }
2482 if (!di) {
2483 ret = -ENOENT;
2484 goto err;
2485 }
2486 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002487 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002488
Chris Masone02119d2008-09-05 16:13:11 -04002489 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2490 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002491 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002492
2493 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2494 dir, index);
2495 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002496err:
2497 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002498 if (ret)
2499 goto out;
2500
2501 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2502 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2503 btrfs_update_inode(trans, root, dir);
2504 btrfs_drop_nlink(inode);
2505 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002506out:
Chris Mason39279cc2007-06-12 06:35:45 -04002507 return ret;
2508}
2509
2510static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2511{
2512 struct btrfs_root *root;
2513 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002514 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002515 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002516 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002517
2518 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002519
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002520 /*
2521 * 5 items for unlink inode
2522 * 1 for orphan
2523 */
2524 ret = btrfs_reserve_metadata_space(root, 6);
2525 if (ret)
2526 return ret;
2527
Chris Mason39279cc2007-06-12 06:35:45 -04002528 trans = btrfs_start_transaction(root, 1);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002529 if (IS_ERR(trans)) {
2530 btrfs_unreserve_metadata_space(root, 6);
2531 return PTR_ERR(trans);
2532 }
Chris Mason5f39d392007-10-15 16:14:19 -04002533
Chris Mason39279cc2007-06-12 06:35:45 -04002534 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002535
2536 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2537
Chris Masone02119d2008-09-05 16:13:11 -04002538 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2539 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002540
2541 if (inode->i_nlink == 0)
2542 ret = btrfs_orphan_add(trans, inode);
2543
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002544 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002545
Chris Mason89ce8a62008-06-25 16:01:31 -04002546 btrfs_end_transaction_throttle(trans, root);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002547 btrfs_unreserve_metadata_space(root, 6);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002548 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002549 return ret;
2550}
2551
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002552int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2553 struct btrfs_root *root,
2554 struct inode *dir, u64 objectid,
2555 const char *name, int name_len)
2556{
2557 struct btrfs_path *path;
2558 struct extent_buffer *leaf;
2559 struct btrfs_dir_item *di;
2560 struct btrfs_key key;
2561 u64 index;
2562 int ret;
2563
2564 path = btrfs_alloc_path();
2565 if (!path)
2566 return -ENOMEM;
2567
2568 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2569 name, name_len, -1);
2570 BUG_ON(!di || IS_ERR(di));
2571
2572 leaf = path->nodes[0];
2573 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2574 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2575 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2576 BUG_ON(ret);
2577 btrfs_release_path(root, path);
2578
2579 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2580 objectid, root->root_key.objectid,
2581 dir->i_ino, &index, name, name_len);
2582 if (ret < 0) {
2583 BUG_ON(ret != -ENOENT);
2584 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2585 name, name_len);
2586 BUG_ON(!di || IS_ERR(di));
2587
2588 leaf = path->nodes[0];
2589 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2590 btrfs_release_path(root, path);
2591 index = key.offset;
2592 }
2593
2594 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2595 index, name, name_len, -1);
2596 BUG_ON(!di || IS_ERR(di));
2597
2598 leaf = path->nodes[0];
2599 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2600 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2601 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2602 BUG_ON(ret);
2603 btrfs_release_path(root, path);
2604
2605 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2606 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2607 ret = btrfs_update_inode(trans, root, dir);
2608 BUG_ON(ret);
2609 dir->i_sb->s_dirt = 1;
2610
2611 btrfs_free_path(path);
2612 return 0;
2613}
2614
Chris Mason39279cc2007-06-12 06:35:45 -04002615static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2616{
2617 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002618 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002619 int ret;
2620 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002621 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002622 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002623
Chris Mason3394e162008-11-17 20:42:26 -05002624 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002625 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002626 return -ENOTEMPTY;
2627
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002628 ret = btrfs_reserve_metadata_space(root, 5);
2629 if (ret)
2630 return ret;
2631
Chris Mason39279cc2007-06-12 06:35:45 -04002632 trans = btrfs_start_transaction(root, 1);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002633 if (IS_ERR(trans)) {
2634 btrfs_unreserve_metadata_space(root, 5);
2635 return PTR_ERR(trans);
2636 }
2637
Chris Mason39279cc2007-06-12 06:35:45 -04002638 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002639
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002640 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2641 err = btrfs_unlink_subvol(trans, root, dir,
2642 BTRFS_I(inode)->location.objectid,
2643 dentry->d_name.name,
2644 dentry->d_name.len);
2645 goto out;
2646 }
2647
Josef Bacik7b128762008-07-24 12:17:14 -04002648 err = btrfs_orphan_add(trans, inode);
2649 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002650 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002651
Chris Mason39279cc2007-06-12 06:35:45 -04002652 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002653 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2654 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002655 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002656 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002657out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002658 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002659 ret = btrfs_end_transaction_throttle(trans, root);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002660 btrfs_unreserve_metadata_space(root, 5);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002661 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002662
Chris Mason39279cc2007-06-12 06:35:45 -04002663 if (ret && !err)
2664 err = ret;
2665 return err;
2666}
2667
Chris Masond20f7042008-12-08 16:58:54 -05002668#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002669/*
Chris Mason323ac952008-10-01 19:05:46 -04002670 * when truncating bytes in a file, it is possible to avoid reading
2671 * the leaves that contain only checksum items. This can be the
2672 * majority of the IO required to delete a large file, but it must
2673 * be done carefully.
2674 *
2675 * The keys in the level just above the leaves are checked to make sure
2676 * the lowest key in a given leaf is a csum key, and starts at an offset
2677 * after the new size.
2678 *
2679 * Then the key for the next leaf is checked to make sure it also has
2680 * a checksum item for the same file. If it does, we know our target leaf
2681 * contains only checksum items, and it can be safely freed without reading
2682 * it.
2683 *
2684 * This is just an optimization targeted at large files. It may do
2685 * nothing. It will return 0 unless things went badly.
2686 */
2687static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2688 struct btrfs_root *root,
2689 struct btrfs_path *path,
2690 struct inode *inode, u64 new_size)
2691{
2692 struct btrfs_key key;
2693 int ret;
2694 int nritems;
2695 struct btrfs_key found_key;
2696 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002697 struct btrfs_leaf_ref *ref;
2698 u64 leaf_gen;
2699 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002700
2701 path->lowest_level = 1;
2702 key.objectid = inode->i_ino;
2703 key.type = BTRFS_CSUM_ITEM_KEY;
2704 key.offset = new_size;
2705again:
2706 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2707 if (ret < 0)
2708 goto out;
2709
2710 if (path->nodes[1] == NULL) {
2711 ret = 0;
2712 goto out;
2713 }
2714 ret = 0;
2715 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2716 nritems = btrfs_header_nritems(path->nodes[1]);
2717
2718 if (!nritems)
2719 goto out;
2720
2721 if (path->slots[1] >= nritems)
2722 goto next_node;
2723
2724 /* did we find a key greater than anything we want to delete? */
2725 if (found_key.objectid > inode->i_ino ||
2726 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2727 goto out;
2728
2729 /* we check the next key in the node to make sure the leave contains
2730 * only checksum items. This comparison doesn't work if our
2731 * leaf is the last one in the node
2732 */
2733 if (path->slots[1] + 1 >= nritems) {
2734next_node:
2735 /* search forward from the last key in the node, this
2736 * will bring us into the next node in the tree
2737 */
2738 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2739
2740 /* unlikely, but we inc below, so check to be safe */
2741 if (found_key.offset == (u64)-1)
2742 goto out;
2743
2744 /* search_forward needs a path with locks held, do the
2745 * search again for the original key. It is possible
2746 * this will race with a balance and return a path that
2747 * we could modify, but this drop is just an optimization
2748 * and is allowed to miss some leaves.
2749 */
2750 btrfs_release_path(root, path);
2751 found_key.offset++;
2752
2753 /* setup a max key for search_forward */
2754 other_key.offset = (u64)-1;
2755 other_key.type = key.type;
2756 other_key.objectid = key.objectid;
2757
2758 path->keep_locks = 1;
2759 ret = btrfs_search_forward(root, &found_key, &other_key,
2760 path, 0, 0);
2761 path->keep_locks = 0;
2762 if (ret || found_key.objectid != key.objectid ||
2763 found_key.type != key.type) {
2764 ret = 0;
2765 goto out;
2766 }
2767
2768 key.offset = found_key.offset;
2769 btrfs_release_path(root, path);
2770 cond_resched();
2771 goto again;
2772 }
2773
2774 /* we know there's one more slot after us in the tree,
2775 * read that key so we can verify it is also a checksum item
2776 */
2777 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2778
2779 if (found_key.objectid < inode->i_ino)
2780 goto next_key;
2781
2782 if (found_key.type != key.type || found_key.offset < new_size)
2783 goto next_key;
2784
2785 /*
2786 * if the key for the next leaf isn't a csum key from this objectid,
2787 * we can't be sure there aren't good items inside this leaf.
2788 * Bail out
2789 */
2790 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2791 goto out;
2792
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002793 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2794 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002795 /*
2796 * it is safe to delete this leaf, it contains only
2797 * csum items from this inode at an offset >= new_size
2798 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002799 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002800 BUG_ON(ret);
2801
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002802 if (root->ref_cows && leaf_gen < trans->transid) {
2803 ref = btrfs_alloc_leaf_ref(root, 0);
2804 if (ref) {
2805 ref->root_gen = root->root_key.offset;
2806 ref->bytenr = leaf_start;
2807 ref->owner = 0;
2808 ref->generation = leaf_gen;
2809 ref->nritems = 0;
2810
Chris Masonbd56b302009-02-04 09:27:02 -05002811 btrfs_sort_leaf_ref(ref);
2812
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002813 ret = btrfs_add_leaf_ref(root, ref, 0);
2814 WARN_ON(ret);
2815 btrfs_free_leaf_ref(root, ref);
2816 } else {
2817 WARN_ON(1);
2818 }
2819 }
Chris Mason323ac952008-10-01 19:05:46 -04002820next_key:
2821 btrfs_release_path(root, path);
2822
2823 if (other_key.objectid == inode->i_ino &&
2824 other_key.type == key.type && other_key.offset > key.offset) {
2825 key.offset = other_key.offset;
2826 cond_resched();
2827 goto again;
2828 }
2829 ret = 0;
2830out:
2831 /* fixup any changes we've made to the path */
2832 path->lowest_level = 0;
2833 path->keep_locks = 0;
2834 btrfs_release_path(root, path);
2835 return ret;
2836}
2837
Chris Masond20f7042008-12-08 16:58:54 -05002838#endif
2839
Chris Mason323ac952008-10-01 19:05:46 -04002840/*
Chris Mason39279cc2007-06-12 06:35:45 -04002841 * this can truncate away extent items, csum items and directory items.
2842 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002843 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002844 *
2845 * csum items that cross the new i_size are truncated to the new size
2846 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002847 *
2848 * min_type is the minimum key type to truncate down to. If set to 0, this
2849 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002850 */
Chris Masone02119d2008-09-05 16:13:11 -04002851noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2852 struct btrfs_root *root,
2853 struct inode *inode,
2854 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002855{
2856 int ret;
2857 struct btrfs_path *path;
2858 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002859 struct btrfs_key found_key;
Chris Mason06d9a8d2009-02-04 09:30:58 -05002860 u32 found_type = (u8)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002861 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002862 struct btrfs_file_extent_item *fi;
2863 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002864 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002865 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002866 u64 item_end = 0;
2867 int found_extent;
2868 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002869 int pending_del_nr = 0;
2870 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002871 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002872 int encoding;
Chris Mason3b951512008-04-17 11:29:12 -04002873 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002874
Chris Masone02119d2008-09-05 16:13:11 -04002875 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002876 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002877 path = btrfs_alloc_path();
2878 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04002879 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04002880
Chris Mason39279cc2007-06-12 06:35:45 -04002881 /* FIXME, add redo link to tree so we don't leak on crash */
2882 key.objectid = inode->i_ino;
2883 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002884 key.type = (u8)-1;
2885
Chris Mason85e21ba2008-01-29 15:11:36 -05002886search_again:
Chris Masonb9473432009-03-13 11:00:37 -04002887 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05002888 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002889 if (ret < 0)
Chris Mason85e21ba2008-01-29 15:11:36 -05002890 goto error;
Chris Masond3977122009-01-05 21:25:51 -05002891
Chris Mason85e21ba2008-01-29 15:11:36 -05002892 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002893 /* there are no items in the tree for us to truncate, we're
2894 * done
2895 */
2896 if (path->slots[0] == 0) {
2897 ret = 0;
2898 goto error;
2899 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002900 path->slots[0]--;
2901 }
2902
Chris Masond3977122009-01-05 21:25:51 -05002903 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002904 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002905 leaf = path->nodes[0];
2906 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2907 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002908 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002909
Chris Mason5f39d392007-10-15 16:14:19 -04002910 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002911 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002912
Chris Mason85e21ba2008-01-29 15:11:36 -05002913 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002914 break;
2915
Chris Mason5f39d392007-10-15 16:14:19 -04002916 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002917 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002918 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002919 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002920 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002921 encoding = btrfs_file_extent_compression(leaf, fi);
2922 encoding |= btrfs_file_extent_encryption(leaf, fi);
2923 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2924
Chris Mason179e29e2007-11-01 11:28:41 -04002925 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002926 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002927 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002928 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002929 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002930 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002931 }
Yan008630c2007-11-07 13:31:09 -05002932 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002933 }
Chris Masone02119d2008-09-05 16:13:11 -04002934 if (item_end < new_size) {
Chris Masond3977122009-01-05 21:25:51 -05002935 if (found_type == BTRFS_DIR_ITEM_KEY)
Chris Masonb888db22007-08-27 16:49:44 -04002936 found_type = BTRFS_INODE_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002937 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
Chris Masond20f7042008-12-08 16:58:54 -05002938 found_type = BTRFS_EXTENT_DATA_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002939 else if (found_type == BTRFS_EXTENT_DATA_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002940 found_type = BTRFS_XATTR_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002941 else if (found_type == BTRFS_XATTR_ITEM_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002942 found_type = BTRFS_INODE_REF_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002943 else if (found_type)
Chris Masonb888db22007-08-27 16:49:44 -04002944 found_type--;
Chris Masond3977122009-01-05 21:25:51 -05002945 else
Chris Masonb888db22007-08-27 16:49:44 -04002946 break;
Yana61721d2007-09-17 11:08:38 -04002947 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002948 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002949 }
Chris Masone02119d2008-09-05 16:13:11 -04002950 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002951 del_item = 1;
2952 else
2953 del_item = 0;
2954 found_extent = 0;
2955
2956 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002957 if (found_type != BTRFS_EXTENT_DATA_KEY)
2958 goto delete;
2959
2960 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002961 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002962 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002963 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002964 u64 orig_num_bytes =
2965 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002966 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002967 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002968 extent_num_bytes = extent_num_bytes &
2969 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002970 btrfs_set_file_extent_num_bytes(leaf, fi,
2971 extent_num_bytes);
2972 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002973 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002974 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002975 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002976 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002977 } else {
Chris Masondb945352007-10-15 16:15:53 -04002978 extent_num_bytes =
2979 btrfs_file_extent_disk_num_bytes(leaf,
2980 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002981 extent_offset = found_key.offset -
2982 btrfs_file_extent_offset(leaf, fi);
2983
Chris Mason39279cc2007-06-12 06:35:45 -04002984 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002985 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002986 if (extent_start != 0) {
2987 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002988 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002989 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002990 }
2991 }
Chris Mason90692182008-02-08 13:49:28 -05002992 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002993 /*
2994 * we can't truncate inline items that have had
2995 * special encodings
2996 */
2997 if (!del_item &&
2998 btrfs_file_extent_compression(leaf, fi) == 0 &&
2999 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3000 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003001 u32 size = new_size - found_key.offset;
3002
3003 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003004 inode_sub_bytes(inode, item_end + 1 -
3005 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003006 }
3007 size =
3008 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003009 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003010 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003011 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003012 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003013 inode_sub_bytes(inode, item_end + 1 -
3014 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003015 }
Chris Mason39279cc2007-06-12 06:35:45 -04003016 }
Chris Mason179e29e2007-11-01 11:28:41 -04003017delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003018 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003019 if (!pending_del_nr) {
3020 /* no pending yet, add ourselves */
3021 pending_del_slot = path->slots[0];
3022 pending_del_nr = 1;
3023 } else if (pending_del_nr &&
3024 path->slots[0] + 1 == pending_del_slot) {
3025 /* hop on the pending chunk */
3026 pending_del_nr++;
3027 pending_del_slot = path->slots[0];
3028 } else {
Chris Masond3977122009-01-05 21:25:51 -05003029 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003030 }
Chris Mason39279cc2007-06-12 06:35:45 -04003031 } else {
3032 break;
3033 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003034 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04003035 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003036 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003037 extent_num_bytes, 0,
3038 btrfs_header_owner(leaf),
3039 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003040 BUG_ON(ret);
3041 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003042next:
3043 if (path->slots[0] == 0) {
3044 if (pending_del_nr)
3045 goto del_pending;
3046 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05003047 if (found_type == BTRFS_INODE_ITEM_KEY)
3048 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05003049 goto search_again;
3050 }
3051
3052 path->slots[0]--;
3053 if (pending_del_nr &&
3054 path->slots[0] + 1 != pending_del_slot) {
3055 struct btrfs_key debug;
3056del_pending:
3057 btrfs_item_key_to_cpu(path->nodes[0], &debug,
3058 pending_del_slot);
3059 ret = btrfs_del_items(trans, root, path,
3060 pending_del_slot,
3061 pending_del_nr);
3062 BUG_ON(ret);
3063 pending_del_nr = 0;
3064 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05003065 if (found_type == BTRFS_INODE_ITEM_KEY)
3066 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05003067 goto search_again;
3068 }
Chris Mason39279cc2007-06-12 06:35:45 -04003069 }
3070 ret = 0;
3071error:
Chris Mason85e21ba2008-01-29 15:11:36 -05003072 if (pending_del_nr) {
3073 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3074 pending_del_nr);
3075 }
Chris Mason39279cc2007-06-12 06:35:45 -04003076 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003077 return ret;
3078}
3079
Chris Masona52d9a82007-08-27 16:49:44 -04003080/*
3081 * taken from block_truncate_page, but does cow as it zeros out
3082 * any bytes left in the last page in the file.
3083 */
3084static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3085{
3086 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003087 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003088 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3089 struct btrfs_ordered_extent *ordered;
3090 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003091 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003092 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3093 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3094 struct page *page;
3095 int ret = 0;
3096 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003097 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003098
3099 if ((offset & (blocksize - 1)) == 0)
3100 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003101 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
3102 if (ret)
3103 goto out;
3104
3105 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
3106 if (ret)
3107 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003108
3109 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003110again:
Chris Masona52d9a82007-08-27 16:49:44 -04003111 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003112 if (!page) {
3113 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3114 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04003115 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003116 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003117
3118 page_start = page_offset(page);
3119 page_end = page_start + PAGE_CACHE_SIZE - 1;
3120
Chris Masona52d9a82007-08-27 16:49:44 -04003121 if (!PageUptodate(page)) {
3122 ret = btrfs_readpage(NULL, page);
3123 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003124 if (page->mapping != mapping) {
3125 unlock_page(page);
3126 page_cache_release(page);
3127 goto again;
3128 }
Chris Masona52d9a82007-08-27 16:49:44 -04003129 if (!PageUptodate(page)) {
3130 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003131 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003132 }
3133 }
Chris Mason211c17f2008-05-15 09:13:45 -04003134 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003135
3136 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3137 set_page_extent_mapped(page);
3138
3139 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3140 if (ordered) {
3141 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3142 unlock_page(page);
3143 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003144 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003145 btrfs_put_ordered_extent(ordered);
3146 goto again;
3147 }
3148
Josef Bacik5d5e1032009-10-13 16:46:49 -04003149 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
3150 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3151 GFP_NOFS);
3152
Josef Bacik9ed74f22009-09-11 16:12:44 -04003153 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3154 if (ret) {
3155 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3156 goto out_unlock;
3157 }
3158
Chris Masone6dcd2d2008-07-17 12:53:50 -04003159 ret = 0;
3160 if (offset != PAGE_CACHE_SIZE) {
3161 kaddr = kmap(page);
3162 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3163 flush_dcache_page(page);
3164 kunmap(page);
3165 }
Chris Mason247e7432008-07-17 12:53:51 -04003166 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003167 set_page_dirty(page);
3168 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003169
Chris Mason89642222008-07-24 09:41:53 -04003170out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003171 if (ret)
3172 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3173 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003174 unlock_page(page);
3175 page_cache_release(page);
3176out:
3177 return ret;
3178}
3179
Yan Zheng9036c102008-10-30 14:19:41 -04003180int btrfs_cont_expand(struct inode *inode, loff_t size)
3181{
3182 struct btrfs_trans_handle *trans;
3183 struct btrfs_root *root = BTRFS_I(inode)->root;
3184 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3185 struct extent_map *em;
3186 u64 mask = root->sectorsize - 1;
3187 u64 hole_start = (inode->i_size + mask) & ~mask;
3188 u64 block_end = (size + mask) & ~mask;
3189 u64 last_byte;
3190 u64 cur_offset;
3191 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003192 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003193
3194 if (size <= hole_start)
3195 return 0;
3196
Josef Bacik5d5e1032009-10-13 16:46:49 -04003197 err = btrfs_truncate_page(inode->i_mapping, inode->i_size);
3198 if (err)
3199 return err;
Yan Zheng9036c102008-10-30 14:19:41 -04003200
3201 while (1) {
3202 struct btrfs_ordered_extent *ordered;
3203 btrfs_wait_ordered_range(inode, hole_start,
3204 block_end - hole_start);
3205 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3206 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3207 if (!ordered)
3208 break;
3209 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3210 btrfs_put_ordered_extent(ordered);
3211 }
3212
3213 trans = btrfs_start_transaction(root, 1);
3214 btrfs_set_trans_block_group(trans, inode);
3215
3216 cur_offset = hole_start;
3217 while (1) {
3218 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3219 block_end - cur_offset, 0);
3220 BUG_ON(IS_ERR(em) || !em);
3221 last_byte = min(extent_map_end(em), block_end);
3222 last_byte = (last_byte + mask) & ~mask;
3223 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003224 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003225 hole_size = last_byte - cur_offset;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00003226 err = btrfs_drop_extents(trans, inode, cur_offset,
Chris Mason771ed682008-11-06 22:02:51 -05003227 cur_offset + hole_size,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00003228 &hint_byte, 1);
Chris Mason771ed682008-11-06 22:02:51 -05003229 if (err)
3230 break;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003231
3232 err = btrfs_reserve_metadata_space(root, 1);
3233 if (err)
3234 break;
3235
Yan Zheng9036c102008-10-30 14:19:41 -04003236 err = btrfs_insert_file_extent(trans, root,
3237 inode->i_ino, cur_offset, 0,
3238 0, hole_size, 0, hole_size,
3239 0, 0, 0);
3240 btrfs_drop_extent_cache(inode, hole_start,
3241 last_byte - 1, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003242 btrfs_unreserve_metadata_space(root, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04003243 }
3244 free_extent_map(em);
3245 cur_offset = last_byte;
3246 if (err || cur_offset >= block_end)
3247 break;
3248 }
3249
3250 btrfs_end_transaction(trans, root);
3251 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3252 return err;
3253}
3254
Chris Mason39279cc2007-06-12 06:35:45 -04003255static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3256{
3257 struct inode *inode = dentry->d_inode;
3258 int err;
3259
3260 err = inode_change_ok(inode, attr);
3261 if (err)
3262 return err;
3263
Chris Mason5a3f23d2009-03-31 13:27:11 -04003264 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3265 if (attr->ia_size > inode->i_size) {
3266 err = btrfs_cont_expand(inode, attr->ia_size);
3267 if (err)
3268 return err;
3269 } else if (inode->i_size > 0 &&
3270 attr->ia_size == 0) {
3271
3272 /* we're truncating a file that used to have good
3273 * data down to zero. Make sure it gets into
3274 * the ordered flush list so that any new writes
3275 * get down to disk quickly.
3276 */
3277 BTRFS_I(inode)->ordered_data_close = 1;
3278 }
Chris Mason39279cc2007-06-12 06:35:45 -04003279 }
Yan Zheng9036c102008-10-30 14:19:41 -04003280
Chris Mason39279cc2007-06-12 06:35:45 -04003281 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003282
3283 if (!err && ((attr->ia_valid & ATTR_MODE)))
3284 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003285 return err;
3286}
Chris Mason61295eb2008-01-14 16:24:38 -05003287
Chris Mason39279cc2007-06-12 06:35:45 -04003288void btrfs_delete_inode(struct inode *inode)
3289{
3290 struct btrfs_trans_handle *trans;
3291 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003292 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003293 int ret;
3294
3295 truncate_inode_pages(&inode->i_data, 0);
3296 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003297 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003298 goto no_delete;
3299 }
Chris Mason4a096752008-07-21 10:29:44 -04003300 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003301
Yan, Zhengc71bf092009-11-12 09:34:40 +00003302 if (root->fs_info->log_root_recovering) {
3303 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3304 goto no_delete;
3305 }
3306
Yan, Zheng76dda932009-09-21 16:00:26 -04003307 if (inode->i_nlink > 0) {
3308 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3309 goto no_delete;
3310 }
3311
Chris Masondbe674a2008-07-17 12:54:05 -04003312 btrfs_i_size_write(inode, 0);
Yan Zheng180591b2009-01-06 09:58:06 -05003313 trans = btrfs_join_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003314
Chris Mason39279cc2007-06-12 06:35:45 -04003315 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04003316 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04003317 if (ret) {
3318 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04003319 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04003320 }
3321
3322 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05003323
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003324 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05003325 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003326
Chris Mason39279cc2007-06-12 06:35:45 -04003327 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003328 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003329 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04003330
3331no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003332 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003333 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003334 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003335no_delete:
3336 clear_inode(inode);
3337}
3338
3339/*
3340 * this returns the key found in the dir entry in the location pointer.
3341 * If no dir entries were found, location->objectid is 0.
3342 */
3343static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3344 struct btrfs_key *location)
3345{
3346 const char *name = dentry->d_name.name;
3347 int namelen = dentry->d_name.len;
3348 struct btrfs_dir_item *di;
3349 struct btrfs_path *path;
3350 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003351 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003352
3353 path = btrfs_alloc_path();
3354 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003355
Chris Mason39279cc2007-06-12 06:35:45 -04003356 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3357 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003358 if (IS_ERR(di))
3359 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003360
3361 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003362 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003363
Chris Mason5f39d392007-10-15 16:14:19 -04003364 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003365out:
Chris Mason39279cc2007-06-12 06:35:45 -04003366 btrfs_free_path(path);
3367 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003368out_err:
3369 location->objectid = 0;
3370 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003371}
3372
3373/*
3374 * when we hit a tree root in a directory, the btrfs part of the inode
3375 * needs to be changed to reflect the root directory of the tree root. This
3376 * is kind of like crossing a mount point.
3377 */
3378static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003379 struct inode *dir,
3380 struct dentry *dentry,
3381 struct btrfs_key *location,
3382 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003383{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003384 struct btrfs_path *path;
3385 struct btrfs_root *new_root;
3386 struct btrfs_root_ref *ref;
3387 struct extent_buffer *leaf;
3388 int ret;
3389 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003390
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003391 path = btrfs_alloc_path();
3392 if (!path) {
3393 err = -ENOMEM;
3394 goto out;
3395 }
Chris Mason39279cc2007-06-12 06:35:45 -04003396
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003397 err = -ENOENT;
3398 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3399 BTRFS_I(dir)->root->root_key.objectid,
3400 location->objectid);
3401 if (ret) {
3402 if (ret < 0)
3403 err = ret;
3404 goto out;
3405 }
Chris Mason39279cc2007-06-12 06:35:45 -04003406
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003407 leaf = path->nodes[0];
3408 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3409 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3410 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3411 goto out;
3412
3413 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3414 (unsigned long)(ref + 1),
3415 dentry->d_name.len);
3416 if (ret)
3417 goto out;
3418
3419 btrfs_release_path(root->fs_info->tree_root, path);
3420
3421 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3422 if (IS_ERR(new_root)) {
3423 err = PTR_ERR(new_root);
3424 goto out;
3425 }
3426
3427 if (btrfs_root_refs(&new_root->root_item) == 0) {
3428 err = -ENOENT;
3429 goto out;
3430 }
3431
3432 *sub_root = new_root;
3433 location->objectid = btrfs_root_dirid(&new_root->root_item);
3434 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003435 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003436 err = 0;
3437out:
3438 btrfs_free_path(path);
3439 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003440}
3441
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003442static void inode_tree_add(struct inode *inode)
3443{
3444 struct btrfs_root *root = BTRFS_I(inode)->root;
3445 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003446 struct rb_node **p;
3447 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003448again:
3449 p = &root->inode_tree.rb_node;
3450 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003451
Yan, Zheng76dda932009-09-21 16:00:26 -04003452 if (hlist_unhashed(&inode->i_hash))
3453 return;
3454
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003455 spin_lock(&root->inode_lock);
3456 while (*p) {
3457 parent = *p;
3458 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3459
3460 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003461 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003462 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003463 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003464 else {
3465 WARN_ON(!(entry->vfs_inode.i_state &
3466 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003467 rb_erase(parent, &root->inode_tree);
3468 RB_CLEAR_NODE(parent);
3469 spin_unlock(&root->inode_lock);
3470 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003471 }
3472 }
3473 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3474 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3475 spin_unlock(&root->inode_lock);
3476}
3477
3478static void inode_tree_del(struct inode *inode)
3479{
3480 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003481 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003482
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003483 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003484 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003485 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003486 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003487 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003488 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003489 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003490
3491 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3492 synchronize_srcu(&root->fs_info->subvol_srcu);
3493 spin_lock(&root->inode_lock);
3494 empty = RB_EMPTY_ROOT(&root->inode_tree);
3495 spin_unlock(&root->inode_lock);
3496 if (empty)
3497 btrfs_add_dead_root(root);
3498 }
3499}
3500
3501int btrfs_invalidate_inodes(struct btrfs_root *root)
3502{
3503 struct rb_node *node;
3504 struct rb_node *prev;
3505 struct btrfs_inode *entry;
3506 struct inode *inode;
3507 u64 objectid = 0;
3508
3509 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3510
3511 spin_lock(&root->inode_lock);
3512again:
3513 node = root->inode_tree.rb_node;
3514 prev = NULL;
3515 while (node) {
3516 prev = node;
3517 entry = rb_entry(node, struct btrfs_inode, rb_node);
3518
3519 if (objectid < entry->vfs_inode.i_ino)
3520 node = node->rb_left;
3521 else if (objectid > entry->vfs_inode.i_ino)
3522 node = node->rb_right;
3523 else
3524 break;
3525 }
3526 if (!node) {
3527 while (prev) {
3528 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3529 if (objectid <= entry->vfs_inode.i_ino) {
3530 node = prev;
3531 break;
3532 }
3533 prev = rb_next(prev);
3534 }
3535 }
3536 while (node) {
3537 entry = rb_entry(node, struct btrfs_inode, rb_node);
3538 objectid = entry->vfs_inode.i_ino + 1;
3539 inode = igrab(&entry->vfs_inode);
3540 if (inode) {
3541 spin_unlock(&root->inode_lock);
3542 if (atomic_read(&inode->i_count) > 1)
3543 d_prune_aliases(inode);
3544 /*
3545 * btrfs_drop_inode will remove it from
3546 * the inode cache when its usage count
3547 * hits zero.
3548 */
3549 iput(inode);
3550 cond_resched();
3551 spin_lock(&root->inode_lock);
3552 goto again;
3553 }
3554
3555 if (cond_resched_lock(&root->inode_lock))
3556 goto again;
3557
3558 node = rb_next(node);
3559 }
3560 spin_unlock(&root->inode_lock);
3561 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003562}
3563
Chris Masone02119d2008-09-05 16:13:11 -04003564static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003565{
Chris Masone02119d2008-09-05 16:13:11 -04003566 struct btrfs_inode *bi = BTRFS_I(inode);
3567
Chris Masone02119d2008-09-05 16:13:11 -04003568 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05003569 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003570 bi->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04003571 bi->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003572 bi->logged_trans = 0;
3573 bi->delalloc_bytes = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003574 bi->reserved_bytes = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003575 bi->disk_i_size = 0;
3576 bi->flags = 0;
3577 bi->index_cnt = (u64)-1;
Chris Mason12fcfd22009-03-24 10:24:20 -04003578 bi->last_unlink_trans = 0;
Chris Mason27574952009-05-14 13:10:02 -04003579 bi->ordered_data_close = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003580 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3581 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04003582 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003583 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3584 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003585 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04003586 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003587 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Chris Masonba1da2f2008-07-17 12:54:15 -04003588 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masone02119d2008-09-05 16:13:11 -04003589 mutex_init(&BTRFS_I(inode)->log_mutex);
3590}
3591
3592static int btrfs_init_locked_inode(struct inode *inode, void *p)
3593{
3594 struct btrfs_iget_args *args = p;
3595 inode->i_ino = args->ino;
3596 init_btrfs_i(inode);
3597 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003598 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003599 return 0;
3600}
3601
3602static int btrfs_find_actor(struct inode *inode, void *opaque)
3603{
3604 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003605 return args->ino == inode->i_ino &&
3606 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003607}
3608
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003609static struct inode *btrfs_iget_locked(struct super_block *s,
3610 u64 objectid,
3611 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003612{
3613 struct inode *inode;
3614 struct btrfs_iget_args args;
3615 args.ino = objectid;
3616 args.root = root;
3617
3618 inode = iget5_locked(s, objectid, btrfs_find_actor,
3619 btrfs_init_locked_inode,
3620 (void *)&args);
3621 return inode;
3622}
3623
Balaji Rao1a54ef82008-07-21 02:01:04 +05303624/* Get an inode object given its location and corresponding root.
3625 * Returns in *is_new if the inode was read from disk
3626 */
3627struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003628 struct btrfs_root *root)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303629{
3630 struct inode *inode;
3631
3632 inode = btrfs_iget_locked(s, location->objectid, root);
3633 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003634 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303635
3636 if (inode->i_state & I_NEW) {
3637 BTRFS_I(inode)->root = root;
3638 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3639 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003640
3641 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303642 unlock_new_inode(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303643 }
3644
3645 return inode;
3646}
3647
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003648static struct inode *new_simple_dir(struct super_block *s,
3649 struct btrfs_key *key,
3650 struct btrfs_root *root)
3651{
3652 struct inode *inode = new_inode(s);
3653
3654 if (!inode)
3655 return ERR_PTR(-ENOMEM);
3656
3657 init_btrfs_i(inode);
3658
3659 BTRFS_I(inode)->root = root;
3660 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3661 BTRFS_I(inode)->dummy_inode = 1;
3662
3663 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3664 inode->i_op = &simple_dir_inode_operations;
3665 inode->i_fop = &simple_dir_operations;
3666 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3667 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3668
3669 return inode;
3670}
3671
Chris Mason3de45862008-11-17 21:02:50 -05003672struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003673{
Chris Masond3977122009-01-05 21:25:51 -05003674 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003675 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003676 struct btrfs_root *sub_root = root;
3677 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003678 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003679 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003680
Yan, Zheng76dda932009-09-21 16:00:26 -04003681 dentry->d_op = &btrfs_dentry_operations;
3682
Chris Mason39279cc2007-06-12 06:35:45 -04003683 if (dentry->d_name.len > BTRFS_NAME_LEN)
3684 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003685
Chris Mason39279cc2007-06-12 06:35:45 -04003686 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003687
Chris Mason39279cc2007-06-12 06:35:45 -04003688 if (ret < 0)
3689 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003690
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003691 if (location.objectid == 0)
3692 return NULL;
3693
3694 if (location.type == BTRFS_INODE_ITEM_KEY) {
3695 inode = btrfs_iget(dir->i_sb, &location, root);
3696 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003697 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003698
3699 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3700
Yan, Zheng76dda932009-09-21 16:00:26 -04003701 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003702 ret = fixup_tree_root_location(root, dir, dentry,
3703 &location, &sub_root);
3704 if (ret < 0) {
3705 if (ret != -ENOENT)
3706 inode = ERR_PTR(ret);
3707 else
3708 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3709 } else {
Chris Mason39279cc2007-06-12 06:35:45 -04003710 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04003711 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003712 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3713
Yan, Zhengc71bf092009-11-12 09:34:40 +00003714 if (root != sub_root) {
3715 down_read(&root->fs_info->cleanup_work_sem);
3716 if (!(inode->i_sb->s_flags & MS_RDONLY))
3717 btrfs_orphan_cleanup(sub_root);
3718 up_read(&root->fs_info->cleanup_work_sem);
3719 }
3720
Chris Mason3de45862008-11-17 21:02:50 -05003721 return inode;
3722}
3723
Yan, Zheng76dda932009-09-21 16:00:26 -04003724static int btrfs_dentry_delete(struct dentry *dentry)
3725{
3726 struct btrfs_root *root;
3727
Yan, Zhengefefb142009-10-09 09:25:16 -04003728 if (!dentry->d_inode && !IS_ROOT(dentry))
3729 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04003730
Yan, Zhengefefb142009-10-09 09:25:16 -04003731 if (dentry->d_inode) {
3732 root = BTRFS_I(dentry->d_inode)->root;
3733 if (btrfs_root_refs(&root->root_item) == 0)
3734 return 1;
3735 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003736 return 0;
3737}
3738
Chris Mason3de45862008-11-17 21:02:50 -05003739static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3740 struct nameidata *nd)
3741{
3742 struct inode *inode;
3743
Chris Mason3de45862008-11-17 21:02:50 -05003744 inode = btrfs_lookup_dentry(dir, dentry);
3745 if (IS_ERR(inode))
3746 return ERR_CAST(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003747
Chris Mason39279cc2007-06-12 06:35:45 -04003748 return d_splice_alias(inode, dentry);
3749}
3750
3751static unsigned char btrfs_filetype_table[] = {
3752 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3753};
3754
David Woodhousecbdf5a22008-08-06 19:42:33 +01003755static int btrfs_real_readdir(struct file *filp, void *dirent,
3756 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003757{
3758 struct inode *inode = filp->f_dentry->d_inode;
3759 struct btrfs_root *root = BTRFS_I(inode)->root;
3760 struct btrfs_item *item;
3761 struct btrfs_dir_item *di;
3762 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003763 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003764 struct btrfs_path *path;
3765 int ret;
3766 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003767 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003768 int slot;
3769 int advance;
3770 unsigned char d_type;
3771 int over = 0;
3772 u32 di_cur;
3773 u32 di_total;
3774 u32 di_len;
3775 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003776 char tmp_name[32];
3777 char *name_ptr;
3778 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003779
3780 /* FIXME, use a real flag for deciding about the key type */
3781 if (root->fs_info->tree_root == root)
3782 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003783
Chris Mason39544012007-12-12 14:38:19 -05003784 /* special case for "." */
3785 if (filp->f_pos == 0) {
3786 over = filldir(dirent, ".", 1,
3787 1, inode->i_ino,
3788 DT_DIR);
3789 if (over)
3790 return 0;
3791 filp->f_pos = 1;
3792 }
Chris Mason39544012007-12-12 14:38:19 -05003793 /* special case for .., just use the back ref */
3794 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003795 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003796 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003797 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003798 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003799 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003800 filp->f_pos = 2;
3801 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003802 path = btrfs_alloc_path();
3803 path->reada = 2;
3804
Chris Mason39279cc2007-06-12 06:35:45 -04003805 btrfs_set_key_type(&key, key_type);
3806 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003807 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003808
Chris Mason39279cc2007-06-12 06:35:45 -04003809 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3810 if (ret < 0)
3811 goto err;
3812 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003813
3814 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003815 leaf = path->nodes[0];
3816 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003817 slot = path->slots[0];
3818 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003819 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003820 ret = btrfs_next_leaf(root, path);
3821 if (ret)
3822 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003823 leaf = path->nodes[0];
3824 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003825 slot = path->slots[0];
3826 } else {
3827 slot++;
3828 path->slots[0]++;
3829 }
3830 }
Chris Mason3de45862008-11-17 21:02:50 -05003831
Chris Mason39279cc2007-06-12 06:35:45 -04003832 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003833 item = btrfs_item_nr(leaf, slot);
3834 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3835
3836 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003837 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003838 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003839 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003840 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003841 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003842
3843 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003844
Chris Mason39279cc2007-06-12 06:35:45 -04003845 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3846 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003847 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003848
3849 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003850 struct btrfs_key location;
3851
3852 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003853 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003854 name_ptr = tmp_name;
3855 } else {
3856 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003857 if (!name_ptr) {
3858 ret = -ENOMEM;
3859 goto err;
3860 }
Chris Mason5f39d392007-10-15 16:14:19 -04003861 }
3862 read_extent_buffer(leaf, name_ptr,
3863 (unsigned long)(di + 1), name_len);
3864
3865 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3866 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003867
3868 /* is this a reference to our own snapshot? If so
3869 * skip it
3870 */
3871 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3872 location.objectid == root->root_key.objectid) {
3873 over = 0;
3874 goto skip;
3875 }
Chris Mason5f39d392007-10-15 16:14:19 -04003876 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003877 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003878 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003879
Chris Mason3de45862008-11-17 21:02:50 -05003880skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003881 if (name_ptr != tmp_name)
3882 kfree(name_ptr);
3883
Chris Mason39279cc2007-06-12 06:35:45 -04003884 if (over)
3885 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003886 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003887 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003888 di_cur += di_len;
3889 di = (struct btrfs_dir_item *)((char *)di + di_len);
3890 }
3891 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003892
3893 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003894 if (key_type == BTRFS_DIR_INDEX_KEY)
Chris Mason89f135d2009-01-28 15:34:27 -05003895 filp->f_pos = INT_LIMIT(off_t);
Yan Zheng5e591a02008-02-19 11:41:02 -05003896 else
3897 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003898nopos:
3899 ret = 0;
3900err:
Chris Mason39279cc2007-06-12 06:35:45 -04003901 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003902 return ret;
3903}
3904
3905int btrfs_write_inode(struct inode *inode, int wait)
3906{
3907 struct btrfs_root *root = BTRFS_I(inode)->root;
3908 struct btrfs_trans_handle *trans;
3909 int ret = 0;
3910
Yan Zhengc146afa2008-11-12 14:34:12 -05003911 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003912 return 0;
3913
Chris Mason39279cc2007-06-12 06:35:45 -04003914 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003915 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003916 btrfs_set_trans_block_group(trans, inode);
3917 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003918 }
3919 return ret;
3920}
3921
3922/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003923 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003924 * inode changes. But, it is most likely to find the inode in cache.
3925 * FIXME, needs more benchmarking...there are no reasons other than performance
3926 * to keep or drop this code.
3927 */
3928void btrfs_dirty_inode(struct inode *inode)
3929{
3930 struct btrfs_root *root = BTRFS_I(inode)->root;
3931 struct btrfs_trans_handle *trans;
3932
Chris Masonf9295742008-07-17 12:54:14 -04003933 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003934 btrfs_set_trans_block_group(trans, inode);
3935 btrfs_update_inode(trans, root, inode);
3936 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003937}
3938
Chris Masond352ac62008-09-29 15:18:18 -04003939/*
3940 * find the highest existing sequence number in a directory
3941 * and then set the in-memory index_cnt variable to reflect
3942 * free sequence numbers
3943 */
Josef Bacikaec74772008-07-24 12:12:38 -04003944static int btrfs_set_inode_index_count(struct inode *inode)
3945{
3946 struct btrfs_root *root = BTRFS_I(inode)->root;
3947 struct btrfs_key key, found_key;
3948 struct btrfs_path *path;
3949 struct extent_buffer *leaf;
3950 int ret;
3951
3952 key.objectid = inode->i_ino;
3953 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3954 key.offset = (u64)-1;
3955
3956 path = btrfs_alloc_path();
3957 if (!path)
3958 return -ENOMEM;
3959
3960 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3961 if (ret < 0)
3962 goto out;
3963 /* FIXME: we should be able to handle this */
3964 if (ret == 0)
3965 goto out;
3966 ret = 0;
3967
3968 /*
3969 * MAGIC NUMBER EXPLANATION:
3970 * since we search a directory based on f_pos we have to start at 2
3971 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3972 * else has to start at 2
3973 */
3974 if (path->slots[0] == 0) {
3975 BTRFS_I(inode)->index_cnt = 2;
3976 goto out;
3977 }
3978
3979 path->slots[0]--;
3980
3981 leaf = path->nodes[0];
3982 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3983
3984 if (found_key.objectid != inode->i_ino ||
3985 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3986 BTRFS_I(inode)->index_cnt = 2;
3987 goto out;
3988 }
3989
3990 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3991out:
3992 btrfs_free_path(path);
3993 return ret;
3994}
3995
Chris Masond352ac62008-09-29 15:18:18 -04003996/*
3997 * helper to find a free sequence number in a given directory. This current
3998 * code is very simple, later versions will do smarter things in the btree
3999 */
Chris Mason3de45862008-11-17 21:02:50 -05004000int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004001{
4002 int ret = 0;
4003
4004 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4005 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004006 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004007 return ret;
4008 }
4009
Chris Mason00e4e6b2008-08-05 11:18:09 -04004010 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004011 BTRFS_I(dir)->index_cnt++;
4012
4013 return ret;
4014}
4015
Chris Mason39279cc2007-06-12 06:35:45 -04004016static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4017 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004018 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004019 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004020 u64 ref_objectid, u64 objectid,
4021 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004022{
4023 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004024 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004025 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004026 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004027 struct btrfs_inode_ref *ref;
4028 struct btrfs_key key[2];
4029 u32 sizes[2];
4030 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004031 int ret;
4032 int owner;
4033
Chris Mason5f39d392007-10-15 16:14:19 -04004034 path = btrfs_alloc_path();
4035 BUG_ON(!path);
4036
Chris Mason39279cc2007-06-12 06:35:45 -04004037 inode = new_inode(root->fs_info->sb);
4038 if (!inode)
4039 return ERR_PTR(-ENOMEM);
4040
Josef Bacikaec74772008-07-24 12:12:38 -04004041 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004042 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004043 if (ret) {
4044 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004045 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004046 }
Josef Bacikaec74772008-07-24 12:12:38 -04004047 }
4048 /*
4049 * index_cnt is ignored for everything but a dir,
4050 * btrfs_get_inode_index_count has an explanation for the magic
4051 * number
4052 */
Chris Masone02119d2008-09-05 16:13:11 -04004053 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004054 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004055 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004056 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05004057 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004058
Chris Mason39279cc2007-06-12 06:35:45 -04004059 if (mode & S_IFDIR)
4060 owner = 0;
4061 else
4062 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004063 BTRFS_I(inode)->block_group =
4064 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004065
4066 key[0].objectid = objectid;
4067 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4068 key[0].offset = 0;
4069
4070 key[1].objectid = objectid;
4071 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4072 key[1].offset = ref_objectid;
4073
4074 sizes[0] = sizeof(struct btrfs_inode_item);
4075 sizes[1] = name_len + sizeof(*ref);
4076
Chris Masonb9473432009-03-13 11:00:37 -04004077 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004078 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4079 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004080 goto fail;
4081
Chris Mason79683f22008-11-19 22:00:53 -05004082 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05004083
Chris Mason42f15d72009-02-06 11:35:57 -05004084 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05004085 inode->i_gid = dir->i_gid;
4086 if (S_ISDIR(mode))
4087 mode |= S_ISGID;
4088 } else
4089 inode->i_gid = current_fsgid();
4090
Chris Mason39279cc2007-06-12 06:35:45 -04004091 inode->i_mode = mode;
4092 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004093 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004094 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004095 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4096 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004097 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004098
4099 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4100 struct btrfs_inode_ref);
4101 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004102 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004103 ptr = (unsigned long)(ref + 1);
4104 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4105
Chris Mason5f39d392007-10-15 16:14:19 -04004106 btrfs_mark_buffer_dirty(path->nodes[0]);
4107 btrfs_free_path(path);
4108
Chris Mason39279cc2007-06-12 06:35:45 -04004109 location = &BTRFS_I(inode)->location;
4110 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004111 location->offset = 0;
4112 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4113
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004114 btrfs_inherit_iflags(inode, dir);
4115
Chris Mason94272162009-07-02 12:26:06 -04004116 if ((mode & S_IFREG)) {
4117 if (btrfs_test_opt(root, NODATASUM))
4118 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4119 if (btrfs_test_opt(root, NODATACOW))
4120 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4121 }
4122
Chris Mason39279cc2007-06-12 06:35:45 -04004123 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004124 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004125 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004126fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004127 if (dir)
4128 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004129 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004130 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004131 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004132}
4133
4134static inline u8 btrfs_inode_type(struct inode *inode)
4135{
4136 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4137}
4138
Chris Masond352ac62008-09-29 15:18:18 -04004139/*
4140 * utility function to add 'inode' into 'parent_inode' with
4141 * a give name and a given sequence number.
4142 * if 'add_backref' is true, also insert a backref from the
4143 * inode to the parent directory.
4144 */
Chris Masone02119d2008-09-05 16:13:11 -04004145int btrfs_add_link(struct btrfs_trans_handle *trans,
4146 struct inode *parent_inode, struct inode *inode,
4147 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004148{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004149 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004150 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004151 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004152
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004153 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4154 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4155 } else {
4156 key.objectid = inode->i_ino;
4157 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4158 key.offset = 0;
4159 }
Chris Mason39279cc2007-06-12 06:35:45 -04004160
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004161 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4162 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4163 key.objectid, root->root_key.objectid,
4164 parent_inode->i_ino,
4165 index, name, name_len);
4166 } else if (add_backref) {
4167 ret = btrfs_insert_inode_ref(trans, root,
4168 name, name_len, inode->i_ino,
4169 parent_inode->i_ino, index);
4170 }
4171
Chris Mason39279cc2007-06-12 06:35:45 -04004172 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004173 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4174 parent_inode->i_ino, &key,
4175 btrfs_inode_type(inode), index);
4176 BUG_ON(ret);
4177
Chris Masondbe674a2008-07-17 12:54:05 -04004178 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004179 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004180 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004181 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004182 }
4183 return ret;
4184}
4185
4186static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004187 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004188 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004189{
Chris Masone02119d2008-09-05 16:13:11 -04004190 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4191 inode, dentry->d_name.name,
4192 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004193 if (!err) {
4194 d_instantiate(dentry, inode);
4195 return 0;
4196 }
4197 if (err > 0)
4198 err = -EEXIST;
4199 return err;
4200}
4201
Josef Bacik618e21d2007-07-11 10:18:17 -04004202static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4203 int mode, dev_t rdev)
4204{
4205 struct btrfs_trans_handle *trans;
4206 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004207 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004208 int err;
4209 int drop_inode = 0;
4210 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004211 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004212 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004213
4214 if (!new_valid_dev(rdev))
4215 return -EINVAL;
4216
Josef Bacik9ed74f22009-09-11 16:12:44 -04004217 /*
4218 * 2 for inode item and ref
4219 * 2 for dir items
4220 * 1 for xattr if selinux is on
4221 */
4222 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004223 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004224 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004225
Josef Bacik618e21d2007-07-11 10:18:17 -04004226 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004227 if (!trans)
4228 goto fail;
Josef Bacik618e21d2007-07-11 10:18:17 -04004229 btrfs_set_trans_block_group(trans, dir);
4230
4231 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4232 if (err) {
4233 err = -ENOSPC;
4234 goto out_unlock;
4235 }
4236
Josef Bacikaec74772008-07-24 12:12:38 -04004237 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004238 dentry->d_name.len,
4239 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004240 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004241 err = PTR_ERR(inode);
4242 if (IS_ERR(inode))
4243 goto out_unlock;
4244
Jim Owens0279b4c2009-02-04 09:29:13 -05004245 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004246 if (err) {
4247 drop_inode = 1;
4248 goto out_unlock;
4249 }
4250
Josef Bacik618e21d2007-07-11 10:18:17 -04004251 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004252 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004253 if (err)
4254 drop_inode = 1;
4255 else {
4256 inode->i_op = &btrfs_special_inode_operations;
4257 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004258 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004259 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004260 btrfs_update_inode_block_group(trans, inode);
4261 btrfs_update_inode_block_group(trans, dir);
4262out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004263 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004264 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004265fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004266 btrfs_unreserve_metadata_space(root, 5);
Josef Bacik618e21d2007-07-11 10:18:17 -04004267 if (drop_inode) {
4268 inode_dec_link_count(inode);
4269 iput(inode);
4270 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004271 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004272 return err;
4273}
4274
Chris Mason39279cc2007-06-12 06:35:45 -04004275static int btrfs_create(struct inode *dir, struct dentry *dentry,
4276 int mode, struct nameidata *nd)
4277{
4278 struct btrfs_trans_handle *trans;
4279 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004280 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004281 int err;
4282 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05004283 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004284 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004285 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004286
Josef Bacik9ed74f22009-09-11 16:12:44 -04004287 /*
4288 * 2 for inode item and ref
4289 * 2 for dir items
4290 * 1 for xattr if selinux is on
4291 */
4292 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004293 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004294 return err;
4295
Chris Mason39279cc2007-06-12 06:35:45 -04004296 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004297 if (!trans)
4298 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04004299 btrfs_set_trans_block_group(trans, dir);
4300
4301 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4302 if (err) {
4303 err = -ENOSPC;
4304 goto out_unlock;
4305 }
4306
Josef Bacikaec74772008-07-24 12:12:38 -04004307 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004308 dentry->d_name.len,
4309 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004310 objectid, BTRFS_I(dir)->block_group, mode,
4311 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004312 err = PTR_ERR(inode);
4313 if (IS_ERR(inode))
4314 goto out_unlock;
4315
Jim Owens0279b4c2009-02-04 09:29:13 -05004316 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004317 if (err) {
4318 drop_inode = 1;
4319 goto out_unlock;
4320 }
4321
Chris Mason39279cc2007-06-12 06:35:45 -04004322 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004323 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004324 if (err)
4325 drop_inode = 1;
4326 else {
4327 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004328 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004329 inode->i_fop = &btrfs_file_operations;
4330 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004331 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004332 }
Chris Mason39279cc2007-06-12 06:35:45 -04004333 btrfs_update_inode_block_group(trans, inode);
4334 btrfs_update_inode_block_group(trans, dir);
4335out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004336 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004337 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004338fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004339 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004340 if (drop_inode) {
4341 inode_dec_link_count(inode);
4342 iput(inode);
4343 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004344 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004345 return err;
4346}
4347
4348static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4349 struct dentry *dentry)
4350{
4351 struct btrfs_trans_handle *trans;
4352 struct btrfs_root *root = BTRFS_I(dir)->root;
4353 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004354 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004355 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004356 int err;
4357 int drop_inode = 0;
4358
4359 if (inode->i_nlink == 0)
4360 return -ENOENT;
4361
Josef Bacik9ed74f22009-09-11 16:12:44 -04004362 /*
4363 * 1 item for inode ref
4364 * 2 items for dir items
4365 */
4366 err = btrfs_reserve_metadata_space(root, 3);
Chris Mason1832a6d2007-12-21 16:27:21 -05004367 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004368 return err;
4369
4370 btrfs_inc_nlink(inode);
4371
Chris Mason3de45862008-11-17 21:02:50 -05004372 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004373 if (err)
4374 goto fail;
4375
Chris Mason39279cc2007-06-12 06:35:45 -04004376 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004377
Chris Mason39279cc2007-06-12 06:35:45 -04004378 btrfs_set_trans_block_group(trans, dir);
4379 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004380
Chris Mason00e4e6b2008-08-05 11:18:09 -04004381 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004382
Yan, Zhenga5719522009-09-24 09:17:31 -04004383 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004384 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004385 } else {
4386 btrfs_update_inode_block_group(trans, dir);
4387 err = btrfs_update_inode(trans, root, inode);
4388 BUG_ON(err);
4389 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4390 }
Chris Mason39279cc2007-06-12 06:35:45 -04004391
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004392 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004393 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004394fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004395 btrfs_unreserve_metadata_space(root, 3);
Chris Mason39279cc2007-06-12 06:35:45 -04004396 if (drop_inode) {
4397 inode_dec_link_count(inode);
4398 iput(inode);
4399 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004400 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004401 return err;
4402}
4403
Chris Mason39279cc2007-06-12 06:35:45 -04004404static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4405{
Chris Masonb9d86662008-05-02 16:13:49 -04004406 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004407 struct btrfs_trans_handle *trans;
4408 struct btrfs_root *root = BTRFS_I(dir)->root;
4409 int err = 0;
4410 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004411 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004412 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004413 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004414
Josef Bacik9ed74f22009-09-11 16:12:44 -04004415 /*
4416 * 2 items for inode and ref
4417 * 2 items for dir items
4418 * 1 for xattr if selinux is on
4419 */
4420 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05004421 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04004422 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004423
Chris Mason39279cc2007-06-12 06:35:45 -04004424 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004425 if (!trans) {
4426 err = -ENOMEM;
Chris Mason39279cc2007-06-12 06:35:45 -04004427 goto out_unlock;
4428 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04004429 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004430
4431 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4432 if (err) {
4433 err = -ENOSPC;
4434 goto out_unlock;
4435 }
4436
Josef Bacikaec74772008-07-24 12:12:38 -04004437 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004438 dentry->d_name.len,
4439 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004440 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4441 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004442 if (IS_ERR(inode)) {
4443 err = PTR_ERR(inode);
4444 goto out_fail;
4445 }
Chris Mason5f39d392007-10-15 16:14:19 -04004446
Chris Mason39279cc2007-06-12 06:35:45 -04004447 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004448
Jim Owens0279b4c2009-02-04 09:29:13 -05004449 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004450 if (err)
4451 goto out_fail;
4452
Chris Mason39279cc2007-06-12 06:35:45 -04004453 inode->i_op = &btrfs_dir_inode_operations;
4454 inode->i_fop = &btrfs_dir_file_operations;
4455 btrfs_set_trans_block_group(trans, inode);
4456
Chris Masondbe674a2008-07-17 12:54:05 -04004457 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004458 err = btrfs_update_inode(trans, root, inode);
4459 if (err)
4460 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004461
Chris Masone02119d2008-09-05 16:13:11 -04004462 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4463 inode, dentry->d_name.name,
4464 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004465 if (err)
4466 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004467
Chris Mason39279cc2007-06-12 06:35:45 -04004468 d_instantiate(dentry, inode);
4469 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004470 btrfs_update_inode_block_group(trans, inode);
4471 btrfs_update_inode_block_group(trans, dir);
4472
4473out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004474 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004475 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04004476
Chris Mason39279cc2007-06-12 06:35:45 -04004477out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04004478 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04004479 if (drop_on_err)
4480 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004481 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004482 return err;
4483}
4484
Chris Masond352ac62008-09-29 15:18:18 -04004485/* helper for btfs_get_extent. Given an existing extent in the tree,
4486 * and an extent that you want to insert, deal with overlap and insert
4487 * the new extent into the tree.
4488 */
Chris Mason3b951512008-04-17 11:29:12 -04004489static int merge_extent_mapping(struct extent_map_tree *em_tree,
4490 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004491 struct extent_map *em,
4492 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004493{
4494 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004495
Chris Masone6dcd2d2008-07-17 12:53:50 -04004496 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4497 start_diff = map_start - em->start;
4498 em->start = map_start;
4499 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004500 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4501 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004502 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004503 em->block_len -= start_diff;
4504 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004505 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004506}
4507
Chris Masonc8b97812008-10-29 14:49:59 -04004508static noinline int uncompress_inline(struct btrfs_path *path,
4509 struct inode *inode, struct page *page,
4510 size_t pg_offset, u64 extent_offset,
4511 struct btrfs_file_extent_item *item)
4512{
4513 int ret;
4514 struct extent_buffer *leaf = path->nodes[0];
4515 char *tmp;
4516 size_t max_size;
4517 unsigned long inline_size;
4518 unsigned long ptr;
4519
4520 WARN_ON(pg_offset != 0);
4521 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4522 inline_size = btrfs_file_extent_inline_item_len(leaf,
4523 btrfs_item_nr(leaf, path->slots[0]));
4524 tmp = kmalloc(inline_size, GFP_NOFS);
4525 ptr = btrfs_file_extent_inline_start(item);
4526
4527 read_extent_buffer(leaf, tmp, ptr, inline_size);
4528
Chris Mason5b050f02008-11-11 09:34:41 -05004529 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004530 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4531 inline_size, max_size);
4532 if (ret) {
4533 char *kaddr = kmap_atomic(page, KM_USER0);
4534 unsigned long copy_size = min_t(u64,
4535 PAGE_CACHE_SIZE - pg_offset,
4536 max_size - extent_offset);
4537 memset(kaddr + pg_offset, 0, copy_size);
4538 kunmap_atomic(kaddr, KM_USER0);
4539 }
4540 kfree(tmp);
4541 return 0;
4542}
4543
Chris Masond352ac62008-09-29 15:18:18 -04004544/*
4545 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004546 * the ugly parts come from merging extents from the disk with the in-ram
4547 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004548 * where the in-ram extents might be locked pending data=ordered completion.
4549 *
4550 * This also copies inline extents directly into the page.
4551 */
Chris Masond3977122009-01-05 21:25:51 -05004552
Chris Masona52d9a82007-08-27 16:49:44 -04004553struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004554 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004555 int create)
4556{
4557 int ret;
4558 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004559 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004560 u64 extent_start = 0;
4561 u64 extent_end = 0;
4562 u64 objectid = inode->i_ino;
4563 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004564 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004565 struct btrfs_root *root = BTRFS_I(inode)->root;
4566 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004567 struct extent_buffer *leaf;
4568 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004569 struct extent_map *em = NULL;
4570 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004571 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004572 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004573 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004574
Chris Masona52d9a82007-08-27 16:49:44 -04004575again:
Chris Mason890871b2009-09-02 16:24:52 -04004576 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004577 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004578 if (em)
4579 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004580 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004581
Chris Masona52d9a82007-08-27 16:49:44 -04004582 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004583 if (em->start > start || em->start + em->len <= start)
4584 free_extent_map(em);
4585 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004586 free_extent_map(em);
4587 else
4588 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004589 }
Chris Masond1310b22008-01-24 16:13:08 -05004590 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004591 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004592 err = -ENOMEM;
4593 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004594 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004595 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004596 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004597 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004598 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004599 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004600
4601 if (!path) {
4602 path = btrfs_alloc_path();
4603 BUG_ON(!path);
4604 }
4605
Chris Mason179e29e2007-11-01 11:28:41 -04004606 ret = btrfs_lookup_file_extent(trans, root, path,
4607 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004608 if (ret < 0) {
4609 err = ret;
4610 goto out;
4611 }
4612
4613 if (ret != 0) {
4614 if (path->slots[0] == 0)
4615 goto not_found;
4616 path->slots[0]--;
4617 }
4618
Chris Mason5f39d392007-10-15 16:14:19 -04004619 leaf = path->nodes[0];
4620 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004621 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004622 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004623 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4624 found_type = btrfs_key_type(&found_key);
4625 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004626 found_type != BTRFS_EXTENT_DATA_KEY) {
4627 goto not_found;
4628 }
4629
Chris Mason5f39d392007-10-15 16:14:19 -04004630 found_type = btrfs_file_extent_type(leaf, item);
4631 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004632 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004633 if (found_type == BTRFS_FILE_EXTENT_REG ||
4634 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004635 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004636 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004637 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4638 size_t size;
4639 size = btrfs_file_extent_inline_len(leaf, item);
4640 extent_end = (extent_start + size + root->sectorsize - 1) &
4641 ~((u64)root->sectorsize - 1);
4642 }
4643
4644 if (start >= extent_end) {
4645 path->slots[0]++;
4646 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4647 ret = btrfs_next_leaf(root, path);
4648 if (ret < 0) {
4649 err = ret;
4650 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004651 }
Yan Zheng9036c102008-10-30 14:19:41 -04004652 if (ret > 0)
4653 goto not_found;
4654 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004655 }
Yan Zheng9036c102008-10-30 14:19:41 -04004656 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4657 if (found_key.objectid != objectid ||
4658 found_key.type != BTRFS_EXTENT_DATA_KEY)
4659 goto not_found;
4660 if (start + len <= found_key.offset)
4661 goto not_found;
4662 em->start = start;
4663 em->len = found_key.offset - start;
4664 goto not_found_em;
4665 }
4666
Yan Zhengd899e052008-10-30 14:25:28 -04004667 if (found_type == BTRFS_FILE_EXTENT_REG ||
4668 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004669 em->start = extent_start;
4670 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004671 em->orig_start = extent_start -
4672 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004673 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4674 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004675 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004676 goto insert;
4677 }
Chris Masonc8b97812008-10-29 14:49:59 -04004678 if (compressed) {
4679 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4680 em->block_start = bytenr;
4681 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4682 item);
4683 } else {
4684 bytenr += btrfs_file_extent_offset(leaf, item);
4685 em->block_start = bytenr;
4686 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004687 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4688 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004689 }
Chris Masona52d9a82007-08-27 16:49:44 -04004690 goto insert;
4691 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004692 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004693 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004694 size_t size;
4695 size_t extent_offset;
4696 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004697
Chris Masona52d9a82007-08-27 16:49:44 -04004698 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004699 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004700 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004701 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004702 goto out;
4703 }
4704
Yan Zheng9036c102008-10-30 14:19:41 -04004705 size = btrfs_file_extent_inline_len(leaf, item);
4706 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004707 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004708 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004709 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004710 em->len = (copy_size + root->sectorsize - 1) &
4711 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004712 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004713 if (compressed)
4714 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004715 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004716 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004717 if (btrfs_file_extent_compression(leaf, item) ==
4718 BTRFS_COMPRESS_ZLIB) {
4719 ret = uncompress_inline(path, inode, page,
4720 pg_offset,
4721 extent_offset, item);
4722 BUG_ON(ret);
4723 } else {
4724 map = kmap(page);
4725 read_extent_buffer(leaf, map + pg_offset, ptr,
4726 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04004727 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4728 memset(map + pg_offset + copy_size, 0,
4729 PAGE_CACHE_SIZE - pg_offset -
4730 copy_size);
4731 }
Chris Masonc8b97812008-10-29 14:49:59 -04004732 kunmap(page);
4733 }
Chris Mason179e29e2007-11-01 11:28:41 -04004734 flush_dcache_page(page);
4735 } else if (create && PageUptodate(page)) {
4736 if (!trans) {
4737 kunmap(page);
4738 free_extent_map(em);
4739 em = NULL;
4740 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004741 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004742 goto again;
4743 }
Chris Masonc8b97812008-10-29 14:49:59 -04004744 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004745 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004746 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004747 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004748 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004749 }
Chris Masond1310b22008-01-24 16:13:08 -05004750 set_extent_uptodate(io_tree, em->start,
4751 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004752 goto insert;
4753 } else {
Chris Masond3977122009-01-05 21:25:51 -05004754 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004755 WARN_ON(1);
4756 }
4757not_found:
4758 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004759 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004760not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004761 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004762 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004763insert:
4764 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004765 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004766 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4767 "[%llu %llu]\n", (unsigned long long)em->start,
4768 (unsigned long long)em->len,
4769 (unsigned long long)start,
4770 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004771 err = -EIO;
4772 goto out;
4773 }
Chris Masond1310b22008-01-24 16:13:08 -05004774
4775 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04004776 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004777 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004778 /* it is possible that someone inserted the extent into the tree
4779 * while we had the lock dropped. It is also possible that
4780 * an overlapping map exists in the tree
4781 */
Chris Masona52d9a82007-08-27 16:49:44 -04004782 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004783 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004784
4785 ret = 0;
4786
Chris Mason3b951512008-04-17 11:29:12 -04004787 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004788 if (existing && (existing->start > start ||
4789 existing->start + existing->len <= start)) {
4790 free_extent_map(existing);
4791 existing = NULL;
4792 }
Chris Mason3b951512008-04-17 11:29:12 -04004793 if (!existing) {
4794 existing = lookup_extent_mapping(em_tree, em->start,
4795 em->len);
4796 if (existing) {
4797 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004798 em, start,
4799 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004800 free_extent_map(existing);
4801 if (err) {
4802 free_extent_map(em);
4803 em = NULL;
4804 }
4805 } else {
4806 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004807 free_extent_map(em);
4808 em = NULL;
4809 }
4810 } else {
4811 free_extent_map(em);
4812 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004813 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004814 }
Chris Masona52d9a82007-08-27 16:49:44 -04004815 }
Chris Mason890871b2009-09-02 16:24:52 -04004816 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004817out:
Chris Masonf4219502008-07-22 11:18:09 -04004818 if (path)
4819 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004820 if (trans) {
4821 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004822 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004823 err = ret;
4824 }
Chris Masona52d9a82007-08-27 16:49:44 -04004825 if (err) {
4826 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04004827 return ERR_PTR(err);
4828 }
4829 return em;
4830}
4831
Chris Mason16432982008-04-10 10:23:21 -04004832static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4833 const struct iovec *iov, loff_t offset,
4834 unsigned long nr_segs)
4835{
Chris Masone1c4b742008-04-22 13:26:46 -04004836 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004837}
4838
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004839static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4840 __u64 start, __u64 len)
4841{
4842 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4843}
4844
Chris Mason9ebefb182007-06-15 13:50:00 -04004845int btrfs_readpage(struct file *file, struct page *page)
4846{
Chris Masond1310b22008-01-24 16:13:08 -05004847 struct extent_io_tree *tree;
4848 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004849 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004850}
Chris Mason1832a6d2007-12-21 16:27:21 -05004851
Chris Mason39279cc2007-06-12 06:35:45 -04004852static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4853{
Chris Masond1310b22008-01-24 16:13:08 -05004854 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004855
4856
4857 if (current->flags & PF_MEMALLOC) {
4858 redirty_page_for_writepage(wbc, page);
4859 unlock_page(page);
4860 return 0;
4861 }
Chris Masond1310b22008-01-24 16:13:08 -05004862 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004863 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4864}
Chris Mason39279cc2007-06-12 06:35:45 -04004865
Chris Masonf4219502008-07-22 11:18:09 -04004866int btrfs_writepages(struct address_space *mapping,
4867 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004868{
Chris Masond1310b22008-01-24 16:13:08 -05004869 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004870
Chris Masond1310b22008-01-24 16:13:08 -05004871 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004872 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4873}
4874
Chris Mason3ab2fb52007-11-08 10:59:22 -05004875static int
4876btrfs_readpages(struct file *file, struct address_space *mapping,
4877 struct list_head *pages, unsigned nr_pages)
4878{
Chris Masond1310b22008-01-24 16:13:08 -05004879 struct extent_io_tree *tree;
4880 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004881 return extent_readpages(tree, mapping, pages, nr_pages,
4882 btrfs_get_extent);
4883}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004884static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004885{
Chris Masond1310b22008-01-24 16:13:08 -05004886 struct extent_io_tree *tree;
4887 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004888 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004889
Chris Masond1310b22008-01-24 16:13:08 -05004890 tree = &BTRFS_I(page->mapping->host)->io_tree;
4891 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004892 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004893 if (ret == 1) {
4894 ClearPagePrivate(page);
4895 set_page_private(page, 0);
4896 page_cache_release(page);
4897 }
4898 return ret;
4899}
Chris Mason39279cc2007-06-12 06:35:45 -04004900
Chris Masone6dcd2d2008-07-17 12:53:50 -04004901static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4902{
Chris Mason98509cf2008-09-11 15:51:43 -04004903 if (PageWriteback(page) || PageDirty(page))
4904 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05004905 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004906}
4907
Chris Masona52d9a82007-08-27 16:49:44 -04004908static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4909{
Chris Masond1310b22008-01-24 16:13:08 -05004910 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004911 struct btrfs_ordered_extent *ordered;
4912 u64 page_start = page_offset(page);
4913 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004914
Chris Mason8b62b722009-09-02 16:53:46 -04004915
4916 /*
4917 * we have the page locked, so new writeback can't start,
4918 * and the dirty bit won't be cleared while we are here.
4919 *
4920 * Wait for IO on this page so that we can safely clear
4921 * the PagePrivate2 bit and do ordered accounting
4922 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004923 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04004924
Chris Masond1310b22008-01-24 16:13:08 -05004925 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004926 if (offset) {
4927 btrfs_releasepage(page, GFP_NOFS);
4928 return;
4929 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004930 lock_extent(tree, page_start, page_end, GFP_NOFS);
4931 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4932 page_offset(page));
4933 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004934 /*
4935 * IO on this page will never be started, so we need
4936 * to account for any ordered extents now
4937 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004938 clear_extent_bit(tree, page_start, page_end,
4939 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04004940 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
4941 NULL, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04004942 /*
4943 * whoever cleared the private bit is responsible
4944 * for the finish_ordered_io
4945 */
4946 if (TestClearPagePrivate2(page)) {
4947 btrfs_finish_ordered_io(page->mapping->host,
4948 page_start, page_end);
4949 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004950 btrfs_put_ordered_extent(ordered);
4951 lock_extent(tree, page_start, page_end, GFP_NOFS);
4952 }
4953 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04004954 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4955 EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004956 __btrfs_releasepage(page, GFP_NOFS);
4957
Chris Mason4a096752008-07-21 10:29:44 -04004958 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004959 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004960 ClearPagePrivate(page);
4961 set_page_private(page, 0);
4962 page_cache_release(page);
4963 }
Chris Mason39279cc2007-06-12 06:35:45 -04004964}
4965
Chris Mason9ebefb182007-06-15 13:50:00 -04004966/*
4967 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4968 * called from a page fault handler when a page is first dirtied. Hence we must
4969 * be careful to check for EOF conditions here. We set the page up correctly
4970 * for a written page which means we get ENOSPC checking when writing into
4971 * holes and correct delalloc and unwritten extent mapping on filesystems that
4972 * support these features.
4973 *
4974 * We are not allowed to take the i_mutex here so we have to play games to
4975 * protect against truncate races as the page could now be beyond EOF. Because
4976 * vmtruncate() writes the inode size before removing pages, once we have the
4977 * page lock we can determine safely if the page is beyond EOF. If it is not
4978 * beyond EOF, then the page is guaranteed safe against truncation until we
4979 * unlock the page.
4980 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07004981int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04004982{
Nick Pigginc2ec1752009-03-31 15:23:21 -07004983 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05004984 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004985 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004986 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4987 struct btrfs_ordered_extent *ordered;
4988 char *kaddr;
4989 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04004990 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05004991 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04004992 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004993 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04004994
Josef Bacik6a632092009-02-20 11:00:09 -05004995 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07004996 if (ret) {
4997 if (ret == -ENOMEM)
4998 ret = VM_FAULT_OOM;
4999 else /* -ENOSPC, -EIO, etc */
5000 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05005001 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07005002 }
Chris Mason1832a6d2007-12-21 16:27:21 -05005003
Josef Bacik9ed74f22009-09-11 16:12:44 -04005004 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
5005 if (ret) {
5006 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5007 ret = VM_FAULT_SIGBUS;
5008 goto out;
5009 }
5010
Nick Piggin56a76f82009-03-31 15:23:23 -07005011 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005012again:
Chris Mason9ebefb182007-06-15 13:50:00 -04005013 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04005014 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005015 page_start = page_offset(page);
5016 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005017
Chris Mason9ebefb182007-06-15 13:50:00 -04005018 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04005019 (page_start >= size)) {
Josef Bacik6a632092009-02-20 11:00:09 -05005020 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason9ebefb182007-06-15 13:50:00 -04005021 /* page got truncated out from underneath us */
5022 goto out_unlock;
5023 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005024 wait_on_page_writeback(page);
5025
5026 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
5027 set_page_extent_mapped(page);
5028
Chris Masoneb84ae02008-07-17 13:53:27 -04005029 /*
5030 * we can't set the delalloc bits if there are pending ordered
5031 * extents. Drop our locks and wait for them to finish
5032 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005033 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5034 if (ordered) {
5035 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5036 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005037 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005038 btrfs_put_ordered_extent(ordered);
5039 goto again;
5040 }
5041
Josef Bacikfbf19082009-10-01 17:10:23 -04005042 /*
5043 * XXX - page_mkwrite gets called every time the page is dirtied, even
5044 * if it was already dirty, so for space accounting reasons we need to
5045 * clear any delalloc bits for the range we are fixing to save. There
5046 * is probably a better way to do this, but for now keep consistent with
5047 * prepare_pages in the normal write path.
5048 */
5049 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005050 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
5051 GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04005052
Josef Bacik9ed74f22009-09-11 16:12:44 -04005053 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
5054 if (ret) {
5055 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5056 ret = VM_FAULT_SIGBUS;
Josef Bacikfbf19082009-10-01 17:10:23 -04005057 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005058 goto out_unlock;
5059 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005060 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04005061
5062 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04005063 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005064 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04005065 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04005066 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04005067
Chris Masone6dcd2d2008-07-17 12:53:50 -04005068 if (zero_start != PAGE_CACHE_SIZE) {
5069 kaddr = kmap(page);
5070 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5071 flush_dcache_page(page);
5072 kunmap(page);
5073 }
Chris Mason247e7432008-07-17 12:53:51 -04005074 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005075 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04005076 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005077
Chris Mason257c62e2009-10-13 13:21:08 -04005078 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5079 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5080
Chris Masone6dcd2d2008-07-17 12:53:50 -04005081 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04005082
5083out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005084 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason50a9b212009-09-11 12:33:12 -04005085 if (!ret)
5086 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04005087 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05005088out:
Chris Mason9ebefb182007-06-15 13:50:00 -04005089 return ret;
5090}
5091
Chris Mason39279cc2007-06-12 06:35:45 -04005092static void btrfs_truncate(struct inode *inode)
5093{
5094 struct btrfs_root *root = BTRFS_I(inode)->root;
5095 int ret;
5096 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005097 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04005098 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005099
5100 if (!S_ISREG(inode->i_mode))
5101 return;
5102 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
5103 return;
5104
Josef Bacik5d5e1032009-10-13 16:46:49 -04005105 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5106 if (ret)
5107 return;
Chris Mason4a096752008-07-21 10:29:44 -04005108 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04005109
Chris Mason39279cc2007-06-12 06:35:45 -04005110 trans = btrfs_start_transaction(root, 1);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005111
5112 /*
5113 * setattr is responsible for setting the ordered_data_close flag,
5114 * but that is only tested during the last file release. That
5115 * could happen well after the next commit, leaving a great big
5116 * window where new writes may get lost if someone chooses to write
5117 * to this file after truncating to zero
5118 *
5119 * The inode doesn't have any dirty data here, and so if we commit
5120 * this is a noop. If someone immediately starts writing to the inode
5121 * it is very likely we'll catch some of their writes in this
5122 * transaction, and the commit will find this file on the ordered
5123 * data list with good things to send down.
5124 *
5125 * This is a best effort solution, there is still a window where
5126 * using truncate to replace the contents of the file will
5127 * end up with a zero length file after a crash.
5128 */
5129 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5130 btrfs_add_ordered_operation(trans, root, inode);
5131
Chris Mason39279cc2007-06-12 06:35:45 -04005132 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04005133 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04005134
Josef Bacik7b128762008-07-24 12:17:14 -04005135 ret = btrfs_orphan_add(trans, inode);
5136 if (ret)
5137 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005138 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04005139 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05005140 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04005141 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005142
Josef Bacik7b128762008-07-24 12:17:14 -04005143 ret = btrfs_orphan_del(trans, inode);
5144 BUG_ON(ret);
5145
5146out:
5147 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04005148 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005149 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005150 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005151}
5152
Sven Wegener3b963622008-06-09 21:57:42 -04005153/*
Chris Masond352ac62008-09-29 15:18:18 -04005154 * create a new subvolume directory/inode (helper for the ioctl).
5155 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05005156int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04005157 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005158 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04005159{
Chris Mason39279cc2007-06-12 06:35:45 -04005160 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005161 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005162 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005163
Josef Bacikaec74772008-07-24 12:12:38 -04005164 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005165 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04005166 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005167 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005168 inode->i_op = &btrfs_dir_inode_operations;
5169 inode->i_fop = &btrfs_dir_file_operations;
5170
Chris Mason39279cc2007-06-12 06:35:45 -04005171 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04005172 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04005173
Yan, Zheng76dda932009-09-21 16:00:26 -04005174 err = btrfs_update_inode(trans, new_root, inode);
5175 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005176
Yan, Zheng76dda932009-09-21 16:00:26 -04005177 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005178 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005179}
5180
Chris Masond352ac62008-09-29 15:18:18 -04005181/* helper function for file defrag and space balancing. This
5182 * forces readahead on a given range of bytes in an inode
5183 */
Chris Masonedbd8d42007-12-21 16:27:24 -05005184unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04005185 struct file_ra_state *ra, struct file *file,
5186 pgoff_t offset, pgoff_t last_index)
5187{
Chris Mason8e7bf942008-04-28 09:02:36 -04005188 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04005189
Chris Mason86479a02007-09-10 19:58:16 -04005190 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5191 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04005192}
5193
Chris Mason39279cc2007-06-12 06:35:45 -04005194struct inode *btrfs_alloc_inode(struct super_block *sb)
5195{
5196 struct btrfs_inode *ei;
5197
5198 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5199 if (!ei)
5200 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04005201 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04005202 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005203 ei->logged_trans = 0;
Josef Bacik32c00af2009-10-08 13:34:05 -04005204 ei->outstanding_extents = 0;
5205 ei->reserved_extents = 0;
Josef Bacika6dbd422009-11-11 15:53:34 -05005206 ei->root = NULL;
Josef Bacik32c00af2009-10-08 13:34:05 -04005207 spin_lock_init(&ei->accounting_lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005208 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04005209 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005210 INIT_LIST_HEAD(&ei->ordered_operations);
Chris Mason39279cc2007-06-12 06:35:45 -04005211 return &ei->vfs_inode;
5212}
5213
5214void btrfs_destroy_inode(struct inode *inode)
5215{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005216 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005217 struct btrfs_root *root = BTRFS_I(inode)->root;
5218
Chris Mason39279cc2007-06-12 06:35:45 -04005219 WARN_ON(!list_empty(&inode->i_dentry));
5220 WARN_ON(inode->i_data.nrpages);
5221
Chris Mason5a3f23d2009-03-31 13:27:11 -04005222 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05005223 * This can happen where we create an inode, but somebody else also
5224 * created the same inode and we need to destroy the one we already
5225 * created.
5226 */
5227 if (!root)
5228 goto free;
5229
5230 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005231 * Make sure we're properly removed from the ordered operation
5232 * lists.
5233 */
5234 smp_mb();
5235 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5236 spin_lock(&root->fs_info->ordered_extent_lock);
5237 list_del_init(&BTRFS_I(inode)->ordered_operations);
5238 spin_unlock(&root->fs_info->ordered_extent_lock);
5239 }
5240
5241 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005242 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
5243 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
5244 " list\n", inode->i_ino);
5245 dump_stack();
5246 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04005247 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005248
Chris Masond3977122009-01-05 21:25:51 -05005249 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005250 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5251 if (!ordered)
5252 break;
5253 else {
Chris Masond3977122009-01-05 21:25:51 -05005254 printk(KERN_ERR "btrfs found ordered "
5255 "extent %llu %llu on inode cleanup\n",
5256 (unsigned long long)ordered->file_offset,
5257 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005258 btrfs_remove_ordered_extent(inode, ordered);
5259 btrfs_put_ordered_extent(ordered);
5260 btrfs_put_ordered_extent(ordered);
5261 }
5262 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005263 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005264 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05005265free:
Chris Mason39279cc2007-06-12 06:35:45 -04005266 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5267}
5268
Yan, Zheng76dda932009-09-21 16:00:26 -04005269void btrfs_drop_inode(struct inode *inode)
5270{
5271 struct btrfs_root *root = BTRFS_I(inode)->root;
5272
5273 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5274 generic_delete_inode(inode);
5275 else
5276 generic_drop_inode(inode);
5277}
5278
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005279static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04005280{
5281 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5282
5283 inode_init_once(&ei->vfs_inode);
5284}
5285
5286void btrfs_destroy_cachep(void)
5287{
5288 if (btrfs_inode_cachep)
5289 kmem_cache_destroy(btrfs_inode_cachep);
5290 if (btrfs_trans_handle_cachep)
5291 kmem_cache_destroy(btrfs_trans_handle_cachep);
5292 if (btrfs_transaction_cachep)
5293 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04005294 if (btrfs_path_cachep)
5295 kmem_cache_destroy(btrfs_path_cachep);
5296}
5297
5298int btrfs_init_cachep(void)
5299{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005300 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5301 sizeof(struct btrfs_inode), 0,
5302 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04005303 if (!btrfs_inode_cachep)
5304 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005305
5306 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5307 sizeof(struct btrfs_trans_handle), 0,
5308 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005309 if (!btrfs_trans_handle_cachep)
5310 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005311
5312 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5313 sizeof(struct btrfs_transaction), 0,
5314 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005315 if (!btrfs_transaction_cachep)
5316 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005317
5318 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5319 sizeof(struct btrfs_path), 0,
5320 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005321 if (!btrfs_path_cachep)
5322 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005323
Chris Mason39279cc2007-06-12 06:35:45 -04005324 return 0;
5325fail:
5326 btrfs_destroy_cachep();
5327 return -ENOMEM;
5328}
5329
5330static int btrfs_getattr(struct vfsmount *mnt,
5331 struct dentry *dentry, struct kstat *stat)
5332{
5333 struct inode *inode = dentry->d_inode;
5334 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05005335 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05005336 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005337 stat->blocks = (inode_get_bytes(inode) +
5338 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04005339 return 0;
5340}
5341
Chris Masond3977122009-01-05 21:25:51 -05005342static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5343 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005344{
5345 struct btrfs_trans_handle *trans;
5346 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005347 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005348 struct inode *new_inode = new_dentry->d_inode;
5349 struct inode *old_inode = old_dentry->d_inode;
5350 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005351 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005352 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005353 int ret;
5354
Yan, Zhengf679a842009-09-24 09:17:31 -04005355 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5356 return -EPERM;
5357
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005358 /* we only allow rename subvolume link between subvolumes */
5359 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05005360 return -EXDEV;
5361
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005362 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5363 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5364 return -ENOTEMPTY;
5365
Chris Mason39279cc2007-06-12 06:35:45 -04005366 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005367 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04005368 return -ENOTEMPTY;
Chris Mason0660b5a2008-11-17 20:37:39 -05005369
Josef Bacik9ed74f22009-09-11 16:12:44 -04005370 /*
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005371 * We want to reserve the absolute worst case amount of items. So if
5372 * both inodes are subvols and we need to unlink them then that would
5373 * require 4 item modifications, but if they are both normal inodes it
5374 * would require 5 item modifications, so we'll assume their normal
5375 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5376 * should cover the worst case number of items we'll modify.
Josef Bacik9ed74f22009-09-11 16:12:44 -04005377 */
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005378 ret = btrfs_reserve_metadata_space(root, 11);
Chris Mason1832a6d2007-12-21 16:27:21 -05005379 if (ret)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005380 return ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05005381
Chris Mason5a3f23d2009-03-31 13:27:11 -04005382 /*
5383 * we're using rename to replace one file with another.
5384 * and the replacement file is large. Start IO on it now so
5385 * we don't add too much work to the end of the transaction
5386 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04005387 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04005388 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5389 filemap_flush(old_inode->i_mapping);
5390
Yan, Zheng76dda932009-09-21 16:00:26 -04005391 /* close the racy window with snapshot create/destroy ioctl */
5392 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5393 down_read(&root->fs_info->subvol_sem);
Chris Mason5f39d392007-10-15 16:14:19 -04005394
Chris Mason39279cc2007-06-12 06:35:45 -04005395 trans = btrfs_start_transaction(root, 1);
Yan, Zhenga5719522009-09-24 09:17:31 -04005396 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04005397
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005398 if (dest != root)
5399 btrfs_record_root_in_trans(trans, dest);
5400
Yan, Zhenga5719522009-09-24 09:17:31 -04005401 ret = btrfs_set_inode_index(new_dir, &index);
5402 if (ret)
5403 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005404
Yan, Zhenga5719522009-09-24 09:17:31 -04005405 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005406 /* force full log commit if subvolume involved. */
5407 root->fs_info->last_trans_log_full_commit = trans->transid;
5408 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04005409 ret = btrfs_insert_inode_ref(trans, dest,
5410 new_dentry->d_name.name,
5411 new_dentry->d_name.len,
5412 old_inode->i_ino,
5413 new_dir->i_ino, index);
5414 if (ret)
5415 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005416 /*
5417 * this is an ugly little race, but the rename is required
5418 * to make sure that if we crash, the inode is either at the
5419 * old name or the new one. pinning the log transaction lets
5420 * us make sure we don't allow a log commit to come in after
5421 * we unlink the name but before we add the new name back in.
5422 */
5423 btrfs_pin_log_trans(root);
5424 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005425 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005426 * make sure the inode gets flushed if it is replacing
5427 * something.
5428 */
5429 if (new_inode && new_inode->i_size &&
5430 old_inode && S_ISREG(old_inode->i_mode)) {
5431 btrfs_add_ordered_operation(trans, root, old_inode);
5432 }
5433
Chris Mason39279cc2007-06-12 06:35:45 -04005434 old_dir->i_ctime = old_dir->i_mtime = ctime;
5435 new_dir->i_ctime = new_dir->i_mtime = ctime;
5436 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04005437
Chris Mason12fcfd22009-03-24 10:24:20 -04005438 if (old_dentry->d_parent != new_dentry->d_parent)
5439 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5440
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005441 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5442 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5443 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5444 old_dentry->d_name.name,
5445 old_dentry->d_name.len);
5446 } else {
5447 btrfs_inc_nlink(old_dentry->d_inode);
5448 ret = btrfs_unlink_inode(trans, root, old_dir,
5449 old_dentry->d_inode,
5450 old_dentry->d_name.name,
5451 old_dentry->d_name.len);
5452 }
5453 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005454
5455 if (new_inode) {
5456 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005457 if (unlikely(new_inode->i_ino ==
5458 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5459 root_objectid = BTRFS_I(new_inode)->location.objectid;
5460 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5461 root_objectid,
5462 new_dentry->d_name.name,
5463 new_dentry->d_name.len);
5464 BUG_ON(new_inode->i_nlink == 0);
5465 } else {
5466 ret = btrfs_unlink_inode(trans, dest, new_dir,
5467 new_dentry->d_inode,
5468 new_dentry->d_name.name,
5469 new_dentry->d_name.len);
5470 }
5471 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005472 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04005473 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005474 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005475 }
Chris Mason39279cc2007-06-12 06:35:45 -04005476 }
Josef Bacikaec74772008-07-24 12:12:38 -04005477
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005478 ret = btrfs_add_link(trans, new_dir, old_inode,
5479 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04005480 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005481 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005482
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005483 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5484 btrfs_log_new_name(trans, old_inode, old_dir,
5485 new_dentry->d_parent);
5486 btrfs_end_log_trans(root);
5487 }
Chris Mason39279cc2007-06-12 06:35:45 -04005488out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04005489 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005490
Yan, Zheng76dda932009-09-21 16:00:26 -04005491 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5492 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005493
Josef Bacik5df6a9f2009-11-10 21:23:48 -05005494 btrfs_unreserve_metadata_space(root, 11);
Chris Mason39279cc2007-06-12 06:35:45 -04005495 return ret;
5496}
5497
Chris Masond352ac62008-09-29 15:18:18 -04005498/*
5499 * some fairly slow code that needs optimization. This walks the list
5500 * of all the inodes with pending delalloc and forces them to disk.
5501 */
Chris Masonea8c2812008-08-04 23:17:27 -04005502int btrfs_start_delalloc_inodes(struct btrfs_root *root)
5503{
5504 struct list_head *head = &root->fs_info->delalloc_inodes;
5505 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005506 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04005507
Yan Zhengc146afa2008-11-12 14:34:12 -05005508 if (root->fs_info->sb->s_flags & MS_RDONLY)
5509 return -EROFS;
5510
Chris Mason75eff682008-12-15 15:54:40 -05005511 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05005512 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04005513 binode = list_entry(head->next, struct btrfs_inode,
5514 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005515 inode = igrab(&binode->vfs_inode);
5516 if (!inode)
5517 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05005518 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005519 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005520 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005521 iput(inode);
5522 }
5523 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05005524 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04005525 }
Chris Mason75eff682008-12-15 15:54:40 -05005526 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04005527
5528 /* the filemap_flush will queue IO into the worker threads, but
5529 * we have to make sure the IO is actually started and that
5530 * ordered extents get created before we return
5531 */
5532 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05005533 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05005534 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005535 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05005536 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5537 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04005538 }
5539 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04005540 return 0;
5541}
5542
Chris Mason39279cc2007-06-12 06:35:45 -04005543static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5544 const char *symname)
5545{
5546 struct btrfs_trans_handle *trans;
5547 struct btrfs_root *root = BTRFS_I(dir)->root;
5548 struct btrfs_path *path;
5549 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05005550 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005551 int err;
5552 int drop_inode = 0;
5553 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005554 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04005555 int name_len;
5556 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04005557 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005558 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04005559 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05005560 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005561
5562 name_len = strlen(symname) + 1;
5563 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5564 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05005565
Josef Bacik9ed74f22009-09-11 16:12:44 -04005566 /*
5567 * 2 items for inode item and ref
5568 * 2 items for dir items
5569 * 1 item for xattr if selinux is on
5570 */
5571 err = btrfs_reserve_metadata_space(root, 5);
Chris Mason1832a6d2007-12-21 16:27:21 -05005572 if (err)
Josef Bacik9ed74f22009-09-11 16:12:44 -04005573 return err;
Chris Mason1832a6d2007-12-21 16:27:21 -05005574
Chris Mason39279cc2007-06-12 06:35:45 -04005575 trans = btrfs_start_transaction(root, 1);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005576 if (!trans)
5577 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04005578 btrfs_set_trans_block_group(trans, dir);
5579
5580 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5581 if (err) {
5582 err = -ENOSPC;
5583 goto out_unlock;
5584 }
5585
Josef Bacikaec74772008-07-24 12:12:38 -04005586 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05005587 dentry->d_name.len,
5588 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04005589 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5590 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005591 err = PTR_ERR(inode);
5592 if (IS_ERR(inode))
5593 goto out_unlock;
5594
Jim Owens0279b4c2009-02-04 09:29:13 -05005595 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04005596 if (err) {
5597 drop_inode = 1;
5598 goto out_unlock;
5599 }
5600
Chris Mason39279cc2007-06-12 06:35:45 -04005601 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005602 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005603 if (err)
5604 drop_inode = 1;
5605 else {
5606 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005607 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04005608 inode->i_fop = &btrfs_file_operations;
5609 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05005610 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04005611 }
Chris Mason39279cc2007-06-12 06:35:45 -04005612 btrfs_update_inode_block_group(trans, inode);
5613 btrfs_update_inode_block_group(trans, dir);
5614 if (drop_inode)
5615 goto out_unlock;
5616
5617 path = btrfs_alloc_path();
5618 BUG_ON(!path);
5619 key.objectid = inode->i_ino;
5620 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005621 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5622 datasize = btrfs_file_extent_calc_inline_size(name_len);
5623 err = btrfs_insert_empty_item(trans, root, path, &key,
5624 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005625 if (err) {
5626 drop_inode = 1;
5627 goto out_unlock;
5628 }
Chris Mason5f39d392007-10-15 16:14:19 -04005629 leaf = path->nodes[0];
5630 ei = btrfs_item_ptr(leaf, path->slots[0],
5631 struct btrfs_file_extent_item);
5632 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5633 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04005634 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04005635 btrfs_set_file_extent_encryption(leaf, ei, 0);
5636 btrfs_set_file_extent_compression(leaf, ei, 0);
5637 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5638 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5639
Chris Mason39279cc2007-06-12 06:35:45 -04005640 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04005641 write_extent_buffer(leaf, symname, ptr, name_len);
5642 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04005643 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04005644
Chris Mason39279cc2007-06-12 06:35:45 -04005645 inode->i_op = &btrfs_symlink_inode_operations;
5646 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04005647 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04005648 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04005649 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005650 err = btrfs_update_inode(trans, root, inode);
5651 if (err)
5652 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005653
5654out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005655 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04005656 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05005657out_fail:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005658 btrfs_unreserve_metadata_space(root, 5);
Chris Mason39279cc2007-06-12 06:35:45 -04005659 if (drop_inode) {
5660 inode_dec_link_count(inode);
5661 iput(inode);
5662 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005663 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005664 return err;
5665}
Chris Mason16432982008-04-10 10:23:21 -04005666
Yan, Zheng5a303d52009-11-12 09:34:52 +00005667static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00005668 u64 alloc_hint, int mode)
Yan Zhengd899e052008-10-30 14:25:28 -04005669{
Yan, Zheng5a303d52009-11-12 09:34:52 +00005670 struct btrfs_trans_handle *trans;
Yan Zhengd899e052008-10-30 14:25:28 -04005671 struct btrfs_root *root = BTRFS_I(inode)->root;
5672 struct btrfs_key ins;
5673 u64 alloc_size;
5674 u64 cur_offset = start;
5675 u64 num_bytes = end - start;
5676 int ret = 0;
5677
Yan Zhengd899e052008-10-30 14:25:28 -04005678 while (num_bytes > 0) {
5679 alloc_size = min(num_bytes, root->fs_info->max_extent);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005680
Yan Zhengd899e052008-10-30 14:25:28 -04005681 ret = btrfs_reserve_extent(trans, root, alloc_size,
5682 root->sectorsize, 0, alloc_hint,
5683 (u64)-1, &ins, 1);
5684 if (ret) {
5685 WARN_ON(1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005686 break;
Yan Zhengd899e052008-10-30 14:25:28 -04005687 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00005688
5689 ret = btrfs_reserve_metadata_space(root, 3);
5690 if (ret) {
5691 btrfs_free_reserved_extent(root, ins.objectid,
5692 ins.offset);
5693 break;
5694 }
5695
5696 trans = btrfs_start_transaction(root, 1);
5697
Yan Zhengd899e052008-10-30 14:25:28 -04005698 ret = insert_reserved_file_extent(trans, inode,
5699 cur_offset, ins.objectid,
5700 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00005701 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04005702 BTRFS_FILE_EXTENT_PREALLOC);
5703 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04005704 btrfs_drop_extent_cache(inode, cur_offset,
5705 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005706
Yan Zhengd899e052008-10-30 14:25:28 -04005707 num_bytes -= ins.offset;
5708 cur_offset += ins.offset;
5709 alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005710
Yan Zhengd899e052008-10-30 14:25:28 -04005711 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005712 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04005713 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zheng5a303d52009-11-12 09:34:52 +00005714 cur_offset > inode->i_size) {
5715 i_size_write(inode, cur_offset);
5716 btrfs_ordered_update_i_size(inode, cur_offset, NULL);
5717 }
5718
Yan Zhengd899e052008-10-30 14:25:28 -04005719 ret = btrfs_update_inode(trans, root, inode);
5720 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04005721
Yan, Zheng5a303d52009-11-12 09:34:52 +00005722 btrfs_end_transaction(trans, root);
5723 btrfs_unreserve_metadata_space(root, 3);
5724 }
Yan Zhengd899e052008-10-30 14:25:28 -04005725 return ret;
5726}
5727
5728static long btrfs_fallocate(struct inode *inode, int mode,
5729 loff_t offset, loff_t len)
5730{
5731 u64 cur_offset;
5732 u64 last_byte;
5733 u64 alloc_start;
5734 u64 alloc_end;
5735 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04005736 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04005737 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5738 struct extent_map *em;
5739 int ret;
5740
5741 alloc_start = offset & ~mask;
5742 alloc_end = (offset + len + mask) & ~mask;
5743
Chris Mason546888d2009-04-21 11:53:38 -04005744 /*
5745 * wait for ordered IO before we have any locks. We'll loop again
5746 * below with the locks held.
5747 */
5748 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5749
Yan Zhengd899e052008-10-30 14:25:28 -04005750 mutex_lock(&inode->i_mutex);
5751 if (alloc_start > inode->i_size) {
5752 ret = btrfs_cont_expand(inode, alloc_start);
5753 if (ret)
5754 goto out;
5755 }
5756
Yan, Zheng5a303d52009-11-12 09:34:52 +00005757 ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode,
Josef Bacika970b0a2009-06-27 21:07:34 -04005758 alloc_end - alloc_start);
5759 if (ret)
5760 goto out;
5761
Chris Masone980b502009-04-24 14:39:24 -04005762 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04005763 while (1) {
5764 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04005765
Chris Mason546888d2009-04-21 11:53:38 -04005766 /* the extent lock is ordered inside the running
5767 * transaction
5768 */
Chris Masone980b502009-04-24 14:39:24 -04005769 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5770 GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04005771 ordered = btrfs_lookup_first_ordered_extent(inode,
5772 alloc_end - 1);
5773 if (ordered &&
5774 ordered->file_offset + ordered->len > alloc_start &&
5775 ordered->file_offset < alloc_end) {
5776 btrfs_put_ordered_extent(ordered);
5777 unlock_extent(&BTRFS_I(inode)->io_tree,
Chris Masone980b502009-04-24 14:39:24 -04005778 alloc_start, locked_end, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005779 /*
5780 * we can't wait on the range with the transaction
5781 * running or with the extent lock held
5782 */
Yan Zhengd899e052008-10-30 14:25:28 -04005783 btrfs_wait_ordered_range(inode, alloc_start,
5784 alloc_end - alloc_start);
5785 } else {
5786 if (ordered)
5787 btrfs_put_ordered_extent(ordered);
5788 break;
5789 }
5790 }
5791
5792 cur_offset = alloc_start;
5793 while (1) {
5794 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5795 alloc_end - cur_offset, 0);
5796 BUG_ON(IS_ERR(em) || !em);
5797 last_byte = min(extent_map_end(em), alloc_end);
5798 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005799 if (em->block_start == EXTENT_MAP_HOLE ||
5800 (cur_offset >= inode->i_size &&
5801 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5802 ret = prealloc_file_range(inode,
5803 cur_offset, last_byte,
5804 alloc_hint, mode);
Yan Zhengd899e052008-10-30 14:25:28 -04005805 if (ret < 0) {
5806 free_extent_map(em);
5807 break;
5808 }
5809 }
5810 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5811 alloc_hint = em->block_start;
5812 free_extent_map(em);
5813
5814 cur_offset = last_byte;
5815 if (cur_offset >= alloc_end) {
5816 ret = 0;
5817 break;
5818 }
5819 }
Chris Masone980b502009-04-24 14:39:24 -04005820 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
Yan Zhengd899e052008-10-30 14:25:28 -04005821 GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005822
Yan, Zheng5a303d52009-11-12 09:34:52 +00005823 btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode,
5824 alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04005825out:
5826 mutex_unlock(&inode->i_mutex);
5827 return ret;
5828}
5829
Chris Masone6dcd2d2008-07-17 12:53:50 -04005830static int btrfs_set_page_dirty(struct page *page)
5831{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005832 return __set_page_dirty_nobuffers(page);
5833}
5834
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005835static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05005836{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005837 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05005838 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04005839 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05005840}
Chris Mason39279cc2007-06-12 06:35:45 -04005841
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005842static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05005843 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005844 .lookup = btrfs_lookup,
5845 .create = btrfs_create,
5846 .unlink = btrfs_unlink,
5847 .link = btrfs_link,
5848 .mkdir = btrfs_mkdir,
5849 .rmdir = btrfs_rmdir,
5850 .rename = btrfs_rename,
5851 .symlink = btrfs_symlink,
5852 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005853 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005854 .setxattr = btrfs_setxattr,
5855 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005856 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005857 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005858 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005859};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005860static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005861 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05005862 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005863};
Yan, Zheng76dda932009-09-21 16:00:26 -04005864
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005865static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005866 .llseek = generic_file_llseek,
5867 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01005868 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005869 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005870#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005871 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005872#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04005873 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04005874 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04005875};
5876
Chris Masond1310b22008-01-24 16:13:08 -05005877static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04005878 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05005879 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04005880 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005881 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005882 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04005883 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04005884 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05005885 .set_bit_hook = btrfs_set_bit_hook,
5886 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04005887 .merge_extent_hook = btrfs_merge_extent_hook,
5888 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005889};
5890
Chris Mason35054392009-01-21 13:11:13 -05005891/*
5892 * btrfs doesn't support the bmap operation because swapfiles
5893 * use bmap to make a mapping of extents in the file. They assume
5894 * these extents won't change over the life of the file and they
5895 * use the bmap result to do IO directly to the drive.
5896 *
5897 * the btrfs bmap call would return logical addresses that aren't
5898 * suitable for IO and they also will change frequently as COW
5899 * operations happen. So, swapfile + btrfs == corruption.
5900 *
5901 * For now we're avoiding this by dropping bmap.
5902 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07005903static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04005904 .readpage = btrfs_readpage,
5905 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04005906 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05005907 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005908 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04005909 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005910 .invalidatepage = btrfs_invalidatepage,
5911 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005912 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02005913 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04005914};
5915
Alexey Dobriyan7f094102009-09-21 17:01:10 -07005916static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04005917 .readpage = btrfs_readpage,
5918 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04005919 .invalidatepage = btrfs_invalidatepage,
5920 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04005921};
5922
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005923static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005924 .truncate = btrfs_truncate,
5925 .getattr = btrfs_getattr,
5926 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005927 .setxattr = btrfs_setxattr,
5928 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005929 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005930 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005931 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04005932 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005933 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04005934};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005935static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04005936 .getattr = btrfs_getattr,
5937 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05005938 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005939 .setxattr = btrfs_setxattr,
5940 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04005941 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005942 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005943};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07005944static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04005945 .readlink = generic_readlink,
5946 .follow_link = page_follow_link_light,
5947 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05005948 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05005949 .setxattr = btrfs_setxattr,
5950 .getxattr = btrfs_getxattr,
5951 .listxattr = btrfs_listxattr,
5952 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005953};
Yan, Zheng76dda932009-09-21 16:00:26 -04005954
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04005955const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04005956 .d_delete = btrfs_dentry_delete,
5957};