blob: c4b0fd12df680ccb245ed252c83b4d7217ff17d0 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050040#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040041#include "ctree.h"
42#include "disk-io.h"
43#include "transaction.h"
44#include "btrfs_inode.h"
45#include "ioctl.h"
46#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040047#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040048#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040049#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040050#include "tree-log.h"
Chris Masonc8b97812008-10-29 14:49:59 -040051#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050052#include "locking.h"
Chris Mason39279cc2007-06-12 06:35:45 -040053
54struct btrfs_iget_args {
55 u64 ino;
56 struct btrfs_root *root;
57};
58
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070059static const struct inode_operations btrfs_dir_inode_operations;
60static const struct inode_operations btrfs_symlink_inode_operations;
61static const struct inode_operations btrfs_dir_ro_inode_operations;
62static const struct inode_operations btrfs_special_inode_operations;
63static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070064static const struct address_space_operations btrfs_aops;
65static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070066static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050067static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040068
69static struct kmem_cache *btrfs_inode_cachep;
70struct kmem_cache *btrfs_trans_handle_cachep;
71struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040072struct kmem_cache *btrfs_path_cachep;
73
74#define S_SHIFT 12
75static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
76 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
77 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
78 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
79 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
80 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
81 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
82 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
83};
84
Josef Bacik7b128762008-07-24 12:17:14 -040085static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040086static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050087static noinline int cow_file_range(struct inode *inode,
88 struct page *locked_page,
89 u64 start, u64 end, int *page_started,
90 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040091
Yan, Zhengf34f57a2009-11-12 09:35:27 +000092static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
93 struct inode *inode, struct inode *dir)
Jim Owens0279b4c2009-02-04 09:29:13 -050094{
95 int err;
96
Yan, Zhengf34f57a2009-11-12 09:35:27 +000097 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -050098 if (!err)
Yan, Zhengf34f57a2009-11-12 09:35:27 +000099 err = btrfs_xattr_security_init(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500100 return err;
101}
102
Chris Masond352ac62008-09-29 15:18:18 -0400103/*
Chris Masonc8b97812008-10-29 14:49:59 -0400104 * this does all the hard work for inserting an inline extent into
105 * the btree. The caller should have done a btrfs_drop_extents so that
106 * no overlapping inline items exist in the btree
107 */
Chris Masond3977122009-01-05 21:25:51 -0500108static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400109 struct btrfs_root *root, struct inode *inode,
110 u64 start, size_t size, size_t compressed_size,
111 struct page **compressed_pages)
112{
113 struct btrfs_key key;
114 struct btrfs_path *path;
115 struct extent_buffer *leaf;
116 struct page *page = NULL;
117 char *kaddr;
118 unsigned long ptr;
119 struct btrfs_file_extent_item *ei;
120 int err = 0;
121 int ret;
122 size_t cur_size = size;
123 size_t datasize;
124 unsigned long offset;
125 int use_compress = 0;
126
127 if (compressed_size && compressed_pages) {
128 use_compress = 1;
129 cur_size = compressed_size;
130 }
131
Chris Masond3977122009-01-05 21:25:51 -0500132 path = btrfs_alloc_path();
133 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400134 return -ENOMEM;
135
Chris Masonb9473432009-03-13 11:00:37 -0400136 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400137 btrfs_set_trans_block_group(trans, inode);
138
139 key.objectid = inode->i_ino;
140 key.offset = start;
141 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400142 datasize = btrfs_file_extent_calc_inline_size(cur_size);
143
144 inode_add_bytes(inode, size);
145 ret = btrfs_insert_empty_item(trans, root, path, &key,
146 datasize);
147 BUG_ON(ret);
148 if (ret) {
149 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400150 goto fail;
151 }
152 leaf = path->nodes[0];
153 ei = btrfs_item_ptr(leaf, path->slots[0],
154 struct btrfs_file_extent_item);
155 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
156 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
157 btrfs_set_file_extent_encryption(leaf, ei, 0);
158 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
159 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
160 ptr = btrfs_file_extent_inline_start(ei);
161
162 if (use_compress) {
163 struct page *cpage;
164 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500165 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400166 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500167 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400168 PAGE_CACHE_SIZE);
169
Chris Masonb9473432009-03-13 11:00:37 -0400170 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400171 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400172 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400173
174 i++;
175 ptr += cur_size;
176 compressed_size -= cur_size;
177 }
178 btrfs_set_file_extent_compression(leaf, ei,
179 BTRFS_COMPRESS_ZLIB);
180 } else {
181 page = find_get_page(inode->i_mapping,
182 start >> PAGE_CACHE_SHIFT);
183 btrfs_set_file_extent_compression(leaf, ei, 0);
184 kaddr = kmap_atomic(page, KM_USER0);
185 offset = start & (PAGE_CACHE_SIZE - 1);
186 write_extent_buffer(leaf, kaddr + offset, ptr, size);
187 kunmap_atomic(kaddr, KM_USER0);
188 page_cache_release(page);
189 }
190 btrfs_mark_buffer_dirty(leaf);
191 btrfs_free_path(path);
192
Yan, Zhengc2167752009-11-12 09:34:21 +0000193 /*
194 * we're an inline extent, so nobody can
195 * extend the file past i_size without locking
196 * a page we already have locked.
197 *
198 * We must do any isize and inode updates
199 * before we unlock the pages. Otherwise we
200 * could end up racing with unlink.
201 */
Chris Masonc8b97812008-10-29 14:49:59 -0400202 BTRFS_I(inode)->disk_i_size = inode->i_size;
203 btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000204
Chris Masonc8b97812008-10-29 14:49:59 -0400205 return 0;
206fail:
207 btrfs_free_path(path);
208 return err;
209}
210
211
212/*
213 * conditionally insert an inline extent into the file. This
214 * does the checks required to make sure the data is small enough
215 * to fit as an inline extent.
216 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400217static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400218 struct btrfs_root *root,
219 struct inode *inode, u64 start, u64 end,
220 size_t compressed_size,
221 struct page **compressed_pages)
222{
223 u64 isize = i_size_read(inode);
224 u64 actual_end = min(end + 1, isize);
225 u64 inline_len = actual_end - start;
226 u64 aligned_end = (end + root->sectorsize - 1) &
227 ~((u64)root->sectorsize - 1);
228 u64 hint_byte;
229 u64 data_len = inline_len;
230 int ret;
231
232 if (compressed_size)
233 data_len = compressed_size;
234
235 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400236 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400237 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
238 (!compressed_size &&
239 (actual_end & (root->sectorsize - 1)) == 0) ||
240 end + 1 < isize ||
241 data_len > root->fs_info->max_inline) {
242 return 1;
243 }
244
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000245 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400246 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 BUG_ON(ret);
248
249 if (isize > actual_end)
250 inline_len = min_t(u64, isize, actual_end);
251 ret = insert_inline_extent(trans, root, inode, start,
252 inline_len, compressed_size,
253 compressed_pages);
254 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -0400255 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400256 return 0;
257}
258
Chris Mason771ed682008-11-06 22:02:51 -0500259struct async_extent {
260 u64 start;
261 u64 ram_size;
262 u64 compressed_size;
263 struct page **pages;
264 unsigned long nr_pages;
265 struct list_head list;
266};
267
268struct async_cow {
269 struct inode *inode;
270 struct btrfs_root *root;
271 struct page *locked_page;
272 u64 start;
273 u64 end;
274 struct list_head extents;
275 struct btrfs_work work;
276};
277
278static noinline int add_async_extent(struct async_cow *cow,
279 u64 start, u64 ram_size,
280 u64 compressed_size,
281 struct page **pages,
282 unsigned long nr_pages)
283{
284 struct async_extent *async_extent;
285
286 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
287 async_extent->start = start;
288 async_extent->ram_size = ram_size;
289 async_extent->compressed_size = compressed_size;
290 async_extent->pages = pages;
291 async_extent->nr_pages = nr_pages;
292 list_add_tail(&async_extent->list, &cow->extents);
293 return 0;
294}
295
Chris Masonc8b97812008-10-29 14:49:59 -0400296/*
Chris Mason771ed682008-11-06 22:02:51 -0500297 * we create compressed extents in two phases. The first
298 * phase compresses a range of pages that have already been
299 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400300 *
Chris Mason771ed682008-11-06 22:02:51 -0500301 * This is done inside an ordered work queue, and the compression
302 * is spread across many cpus. The actual IO submission is step
303 * two, and the ordered work queue takes care of making sure that
304 * happens in the same order things were put onto the queue by
305 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400306 *
Chris Mason771ed682008-11-06 22:02:51 -0500307 * If this code finds it can't get good compression, it puts an
308 * entry onto the work queue to write the uncompressed bytes. This
309 * makes sure that both compressed inodes and uncompressed inodes
310 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400311 */
Chris Mason771ed682008-11-06 22:02:51 -0500312static noinline int compress_file_range(struct inode *inode,
313 struct page *locked_page,
314 u64 start, u64 end,
315 struct async_cow *async_cow,
316 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400317{
318 struct btrfs_root *root = BTRFS_I(inode)->root;
319 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400320 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400321 u64 orig_start;
322 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400323 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400324 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500325 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400326 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400327 struct page **pages = NULL;
328 unsigned long nr_pages;
329 unsigned long nr_pages_ret = 0;
330 unsigned long total_compressed = 0;
331 unsigned long total_in = 0;
332 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500333 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400334 int i;
335 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400336
Chris Masonc8b97812008-10-29 14:49:59 -0400337 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500338
Chris Mason42dc7ba2008-12-15 11:44:56 -0500339 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400340again:
341 will_compress = 0;
342 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
343 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
344
Chris Masonf03d9302009-02-04 09:31:06 -0500345 /*
346 * we don't want to send crud past the end of i_size through
347 * compression, that's just a waste of CPU time. So, if the
348 * end of the file is before the start of our current
349 * requested range of bytes, we bail out to the uncompressed
350 * cleanup code that can deal with all of this.
351 *
352 * It isn't really the fastest way to fix things, but this is a
353 * very uncommon corner.
354 */
355 if (actual_end <= start)
356 goto cleanup_and_bail_uncompressed;
357
Chris Masonc8b97812008-10-29 14:49:59 -0400358 total_compressed = actual_end - start;
359
360 /* we want to make sure that amount of ram required to uncompress
361 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500362 * of a compressed extent to 128k. This is a crucial number
363 * because it also controls how easily we can spread reads across
364 * cpus for decompression.
365 *
366 * We also want to make sure the amount of IO required to do
367 * a random read is reasonably small, so we limit the size of
368 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400369 */
370 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400371 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500372 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400373 disk_num_bytes = num_bytes;
374 total_in = 0;
375 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400376
Chris Mason771ed682008-11-06 22:02:51 -0500377 /*
378 * we do compression for mount -o compress and when the
379 * inode has not been flagged as nocompress. This flag can
380 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400381 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200382 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500383 (btrfs_test_opt(root, COMPRESS) ||
384 (BTRFS_I(inode)->force_compress))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400385 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400386 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400387
Chris Masonc8b97812008-10-29 14:49:59 -0400388 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
389 total_compressed, pages,
390 nr_pages, &nr_pages_ret,
391 &total_in,
392 &total_compressed,
393 max_compressed);
394
395 if (!ret) {
396 unsigned long offset = total_compressed &
397 (PAGE_CACHE_SIZE - 1);
398 struct page *page = pages[nr_pages_ret - 1];
399 char *kaddr;
400
401 /* zero the tail end of the last page, we might be
402 * sending it down to disk
403 */
404 if (offset) {
405 kaddr = kmap_atomic(page, KM_USER0);
406 memset(kaddr + offset, 0,
407 PAGE_CACHE_SIZE - offset);
408 kunmap_atomic(kaddr, KM_USER0);
409 }
410 will_compress = 1;
411 }
412 }
413 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500414 trans = btrfs_join_transaction(root, 1);
415 BUG_ON(!trans);
416 btrfs_set_trans_block_group(trans, inode);
417
Chris Masonc8b97812008-10-29 14:49:59 -0400418 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500419 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400420 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500421 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400422 */
423 ret = cow_file_range_inline(trans, root, inode,
424 start, end, 0, NULL);
425 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500426 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400427 ret = cow_file_range_inline(trans, root, inode,
428 start, end,
429 total_compressed, pages);
430 }
431 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500432 /*
433 * inline extent creation worked, we don't need
434 * to create any more async work items. Unlock
435 * and free up our temp pages.
436 */
Chris Masonc8b97812008-10-29 14:49:59 -0400437 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400438 &BTRFS_I(inode)->io_tree,
439 start, end, NULL,
440 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400441 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400442 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400443 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000444
445 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400446 goto free_pages_out;
447 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000448 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400449 }
450
451 if (will_compress) {
452 /*
453 * we aren't doing an inline extent round the compressed size
454 * up to a block size boundary so the allocator does sane
455 * things
456 */
457 total_compressed = (total_compressed + blocksize - 1) &
458 ~(blocksize - 1);
459
460 /*
461 * one last check to make sure the compression is really a
462 * win, compare the page count read with the blocks on disk
463 */
464 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
465 ~(PAGE_CACHE_SIZE - 1);
466 if (total_compressed >= total_in) {
467 will_compress = 0;
468 } else {
469 disk_num_bytes = total_compressed;
470 num_bytes = total_in;
471 }
472 }
473 if (!will_compress && pages) {
474 /*
475 * the compression code ran but failed to make things smaller,
476 * free any pages it allocated and our page pointer array
477 */
478 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400479 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400480 page_cache_release(pages[i]);
481 }
482 kfree(pages);
483 pages = NULL;
484 total_compressed = 0;
485 nr_pages_ret = 0;
486
487 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500488 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
489 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500490 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500491 }
Chris Masonc8b97812008-10-29 14:49:59 -0400492 }
Chris Mason771ed682008-11-06 22:02:51 -0500493 if (will_compress) {
494 *num_added += 1;
495
496 /* the async work queues will take care of doing actual
497 * allocation on disk for these compressed pages,
498 * and will submit them to the elevator.
499 */
500 add_async_extent(async_cow, start, num_bytes,
501 total_compressed, pages, nr_pages_ret);
502
Chris Mason42dc7ba2008-12-15 11:44:56 -0500503 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500504 start += num_bytes;
505 pages = NULL;
506 cond_resched();
507 goto again;
508 }
509 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500510cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500511 /*
512 * No compression, but we still need to write the pages in
513 * the file we've been given so far. redirty the locked
514 * page if it corresponds to our extent and set things up
515 * for the async work queue to run cow_file_range to do
516 * the normal delalloc dance
517 */
518 if (page_offset(locked_page) >= start &&
519 page_offset(locked_page) <= end) {
520 __set_page_dirty_nobuffers(locked_page);
521 /* unlocked later on in the async handlers */
522 }
523 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
524 *num_added += 1;
525 }
526
527out:
528 return 0;
529
530free_pages_out:
531 for (i = 0; i < nr_pages_ret; i++) {
532 WARN_ON(pages[i]->mapping);
533 page_cache_release(pages[i]);
534 }
Chris Masond3977122009-01-05 21:25:51 -0500535 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500536
537 goto out;
538}
539
540/*
541 * phase two of compressed writeback. This is the ordered portion
542 * of the code, which only gets called in the order the work was
543 * queued. We walk all the async extents created by compress_file_range
544 * and send them down to the disk.
545 */
546static noinline int submit_compressed_extents(struct inode *inode,
547 struct async_cow *async_cow)
548{
549 struct async_extent *async_extent;
550 u64 alloc_hint = 0;
551 struct btrfs_trans_handle *trans;
552 struct btrfs_key ins;
553 struct extent_map *em;
554 struct btrfs_root *root = BTRFS_I(inode)->root;
555 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
556 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500557 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500558
559 if (list_empty(&async_cow->extents))
560 return 0;
561
Chris Mason771ed682008-11-06 22:02:51 -0500562
Chris Masond3977122009-01-05 21:25:51 -0500563 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500564 async_extent = list_entry(async_cow->extents.next,
565 struct async_extent, list);
566 list_del(&async_extent->list);
567
568 io_tree = &BTRFS_I(inode)->io_tree;
569
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500570retry:
Chris Mason771ed682008-11-06 22:02:51 -0500571 /* did the compression code fall back to uncompressed IO? */
572 if (!async_extent->pages) {
573 int page_started = 0;
574 unsigned long nr_written = 0;
575
576 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000577 async_extent->start +
578 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500579
580 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500581 ret = cow_file_range(inode, async_cow->locked_page,
582 async_extent->start,
583 async_extent->start +
584 async_extent->ram_size - 1,
585 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500586
587 /*
588 * if page_started, cow_file_range inserted an
589 * inline extent and took care of all the unlocking
590 * and IO for us. Otherwise, we need to submit
591 * all those pages down to the drive.
592 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500593 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500594 extent_write_locked_range(io_tree,
595 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500596 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500597 async_extent->ram_size - 1,
598 btrfs_get_extent,
599 WB_SYNC_ALL);
600 kfree(async_extent);
601 cond_resched();
602 continue;
603 }
604
605 lock_extent(io_tree, async_extent->start,
606 async_extent->start + async_extent->ram_size - 1,
607 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500608
Yan, Zhengc2167752009-11-12 09:34:21 +0000609 trans = btrfs_join_transaction(root, 1);
Chris Mason771ed682008-11-06 22:02:51 -0500610 ret = btrfs_reserve_extent(trans, root,
611 async_extent->compressed_size,
612 async_extent->compressed_size,
613 0, alloc_hint,
614 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000615 btrfs_end_transaction(trans, root);
616
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500617 if (ret) {
618 int i;
619 for (i = 0; i < async_extent->nr_pages; i++) {
620 WARN_ON(async_extent->pages[i]->mapping);
621 page_cache_release(async_extent->pages[i]);
622 }
623 kfree(async_extent->pages);
624 async_extent->nr_pages = 0;
625 async_extent->pages = NULL;
626 unlock_extent(io_tree, async_extent->start,
627 async_extent->start +
628 async_extent->ram_size - 1, GFP_NOFS);
629 goto retry;
630 }
631
Yan, Zhengc2167752009-11-12 09:34:21 +0000632 /*
633 * here we're doing allocation and writeback of the
634 * compressed pages
635 */
636 btrfs_drop_extent_cache(inode, async_extent->start,
637 async_extent->start +
638 async_extent->ram_size - 1, 0);
639
Chris Mason771ed682008-11-06 22:02:51 -0500640 em = alloc_extent_map(GFP_NOFS);
641 em->start = async_extent->start;
642 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500643 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500644
645 em->block_start = ins.objectid;
646 em->block_len = ins.offset;
647 em->bdev = root->fs_info->fs_devices->latest_bdev;
648 set_bit(EXTENT_FLAG_PINNED, &em->flags);
649 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
650
Chris Masond3977122009-01-05 21:25:51 -0500651 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400652 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500653 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400654 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500655 if (ret != -EEXIST) {
656 free_extent_map(em);
657 break;
658 }
659 btrfs_drop_extent_cache(inode, async_extent->start,
660 async_extent->start +
661 async_extent->ram_size - 1, 0);
662 }
663
664 ret = btrfs_add_ordered_extent(inode, async_extent->start,
665 ins.objectid,
666 async_extent->ram_size,
667 ins.offset,
668 BTRFS_ORDERED_COMPRESSED);
669 BUG_ON(ret);
670
Chris Mason771ed682008-11-06 22:02:51 -0500671 /*
672 * clear dirty, set writeback and unlock the pages.
673 */
674 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400675 &BTRFS_I(inode)->io_tree,
676 async_extent->start,
677 async_extent->start +
678 async_extent->ram_size - 1,
679 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
680 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400681 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400682 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500683
684 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500685 async_extent->start,
686 async_extent->ram_size,
687 ins.objectid,
688 ins.offset, async_extent->pages,
689 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500690
691 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500692 alloc_hint = ins.objectid + ins.offset;
693 kfree(async_extent);
694 cond_resched();
695 }
696
Chris Mason771ed682008-11-06 22:02:51 -0500697 return 0;
698}
699
700/*
701 * when extent_io.c finds a delayed allocation range in the file,
702 * the call backs end up in this code. The basic idea is to
703 * allocate extents on disk for the range, and create ordered data structs
704 * in ram to track those extents.
705 *
706 * locked_page is the page that writepage had locked already. We use
707 * it to make sure we don't do extra locks or unlocks.
708 *
709 * *page_started is set to one if we unlock locked_page and do everything
710 * required to start IO on it. It may be clean and already done with
711 * IO when we return.
712 */
713static noinline int cow_file_range(struct inode *inode,
714 struct page *locked_page,
715 u64 start, u64 end, int *page_started,
716 unsigned long *nr_written,
717 int unlock)
718{
719 struct btrfs_root *root = BTRFS_I(inode)->root;
720 struct btrfs_trans_handle *trans;
721 u64 alloc_hint = 0;
722 u64 num_bytes;
723 unsigned long ram_size;
724 u64 disk_num_bytes;
725 u64 cur_alloc_size;
726 u64 blocksize = root->sectorsize;
727 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500728 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500729 struct btrfs_key ins;
730 struct extent_map *em;
731 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
732 int ret = 0;
733
734 trans = btrfs_join_transaction(root, 1);
735 BUG_ON(!trans);
736 btrfs_set_trans_block_group(trans, inode);
737
Chris Mason42dc7ba2008-12-15 11:44:56 -0500738 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500739
740 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
741 num_bytes = max(blocksize, num_bytes);
742 disk_num_bytes = num_bytes;
743 ret = 0;
744
745 if (start == 0) {
746 /* lets try to make an inline extent */
747 ret = cow_file_range_inline(trans, root, inode,
748 start, end, 0, NULL);
749 if (ret == 0) {
750 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400751 &BTRFS_I(inode)->io_tree,
752 start, end, NULL,
753 EXTENT_CLEAR_UNLOCK_PAGE |
754 EXTENT_CLEAR_UNLOCK |
755 EXTENT_CLEAR_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -0400756 EXTENT_CLEAR_ACCOUNTING |
Chris Masona791e352009-10-08 11:27:10 -0400757 EXTENT_CLEAR_DIRTY |
758 EXTENT_SET_WRITEBACK |
759 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000760
Chris Mason771ed682008-11-06 22:02:51 -0500761 *nr_written = *nr_written +
762 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
763 *page_started = 1;
764 ret = 0;
765 goto out;
766 }
767 }
Chris Masonc8b97812008-10-29 14:49:59 -0400768
769 BUG_ON(disk_num_bytes >
770 btrfs_super_total_bytes(&root->fs_info->super_copy));
771
Chris Masonb917b7c2009-09-18 16:07:03 -0400772
773 read_lock(&BTRFS_I(inode)->extent_tree.lock);
774 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
775 start, num_bytes);
776 if (em) {
Josef Bacik6346c932009-11-10 21:23:47 -0500777 /*
778 * if block start isn't an actual block number then find the
779 * first block in this inode and use that as a hint. If that
780 * block is also bogus then just don't worry about it.
781 */
782 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
783 free_extent_map(em);
784 em = search_extent_mapping(em_tree, 0, 0);
785 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
786 alloc_hint = em->block_start;
787 if (em)
788 free_extent_map(em);
789 } else {
790 alloc_hint = em->block_start;
791 free_extent_map(em);
792 }
Chris Masonb917b7c2009-09-18 16:07:03 -0400793 }
794 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400795 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400796
Chris Masond3977122009-01-05 21:25:51 -0500797 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400798 unsigned long op;
799
Josef Bacik287a0ab2010-03-19 18:07:23 +0000800 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400801 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500802 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400803 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500804 BUG_ON(ret);
805
Chris Masone6dcd2d2008-07-17 12:53:50 -0400806 em = alloc_extent_map(GFP_NOFS);
807 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500808 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500809 ram_size = ins.offset;
810 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400811
Chris Masone6dcd2d2008-07-17 12:53:50 -0400812 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400813 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400814 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400815 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400816
Chris Masond3977122009-01-05 21:25:51 -0500817 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400818 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400819 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400820 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400821 if (ret != -EEXIST) {
822 free_extent_map(em);
823 break;
824 }
825 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400826 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400827 }
828
Chris Mason98d20f62008-04-14 09:46:10 -0400829 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400830 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500831 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400832 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400833
Yan Zheng17d217f2008-12-12 10:03:38 -0500834 if (root->root_key.objectid ==
835 BTRFS_DATA_RELOC_TREE_OBJECTID) {
836 ret = btrfs_reloc_clone_csums(inode, start,
837 cur_alloc_size);
838 BUG_ON(ret);
839 }
840
Chris Masond3977122009-01-05 21:25:51 -0500841 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400842 break;
Chris Masond3977122009-01-05 21:25:51 -0500843
Chris Masonc8b97812008-10-29 14:49:59 -0400844 /* we're not doing compressed IO, don't unlock the first
845 * page (which the caller expects to stay locked), don't
846 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400847 *
848 * Do set the Private2 bit so we know this page was properly
849 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400850 */
Chris Masona791e352009-10-08 11:27:10 -0400851 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
852 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
853 EXTENT_SET_PRIVATE2;
854
Chris Masonc8b97812008-10-29 14:49:59 -0400855 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
856 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400857 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400858 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500859 num_bytes -= cur_alloc_size;
860 alloc_hint = ins.objectid + ins.offset;
861 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400862 }
Chris Masonb888db22007-08-27 16:49:44 -0400863out:
Chris Mason771ed682008-11-06 22:02:51 -0500864 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400865 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400866
Chris Masonbe20aa92007-12-17 20:14:01 -0500867 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500868}
Chris Masonc8b97812008-10-29 14:49:59 -0400869
Chris Mason771ed682008-11-06 22:02:51 -0500870/*
871 * work queue call back to started compression on a file and pages
872 */
873static noinline void async_cow_start(struct btrfs_work *work)
874{
875 struct async_cow *async_cow;
876 int num_added = 0;
877 async_cow = container_of(work, struct async_cow, work);
878
879 compress_file_range(async_cow->inode, async_cow->locked_page,
880 async_cow->start, async_cow->end, async_cow,
881 &num_added);
882 if (num_added == 0)
883 async_cow->inode = NULL;
884}
885
886/*
887 * work queue call back to submit previously compressed pages
888 */
889static noinline void async_cow_submit(struct btrfs_work *work)
890{
891 struct async_cow *async_cow;
892 struct btrfs_root *root;
893 unsigned long nr_pages;
894
895 async_cow = container_of(work, struct async_cow, work);
896
897 root = async_cow->root;
898 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
899 PAGE_CACHE_SHIFT;
900
901 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
902
903 if (atomic_read(&root->fs_info->async_delalloc_pages) <
904 5 * 1042 * 1024 &&
905 waitqueue_active(&root->fs_info->async_submit_wait))
906 wake_up(&root->fs_info->async_submit_wait);
907
Chris Masond3977122009-01-05 21:25:51 -0500908 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500909 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500910}
Chris Masonc8b97812008-10-29 14:49:59 -0400911
Chris Mason771ed682008-11-06 22:02:51 -0500912static noinline void async_cow_free(struct btrfs_work *work)
913{
914 struct async_cow *async_cow;
915 async_cow = container_of(work, struct async_cow, work);
916 kfree(async_cow);
917}
918
919static int cow_file_range_async(struct inode *inode, struct page *locked_page,
920 u64 start, u64 end, int *page_started,
921 unsigned long *nr_written)
922{
923 struct async_cow *async_cow;
924 struct btrfs_root *root = BTRFS_I(inode)->root;
925 unsigned long nr_pages;
926 u64 cur_end;
927 int limit = 10 * 1024 * 1042;
928
Chris Masona3429ab2009-10-08 12:30:20 -0400929 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
930 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500931 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500932 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
933 async_cow->inode = inode;
934 async_cow->root = root;
935 async_cow->locked_page = locked_page;
936 async_cow->start = start;
937
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200938 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500939 cur_end = end;
940 else
941 cur_end = min(end, start + 512 * 1024 - 1);
942
943 async_cow->end = cur_end;
944 INIT_LIST_HEAD(&async_cow->extents);
945
946 async_cow->work.func = async_cow_start;
947 async_cow->work.ordered_func = async_cow_submit;
948 async_cow->work.ordered_free = async_cow_free;
949 async_cow->work.flags = 0;
950
Chris Mason771ed682008-11-06 22:02:51 -0500951 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
952 PAGE_CACHE_SHIFT;
953 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
954
955 btrfs_queue_worker(&root->fs_info->delalloc_workers,
956 &async_cow->work);
957
958 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
959 wait_event(root->fs_info->async_submit_wait,
960 (atomic_read(&root->fs_info->async_delalloc_pages) <
961 limit));
962 }
963
Chris Masond3977122009-01-05 21:25:51 -0500964 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500965 atomic_read(&root->fs_info->async_delalloc_pages)) {
966 wait_event(root->fs_info->async_submit_wait,
967 (atomic_read(&root->fs_info->async_delalloc_pages) ==
968 0));
969 }
970
971 *nr_written += nr_pages;
972 start = cur_end + 1;
973 }
974 *page_started = 1;
975 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500976}
977
Chris Masond3977122009-01-05 21:25:51 -0500978static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500979 u64 bytenr, u64 num_bytes)
980{
981 int ret;
982 struct btrfs_ordered_sum *sums;
983 LIST_HEAD(list);
984
Yan Zheng07d400a2009-01-06 11:42:00 -0500985 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
986 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500987 if (ret == 0 && list_empty(&list))
988 return 0;
989
990 while (!list_empty(&list)) {
991 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
992 list_del(&sums->list);
993 kfree(sums);
994 }
995 return 1;
996}
997
Chris Masond352ac62008-09-29 15:18:18 -0400998/*
999 * when nowcow writeback call back. This checks for snapshots or COW copies
1000 * of the extents that exist in the file, and COWs the file as required.
1001 *
1002 * If no cow copies or snapshots exist, we write directly to the existing
1003 * blocks on disk
1004 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001005static noinline int run_delalloc_nocow(struct inode *inode,
1006 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001007 u64 start, u64 end, int *page_started, int force,
1008 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001009{
Chris Masonbe20aa92007-12-17 20:14:01 -05001010 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001011 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001012 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001013 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001014 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001015 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001016 u64 cow_start;
1017 u64 cur_offset;
1018 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001019 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001020 u64 disk_bytenr;
1021 u64 num_bytes;
1022 int extent_type;
1023 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001024 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001025 int nocow;
1026 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -05001027
1028 path = btrfs_alloc_path();
1029 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001030 trans = btrfs_join_transaction(root, 1);
1031 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -05001032
Yan Zheng80ff3852008-10-30 14:20:02 -04001033 cow_start = (u64)-1;
1034 cur_offset = start;
1035 while (1) {
1036 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1037 cur_offset, 0);
1038 BUG_ON(ret < 0);
1039 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1040 leaf = path->nodes[0];
1041 btrfs_item_key_to_cpu(leaf, &found_key,
1042 path->slots[0] - 1);
1043 if (found_key.objectid == inode->i_ino &&
1044 found_key.type == BTRFS_EXTENT_DATA_KEY)
1045 path->slots[0]--;
1046 }
1047 check_prev = 0;
1048next_slot:
1049 leaf = path->nodes[0];
1050 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1051 ret = btrfs_next_leaf(root, path);
1052 if (ret < 0)
1053 BUG_ON(1);
1054 if (ret > 0)
1055 break;
1056 leaf = path->nodes[0];
1057 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001058
Yan Zheng80ff3852008-10-30 14:20:02 -04001059 nocow = 0;
1060 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001061 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001062 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001063
Yan Zheng80ff3852008-10-30 14:20:02 -04001064 if (found_key.objectid > inode->i_ino ||
1065 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1066 found_key.offset > end)
1067 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001068
Yan Zheng80ff3852008-10-30 14:20:02 -04001069 if (found_key.offset > cur_offset) {
1070 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001071 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001072 goto out_check;
1073 }
Chris Masonc31f8832008-01-08 15:46:31 -05001074
Yan Zheng80ff3852008-10-30 14:20:02 -04001075 fi = btrfs_item_ptr(leaf, path->slots[0],
1076 struct btrfs_file_extent_item);
1077 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001078
Yan Zhengd899e052008-10-30 14:25:28 -04001079 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1080 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001081 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001082 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001083 extent_end = found_key.offset +
1084 btrfs_file_extent_num_bytes(leaf, fi);
1085 if (extent_end <= start) {
1086 path->slots[0]++;
1087 goto next_slot;
1088 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001089 if (disk_bytenr == 0)
1090 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001091 if (btrfs_file_extent_compression(leaf, fi) ||
1092 btrfs_file_extent_encryption(leaf, fi) ||
1093 btrfs_file_extent_other_encoding(leaf, fi))
1094 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001095 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1096 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001097 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001098 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001099 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001100 found_key.offset -
1101 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001102 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001103 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001104 disk_bytenr += cur_offset - found_key.offset;
1105 num_bytes = min(end + 1, extent_end) - cur_offset;
1106 /*
1107 * force cow if csum exists in the range.
1108 * this ensure that csum for a given extent are
1109 * either valid or do not exist.
1110 */
1111 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1112 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001113 nocow = 1;
1114 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1115 extent_end = found_key.offset +
1116 btrfs_file_extent_inline_len(leaf, fi);
1117 extent_end = ALIGN(extent_end, root->sectorsize);
1118 } else {
1119 BUG_ON(1);
1120 }
1121out_check:
1122 if (extent_end <= start) {
1123 path->slots[0]++;
1124 goto next_slot;
1125 }
1126 if (!nocow) {
1127 if (cow_start == (u64)-1)
1128 cow_start = cur_offset;
1129 cur_offset = extent_end;
1130 if (cur_offset > end)
1131 break;
1132 path->slots[0]++;
1133 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001134 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001135
Yan Zheng7ea394f2008-08-05 13:05:02 -04001136 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001137 if (cow_start != (u64)-1) {
1138 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001139 found_key.offset - 1, page_started,
1140 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001141 BUG_ON(ret);
1142 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001143 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001144
Yan Zhengd899e052008-10-30 14:25:28 -04001145 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1146 struct extent_map *em;
1147 struct extent_map_tree *em_tree;
1148 em_tree = &BTRFS_I(inode)->extent_tree;
1149 em = alloc_extent_map(GFP_NOFS);
1150 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001151 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001152 em->len = num_bytes;
1153 em->block_len = num_bytes;
1154 em->block_start = disk_bytenr;
1155 em->bdev = root->fs_info->fs_devices->latest_bdev;
1156 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1157 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001158 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001159 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001160 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001161 if (ret != -EEXIST) {
1162 free_extent_map(em);
1163 break;
1164 }
1165 btrfs_drop_extent_cache(inode, em->start,
1166 em->start + em->len - 1, 0);
1167 }
1168 type = BTRFS_ORDERED_PREALLOC;
1169 } else {
1170 type = BTRFS_ORDERED_NOCOW;
1171 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001172
1173 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001174 num_bytes, num_bytes, type);
1175 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001176
Yan Zhengd899e052008-10-30 14:25:28 -04001177 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001178 cur_offset, cur_offset + num_bytes - 1,
1179 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1180 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1181 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001182 cur_offset = extent_end;
1183 if (cur_offset > end)
1184 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001185 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001186 btrfs_release_path(root, path);
1187
1188 if (cur_offset <= end && cow_start == (u64)-1)
1189 cow_start = cur_offset;
1190 if (cow_start != (u64)-1) {
1191 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001192 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001193 BUG_ON(ret);
1194 }
1195
1196 ret = btrfs_end_transaction(trans, root);
1197 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001198 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001199 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001200}
1201
Chris Masond352ac62008-09-29 15:18:18 -04001202/*
1203 * extent_io.c call back to do delayed allocation processing
1204 */
Chris Masonc8b97812008-10-29 14:49:59 -04001205static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001206 u64 start, u64 end, int *page_started,
1207 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001208{
Chris Masonbe20aa92007-12-17 20:14:01 -05001209 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001210 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001211
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001212 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001213 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001214 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001215 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001216 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001217 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001218 else if (!btrfs_test_opt(root, COMPRESS) &&
1219 !(BTRFS_I(inode)->force_compress))
Chris Mason7f366cf2009-03-12 20:12:45 -04001220 ret = cow_file_range(inode, locked_page, start, end,
1221 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001222 else
Chris Mason771ed682008-11-06 22:02:51 -05001223 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001224 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001225 return ret;
1226}
1227
Josef Bacik9ed74f22009-09-11 16:12:44 -04001228static int btrfs_split_extent_hook(struct inode *inode,
1229 struct extent_state *orig, u64 split)
1230{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001231 if (!(orig->state & EXTENT_DELALLOC))
1232 return 0;
1233
Josef Bacik32c00af2009-10-08 13:34:05 -04001234 spin_lock(&BTRFS_I(inode)->accounting_lock);
1235 BTRFS_I(inode)->outstanding_extents++;
1236 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001237
1238 return 0;
1239}
1240
1241/*
1242 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1243 * extents so we can keep track of new extents that are just merged onto old
1244 * extents, such as when we are doing sequential writes, so we can properly
1245 * account for the metadata space we'll need.
1246 */
1247static int btrfs_merge_extent_hook(struct inode *inode,
1248 struct extent_state *new,
1249 struct extent_state *other)
1250{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001251 /* not delalloc, ignore it */
1252 if (!(other->state & EXTENT_DELALLOC))
1253 return 0;
1254
Josef Bacik32c00af2009-10-08 13:34:05 -04001255 spin_lock(&BTRFS_I(inode)->accounting_lock);
1256 BTRFS_I(inode)->outstanding_extents--;
1257 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001258
1259 return 0;
1260}
1261
Chris Masond352ac62008-09-29 15:18:18 -04001262/*
1263 * extent_io.c set_bit_hook, used to track delayed allocation
1264 * bytes in this file, and to maintain the list of inodes that
1265 * have pending delalloc work to be done.
1266 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001267static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001268 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001269{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001270
Chris Mason75eff682008-12-15 15:54:40 -05001271 /*
1272 * set_bit and clear bit hooks normally require _irqsave/restore
1273 * but in this case, we are only testeing for the DELALLOC
1274 * bit, which is only set or cleared with irqs on
1275 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001276 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001277 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001278
Josef Bacik32c00af2009-10-08 13:34:05 -04001279 spin_lock(&BTRFS_I(inode)->accounting_lock);
1280 BTRFS_I(inode)->outstanding_extents++;
1281 spin_unlock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik6a632092009-02-20 11:00:09 -05001282 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001283
Chris Mason75eff682008-12-15 15:54:40 -05001284 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001285 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001286 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001287 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1288 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1289 &root->fs_info->delalloc_inodes);
1290 }
Chris Mason75eff682008-12-15 15:54:40 -05001291 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001292 }
1293 return 0;
1294}
1295
Chris Masond352ac62008-09-29 15:18:18 -04001296/*
1297 * extent_io.c clear_bit_hook, see set_bit_hook for why
1298 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001299static int btrfs_clear_bit_hook(struct inode *inode,
1300 struct extent_state *state, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001301{
Chris Mason75eff682008-12-15 15:54:40 -05001302 /*
1303 * set_bit and clear bit hooks normally require _irqsave/restore
1304 * but in this case, we are only testeing for the DELALLOC
1305 * bit, which is only set or cleared with irqs on
1306 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001307 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001308 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001309
Josef Bacik32c00af2009-10-08 13:34:05 -04001310 if (bits & EXTENT_DO_ACCOUNTING) {
1311 spin_lock(&BTRFS_I(inode)->accounting_lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001312 WARN_ON(!BTRFS_I(inode)->outstanding_extents);
Josef Bacik32c00af2009-10-08 13:34:05 -04001313 BTRFS_I(inode)->outstanding_extents--;
1314 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1315 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1316 }
Josef Bacik9ed74f22009-09-11 16:12:44 -04001317
Chris Mason75eff682008-12-15 15:54:40 -05001318 spin_lock(&root->fs_info->delalloc_lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001319 if (state->end - state->start + 1 >
1320 root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001321 printk(KERN_INFO "btrfs warning: delalloc account "
1322 "%llu %llu\n",
Josef Bacik9ed74f22009-09-11 16:12:44 -04001323 (unsigned long long)
1324 state->end - state->start + 1,
Chris Masond3977122009-01-05 21:25:51 -05001325 (unsigned long long)
1326 root->fs_info->delalloc_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001327 btrfs_delalloc_free_space(root, inode, (u64)-1);
Chris Masonb0c68f82008-01-31 11:05:37 -05001328 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001329 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001330 } else {
Josef Bacik6a632092009-02-20 11:00:09 -05001331 btrfs_delalloc_free_space(root, inode,
Josef Bacik9ed74f22009-09-11 16:12:44 -04001332 state->end -
1333 state->start + 1);
1334 root->fs_info->delalloc_bytes -= state->end -
1335 state->start + 1;
1336 BTRFS_I(inode)->delalloc_bytes -= state->end -
1337 state->start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001338 }
Chris Masonea8c2812008-08-04 23:17:27 -04001339 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1340 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1341 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1342 }
Chris Mason75eff682008-12-15 15:54:40 -05001343 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001344 }
1345 return 0;
1346}
1347
Chris Masond352ac62008-09-29 15:18:18 -04001348/*
1349 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1350 * we don't create bios that span stripes or chunks
1351 */
Chris Mason239b14b2008-03-24 15:02:07 -04001352int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001353 size_t size, struct bio *bio,
1354 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001355{
1356 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1357 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001358 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001359 u64 length = 0;
1360 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001361 int ret;
1362
Chris Mason771ed682008-11-06 22:02:51 -05001363 if (bio_flags & EXTENT_BIO_COMPRESSED)
1364 return 0;
1365
Chris Masonf2d8d742008-04-21 10:03:05 -04001366 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001367 map_tree = &root->fs_info->mapping_tree;
1368 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001369 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001370 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001371
Chris Masond3977122009-01-05 21:25:51 -05001372 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001373 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001374 return 0;
1375}
1376
Chris Masond352ac62008-09-29 15:18:18 -04001377/*
1378 * in order to insert checksums into the metadata in large chunks,
1379 * we wait until bio submission time. All the pages in the bio are
1380 * checksummed and sums are attached onto the ordered extent record.
1381 *
1382 * At IO completion time the cums attached on the ordered extent record
1383 * are inserted into the btree
1384 */
Chris Masond3977122009-01-05 21:25:51 -05001385static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1386 struct bio *bio, int mirror_num,
1387 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001388{
Chris Mason065631f2008-02-20 12:07:25 -05001389 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001390 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001391
Chris Masond20f7042008-12-08 16:58:54 -05001392 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001393 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001394 return 0;
1395}
Chris Masone0156402008-04-16 11:15:20 -04001396
Chris Mason4a69a412008-11-06 22:03:00 -05001397/*
1398 * in order to insert checksums into the metadata in large chunks,
1399 * we wait until bio submission time. All the pages in the bio are
1400 * checksummed and sums are attached onto the ordered extent record.
1401 *
1402 * At IO completion time the cums attached on the ordered extent record
1403 * are inserted into the btree
1404 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001405static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001406 int mirror_num, unsigned long bio_flags)
1407{
1408 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001409 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001410}
1411
Chris Masond352ac62008-09-29 15:18:18 -04001412/*
Chris Masoncad321a2008-12-17 14:51:42 -05001413 * extent_io.c submission hook. This does the right thing for csum calculation
1414 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001415 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001416static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001417 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001418{
1419 struct btrfs_root *root = BTRFS_I(inode)->root;
1420 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001421 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001422
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001423 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001424
Chris Masone6dcd2d2008-07-17 12:53:50 -04001425 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1426 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001427
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001428 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001429 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001430 return btrfs_submit_compressed_read(inode, bio,
1431 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001432 } else if (!skip_sum)
1433 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001434 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001435 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001436 /* csum items have already been cloned */
1437 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1438 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001439 /* we're doing a write, do the async checksumming */
1440 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001441 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001442 bio_flags, __btrfs_submit_bio_start,
1443 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001444 }
1445
Chris Mason0b86a832008-03-24 15:01:56 -04001446mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001447 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001448}
Chris Mason6885f302008-02-20 16:11:05 -05001449
Chris Masond352ac62008-09-29 15:18:18 -04001450/*
1451 * given a list of ordered sums record them in the inode. This happens
1452 * at IO completion time based on sums calculated at bio submission time.
1453 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001454static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001455 struct inode *inode, u64 file_offset,
1456 struct list_head *list)
1457{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001458 struct btrfs_ordered_sum *sum;
1459
1460 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001461
1462 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001463 btrfs_csum_file_blocks(trans,
1464 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001465 }
1466 return 0;
1467}
1468
Josef Bacik2ac55d42010-02-03 19:33:23 +00001469int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1470 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001471{
Chris Masond3977122009-01-05 21:25:51 -05001472 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001473 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001474 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001475 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001476}
1477
Chris Masond352ac62008-09-29 15:18:18 -04001478/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001479struct btrfs_writepage_fixup {
1480 struct page *page;
1481 struct btrfs_work work;
1482};
1483
Christoph Hellwigb2950862008-12-02 09:54:17 -05001484static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001485{
1486 struct btrfs_writepage_fixup *fixup;
1487 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001488 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001489 struct page *page;
1490 struct inode *inode;
1491 u64 page_start;
1492 u64 page_end;
1493
1494 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1495 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001496again:
Chris Mason247e7432008-07-17 12:53:51 -04001497 lock_page(page);
1498 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1499 ClearPageChecked(page);
1500 goto out_page;
1501 }
1502
1503 inode = page->mapping->host;
1504 page_start = page_offset(page);
1505 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1506
Josef Bacik2ac55d42010-02-03 19:33:23 +00001507 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1508 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001509
1510 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001511 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001512 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001513
1514 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1515 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001516 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1517 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001518 unlock_page(page);
1519 btrfs_start_ordered_extent(inode, ordered, 1);
1520 goto again;
1521 }
Chris Mason247e7432008-07-17 12:53:51 -04001522
Josef Bacik2ac55d42010-02-03 19:33:23 +00001523 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001524 ClearPageChecked(page);
1525out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001526 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1527 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001528out_page:
1529 unlock_page(page);
1530 page_cache_release(page);
1531}
1532
1533/*
1534 * There are a few paths in the higher layers of the kernel that directly
1535 * set the page dirty bit without asking the filesystem if it is a
1536 * good idea. This causes problems because we want to make sure COW
1537 * properly happens and the data=ordered rules are followed.
1538 *
Chris Masonc8b97812008-10-29 14:49:59 -04001539 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001540 * hasn't been properly setup for IO. We kick off an async process
1541 * to fix it up. The async helper will wait for ordered extents, set
1542 * the delalloc bit and make it safe to write the page.
1543 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001544static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001545{
1546 struct inode *inode = page->mapping->host;
1547 struct btrfs_writepage_fixup *fixup;
1548 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001549
Chris Mason8b62b722009-09-02 16:53:46 -04001550 /* this page is properly in the ordered list */
1551 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001552 return 0;
1553
1554 if (PageChecked(page))
1555 return -EAGAIN;
1556
1557 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1558 if (!fixup)
1559 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001560
Chris Mason247e7432008-07-17 12:53:51 -04001561 SetPageChecked(page);
1562 page_cache_get(page);
1563 fixup->work.func = btrfs_writepage_fixup_worker;
1564 fixup->page = page;
1565 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1566 return -EAGAIN;
1567}
1568
Yan Zhengd899e052008-10-30 14:25:28 -04001569static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1570 struct inode *inode, u64 file_pos,
1571 u64 disk_bytenr, u64 disk_num_bytes,
1572 u64 num_bytes, u64 ram_bytes,
1573 u8 compression, u8 encryption,
1574 u16 other_encoding, int extent_type)
1575{
1576 struct btrfs_root *root = BTRFS_I(inode)->root;
1577 struct btrfs_file_extent_item *fi;
1578 struct btrfs_path *path;
1579 struct extent_buffer *leaf;
1580 struct btrfs_key ins;
1581 u64 hint;
1582 int ret;
1583
1584 path = btrfs_alloc_path();
1585 BUG_ON(!path);
1586
Chris Masonb9473432009-03-13 11:00:37 -04001587 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001588
1589 /*
1590 * we may be replacing one extent in the tree with another.
1591 * The new extent is pinned in the extent map, and we don't want
1592 * to drop it from the cache until it is completely in the btree.
1593 *
1594 * So, tell btrfs_drop_extents to leave this extent in the cache.
1595 * the caller is expected to unpin it and allow it to be merged
1596 * with the others.
1597 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001598 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1599 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001600 BUG_ON(ret);
1601
1602 ins.objectid = inode->i_ino;
1603 ins.offset = file_pos;
1604 ins.type = BTRFS_EXTENT_DATA_KEY;
1605 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1606 BUG_ON(ret);
1607 leaf = path->nodes[0];
1608 fi = btrfs_item_ptr(leaf, path->slots[0],
1609 struct btrfs_file_extent_item);
1610 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1611 btrfs_set_file_extent_type(leaf, fi, extent_type);
1612 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1613 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1614 btrfs_set_file_extent_offset(leaf, fi, 0);
1615 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1616 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1617 btrfs_set_file_extent_compression(leaf, fi, compression);
1618 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1619 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001620
1621 btrfs_unlock_up_safe(path, 1);
1622 btrfs_set_lock_blocking(leaf);
1623
Yan Zhengd899e052008-10-30 14:25:28 -04001624 btrfs_mark_buffer_dirty(leaf);
1625
1626 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001627
1628 ins.objectid = disk_bytenr;
1629 ins.offset = disk_num_bytes;
1630 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001631 ret = btrfs_alloc_reserved_file_extent(trans, root,
1632 root->root_key.objectid,
1633 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001634 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001635 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001636
Yan Zhengd899e052008-10-30 14:25:28 -04001637 return 0;
1638}
1639
Chris Mason5d13a982009-03-13 11:41:46 -04001640/*
1641 * helper function for btrfs_finish_ordered_io, this
1642 * just reads in some of the csum leaves to prime them into ram
1643 * before we start the transaction. It limits the amount of btree
1644 * reads required while inside the transaction.
1645 */
Chris Masond352ac62008-09-29 15:18:18 -04001646/* as ordered data IO finishes, this gets called so we can finish
1647 * an ordered extent if the range of bytes in the file it covers are
1648 * fully written.
1649 */
Chris Mason211f90e2008-07-18 11:56:15 -04001650static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001651{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001652 struct btrfs_root *root = BTRFS_I(inode)->root;
1653 struct btrfs_trans_handle *trans;
Chris Mason5d13a982009-03-13 11:41:46 -04001654 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001655 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001656 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04001657 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001658 int ret;
1659
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001660 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1661 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001662 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001663 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001664 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001665
Yan, Zhengc2167752009-11-12 09:34:21 +00001666 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1667 BUG_ON(!list_empty(&ordered_extent->list));
1668 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1669 if (!ret) {
1670 trans = btrfs_join_transaction(root, 1);
1671 ret = btrfs_update_inode(trans, root, inode);
1672 BUG_ON(ret);
1673 btrfs_end_transaction(trans, root);
1674 }
1675 goto out;
1676 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001677
Josef Bacik2ac55d42010-02-03 19:33:23 +00001678 lock_extent_bits(io_tree, ordered_extent->file_offset,
1679 ordered_extent->file_offset + ordered_extent->len - 1,
1680 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001681
Yan, Zhengc2167752009-11-12 09:34:21 +00001682 trans = btrfs_join_transaction(root, 1);
1683
Chris Masonc8b97812008-10-29 14:49:59 -04001684 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001685 compressed = 1;
1686 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1687 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001688 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001689 ordered_extent->file_offset,
1690 ordered_extent->file_offset +
1691 ordered_extent->len);
1692 BUG_ON(ret);
1693 } else {
1694 ret = insert_reserved_file_extent(trans, inode,
1695 ordered_extent->file_offset,
1696 ordered_extent->start,
1697 ordered_extent->disk_len,
1698 ordered_extent->len,
1699 ordered_extent->len,
1700 compressed, 0, 0,
1701 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001702 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1703 ordered_extent->file_offset,
1704 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001705 BUG_ON(ret);
1706 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001707 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1708 ordered_extent->file_offset +
1709 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1710
Chris Masone6dcd2d2008-07-17 12:53:50 -04001711 add_pending_csums(trans, inode, ordered_extent->file_offset,
1712 &ordered_extent->list);
1713
Yan, Zhengc2167752009-11-12 09:34:21 +00001714 /* this also removes the ordered extent from the tree */
1715 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1716 ret = btrfs_update_inode(trans, root, inode);
1717 BUG_ON(ret);
1718 btrfs_end_transaction(trans, root);
1719out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001720 /* once for us */
1721 btrfs_put_ordered_extent(ordered_extent);
1722 /* once for the tree */
1723 btrfs_put_ordered_extent(ordered_extent);
1724
Chris Masone6dcd2d2008-07-17 12:53:50 -04001725 return 0;
1726}
1727
Christoph Hellwigb2950862008-12-02 09:54:17 -05001728static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001729 struct extent_state *state, int uptodate)
1730{
Chris Mason8b62b722009-09-02 16:53:46 -04001731 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001732 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1733}
1734
Chris Masond352ac62008-09-29 15:18:18 -04001735/*
1736 * When IO fails, either with EIO or csum verification fails, we
1737 * try other mirrors that might have a good copy of the data. This
1738 * io_failure_record is used to record state as we go through all the
1739 * mirrors. If another mirror has good data, the page is set up to date
1740 * and things continue. If a good mirror can't be found, the original
1741 * bio end_io callback is called to indicate things have failed.
1742 */
Chris Mason7e383262008-04-09 16:28:12 -04001743struct io_failure_record {
1744 struct page *page;
1745 u64 start;
1746 u64 len;
1747 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001748 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001749 int last_mirror;
1750};
1751
Christoph Hellwigb2950862008-12-02 09:54:17 -05001752static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001753 struct page *page, u64 start, u64 end,
1754 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001755{
1756 struct io_failure_record *failrec = NULL;
1757 u64 private;
1758 struct extent_map *em;
1759 struct inode *inode = page->mapping->host;
1760 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001761 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001762 struct bio *bio;
1763 int num_copies;
1764 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001765 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001766 u64 logical;
1767
1768 ret = get_state_private(failure_tree, start, &private);
1769 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001770 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1771 if (!failrec)
1772 return -ENOMEM;
1773 failrec->start = start;
1774 failrec->len = end - start + 1;
1775 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001776 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001777
Chris Mason890871b2009-09-02 16:24:52 -04001778 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001779 em = lookup_extent_mapping(em_tree, start, failrec->len);
1780 if (em->start > start || em->start + em->len < start) {
1781 free_extent_map(em);
1782 em = NULL;
1783 }
Chris Mason890871b2009-09-02 16:24:52 -04001784 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001785
1786 if (!em || IS_ERR(em)) {
1787 kfree(failrec);
1788 return -EIO;
1789 }
1790 logical = start - em->start;
1791 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001792 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1793 logical = em->block_start;
1794 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1795 }
Chris Mason7e383262008-04-09 16:28:12 -04001796 failrec->logical = logical;
1797 free_extent_map(em);
1798 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1799 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001800 set_state_private(failure_tree, start,
1801 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001802 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001803 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001804 }
1805 num_copies = btrfs_num_copies(
1806 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1807 failrec->logical, failrec->len);
1808 failrec->last_mirror++;
1809 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001810 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001811 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1812 failrec->start,
1813 EXTENT_LOCKED);
1814 if (state && state->start != failrec->start)
1815 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001816 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001817 }
1818 if (!state || failrec->last_mirror > num_copies) {
1819 set_state_private(failure_tree, failrec->start, 0);
1820 clear_extent_bits(failure_tree, failrec->start,
1821 failrec->start + failrec->len - 1,
1822 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1823 kfree(failrec);
1824 return -EIO;
1825 }
1826 bio = bio_alloc(GFP_NOFS, 1);
1827 bio->bi_private = state;
1828 bio->bi_end_io = failed_bio->bi_end_io;
1829 bio->bi_sector = failrec->logical >> 9;
1830 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001831 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001832
Chris Mason7e383262008-04-09 16:28:12 -04001833 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001834 if (failed_bio->bi_rw & (1 << BIO_RW))
1835 rw = WRITE;
1836 else
1837 rw = READ;
1838
1839 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001840 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001841 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001842 return 0;
1843}
1844
Chris Masond352ac62008-09-29 15:18:18 -04001845/*
1846 * each time an IO finishes, we do a fast check in the IO failure tree
1847 * to see if we need to process or clean up an io_failure_record
1848 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001849static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001850{
1851 u64 private;
1852 u64 private_failure;
1853 struct io_failure_record *failure;
1854 int ret;
1855
1856 private = 0;
1857 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1858 (u64)-1, 1, EXTENT_DIRTY)) {
1859 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1860 start, &private_failure);
1861 if (ret == 0) {
1862 failure = (struct io_failure_record *)(unsigned long)
1863 private_failure;
1864 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1865 failure->start, 0);
1866 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1867 failure->start,
1868 failure->start + failure->len - 1,
1869 EXTENT_DIRTY | EXTENT_LOCKED,
1870 GFP_NOFS);
1871 kfree(failure);
1872 }
1873 }
Chris Mason7e383262008-04-09 16:28:12 -04001874 return 0;
1875}
1876
Chris Masond352ac62008-09-29 15:18:18 -04001877/*
1878 * when reads are done, we need to check csums to verify the data is correct
1879 * if there's a match, we allow the bio to finish. If not, we go through
1880 * the io_failure_record routines to find good copies
1881 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001882static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001883 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001884{
Chris Mason35ebb932007-10-30 16:56:53 -04001885 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001886 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001887 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001888 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001889 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001890 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001891 struct btrfs_root *root = BTRFS_I(inode)->root;
1892 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001893
Chris Masond20f7042008-12-08 16:58:54 -05001894 if (PageChecked(page)) {
1895 ClearPageChecked(page);
1896 goto good;
1897 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001898
1899 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001900 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001901
Yan Zheng17d217f2008-12-12 10:03:38 -05001902 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001903 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001904 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1905 GFP_NOFS);
1906 return 0;
1907 }
1908
Yanc2e639f2008-02-04 08:57:25 -05001909 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001910 private = state->private;
1911 ret = 0;
1912 } else {
1913 ret = get_state_private(io_tree, start, &private);
1914 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001915 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001916 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001917 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001918
Chris Masonff79f812007-10-15 16:22:25 -04001919 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1920 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001921 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001922 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001923
Chris Mason9ab86c82009-01-07 09:48:51 -05001924 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001925good:
Chris Mason7e383262008-04-09 16:28:12 -04001926 /* if the io failure tree for this inode is non-empty,
1927 * check to see if we've recovered from a failed IO
1928 */
Chris Mason1259ab72008-05-12 13:39:03 -04001929 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001930 return 0;
1931
1932zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001933 if (printk_ratelimit()) {
1934 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1935 "private %llu\n", page->mapping->host->i_ino,
1936 (unsigned long long)start, csum,
1937 (unsigned long long)private);
1938 }
Chris Masondb945352007-10-15 16:15:53 -04001939 memset(kaddr + offset, 1, end - start + 1);
1940 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001941 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001942 if (private == 0)
1943 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001944 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001945}
Chris Masonb888db22007-08-27 16:49:44 -04001946
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001947struct delayed_iput {
1948 struct list_head list;
1949 struct inode *inode;
1950};
1951
1952void btrfs_add_delayed_iput(struct inode *inode)
1953{
1954 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1955 struct delayed_iput *delayed;
1956
1957 if (atomic_add_unless(&inode->i_count, -1, 1))
1958 return;
1959
1960 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
1961 delayed->inode = inode;
1962
1963 spin_lock(&fs_info->delayed_iput_lock);
1964 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
1965 spin_unlock(&fs_info->delayed_iput_lock);
1966}
1967
1968void btrfs_run_delayed_iputs(struct btrfs_root *root)
1969{
1970 LIST_HEAD(list);
1971 struct btrfs_fs_info *fs_info = root->fs_info;
1972 struct delayed_iput *delayed;
1973 int empty;
1974
1975 spin_lock(&fs_info->delayed_iput_lock);
1976 empty = list_empty(&fs_info->delayed_iputs);
1977 spin_unlock(&fs_info->delayed_iput_lock);
1978 if (empty)
1979 return;
1980
1981 down_read(&root->fs_info->cleanup_work_sem);
1982 spin_lock(&fs_info->delayed_iput_lock);
1983 list_splice_init(&fs_info->delayed_iputs, &list);
1984 spin_unlock(&fs_info->delayed_iput_lock);
1985
1986 while (!list_empty(&list)) {
1987 delayed = list_entry(list.next, struct delayed_iput, list);
1988 list_del(&delayed->list);
1989 iput(delayed->inode);
1990 kfree(delayed);
1991 }
1992 up_read(&root->fs_info->cleanup_work_sem);
1993}
1994
Josef Bacik7b128762008-07-24 12:17:14 -04001995/*
1996 * This creates an orphan entry for the given inode in case something goes
1997 * wrong in the middle of an unlink/truncate.
1998 */
1999int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2000{
2001 struct btrfs_root *root = BTRFS_I(inode)->root;
2002 int ret = 0;
2003
Yanbcc63ab2008-07-30 16:29:20 -04002004 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002005
2006 /* already on the orphan list, we're good */
2007 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002008 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002009 return 0;
2010 }
2011
2012 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2013
Yanbcc63ab2008-07-30 16:29:20 -04002014 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002015
2016 /*
2017 * insert an orphan item to track this unlinked/truncated file
2018 */
2019 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2020
2021 return ret;
2022}
2023
2024/*
2025 * We have done the truncate/delete so we can go ahead and remove the orphan
2026 * item for this particular inode.
2027 */
2028int btrfs_orphan_del(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 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002036 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002037 return 0;
2038 }
2039
2040 list_del_init(&BTRFS_I(inode)->i_orphan);
2041 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04002042 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002043 return 0;
2044 }
2045
Yanbcc63ab2008-07-30 16:29:20 -04002046 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002047
2048 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2049
2050 return ret;
2051}
2052
2053/*
2054 * this cleans up any orphans that may be left on the list from the last use
2055 * of this root.
2056 */
2057void btrfs_orphan_cleanup(struct btrfs_root *root)
2058{
2059 struct btrfs_path *path;
2060 struct extent_buffer *leaf;
2061 struct btrfs_item *item;
2062 struct btrfs_key key, found_key;
2063 struct btrfs_trans_handle *trans;
2064 struct inode *inode;
2065 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2066
Yan, Zhengc71bf092009-11-12 09:34:40 +00002067 if (!xchg(&root->clean_orphans, 0))
Josef Bacik7b128762008-07-24 12:17:14 -04002068 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002069
2070 path = btrfs_alloc_path();
2071 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002072 path->reada = -1;
2073
2074 key.objectid = BTRFS_ORPHAN_OBJECTID;
2075 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2076 key.offset = (u64)-1;
2077
Josef Bacik7b128762008-07-24 12:17:14 -04002078 while (1) {
2079 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2080 if (ret < 0) {
2081 printk(KERN_ERR "Error searching slot for orphan: %d"
2082 "\n", ret);
2083 break;
2084 }
2085
2086 /*
2087 * if ret == 0 means we found what we were searching for, which
2088 * is weird, but possible, so only screw with path if we didnt
2089 * find the key and see if we have stuff that matches
2090 */
2091 if (ret > 0) {
2092 if (path->slots[0] == 0)
2093 break;
2094 path->slots[0]--;
2095 }
2096
2097 /* pull out the item */
2098 leaf = path->nodes[0];
2099 item = btrfs_item_nr(leaf, path->slots[0]);
2100 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2101
2102 /* make sure the item matches what we want */
2103 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2104 break;
2105 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2106 break;
2107
2108 /* release the path since we're done with it */
2109 btrfs_release_path(root, path);
2110
2111 /*
2112 * this is where we are basically btrfs_lookup, without the
2113 * crossing root thing. we store the inode number in the
2114 * offset of the orphan item.
2115 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002116 found_key.objectid = found_key.offset;
2117 found_key.type = BTRFS_INODE_ITEM_KEY;
2118 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002119 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002120 if (IS_ERR(inode))
Josef Bacik7b128762008-07-24 12:17:14 -04002121 break;
2122
Josef Bacik7b128762008-07-24 12:17:14 -04002123 /*
2124 * add this inode to the orphan list so btrfs_orphan_del does
2125 * the proper thing when we hit it
2126 */
Yanbcc63ab2008-07-30 16:29:20 -04002127 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002128 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04002129 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002130
2131 /*
2132 * if this is a bad inode, means we actually succeeded in
2133 * removing the inode, but not the orphan record, which means
2134 * we need to manually delete the orphan since iput will just
2135 * do a destroy_inode
2136 */
2137 if (is_bad_inode(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002138 trans = btrfs_start_transaction(root, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002139 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002140 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002141 iput(inode);
2142 continue;
2143 }
2144
2145 /* if we have links, this was a truncate, lets do that */
2146 if (inode->i_nlink) {
2147 nr_truncate++;
2148 btrfs_truncate(inode);
2149 } else {
2150 nr_unlink++;
2151 }
2152
2153 /* this will do delete_inode and everything for us */
2154 iput(inode);
2155 }
2156
2157 if (nr_unlink)
2158 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2159 if (nr_truncate)
2160 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2161
2162 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002163}
2164
Chris Masond352ac62008-09-29 15:18:18 -04002165/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002166 * very simple check to peek ahead in the leaf looking for xattrs. If we
2167 * don't find any xattrs, we know there can't be any acls.
2168 *
2169 * slot is the slot the inode is in, objectid is the objectid of the inode
2170 */
2171static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2172 int slot, u64 objectid)
2173{
2174 u32 nritems = btrfs_header_nritems(leaf);
2175 struct btrfs_key found_key;
2176 int scanned = 0;
2177
2178 slot++;
2179 while (slot < nritems) {
2180 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2181
2182 /* we found a different objectid, there must not be acls */
2183 if (found_key.objectid != objectid)
2184 return 0;
2185
2186 /* we found an xattr, assume we've got an acl */
2187 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2188 return 1;
2189
2190 /*
2191 * we found a key greater than an xattr key, there can't
2192 * be any acls later on
2193 */
2194 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2195 return 0;
2196
2197 slot++;
2198 scanned++;
2199
2200 /*
2201 * it goes inode, inode backrefs, xattrs, extents,
2202 * so if there are a ton of hard links to an inode there can
2203 * be a lot of backrefs. Don't waste time searching too hard,
2204 * this is just an optimization
2205 */
2206 if (scanned >= 8)
2207 break;
2208 }
2209 /* we hit the end of the leaf before we found an xattr or
2210 * something larger than an xattr. We have to assume the inode
2211 * has acls
2212 */
2213 return 1;
2214}
2215
2216/*
Chris Masond352ac62008-09-29 15:18:18 -04002217 * read an inode from the btree into the in-memory inode
2218 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002219static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002220{
2221 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002222 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002223 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002224 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002225 struct btrfs_root *root = BTRFS_I(inode)->root;
2226 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002227 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002228 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002229 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002230 int ret;
2231
2232 path = btrfs_alloc_path();
2233 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002234 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002235
Chris Mason39279cc2007-06-12 06:35:45 -04002236 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002237 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002238 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002239
Chris Mason5f39d392007-10-15 16:14:19 -04002240 leaf = path->nodes[0];
2241 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2242 struct btrfs_inode_item);
2243
2244 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2245 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2246 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2247 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002248 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002249
2250 tspec = btrfs_inode_atime(inode_item);
2251 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2252 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2253
2254 tspec = btrfs_inode_mtime(inode_item);
2255 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2256 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2257
2258 tspec = btrfs_inode_ctime(inode_item);
2259 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2260 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2261
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002262 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002263 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002264 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002265 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002266 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002267 rdev = btrfs_inode_rdev(leaf, inode_item);
2268
Josef Bacikaec74772008-07-24 12:12:38 -04002269 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002270 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002271
Chris Mason5f39d392007-10-15 16:14:19 -04002272 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002273
Chris Mason46a53cc2009-04-27 11:47:50 -04002274 /*
2275 * try to precache a NULL acl entry for files that don't have
2276 * any xattrs or acls
2277 */
2278 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002279 if (!maybe_acls)
2280 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002281
Yan Zhengd2fb3432008-12-11 16:30:39 -05002282 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2283 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002284 btrfs_free_path(path);
2285 inode_item = NULL;
2286
Chris Mason39279cc2007-06-12 06:35:45 -04002287 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002288 case S_IFREG:
2289 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002290 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002291 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002292 inode->i_fop = &btrfs_file_operations;
2293 inode->i_op = &btrfs_file_inode_operations;
2294 break;
2295 case S_IFDIR:
2296 inode->i_fop = &btrfs_dir_file_operations;
2297 if (root == root->fs_info->tree_root)
2298 inode->i_op = &btrfs_dir_ro_inode_operations;
2299 else
2300 inode->i_op = &btrfs_dir_inode_operations;
2301 break;
2302 case S_IFLNK:
2303 inode->i_op = &btrfs_symlink_inode_operations;
2304 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002305 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002306 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002307 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002308 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002309 init_special_inode(inode, inode->i_mode, rdev);
2310 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002311 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002312
2313 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002314 return;
2315
2316make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002317 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002318 make_bad_inode(inode);
2319}
2320
Chris Masond352ac62008-09-29 15:18:18 -04002321/*
2322 * given a leaf and an inode, copy the inode fields into the leaf
2323 */
Chris Masone02119d2008-09-05 16:13:11 -04002324static void fill_inode_item(struct btrfs_trans_handle *trans,
2325 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002326 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002327 struct inode *inode)
2328{
Chris Mason5f39d392007-10-15 16:14:19 -04002329 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2330 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002331 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002332 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2333 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2334
2335 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2336 inode->i_atime.tv_sec);
2337 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2338 inode->i_atime.tv_nsec);
2339
2340 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2341 inode->i_mtime.tv_sec);
2342 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2343 inode->i_mtime.tv_nsec);
2344
2345 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2346 inode->i_ctime.tv_sec);
2347 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2348 inode->i_ctime.tv_nsec);
2349
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002350 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002351 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002352 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002353 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002354 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002355 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002356 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002357}
2358
Chris Masond352ac62008-09-29 15:18:18 -04002359/*
2360 * copy everything in the in-memory inode into the btree.
2361 */
Chris Masond3977122009-01-05 21:25:51 -05002362noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2363 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002364{
2365 struct btrfs_inode_item *inode_item;
2366 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002367 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002368 int ret;
2369
2370 path = btrfs_alloc_path();
2371 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002372 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002373 ret = btrfs_lookup_inode(trans, root, path,
2374 &BTRFS_I(inode)->location, 1);
2375 if (ret) {
2376 if (ret > 0)
2377 ret = -ENOENT;
2378 goto failed;
2379 }
2380
Chris Masonb4ce94d2009-02-04 09:25:08 -05002381 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002382 leaf = path->nodes[0];
2383 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002384 struct btrfs_inode_item);
2385
Chris Masone02119d2008-09-05 16:13:11 -04002386 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002387 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002388 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002389 ret = 0;
2390failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002391 btrfs_free_path(path);
2392 return ret;
2393}
2394
2395
Chris Masond352ac62008-09-29 15:18:18 -04002396/*
2397 * unlink helper that gets used here in inode.c and in the tree logging
2398 * recovery code. It remove a link in a directory with a given name, and
2399 * also drops the back refs in the inode to the directory
2400 */
Chris Masone02119d2008-09-05 16:13:11 -04002401int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2402 struct btrfs_root *root,
2403 struct inode *dir, struct inode *inode,
2404 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002405{
2406 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002407 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002408 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002409 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002410 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002411 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002412
2413 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002414 if (!path) {
2415 ret = -ENOMEM;
2416 goto err;
2417 }
2418
Chris Masonb9473432009-03-13 11:00:37 -04002419 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002420 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2421 name, name_len, -1);
2422 if (IS_ERR(di)) {
2423 ret = PTR_ERR(di);
2424 goto err;
2425 }
2426 if (!di) {
2427 ret = -ENOENT;
2428 goto err;
2429 }
Chris Mason5f39d392007-10-15 16:14:19 -04002430 leaf = path->nodes[0];
2431 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002432 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002433 if (ret)
2434 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002435 btrfs_release_path(root, path);
2436
Josef Bacikaec74772008-07-24 12:12:38 -04002437 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002438 inode->i_ino,
2439 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002440 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002441 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002442 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002443 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002444 goto err;
2445 }
2446
Chris Mason39279cc2007-06-12 06:35:45 -04002447 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002448 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002449 if (IS_ERR(di)) {
2450 ret = PTR_ERR(di);
2451 goto err;
2452 }
2453 if (!di) {
2454 ret = -ENOENT;
2455 goto err;
2456 }
2457 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002458 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002459
Chris Masone02119d2008-09-05 16:13:11 -04002460 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2461 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002462 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002463
2464 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2465 dir, index);
2466 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002467err:
2468 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002469 if (ret)
2470 goto out;
2471
2472 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2473 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2474 btrfs_update_inode(trans, root, dir);
2475 btrfs_drop_nlink(inode);
2476 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002477out:
Chris Mason39279cc2007-06-12 06:35:45 -04002478 return ret;
2479}
2480
Yan, Zhenga22285a2010-05-16 10:48:46 -04002481/* helper to check if there is any shared block in the path */
2482static int check_path_shared(struct btrfs_root *root,
2483 struct btrfs_path *path)
2484{
2485 struct extent_buffer *eb;
2486 int level;
2487 int ret;
2488 u64 refs;
2489
2490 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2491 if (!path->nodes[level])
2492 break;
2493 eb = path->nodes[level];
2494 if (!btrfs_block_can_be_shared(root, eb))
2495 continue;
2496 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2497 &refs, NULL);
2498 if (refs > 1)
2499 return 1;
2500 }
2501 return 0;
2502}
2503
2504/*
2505 * helper to start transaction for unlink and rmdir.
2506 *
2507 * unlink and rmdir are special in btrfs, they do not always free space.
2508 * so in enospc case, we should make sure they will free space before
2509 * allowing them to use the global metadata reservation.
2510 */
2511static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2512 struct dentry *dentry)
2513{
2514 struct btrfs_trans_handle *trans;
2515 struct btrfs_root *root = BTRFS_I(dir)->root;
2516 struct btrfs_path *path;
2517 struct btrfs_inode_ref *ref;
2518 struct btrfs_dir_item *di;
2519 struct inode *inode = dentry->d_inode;
2520 u64 index;
2521 int check_link = 1;
2522 int err = -ENOSPC;
2523 int ret;
2524
2525 trans = btrfs_start_transaction(root, 10);
2526 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2527 return trans;
2528
2529 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2530 return ERR_PTR(-ENOSPC);
2531
2532 /* check if there is someone else holds reference */
2533 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2534 return ERR_PTR(-ENOSPC);
2535
2536 if (atomic_read(&inode->i_count) > 2)
2537 return ERR_PTR(-ENOSPC);
2538
2539 if (xchg(&root->fs_info->enospc_unlink, 1))
2540 return ERR_PTR(-ENOSPC);
2541
2542 path = btrfs_alloc_path();
2543 if (!path) {
2544 root->fs_info->enospc_unlink = 0;
2545 return ERR_PTR(-ENOMEM);
2546 }
2547
2548 trans = btrfs_start_transaction(root, 0);
2549 if (IS_ERR(trans)) {
2550 btrfs_free_path(path);
2551 root->fs_info->enospc_unlink = 0;
2552 return trans;
2553 }
2554
2555 path->skip_locking = 1;
2556 path->search_commit_root = 1;
2557
2558 ret = btrfs_lookup_inode(trans, root, path,
2559 &BTRFS_I(dir)->location, 0);
2560 if (ret < 0) {
2561 err = ret;
2562 goto out;
2563 }
2564 if (ret == 0) {
2565 if (check_path_shared(root, path))
2566 goto out;
2567 } else {
2568 check_link = 0;
2569 }
2570 btrfs_release_path(root, path);
2571
2572 ret = btrfs_lookup_inode(trans, root, path,
2573 &BTRFS_I(inode)->location, 0);
2574 if (ret < 0) {
2575 err = ret;
2576 goto out;
2577 }
2578 if (ret == 0) {
2579 if (check_path_shared(root, path))
2580 goto out;
2581 } else {
2582 check_link = 0;
2583 }
2584 btrfs_release_path(root, path);
2585
2586 if (ret == 0 && S_ISREG(inode->i_mode)) {
2587 ret = btrfs_lookup_file_extent(trans, root, path,
2588 inode->i_ino, (u64)-1, 0);
2589 if (ret < 0) {
2590 err = ret;
2591 goto out;
2592 }
2593 BUG_ON(ret == 0);
2594 if (check_path_shared(root, path))
2595 goto out;
2596 btrfs_release_path(root, path);
2597 }
2598
2599 if (!check_link) {
2600 err = 0;
2601 goto out;
2602 }
2603
2604 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2605 dentry->d_name.name, dentry->d_name.len, 0);
2606 if (IS_ERR(di)) {
2607 err = PTR_ERR(di);
2608 goto out;
2609 }
2610 if (di) {
2611 if (check_path_shared(root, path))
2612 goto out;
2613 } else {
2614 err = 0;
2615 goto out;
2616 }
2617 btrfs_release_path(root, path);
2618
2619 ref = btrfs_lookup_inode_ref(trans, root, path,
2620 dentry->d_name.name, dentry->d_name.len,
2621 inode->i_ino, dir->i_ino, 0);
2622 if (IS_ERR(ref)) {
2623 err = PTR_ERR(ref);
2624 goto out;
2625 }
2626 BUG_ON(!ref);
2627 if (check_path_shared(root, path))
2628 goto out;
2629 index = btrfs_inode_ref_index(path->nodes[0], ref);
2630 btrfs_release_path(root, path);
2631
2632 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2633 dentry->d_name.name, dentry->d_name.len, 0);
2634 if (IS_ERR(di)) {
2635 err = PTR_ERR(di);
2636 goto out;
2637 }
2638 BUG_ON(ret == -ENOENT);
2639 if (check_path_shared(root, path))
2640 goto out;
2641
2642 err = 0;
2643out:
2644 btrfs_free_path(path);
2645 if (err) {
2646 btrfs_end_transaction(trans, root);
2647 root->fs_info->enospc_unlink = 0;
2648 return ERR_PTR(err);
2649 }
2650
2651 trans->block_rsv = &root->fs_info->global_block_rsv;
2652 return trans;
2653}
2654
2655static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2656 struct btrfs_root *root)
2657{
2658 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2659 BUG_ON(!root->fs_info->enospc_unlink);
2660 root->fs_info->enospc_unlink = 0;
2661 }
2662 btrfs_end_transaction_throttle(trans, root);
2663}
2664
Chris Mason39279cc2007-06-12 06:35:45 -04002665static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2666{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002667 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002668 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002669 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002670 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002671 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002672
Yan, Zhenga22285a2010-05-16 10:48:46 -04002673 trans = __unlink_start_trans(dir, dentry);
2674 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002675 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002676
Chris Mason39279cc2007-06-12 06:35:45 -04002677 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002678
2679 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2680
Chris Masone02119d2008-09-05 16:13:11 -04002681 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2682 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002683 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002684
Yan, Zhenga22285a2010-05-16 10:48:46 -04002685 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002686 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002687 BUG_ON(ret);
2688 }
Josef Bacik7b128762008-07-24 12:17:14 -04002689
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002690 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002691 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002692 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002693 return ret;
2694}
2695
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002696int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2697 struct btrfs_root *root,
2698 struct inode *dir, u64 objectid,
2699 const char *name, int name_len)
2700{
2701 struct btrfs_path *path;
2702 struct extent_buffer *leaf;
2703 struct btrfs_dir_item *di;
2704 struct btrfs_key key;
2705 u64 index;
2706 int ret;
2707
2708 path = btrfs_alloc_path();
2709 if (!path)
2710 return -ENOMEM;
2711
2712 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2713 name, name_len, -1);
2714 BUG_ON(!di || IS_ERR(di));
2715
2716 leaf = path->nodes[0];
2717 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2718 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2719 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2720 BUG_ON(ret);
2721 btrfs_release_path(root, path);
2722
2723 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2724 objectid, root->root_key.objectid,
2725 dir->i_ino, &index, name, name_len);
2726 if (ret < 0) {
2727 BUG_ON(ret != -ENOENT);
2728 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2729 name, name_len);
2730 BUG_ON(!di || IS_ERR(di));
2731
2732 leaf = path->nodes[0];
2733 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2734 btrfs_release_path(root, path);
2735 index = key.offset;
2736 }
2737
2738 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2739 index, name, name_len, -1);
2740 BUG_ON(!di || IS_ERR(di));
2741
2742 leaf = path->nodes[0];
2743 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2744 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2745 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2746 BUG_ON(ret);
2747 btrfs_release_path(root, path);
2748
2749 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2750 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2751 ret = btrfs_update_inode(trans, root, dir);
2752 BUG_ON(ret);
2753 dir->i_sb->s_dirt = 1;
2754
2755 btrfs_free_path(path);
2756 return 0;
2757}
2758
Chris Mason39279cc2007-06-12 06:35:45 -04002759static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2760{
2761 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002762 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002763 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002764 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002765 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002766
Chris Mason3394e162008-11-17 20:42:26 -05002767 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002768 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002769 return -ENOTEMPTY;
2770
Yan, Zhenga22285a2010-05-16 10:48:46 -04002771 trans = __unlink_start_trans(dir, dentry);
2772 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002773 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002774
Chris Mason39279cc2007-06-12 06:35:45 -04002775 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002776
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002777 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2778 err = btrfs_unlink_subvol(trans, root, dir,
2779 BTRFS_I(inode)->location.objectid,
2780 dentry->d_name.name,
2781 dentry->d_name.len);
2782 goto out;
2783 }
2784
Josef Bacik7b128762008-07-24 12:17:14 -04002785 err = btrfs_orphan_add(trans, inode);
2786 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002787 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002788
Chris Mason39279cc2007-06-12 06:35:45 -04002789 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002790 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2791 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002792 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002793 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002794out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002795 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002796 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002797 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002798
Chris Mason39279cc2007-06-12 06:35:45 -04002799 return err;
2800}
2801
Chris Masond20f7042008-12-08 16:58:54 -05002802#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002803/*
Chris Mason323ac952008-10-01 19:05:46 -04002804 * when truncating bytes in a file, it is possible to avoid reading
2805 * the leaves that contain only checksum items. This can be the
2806 * majority of the IO required to delete a large file, but it must
2807 * be done carefully.
2808 *
2809 * The keys in the level just above the leaves are checked to make sure
2810 * the lowest key in a given leaf is a csum key, and starts at an offset
2811 * after the new size.
2812 *
2813 * Then the key for the next leaf is checked to make sure it also has
2814 * a checksum item for the same file. If it does, we know our target leaf
2815 * contains only checksum items, and it can be safely freed without reading
2816 * it.
2817 *
2818 * This is just an optimization targeted at large files. It may do
2819 * nothing. It will return 0 unless things went badly.
2820 */
2821static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2822 struct btrfs_root *root,
2823 struct btrfs_path *path,
2824 struct inode *inode, u64 new_size)
2825{
2826 struct btrfs_key key;
2827 int ret;
2828 int nritems;
2829 struct btrfs_key found_key;
2830 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002831 struct btrfs_leaf_ref *ref;
2832 u64 leaf_gen;
2833 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002834
2835 path->lowest_level = 1;
2836 key.objectid = inode->i_ino;
2837 key.type = BTRFS_CSUM_ITEM_KEY;
2838 key.offset = new_size;
2839again:
2840 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2841 if (ret < 0)
2842 goto out;
2843
2844 if (path->nodes[1] == NULL) {
2845 ret = 0;
2846 goto out;
2847 }
2848 ret = 0;
2849 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2850 nritems = btrfs_header_nritems(path->nodes[1]);
2851
2852 if (!nritems)
2853 goto out;
2854
2855 if (path->slots[1] >= nritems)
2856 goto next_node;
2857
2858 /* did we find a key greater than anything we want to delete? */
2859 if (found_key.objectid > inode->i_ino ||
2860 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2861 goto out;
2862
2863 /* we check the next key in the node to make sure the leave contains
2864 * only checksum items. This comparison doesn't work if our
2865 * leaf is the last one in the node
2866 */
2867 if (path->slots[1] + 1 >= nritems) {
2868next_node:
2869 /* search forward from the last key in the node, this
2870 * will bring us into the next node in the tree
2871 */
2872 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2873
2874 /* unlikely, but we inc below, so check to be safe */
2875 if (found_key.offset == (u64)-1)
2876 goto out;
2877
2878 /* search_forward needs a path with locks held, do the
2879 * search again for the original key. It is possible
2880 * this will race with a balance and return a path that
2881 * we could modify, but this drop is just an optimization
2882 * and is allowed to miss some leaves.
2883 */
2884 btrfs_release_path(root, path);
2885 found_key.offset++;
2886
2887 /* setup a max key for search_forward */
2888 other_key.offset = (u64)-1;
2889 other_key.type = key.type;
2890 other_key.objectid = key.objectid;
2891
2892 path->keep_locks = 1;
2893 ret = btrfs_search_forward(root, &found_key, &other_key,
2894 path, 0, 0);
2895 path->keep_locks = 0;
2896 if (ret || found_key.objectid != key.objectid ||
2897 found_key.type != key.type) {
2898 ret = 0;
2899 goto out;
2900 }
2901
2902 key.offset = found_key.offset;
2903 btrfs_release_path(root, path);
2904 cond_resched();
2905 goto again;
2906 }
2907
2908 /* we know there's one more slot after us in the tree,
2909 * read that key so we can verify it is also a checksum item
2910 */
2911 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2912
2913 if (found_key.objectid < inode->i_ino)
2914 goto next_key;
2915
2916 if (found_key.type != key.type || found_key.offset < new_size)
2917 goto next_key;
2918
2919 /*
2920 * if the key for the next leaf isn't a csum key from this objectid,
2921 * we can't be sure there aren't good items inside this leaf.
2922 * Bail out
2923 */
2924 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2925 goto out;
2926
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002927 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2928 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002929 /*
2930 * it is safe to delete this leaf, it contains only
2931 * csum items from this inode at an offset >= new_size
2932 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002933 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002934 BUG_ON(ret);
2935
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002936 if (root->ref_cows && leaf_gen < trans->transid) {
2937 ref = btrfs_alloc_leaf_ref(root, 0);
2938 if (ref) {
2939 ref->root_gen = root->root_key.offset;
2940 ref->bytenr = leaf_start;
2941 ref->owner = 0;
2942 ref->generation = leaf_gen;
2943 ref->nritems = 0;
2944
Chris Masonbd56b302009-02-04 09:27:02 -05002945 btrfs_sort_leaf_ref(ref);
2946
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002947 ret = btrfs_add_leaf_ref(root, ref, 0);
2948 WARN_ON(ret);
2949 btrfs_free_leaf_ref(root, ref);
2950 } else {
2951 WARN_ON(1);
2952 }
2953 }
Chris Mason323ac952008-10-01 19:05:46 -04002954next_key:
2955 btrfs_release_path(root, path);
2956
2957 if (other_key.objectid == inode->i_ino &&
2958 other_key.type == key.type && other_key.offset > key.offset) {
2959 key.offset = other_key.offset;
2960 cond_resched();
2961 goto again;
2962 }
2963 ret = 0;
2964out:
2965 /* fixup any changes we've made to the path */
2966 path->lowest_level = 0;
2967 path->keep_locks = 0;
2968 btrfs_release_path(root, path);
2969 return ret;
2970}
2971
Chris Masond20f7042008-12-08 16:58:54 -05002972#endif
2973
Chris Mason323ac952008-10-01 19:05:46 -04002974/*
Chris Mason39279cc2007-06-12 06:35:45 -04002975 * this can truncate away extent items, csum items and directory items.
2976 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002977 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002978 *
2979 * csum items that cross the new i_size are truncated to the new size
2980 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002981 *
2982 * min_type is the minimum key type to truncate down to. If set to 0, this
2983 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002984 */
Yan, Zheng80825102009-11-12 09:35:36 +00002985int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2986 struct btrfs_root *root,
2987 struct inode *inode,
2988 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002989{
Chris Mason39279cc2007-06-12 06:35:45 -04002990 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002991 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002992 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00002993 struct btrfs_key key;
2994 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002995 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002996 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002997 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002998 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00002999 u64 mask = root->sectorsize - 1;
3000 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003001 int found_extent;
3002 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003003 int pending_del_nr = 0;
3004 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003005 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003006 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003007 int ret;
3008 int err = 0;
3009
3010 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003011
Chris Masone02119d2008-09-05 16:13:11 -04003012 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003013 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003014
Chris Mason39279cc2007-06-12 06:35:45 -04003015 path = btrfs_alloc_path();
3016 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003017 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003018
Chris Mason39279cc2007-06-12 06:35:45 -04003019 key.objectid = inode->i_ino;
3020 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003021 key.type = (u8)-1;
3022
Chris Mason85e21ba2008-01-29 15:11:36 -05003023search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003024 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003025 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003026 if (ret < 0) {
3027 err = ret;
3028 goto out;
3029 }
Chris Masond3977122009-01-05 21:25:51 -05003030
Chris Mason85e21ba2008-01-29 15:11:36 -05003031 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003032 /* there are no items in the tree for us to truncate, we're
3033 * done
3034 */
Yan, Zheng80825102009-11-12 09:35:36 +00003035 if (path->slots[0] == 0)
3036 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003037 path->slots[0]--;
3038 }
3039
Chris Masond3977122009-01-05 21:25:51 -05003040 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003041 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003042 leaf = path->nodes[0];
3043 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3044 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003045 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003046
Chris Mason5f39d392007-10-15 16:14:19 -04003047 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003048 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003049
Chris Mason85e21ba2008-01-29 15:11:36 -05003050 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003051 break;
3052
Chris Mason5f39d392007-10-15 16:14:19 -04003053 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003054 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003055 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003056 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003057 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003058 encoding = btrfs_file_extent_compression(leaf, fi);
3059 encoding |= btrfs_file_extent_encryption(leaf, fi);
3060 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3061
Chris Mason179e29e2007-11-01 11:28:41 -04003062 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003063 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003064 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003065 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003066 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003067 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003068 }
Yan008630c2007-11-07 13:31:09 -05003069 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003070 }
Yan, Zheng80825102009-11-12 09:35:36 +00003071 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003072 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003073 } else {
3074 if (item_end < new_size)
3075 break;
3076 if (found_key.offset >= new_size)
3077 del_item = 1;
3078 else
3079 del_item = 0;
3080 }
Chris Mason39279cc2007-06-12 06:35:45 -04003081 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003082 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003083 if (found_type != BTRFS_EXTENT_DATA_KEY)
3084 goto delete;
3085
3086 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003087 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003088 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003089 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003090 u64 orig_num_bytes =
3091 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003092 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003093 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003094 extent_num_bytes = extent_num_bytes &
3095 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003096 btrfs_set_file_extent_num_bytes(leaf, fi,
3097 extent_num_bytes);
3098 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003099 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003100 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003101 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003102 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003103 } else {
Chris Masondb945352007-10-15 16:15:53 -04003104 extent_num_bytes =
3105 btrfs_file_extent_disk_num_bytes(leaf,
3106 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003107 extent_offset = found_key.offset -
3108 btrfs_file_extent_offset(leaf, fi);
3109
Chris Mason39279cc2007-06-12 06:35:45 -04003110 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003111 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003112 if (extent_start != 0) {
3113 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003114 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003115 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003116 }
3117 }
Chris Mason90692182008-02-08 13:49:28 -05003118 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003119 /*
3120 * we can't truncate inline items that have had
3121 * special encodings
3122 */
3123 if (!del_item &&
3124 btrfs_file_extent_compression(leaf, fi) == 0 &&
3125 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3126 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003127 u32 size = new_size - found_key.offset;
3128
3129 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003130 inode_sub_bytes(inode, item_end + 1 -
3131 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003132 }
3133 size =
3134 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003135 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003136 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003137 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003138 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003139 inode_sub_bytes(inode, item_end + 1 -
3140 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003141 }
Chris Mason39279cc2007-06-12 06:35:45 -04003142 }
Chris Mason179e29e2007-11-01 11:28:41 -04003143delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003144 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003145 if (!pending_del_nr) {
3146 /* no pending yet, add ourselves */
3147 pending_del_slot = path->slots[0];
3148 pending_del_nr = 1;
3149 } else if (pending_del_nr &&
3150 path->slots[0] + 1 == pending_del_slot) {
3151 /* hop on the pending chunk */
3152 pending_del_nr++;
3153 pending_del_slot = path->slots[0];
3154 } else {
Chris Masond3977122009-01-05 21:25:51 -05003155 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003156 }
Chris Mason39279cc2007-06-12 06:35:45 -04003157 } else {
3158 break;
3159 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003160 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04003161 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003162 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003163 extent_num_bytes, 0,
3164 btrfs_header_owner(leaf),
3165 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003166 BUG_ON(ret);
3167 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003168
Yan, Zheng80825102009-11-12 09:35:36 +00003169 if (found_type == BTRFS_INODE_ITEM_KEY)
3170 break;
3171
3172 if (path->slots[0] == 0 ||
3173 path->slots[0] != pending_del_slot) {
3174 if (root->ref_cows) {
3175 err = -EAGAIN;
3176 goto out;
3177 }
3178 if (pending_del_nr) {
3179 ret = btrfs_del_items(trans, root, path,
3180 pending_del_slot,
3181 pending_del_nr);
3182 BUG_ON(ret);
3183 pending_del_nr = 0;
3184 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003185 btrfs_release_path(root, path);
3186 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003187 } else {
3188 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003189 }
Chris Mason39279cc2007-06-12 06:35:45 -04003190 }
Yan, Zheng80825102009-11-12 09:35:36 +00003191out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003192 if (pending_del_nr) {
3193 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3194 pending_del_nr);
3195 }
Chris Mason39279cc2007-06-12 06:35:45 -04003196 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003197 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003198}
3199
Chris Masona52d9a82007-08-27 16:49:44 -04003200/*
3201 * taken from block_truncate_page, but does cow as it zeros out
3202 * any bytes left in the last page in the file.
3203 */
3204static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3205{
3206 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003207 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003208 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3209 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003210 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003211 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003212 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003213 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3214 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3215 struct page *page;
3216 int ret = 0;
3217 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003218 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003219
3220 if ((offset & (blocksize - 1)) == 0)
3221 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003222 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
3223 if (ret)
3224 goto out;
3225
3226 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
3227 if (ret)
3228 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003229
3230 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003231again:
Chris Masona52d9a82007-08-27 16:49:44 -04003232 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003233 if (!page) {
3234 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3235 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Masona52d9a82007-08-27 16:49:44 -04003236 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003237 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003238
3239 page_start = page_offset(page);
3240 page_end = page_start + PAGE_CACHE_SIZE - 1;
3241
Chris Masona52d9a82007-08-27 16:49:44 -04003242 if (!PageUptodate(page)) {
3243 ret = btrfs_readpage(NULL, page);
3244 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003245 if (page->mapping != mapping) {
3246 unlock_page(page);
3247 page_cache_release(page);
3248 goto again;
3249 }
Chris Masona52d9a82007-08-27 16:49:44 -04003250 if (!PageUptodate(page)) {
3251 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003252 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003253 }
3254 }
Chris Mason211c17f2008-05-15 09:13:45 -04003255 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003256
Josef Bacik2ac55d42010-02-03 19:33:23 +00003257 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3258 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003259 set_page_extent_mapped(page);
3260
3261 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3262 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003263 unlock_extent_cached(io_tree, page_start, page_end,
3264 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003265 unlock_page(page);
3266 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003267 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003268 btrfs_put_ordered_extent(ordered);
3269 goto again;
3270 }
3271
Josef Bacik2ac55d42010-02-03 19:33:23 +00003272 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003273 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003274 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003275
Josef Bacik2ac55d42010-02-03 19:33:23 +00003276 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3277 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003278 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003279 unlock_extent_cached(io_tree, page_start, page_end,
3280 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003281 goto out_unlock;
3282 }
3283
Chris Masone6dcd2d2008-07-17 12:53:50 -04003284 ret = 0;
3285 if (offset != PAGE_CACHE_SIZE) {
3286 kaddr = kmap(page);
3287 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3288 flush_dcache_page(page);
3289 kunmap(page);
3290 }
Chris Mason247e7432008-07-17 12:53:51 -04003291 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003292 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003293 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3294 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003295
Chris Mason89642222008-07-24 09:41:53 -04003296out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003297 if (ret)
3298 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3299 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003300 unlock_page(page);
3301 page_cache_release(page);
3302out:
3303 return ret;
3304}
3305
Yan Zheng9036c102008-10-30 14:19:41 -04003306int btrfs_cont_expand(struct inode *inode, loff_t size)
3307{
3308 struct btrfs_trans_handle *trans;
3309 struct btrfs_root *root = BTRFS_I(inode)->root;
3310 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003311 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003312 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003313 u64 mask = root->sectorsize - 1;
3314 u64 hole_start = (inode->i_size + mask) & ~mask;
3315 u64 block_end = (size + mask) & ~mask;
3316 u64 last_byte;
3317 u64 cur_offset;
3318 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003319 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003320
3321 if (size <= hole_start)
3322 return 0;
3323
Yan Zheng9036c102008-10-30 14:19:41 -04003324 while (1) {
3325 struct btrfs_ordered_extent *ordered;
3326 btrfs_wait_ordered_range(inode, hole_start,
3327 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003328 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3329 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003330 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3331 if (!ordered)
3332 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003333 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3334 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003335 btrfs_put_ordered_extent(ordered);
3336 }
3337
Yan Zheng9036c102008-10-30 14:19:41 -04003338 cur_offset = hole_start;
3339 while (1) {
3340 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3341 block_end - cur_offset, 0);
3342 BUG_ON(IS_ERR(em) || !em);
3343 last_byte = min(extent_map_end(em), block_end);
3344 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003345 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003346 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003347 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003348
Yan, Zhenga22285a2010-05-16 10:48:46 -04003349 trans = btrfs_start_transaction(root, 2);
3350 if (IS_ERR(trans)) {
3351 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003352 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003353 }
Yan, Zheng80825102009-11-12 09:35:36 +00003354 btrfs_set_trans_block_group(trans, inode);
3355
3356 err = btrfs_drop_extents(trans, inode, cur_offset,
3357 cur_offset + hole_size,
3358 &hint_byte, 1);
3359 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003360
Yan Zheng9036c102008-10-30 14:19:41 -04003361 err = btrfs_insert_file_extent(trans, root,
3362 inode->i_ino, cur_offset, 0,
3363 0, hole_size, 0, hole_size,
3364 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003365 BUG_ON(err);
3366
Yan Zheng9036c102008-10-30 14:19:41 -04003367 btrfs_drop_extent_cache(inode, hole_start,
3368 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003369
3370 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003371 }
3372 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003373 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003374 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003375 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003376 break;
3377 }
3378
Yan, Zhenga22285a2010-05-16 10:48:46 -04003379 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003380 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3381 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003382 return err;
3383}
3384
Yan, Zheng80825102009-11-12 09:35:36 +00003385static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3386{
3387 struct btrfs_root *root = BTRFS_I(inode)->root;
3388 struct btrfs_trans_handle *trans;
3389 unsigned long nr;
3390 int ret;
3391
3392 if (attr->ia_size == inode->i_size)
3393 return 0;
3394
3395 if (attr->ia_size > inode->i_size) {
3396 unsigned long limit;
3397 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3398 if (attr->ia_size > inode->i_sb->s_maxbytes)
3399 return -EFBIG;
3400 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3401 send_sig(SIGXFSZ, current, 0);
3402 return -EFBIG;
3403 }
3404 }
3405
Yan, Zheng80825102009-11-12 09:35:36 +00003406 trans = btrfs_start_transaction(root, 1);
3407 btrfs_set_trans_block_group(trans, inode);
3408
3409 ret = btrfs_orphan_add(trans, inode);
3410 BUG_ON(ret);
3411
3412 nr = trans->blocks_used;
3413 btrfs_end_transaction(trans, root);
Yan, Zheng80825102009-11-12 09:35:36 +00003414 btrfs_btree_balance_dirty(root, nr);
3415
3416 if (attr->ia_size > inode->i_size) {
3417 ret = btrfs_cont_expand(inode, attr->ia_size);
3418 if (ret) {
3419 btrfs_truncate(inode);
3420 return ret;
3421 }
3422
3423 i_size_write(inode, attr->ia_size);
3424 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3425
3426 trans = btrfs_start_transaction(root, 1);
3427 btrfs_set_trans_block_group(trans, inode);
3428
3429 ret = btrfs_update_inode(trans, root, inode);
3430 BUG_ON(ret);
3431 if (inode->i_nlink > 0) {
3432 ret = btrfs_orphan_del(trans, inode);
3433 BUG_ON(ret);
3434 }
3435 nr = trans->blocks_used;
3436 btrfs_end_transaction(trans, root);
3437 btrfs_btree_balance_dirty(root, nr);
3438 return 0;
3439 }
3440
3441 /*
3442 * We're truncating a file that used to have good data down to
3443 * zero. Make sure it gets into the ordered flush list so that
3444 * any new writes get down to disk quickly.
3445 */
3446 if (attr->ia_size == 0)
3447 BTRFS_I(inode)->ordered_data_close = 1;
3448
3449 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3450 ret = vmtruncate(inode, attr->ia_size);
3451 BUG_ON(ret);
3452
3453 return 0;
3454}
3455
Chris Mason39279cc2007-06-12 06:35:45 -04003456static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3457{
3458 struct inode *inode = dentry->d_inode;
3459 int err;
3460
3461 err = inode_change_ok(inode, attr);
3462 if (err)
3463 return err;
3464
Chris Mason5a3f23d2009-03-31 13:27:11 -04003465 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003466 err = btrfs_setattr_size(inode, attr);
3467 if (err)
3468 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003469 }
Yan, Zheng80825102009-11-12 09:35:36 +00003470 attr->ia_valid &= ~ATTR_SIZE;
Yan Zheng9036c102008-10-30 14:19:41 -04003471
Yan, Zheng80825102009-11-12 09:35:36 +00003472 if (attr->ia_valid)
3473 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003474
3475 if (!err && ((attr->ia_valid & ATTR_MODE)))
3476 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003477 return err;
3478}
Chris Mason61295eb2008-01-14 16:24:38 -05003479
Chris Mason39279cc2007-06-12 06:35:45 -04003480void btrfs_delete_inode(struct inode *inode)
3481{
3482 struct btrfs_trans_handle *trans;
3483 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003484 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003485 int ret;
3486
3487 truncate_inode_pages(&inode->i_data, 0);
3488 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003489 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003490 goto no_delete;
3491 }
Chris Mason4a096752008-07-21 10:29:44 -04003492 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003493
Yan, Zhengc71bf092009-11-12 09:34:40 +00003494 if (root->fs_info->log_root_recovering) {
3495 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3496 goto no_delete;
3497 }
3498
Yan, Zheng76dda932009-09-21 16:00:26 -04003499 if (inode->i_nlink > 0) {
3500 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3501 goto no_delete;
3502 }
3503
Chris Masondbe674a2008-07-17 12:54:05 -04003504 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003505
Yan, Zheng80825102009-11-12 09:35:36 +00003506 while (1) {
3507 trans = btrfs_start_transaction(root, 1);
3508 btrfs_set_trans_block_group(trans, inode);
3509 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3510
3511 if (ret != -EAGAIN)
3512 break;
3513
3514 nr = trans->blocks_used;
3515 btrfs_end_transaction(trans, root);
3516 trans = NULL;
3517 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003518 }
3519
Yan, Zheng80825102009-11-12 09:35:36 +00003520 if (ret == 0) {
3521 ret = btrfs_orphan_del(trans, inode);
3522 BUG_ON(ret);
3523 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003524
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003525 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003526 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003527 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003528no_delete:
3529 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003530 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003531}
3532
3533/*
3534 * this returns the key found in the dir entry in the location pointer.
3535 * If no dir entries were found, location->objectid is 0.
3536 */
3537static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3538 struct btrfs_key *location)
3539{
3540 const char *name = dentry->d_name.name;
3541 int namelen = dentry->d_name.len;
3542 struct btrfs_dir_item *di;
3543 struct btrfs_path *path;
3544 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003545 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003546
3547 path = btrfs_alloc_path();
3548 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003549
Chris Mason39279cc2007-06-12 06:35:45 -04003550 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3551 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003552 if (IS_ERR(di))
3553 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003554
3555 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003556 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003557
Chris Mason5f39d392007-10-15 16:14:19 -04003558 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003559out:
Chris Mason39279cc2007-06-12 06:35:45 -04003560 btrfs_free_path(path);
3561 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003562out_err:
3563 location->objectid = 0;
3564 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003565}
3566
3567/*
3568 * when we hit a tree root in a directory, the btrfs part of the inode
3569 * needs to be changed to reflect the root directory of the tree root. This
3570 * is kind of like crossing a mount point.
3571 */
3572static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003573 struct inode *dir,
3574 struct dentry *dentry,
3575 struct btrfs_key *location,
3576 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003577{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003578 struct btrfs_path *path;
3579 struct btrfs_root *new_root;
3580 struct btrfs_root_ref *ref;
3581 struct extent_buffer *leaf;
3582 int ret;
3583 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003584
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003585 path = btrfs_alloc_path();
3586 if (!path) {
3587 err = -ENOMEM;
3588 goto out;
3589 }
Chris Mason39279cc2007-06-12 06:35:45 -04003590
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003591 err = -ENOENT;
3592 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3593 BTRFS_I(dir)->root->root_key.objectid,
3594 location->objectid);
3595 if (ret) {
3596 if (ret < 0)
3597 err = ret;
3598 goto out;
3599 }
Chris Mason39279cc2007-06-12 06:35:45 -04003600
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003601 leaf = path->nodes[0];
3602 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3603 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3604 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3605 goto out;
3606
3607 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3608 (unsigned long)(ref + 1),
3609 dentry->d_name.len);
3610 if (ret)
3611 goto out;
3612
3613 btrfs_release_path(root->fs_info->tree_root, path);
3614
3615 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3616 if (IS_ERR(new_root)) {
3617 err = PTR_ERR(new_root);
3618 goto out;
3619 }
3620
3621 if (btrfs_root_refs(&new_root->root_item) == 0) {
3622 err = -ENOENT;
3623 goto out;
3624 }
3625
3626 *sub_root = new_root;
3627 location->objectid = btrfs_root_dirid(&new_root->root_item);
3628 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003629 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003630 err = 0;
3631out:
3632 btrfs_free_path(path);
3633 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003634}
3635
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003636static void inode_tree_add(struct inode *inode)
3637{
3638 struct btrfs_root *root = BTRFS_I(inode)->root;
3639 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003640 struct rb_node **p;
3641 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003642again:
3643 p = &root->inode_tree.rb_node;
3644 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003645
Yan, Zheng76dda932009-09-21 16:00:26 -04003646 if (hlist_unhashed(&inode->i_hash))
3647 return;
3648
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003649 spin_lock(&root->inode_lock);
3650 while (*p) {
3651 parent = *p;
3652 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3653
3654 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003655 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003656 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003657 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003658 else {
3659 WARN_ON(!(entry->vfs_inode.i_state &
3660 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003661 rb_erase(parent, &root->inode_tree);
3662 RB_CLEAR_NODE(parent);
3663 spin_unlock(&root->inode_lock);
3664 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003665 }
3666 }
3667 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3668 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3669 spin_unlock(&root->inode_lock);
3670}
3671
3672static void inode_tree_del(struct inode *inode)
3673{
3674 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003675 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003676
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003677 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003678 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003679 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003680 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003681 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003682 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003683 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003684
3685 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3686 synchronize_srcu(&root->fs_info->subvol_srcu);
3687 spin_lock(&root->inode_lock);
3688 empty = RB_EMPTY_ROOT(&root->inode_tree);
3689 spin_unlock(&root->inode_lock);
3690 if (empty)
3691 btrfs_add_dead_root(root);
3692 }
3693}
3694
3695int btrfs_invalidate_inodes(struct btrfs_root *root)
3696{
3697 struct rb_node *node;
3698 struct rb_node *prev;
3699 struct btrfs_inode *entry;
3700 struct inode *inode;
3701 u64 objectid = 0;
3702
3703 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3704
3705 spin_lock(&root->inode_lock);
3706again:
3707 node = root->inode_tree.rb_node;
3708 prev = NULL;
3709 while (node) {
3710 prev = node;
3711 entry = rb_entry(node, struct btrfs_inode, rb_node);
3712
3713 if (objectid < entry->vfs_inode.i_ino)
3714 node = node->rb_left;
3715 else if (objectid > entry->vfs_inode.i_ino)
3716 node = node->rb_right;
3717 else
3718 break;
3719 }
3720 if (!node) {
3721 while (prev) {
3722 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3723 if (objectid <= entry->vfs_inode.i_ino) {
3724 node = prev;
3725 break;
3726 }
3727 prev = rb_next(prev);
3728 }
3729 }
3730 while (node) {
3731 entry = rb_entry(node, struct btrfs_inode, rb_node);
3732 objectid = entry->vfs_inode.i_ino + 1;
3733 inode = igrab(&entry->vfs_inode);
3734 if (inode) {
3735 spin_unlock(&root->inode_lock);
3736 if (atomic_read(&inode->i_count) > 1)
3737 d_prune_aliases(inode);
3738 /*
3739 * btrfs_drop_inode will remove it from
3740 * the inode cache when its usage count
3741 * hits zero.
3742 */
3743 iput(inode);
3744 cond_resched();
3745 spin_lock(&root->inode_lock);
3746 goto again;
3747 }
3748
3749 if (cond_resched_lock(&root->inode_lock))
3750 goto again;
3751
3752 node = rb_next(node);
3753 }
3754 spin_unlock(&root->inode_lock);
3755 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003756}
3757
Chris Masone02119d2008-09-05 16:13:11 -04003758static int btrfs_init_locked_inode(struct inode *inode, void *p)
3759{
3760 struct btrfs_iget_args *args = p;
3761 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04003762 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003763 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003764 return 0;
3765}
3766
3767static int btrfs_find_actor(struct inode *inode, void *opaque)
3768{
3769 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003770 return args->ino == inode->i_ino &&
3771 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003772}
3773
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003774static struct inode *btrfs_iget_locked(struct super_block *s,
3775 u64 objectid,
3776 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003777{
3778 struct inode *inode;
3779 struct btrfs_iget_args args;
3780 args.ino = objectid;
3781 args.root = root;
3782
3783 inode = iget5_locked(s, objectid, btrfs_find_actor,
3784 btrfs_init_locked_inode,
3785 (void *)&args);
3786 return inode;
3787}
3788
Balaji Rao1a54ef82008-07-21 02:01:04 +05303789/* Get an inode object given its location and corresponding root.
3790 * Returns in *is_new if the inode was read from disk
3791 */
3792struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00003793 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303794{
3795 struct inode *inode;
3796
3797 inode = btrfs_iget_locked(s, location->objectid, root);
3798 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003799 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303800
3801 if (inode->i_state & I_NEW) {
3802 BTRFS_I(inode)->root = root;
3803 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3804 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003805
3806 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303807 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00003808 if (new)
3809 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05303810 }
3811
3812 return inode;
3813}
3814
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003815static struct inode *new_simple_dir(struct super_block *s,
3816 struct btrfs_key *key,
3817 struct btrfs_root *root)
3818{
3819 struct inode *inode = new_inode(s);
3820
3821 if (!inode)
3822 return ERR_PTR(-ENOMEM);
3823
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003824 BTRFS_I(inode)->root = root;
3825 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3826 BTRFS_I(inode)->dummy_inode = 1;
3827
3828 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3829 inode->i_op = &simple_dir_inode_operations;
3830 inode->i_fop = &simple_dir_operations;
3831 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3832 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3833
3834 return inode;
3835}
3836
Chris Mason3de45862008-11-17 21:02:50 -05003837struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003838{
Chris Masond3977122009-01-05 21:25:51 -05003839 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003840 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003841 struct btrfs_root *sub_root = root;
3842 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003843 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003844 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003845
Yan, Zheng76dda932009-09-21 16:00:26 -04003846 dentry->d_op = &btrfs_dentry_operations;
3847
Chris Mason39279cc2007-06-12 06:35:45 -04003848 if (dentry->d_name.len > BTRFS_NAME_LEN)
3849 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003850
Chris Mason39279cc2007-06-12 06:35:45 -04003851 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003852
Chris Mason39279cc2007-06-12 06:35:45 -04003853 if (ret < 0)
3854 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003855
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003856 if (location.objectid == 0)
3857 return NULL;
3858
3859 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00003860 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003861 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003862 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003863
3864 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3865
Yan, Zheng76dda932009-09-21 16:00:26 -04003866 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003867 ret = fixup_tree_root_location(root, dir, dentry,
3868 &location, &sub_root);
3869 if (ret < 0) {
3870 if (ret != -ENOENT)
3871 inode = ERR_PTR(ret);
3872 else
3873 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3874 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00003875 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003876 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003877 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3878
Yan, Zhengc71bf092009-11-12 09:34:40 +00003879 if (root != sub_root) {
3880 down_read(&root->fs_info->cleanup_work_sem);
3881 if (!(inode->i_sb->s_flags & MS_RDONLY))
3882 btrfs_orphan_cleanup(sub_root);
3883 up_read(&root->fs_info->cleanup_work_sem);
3884 }
3885
Chris Mason3de45862008-11-17 21:02:50 -05003886 return inode;
3887}
3888
Yan, Zheng76dda932009-09-21 16:00:26 -04003889static int btrfs_dentry_delete(struct dentry *dentry)
3890{
3891 struct btrfs_root *root;
3892
Yan, Zhengefefb142009-10-09 09:25:16 -04003893 if (!dentry->d_inode && !IS_ROOT(dentry))
3894 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04003895
Yan, Zhengefefb142009-10-09 09:25:16 -04003896 if (dentry->d_inode) {
3897 root = BTRFS_I(dentry->d_inode)->root;
3898 if (btrfs_root_refs(&root->root_item) == 0)
3899 return 1;
3900 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003901 return 0;
3902}
3903
Chris Mason3de45862008-11-17 21:02:50 -05003904static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3905 struct nameidata *nd)
3906{
3907 struct inode *inode;
3908
Chris Mason3de45862008-11-17 21:02:50 -05003909 inode = btrfs_lookup_dentry(dir, dentry);
3910 if (IS_ERR(inode))
3911 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003912
Chris Mason39279cc2007-06-12 06:35:45 -04003913 return d_splice_alias(inode, dentry);
3914}
3915
Chris Mason39279cc2007-06-12 06:35:45 -04003916static unsigned char btrfs_filetype_table[] = {
3917 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3918};
3919
David Woodhousecbdf5a22008-08-06 19:42:33 +01003920static int btrfs_real_readdir(struct file *filp, void *dirent,
3921 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003922{
Chris Mason6da6aba2007-12-18 16:15:09 -05003923 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003924 struct btrfs_root *root = BTRFS_I(inode)->root;
3925 struct btrfs_item *item;
3926 struct btrfs_dir_item *di;
3927 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003928 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003929 struct btrfs_path *path;
3930 int ret;
3931 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003932 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003933 int slot;
3934 int advance;
3935 unsigned char d_type;
3936 int over = 0;
3937 u32 di_cur;
3938 u32 di_total;
3939 u32 di_len;
3940 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003941 char tmp_name[32];
3942 char *name_ptr;
3943 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003944
3945 /* FIXME, use a real flag for deciding about the key type */
3946 if (root->fs_info->tree_root == root)
3947 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003948
Chris Mason39544012007-12-12 14:38:19 -05003949 /* special case for "." */
3950 if (filp->f_pos == 0) {
3951 over = filldir(dirent, ".", 1,
3952 1, inode->i_ino,
3953 DT_DIR);
3954 if (over)
3955 return 0;
3956 filp->f_pos = 1;
3957 }
Chris Mason39544012007-12-12 14:38:19 -05003958 /* special case for .., just use the back ref */
3959 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003960 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003961 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003962 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003963 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003964 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003965 filp->f_pos = 2;
3966 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003967 path = btrfs_alloc_path();
3968 path->reada = 2;
3969
Chris Mason39279cc2007-06-12 06:35:45 -04003970 btrfs_set_key_type(&key, key_type);
3971 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003972 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003973
Chris Mason39279cc2007-06-12 06:35:45 -04003974 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3975 if (ret < 0)
3976 goto err;
3977 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003978
3979 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003980 leaf = path->nodes[0];
3981 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003982 slot = path->slots[0];
3983 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003984 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003985 ret = btrfs_next_leaf(root, path);
3986 if (ret)
3987 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003988 leaf = path->nodes[0];
3989 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003990 slot = path->slots[0];
3991 } else {
3992 slot++;
3993 path->slots[0]++;
3994 }
3995 }
Chris Mason3de45862008-11-17 21:02:50 -05003996
Chris Mason39279cc2007-06-12 06:35:45 -04003997 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003998 item = btrfs_item_nr(leaf, slot);
3999 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4000
4001 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004002 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004003 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004004 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004005 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04004006 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04004007
4008 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01004009
Chris Mason39279cc2007-06-12 06:35:45 -04004010 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4011 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004012 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004013
4014 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004015 struct btrfs_key location;
4016
4017 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004018 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004019 name_ptr = tmp_name;
4020 } else {
4021 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004022 if (!name_ptr) {
4023 ret = -ENOMEM;
4024 goto err;
4025 }
Chris Mason5f39d392007-10-15 16:14:19 -04004026 }
4027 read_extent_buffer(leaf, name_ptr,
4028 (unsigned long)(di + 1), name_len);
4029
4030 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4031 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004032
4033 /* is this a reference to our own snapshot? If so
4034 * skip it
4035 */
4036 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4037 location.objectid == root->root_key.objectid) {
4038 over = 0;
4039 goto skip;
4040 }
Chris Mason5f39d392007-10-15 16:14:19 -04004041 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004042 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004043 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004044
Chris Mason3de45862008-11-17 21:02:50 -05004045skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004046 if (name_ptr != tmp_name)
4047 kfree(name_ptr);
4048
Chris Mason39279cc2007-06-12 06:35:45 -04004049 if (over)
4050 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004051 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004052 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004053 di_cur += di_len;
4054 di = (struct btrfs_dir_item *)((char *)di + di_len);
4055 }
4056 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004057
4058 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004059 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004060 /*
4061 * 32-bit glibc will use getdents64, but then strtol -
4062 * so the last number we can serve is this.
4063 */
4064 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004065 else
4066 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004067nopos:
4068 ret = 0;
4069err:
Chris Mason39279cc2007-06-12 06:35:45 -04004070 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004071 return ret;
4072}
4073
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004074int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004075{
4076 struct btrfs_root *root = BTRFS_I(inode)->root;
4077 struct btrfs_trans_handle *trans;
4078 int ret = 0;
4079
Yan Zhengc146afa2008-11-12 14:34:12 -05004080 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004081 return 0;
4082
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004083 if (wbc->sync_mode == WB_SYNC_ALL) {
Chris Masonf9295742008-07-17 12:54:14 -04004084 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004085 btrfs_set_trans_block_group(trans, inode);
4086 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004087 }
4088 return ret;
4089}
4090
4091/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004092 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004093 * inode changes. But, it is most likely to find the inode in cache.
4094 * FIXME, needs more benchmarking...there are no reasons other than performance
4095 * to keep or drop this code.
4096 */
4097void btrfs_dirty_inode(struct inode *inode)
4098{
4099 struct btrfs_root *root = BTRFS_I(inode)->root;
4100 struct btrfs_trans_handle *trans;
4101
Chris Masonf9295742008-07-17 12:54:14 -04004102 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004103 btrfs_set_trans_block_group(trans, inode);
4104 btrfs_update_inode(trans, root, inode);
4105 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004106}
4107
Chris Masond352ac62008-09-29 15:18:18 -04004108/*
4109 * find the highest existing sequence number in a directory
4110 * and then set the in-memory index_cnt variable to reflect
4111 * free sequence numbers
4112 */
Josef Bacikaec74772008-07-24 12:12:38 -04004113static int btrfs_set_inode_index_count(struct inode *inode)
4114{
4115 struct btrfs_root *root = BTRFS_I(inode)->root;
4116 struct btrfs_key key, found_key;
4117 struct btrfs_path *path;
4118 struct extent_buffer *leaf;
4119 int ret;
4120
4121 key.objectid = inode->i_ino;
4122 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4123 key.offset = (u64)-1;
4124
4125 path = btrfs_alloc_path();
4126 if (!path)
4127 return -ENOMEM;
4128
4129 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4130 if (ret < 0)
4131 goto out;
4132 /* FIXME: we should be able to handle this */
4133 if (ret == 0)
4134 goto out;
4135 ret = 0;
4136
4137 /*
4138 * MAGIC NUMBER EXPLANATION:
4139 * since we search a directory based on f_pos we have to start at 2
4140 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4141 * else has to start at 2
4142 */
4143 if (path->slots[0] == 0) {
4144 BTRFS_I(inode)->index_cnt = 2;
4145 goto out;
4146 }
4147
4148 path->slots[0]--;
4149
4150 leaf = path->nodes[0];
4151 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4152
4153 if (found_key.objectid != inode->i_ino ||
4154 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4155 BTRFS_I(inode)->index_cnt = 2;
4156 goto out;
4157 }
4158
4159 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4160out:
4161 btrfs_free_path(path);
4162 return ret;
4163}
4164
Chris Masond352ac62008-09-29 15:18:18 -04004165/*
4166 * helper to find a free sequence number in a given directory. This current
4167 * code is very simple, later versions will do smarter things in the btree
4168 */
Chris Mason3de45862008-11-17 21:02:50 -05004169int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004170{
4171 int ret = 0;
4172
4173 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4174 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004175 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004176 return ret;
4177 }
4178
Chris Mason00e4e6b2008-08-05 11:18:09 -04004179 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004180 BTRFS_I(dir)->index_cnt++;
4181
4182 return ret;
4183}
4184
Chris Mason39279cc2007-06-12 06:35:45 -04004185static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4186 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004187 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004188 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004189 u64 ref_objectid, u64 objectid,
4190 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004191{
4192 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004193 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004194 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004195 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004196 struct btrfs_inode_ref *ref;
4197 struct btrfs_key key[2];
4198 u32 sizes[2];
4199 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004200 int ret;
4201 int owner;
4202
Chris Mason5f39d392007-10-15 16:14:19 -04004203 path = btrfs_alloc_path();
4204 BUG_ON(!path);
4205
Chris Mason39279cc2007-06-12 06:35:45 -04004206 inode = new_inode(root->fs_info->sb);
4207 if (!inode)
4208 return ERR_PTR(-ENOMEM);
4209
Josef Bacikaec74772008-07-24 12:12:38 -04004210 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004211 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004212 if (ret) {
4213 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004214 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004215 }
Josef Bacikaec74772008-07-24 12:12:38 -04004216 }
4217 /*
4218 * index_cnt is ignored for everything but a dir,
4219 * btrfs_get_inode_index_count has an explanation for the magic
4220 * number
4221 */
4222 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004223 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004224 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05004225 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004226
Chris Mason39279cc2007-06-12 06:35:45 -04004227 if (mode & S_IFDIR)
4228 owner = 0;
4229 else
4230 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004231 BTRFS_I(inode)->block_group =
4232 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004233
4234 key[0].objectid = objectid;
4235 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4236 key[0].offset = 0;
4237
4238 key[1].objectid = objectid;
4239 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4240 key[1].offset = ref_objectid;
4241
4242 sizes[0] = sizeof(struct btrfs_inode_item);
4243 sizes[1] = name_len + sizeof(*ref);
4244
Chris Masonb9473432009-03-13 11:00:37 -04004245 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004246 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4247 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004248 goto fail;
4249
Chris Mason79683f22008-11-19 22:00:53 -05004250 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05004251
Chris Mason42f15d72009-02-06 11:35:57 -05004252 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05004253 inode->i_gid = dir->i_gid;
4254 if (S_ISDIR(mode))
4255 mode |= S_ISGID;
4256 } else
4257 inode->i_gid = current_fsgid();
4258
Chris Mason39279cc2007-06-12 06:35:45 -04004259 inode->i_mode = mode;
4260 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004261 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004262 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004263 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4264 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004265 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004266
4267 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4268 struct btrfs_inode_ref);
4269 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004270 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004271 ptr = (unsigned long)(ref + 1);
4272 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4273
Chris Mason5f39d392007-10-15 16:14:19 -04004274 btrfs_mark_buffer_dirty(path->nodes[0]);
4275 btrfs_free_path(path);
4276
Chris Mason39279cc2007-06-12 06:35:45 -04004277 location = &BTRFS_I(inode)->location;
4278 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004279 location->offset = 0;
4280 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4281
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004282 btrfs_inherit_iflags(inode, dir);
4283
Chris Mason94272162009-07-02 12:26:06 -04004284 if ((mode & S_IFREG)) {
4285 if (btrfs_test_opt(root, NODATASUM))
4286 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4287 if (btrfs_test_opt(root, NODATACOW))
4288 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4289 }
4290
Chris Mason39279cc2007-06-12 06:35:45 -04004291 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004292 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004293 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004294fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004295 if (dir)
4296 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004297 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004298 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004299 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004300}
4301
4302static inline u8 btrfs_inode_type(struct inode *inode)
4303{
4304 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4305}
4306
Chris Masond352ac62008-09-29 15:18:18 -04004307/*
4308 * utility function to add 'inode' into 'parent_inode' with
4309 * a give name and a given sequence number.
4310 * if 'add_backref' is true, also insert a backref from the
4311 * inode to the parent directory.
4312 */
Chris Masone02119d2008-09-05 16:13:11 -04004313int btrfs_add_link(struct btrfs_trans_handle *trans,
4314 struct inode *parent_inode, struct inode *inode,
4315 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004316{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004317 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004318 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004319 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004320
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004321 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4322 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4323 } else {
4324 key.objectid = inode->i_ino;
4325 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4326 key.offset = 0;
4327 }
Chris Mason39279cc2007-06-12 06:35:45 -04004328
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004329 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4330 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4331 key.objectid, root->root_key.objectid,
4332 parent_inode->i_ino,
4333 index, name, name_len);
4334 } else if (add_backref) {
4335 ret = btrfs_insert_inode_ref(trans, root,
4336 name, name_len, inode->i_ino,
4337 parent_inode->i_ino, index);
4338 }
4339
Chris Mason39279cc2007-06-12 06:35:45 -04004340 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004341 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4342 parent_inode->i_ino, &key,
4343 btrfs_inode_type(inode), index);
4344 BUG_ON(ret);
4345
Chris Masondbe674a2008-07-17 12:54:05 -04004346 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004347 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004348 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004349 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004350 }
4351 return ret;
4352}
4353
4354static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004355 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004356 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004357{
Chris Masone02119d2008-09-05 16:13:11 -04004358 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4359 inode, dentry->d_name.name,
4360 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004361 if (!err) {
4362 d_instantiate(dentry, inode);
4363 return 0;
4364 }
4365 if (err > 0)
4366 err = -EEXIST;
4367 return err;
4368}
4369
Josef Bacik618e21d2007-07-11 10:18:17 -04004370static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4371 int mode, dev_t rdev)
4372{
4373 struct btrfs_trans_handle *trans;
4374 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004375 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004376 int err;
4377 int drop_inode = 0;
4378 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004379 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004380 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004381
4382 if (!new_valid_dev(rdev))
4383 return -EINVAL;
4384
Yan, Zhenga22285a2010-05-16 10:48:46 -04004385 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4386 if (err)
4387 return err;
4388
Josef Bacik9ed74f22009-09-11 16:12:44 -04004389 /*
4390 * 2 for inode item and ref
4391 * 2 for dir items
4392 * 1 for xattr if selinux is on
4393 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004394 trans = btrfs_start_transaction(root, 5);
4395 if (IS_ERR(trans))
4396 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004397
Josef Bacik618e21d2007-07-11 10:18:17 -04004398 btrfs_set_trans_block_group(trans, dir);
4399
Josef Bacikaec74772008-07-24 12:12:38 -04004400 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004401 dentry->d_name.len,
4402 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004403 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004404 err = PTR_ERR(inode);
4405 if (IS_ERR(inode))
4406 goto out_unlock;
4407
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004408 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004409 if (err) {
4410 drop_inode = 1;
4411 goto out_unlock;
4412 }
4413
Josef Bacik618e21d2007-07-11 10:18:17 -04004414 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004415 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004416 if (err)
4417 drop_inode = 1;
4418 else {
4419 inode->i_op = &btrfs_special_inode_operations;
4420 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004421 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004422 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004423 btrfs_update_inode_block_group(trans, inode);
4424 btrfs_update_inode_block_group(trans, dir);
4425out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004426 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004427 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004428 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004429 if (drop_inode) {
4430 inode_dec_link_count(inode);
4431 iput(inode);
4432 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004433 return err;
4434}
4435
Chris Mason39279cc2007-06-12 06:35:45 -04004436static int btrfs_create(struct inode *dir, struct dentry *dentry,
4437 int mode, struct nameidata *nd)
4438{
4439 struct btrfs_trans_handle *trans;
4440 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004441 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004442 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004443 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004444 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004445 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004446 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004447
Yan, Zhenga22285a2010-05-16 10:48:46 -04004448 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4449 if (err)
4450 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004451 /*
4452 * 2 for inode item and ref
4453 * 2 for dir items
4454 * 1 for xattr if selinux is on
4455 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004456 trans = btrfs_start_transaction(root, 5);
4457 if (IS_ERR(trans))
4458 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004459
Chris Mason39279cc2007-06-12 06:35:45 -04004460 btrfs_set_trans_block_group(trans, dir);
4461
Josef Bacikaec74772008-07-24 12:12:38 -04004462 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004463 dentry->d_name.len,
4464 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004465 objectid, BTRFS_I(dir)->block_group, mode,
4466 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004467 err = PTR_ERR(inode);
4468 if (IS_ERR(inode))
4469 goto out_unlock;
4470
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004471 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004472 if (err) {
4473 drop_inode = 1;
4474 goto out_unlock;
4475 }
4476
Chris Mason39279cc2007-06-12 06:35:45 -04004477 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004478 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004479 if (err)
4480 drop_inode = 1;
4481 else {
4482 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004483 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004484 inode->i_fop = &btrfs_file_operations;
4485 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004486 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004487 }
Chris Mason39279cc2007-06-12 06:35:45 -04004488 btrfs_update_inode_block_group(trans, inode);
4489 btrfs_update_inode_block_group(trans, dir);
4490out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004491 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004492 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004493 if (drop_inode) {
4494 inode_dec_link_count(inode);
4495 iput(inode);
4496 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004497 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004498 return err;
4499}
4500
4501static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4502 struct dentry *dentry)
4503{
4504 struct btrfs_trans_handle *trans;
4505 struct btrfs_root *root = BTRFS_I(dir)->root;
4506 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004507 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004508 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004509 int err;
4510 int drop_inode = 0;
4511
4512 if (inode->i_nlink == 0)
4513 return -ENOENT;
4514
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004515 /* do not allow sys_link's with other subvols of the same device */
4516 if (root->objectid != BTRFS_I(inode)->root->objectid)
4517 return -EPERM;
4518
Josef Bacik9ed74f22009-09-11 16:12:44 -04004519 btrfs_inc_nlink(inode);
4520
Chris Mason3de45862008-11-17 21:02:50 -05004521 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004522 if (err)
4523 goto fail;
4524
Yan, Zhenga22285a2010-05-16 10:48:46 -04004525 /*
4526 * 1 item for inode ref
4527 * 2 items for dir items
4528 */
4529 trans = btrfs_start_transaction(root, 3);
4530 if (IS_ERR(trans)) {
4531 err = PTR_ERR(trans);
4532 goto fail;
4533 }
Chris Mason5f39d392007-10-15 16:14:19 -04004534
Chris Mason39279cc2007-06-12 06:35:45 -04004535 btrfs_set_trans_block_group(trans, dir);
4536 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004537
Chris Mason00e4e6b2008-08-05 11:18:09 -04004538 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004539
Yan, Zhenga5719522009-09-24 09:17:31 -04004540 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004541 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004542 } else {
4543 btrfs_update_inode_block_group(trans, dir);
4544 err = btrfs_update_inode(trans, root, inode);
4545 BUG_ON(err);
4546 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4547 }
Chris Mason39279cc2007-06-12 06:35:45 -04004548
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004549 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004550 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004551fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004552 if (drop_inode) {
4553 inode_dec_link_count(inode);
4554 iput(inode);
4555 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004556 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004557 return err;
4558}
4559
Chris Mason39279cc2007-06-12 06:35:45 -04004560static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4561{
Chris Masonb9d86662008-05-02 16:13:49 -04004562 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004563 struct btrfs_trans_handle *trans;
4564 struct btrfs_root *root = BTRFS_I(dir)->root;
4565 int err = 0;
4566 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004567 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004568 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004569 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004570
Yan, Zhenga22285a2010-05-16 10:48:46 -04004571 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4572 if (err)
4573 return err;
4574
Josef Bacik9ed74f22009-09-11 16:12:44 -04004575 /*
4576 * 2 items for inode and ref
4577 * 2 items for dir items
4578 * 1 for xattr if selinux is on
4579 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004580 trans = btrfs_start_transaction(root, 5);
4581 if (IS_ERR(trans))
4582 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004583 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004584
Josef Bacikaec74772008-07-24 12:12:38 -04004585 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004586 dentry->d_name.len,
4587 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004588 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4589 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004590 if (IS_ERR(inode)) {
4591 err = PTR_ERR(inode);
4592 goto out_fail;
4593 }
Chris Mason5f39d392007-10-15 16:14:19 -04004594
Chris Mason39279cc2007-06-12 06:35:45 -04004595 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004596
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004597 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004598 if (err)
4599 goto out_fail;
4600
Chris Mason39279cc2007-06-12 06:35:45 -04004601 inode->i_op = &btrfs_dir_inode_operations;
4602 inode->i_fop = &btrfs_dir_file_operations;
4603 btrfs_set_trans_block_group(trans, inode);
4604
Chris Masondbe674a2008-07-17 12:54:05 -04004605 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004606 err = btrfs_update_inode(trans, root, inode);
4607 if (err)
4608 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004609
Chris Masone02119d2008-09-05 16:13:11 -04004610 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4611 inode, dentry->d_name.name,
4612 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004613 if (err)
4614 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004615
Chris Mason39279cc2007-06-12 06:35:45 -04004616 d_instantiate(dentry, inode);
4617 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004618 btrfs_update_inode_block_group(trans, inode);
4619 btrfs_update_inode_block_group(trans, dir);
4620
4621out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004622 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004623 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004624 if (drop_on_err)
4625 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004626 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004627 return err;
4628}
4629
Chris Masond352ac62008-09-29 15:18:18 -04004630/* helper for btfs_get_extent. Given an existing extent in the tree,
4631 * and an extent that you want to insert, deal with overlap and insert
4632 * the new extent into the tree.
4633 */
Chris Mason3b951512008-04-17 11:29:12 -04004634static int merge_extent_mapping(struct extent_map_tree *em_tree,
4635 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004636 struct extent_map *em,
4637 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004638{
4639 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004640
Chris Masone6dcd2d2008-07-17 12:53:50 -04004641 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4642 start_diff = map_start - em->start;
4643 em->start = map_start;
4644 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004645 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4646 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004647 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004648 em->block_len -= start_diff;
4649 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004650 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004651}
4652
Chris Masonc8b97812008-10-29 14:49:59 -04004653static noinline int uncompress_inline(struct btrfs_path *path,
4654 struct inode *inode, struct page *page,
4655 size_t pg_offset, u64 extent_offset,
4656 struct btrfs_file_extent_item *item)
4657{
4658 int ret;
4659 struct extent_buffer *leaf = path->nodes[0];
4660 char *tmp;
4661 size_t max_size;
4662 unsigned long inline_size;
4663 unsigned long ptr;
4664
4665 WARN_ON(pg_offset != 0);
4666 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4667 inline_size = btrfs_file_extent_inline_item_len(leaf,
4668 btrfs_item_nr(leaf, path->slots[0]));
4669 tmp = kmalloc(inline_size, GFP_NOFS);
4670 ptr = btrfs_file_extent_inline_start(item);
4671
4672 read_extent_buffer(leaf, tmp, ptr, inline_size);
4673
Chris Mason5b050f02008-11-11 09:34:41 -05004674 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004675 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4676 inline_size, max_size);
4677 if (ret) {
4678 char *kaddr = kmap_atomic(page, KM_USER0);
4679 unsigned long copy_size = min_t(u64,
4680 PAGE_CACHE_SIZE - pg_offset,
4681 max_size - extent_offset);
4682 memset(kaddr + pg_offset, 0, copy_size);
4683 kunmap_atomic(kaddr, KM_USER0);
4684 }
4685 kfree(tmp);
4686 return 0;
4687}
4688
Chris Masond352ac62008-09-29 15:18:18 -04004689/*
4690 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004691 * the ugly parts come from merging extents from the disk with the in-ram
4692 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004693 * where the in-ram extents might be locked pending data=ordered completion.
4694 *
4695 * This also copies inline extents directly into the page.
4696 */
Chris Masond3977122009-01-05 21:25:51 -05004697
Chris Masona52d9a82007-08-27 16:49:44 -04004698struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004699 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004700 int create)
4701{
4702 int ret;
4703 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004704 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004705 u64 extent_start = 0;
4706 u64 extent_end = 0;
4707 u64 objectid = inode->i_ino;
4708 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004709 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004710 struct btrfs_root *root = BTRFS_I(inode)->root;
4711 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004712 struct extent_buffer *leaf;
4713 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004714 struct extent_map *em = NULL;
4715 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004716 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004717 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004718 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004719
Chris Masona52d9a82007-08-27 16:49:44 -04004720again:
Chris Mason890871b2009-09-02 16:24:52 -04004721 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004722 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004723 if (em)
4724 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004725 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004726
Chris Masona52d9a82007-08-27 16:49:44 -04004727 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004728 if (em->start > start || em->start + em->len <= start)
4729 free_extent_map(em);
4730 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004731 free_extent_map(em);
4732 else
4733 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004734 }
Chris Masond1310b22008-01-24 16:13:08 -05004735 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004736 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004737 err = -ENOMEM;
4738 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004739 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004740 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004741 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004742 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004743 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004744 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004745
4746 if (!path) {
4747 path = btrfs_alloc_path();
4748 BUG_ON(!path);
4749 }
4750
Chris Mason179e29e2007-11-01 11:28:41 -04004751 ret = btrfs_lookup_file_extent(trans, root, path,
4752 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004753 if (ret < 0) {
4754 err = ret;
4755 goto out;
4756 }
4757
4758 if (ret != 0) {
4759 if (path->slots[0] == 0)
4760 goto not_found;
4761 path->slots[0]--;
4762 }
4763
Chris Mason5f39d392007-10-15 16:14:19 -04004764 leaf = path->nodes[0];
4765 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004766 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004767 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004768 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4769 found_type = btrfs_key_type(&found_key);
4770 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004771 found_type != BTRFS_EXTENT_DATA_KEY) {
4772 goto not_found;
4773 }
4774
Chris Mason5f39d392007-10-15 16:14:19 -04004775 found_type = btrfs_file_extent_type(leaf, item);
4776 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004777 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004778 if (found_type == BTRFS_FILE_EXTENT_REG ||
4779 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004780 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004781 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004782 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4783 size_t size;
4784 size = btrfs_file_extent_inline_len(leaf, item);
4785 extent_end = (extent_start + size + root->sectorsize - 1) &
4786 ~((u64)root->sectorsize - 1);
4787 }
4788
4789 if (start >= extent_end) {
4790 path->slots[0]++;
4791 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4792 ret = btrfs_next_leaf(root, path);
4793 if (ret < 0) {
4794 err = ret;
4795 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004796 }
Yan Zheng9036c102008-10-30 14:19:41 -04004797 if (ret > 0)
4798 goto not_found;
4799 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004800 }
Yan Zheng9036c102008-10-30 14:19:41 -04004801 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4802 if (found_key.objectid != objectid ||
4803 found_key.type != BTRFS_EXTENT_DATA_KEY)
4804 goto not_found;
4805 if (start + len <= found_key.offset)
4806 goto not_found;
4807 em->start = start;
4808 em->len = found_key.offset - start;
4809 goto not_found_em;
4810 }
4811
Yan Zhengd899e052008-10-30 14:25:28 -04004812 if (found_type == BTRFS_FILE_EXTENT_REG ||
4813 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004814 em->start = extent_start;
4815 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004816 em->orig_start = extent_start -
4817 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004818 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4819 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004820 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004821 goto insert;
4822 }
Chris Masonc8b97812008-10-29 14:49:59 -04004823 if (compressed) {
4824 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4825 em->block_start = bytenr;
4826 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4827 item);
4828 } else {
4829 bytenr += btrfs_file_extent_offset(leaf, item);
4830 em->block_start = bytenr;
4831 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004832 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4833 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004834 }
Chris Masona52d9a82007-08-27 16:49:44 -04004835 goto insert;
4836 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004837 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004838 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004839 size_t size;
4840 size_t extent_offset;
4841 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004842
Chris Masona52d9a82007-08-27 16:49:44 -04004843 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004844 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004845 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004846 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004847 goto out;
4848 }
4849
Yan Zheng9036c102008-10-30 14:19:41 -04004850 size = btrfs_file_extent_inline_len(leaf, item);
4851 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004852 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004853 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004854 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004855 em->len = (copy_size + root->sectorsize - 1) &
4856 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004857 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004858 if (compressed)
4859 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004860 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004861 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004862 if (btrfs_file_extent_compression(leaf, item) ==
4863 BTRFS_COMPRESS_ZLIB) {
4864 ret = uncompress_inline(path, inode, page,
4865 pg_offset,
4866 extent_offset, item);
4867 BUG_ON(ret);
4868 } else {
4869 map = kmap(page);
4870 read_extent_buffer(leaf, map + pg_offset, ptr,
4871 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04004872 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4873 memset(map + pg_offset + copy_size, 0,
4874 PAGE_CACHE_SIZE - pg_offset -
4875 copy_size);
4876 }
Chris Masonc8b97812008-10-29 14:49:59 -04004877 kunmap(page);
4878 }
Chris Mason179e29e2007-11-01 11:28:41 -04004879 flush_dcache_page(page);
4880 } else if (create && PageUptodate(page)) {
4881 if (!trans) {
4882 kunmap(page);
4883 free_extent_map(em);
4884 em = NULL;
4885 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004886 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004887 goto again;
4888 }
Chris Masonc8b97812008-10-29 14:49:59 -04004889 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004890 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004891 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004892 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004893 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004894 }
Chris Masond1310b22008-01-24 16:13:08 -05004895 set_extent_uptodate(io_tree, em->start,
4896 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004897 goto insert;
4898 } else {
Chris Masond3977122009-01-05 21:25:51 -05004899 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004900 WARN_ON(1);
4901 }
4902not_found:
4903 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004904 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004905not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004906 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004907 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004908insert:
4909 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004910 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004911 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4912 "[%llu %llu]\n", (unsigned long long)em->start,
4913 (unsigned long long)em->len,
4914 (unsigned long long)start,
4915 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004916 err = -EIO;
4917 goto out;
4918 }
Chris Masond1310b22008-01-24 16:13:08 -05004919
4920 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04004921 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004922 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004923 /* it is possible that someone inserted the extent into the tree
4924 * while we had the lock dropped. It is also possible that
4925 * an overlapping map exists in the tree
4926 */
Chris Masona52d9a82007-08-27 16:49:44 -04004927 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004928 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004929
4930 ret = 0;
4931
Chris Mason3b951512008-04-17 11:29:12 -04004932 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004933 if (existing && (existing->start > start ||
4934 existing->start + existing->len <= start)) {
4935 free_extent_map(existing);
4936 existing = NULL;
4937 }
Chris Mason3b951512008-04-17 11:29:12 -04004938 if (!existing) {
4939 existing = lookup_extent_mapping(em_tree, em->start,
4940 em->len);
4941 if (existing) {
4942 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004943 em, start,
4944 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004945 free_extent_map(existing);
4946 if (err) {
4947 free_extent_map(em);
4948 em = NULL;
4949 }
4950 } else {
4951 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004952 free_extent_map(em);
4953 em = NULL;
4954 }
4955 } else {
4956 free_extent_map(em);
4957 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004958 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004959 }
Chris Masona52d9a82007-08-27 16:49:44 -04004960 }
Chris Mason890871b2009-09-02 16:24:52 -04004961 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004962out:
Chris Masonf4219502008-07-22 11:18:09 -04004963 if (path)
4964 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004965 if (trans) {
4966 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004967 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004968 err = ret;
4969 }
Chris Masona52d9a82007-08-27 16:49:44 -04004970 if (err) {
4971 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04004972 return ERR_PTR(err);
4973 }
4974 return em;
4975}
4976
Chris Mason16432982008-04-10 10:23:21 -04004977static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4978 const struct iovec *iov, loff_t offset,
4979 unsigned long nr_segs)
4980{
Chris Masone1c4b742008-04-22 13:26:46 -04004981 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004982}
4983
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004984static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4985 __u64 start, __u64 len)
4986{
4987 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4988}
4989
Chris Mason9ebefb182007-06-15 13:50:00 -04004990int btrfs_readpage(struct file *file, struct page *page)
4991{
Chris Masond1310b22008-01-24 16:13:08 -05004992 struct extent_io_tree *tree;
4993 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004994 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004995}
Chris Mason1832a6d2007-12-21 16:27:21 -05004996
Chris Mason39279cc2007-06-12 06:35:45 -04004997static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4998{
Chris Masond1310b22008-01-24 16:13:08 -05004999 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04005000
5001
5002 if (current->flags & PF_MEMALLOC) {
5003 redirty_page_for_writepage(wbc, page);
5004 unlock_page(page);
5005 return 0;
5006 }
Chris Masond1310b22008-01-24 16:13:08 -05005007 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005008 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
5009}
Chris Mason39279cc2007-06-12 06:35:45 -04005010
Chris Masonf4219502008-07-22 11:18:09 -04005011int btrfs_writepages(struct address_space *mapping,
5012 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04005013{
Chris Masond1310b22008-01-24 16:13:08 -05005014 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05005015
Chris Masond1310b22008-01-24 16:13:08 -05005016 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04005017 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
5018}
5019
Chris Mason3ab2fb52007-11-08 10:59:22 -05005020static int
5021btrfs_readpages(struct file *file, struct address_space *mapping,
5022 struct list_head *pages, unsigned nr_pages)
5023{
Chris Masond1310b22008-01-24 16:13:08 -05005024 struct extent_io_tree *tree;
5025 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05005026 return extent_readpages(tree, mapping, pages, nr_pages,
5027 btrfs_get_extent);
5028}
Chris Masone6dcd2d2008-07-17 12:53:50 -04005029static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04005030{
Chris Masond1310b22008-01-24 16:13:08 -05005031 struct extent_io_tree *tree;
5032 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04005033 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005034
Chris Masond1310b22008-01-24 16:13:08 -05005035 tree = &BTRFS_I(page->mapping->host)->io_tree;
5036 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05005037 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005038 if (ret == 1) {
5039 ClearPagePrivate(page);
5040 set_page_private(page, 0);
5041 page_cache_release(page);
5042 }
5043 return ret;
5044}
Chris Mason39279cc2007-06-12 06:35:45 -04005045
Chris Masone6dcd2d2008-07-17 12:53:50 -04005046static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
5047{
Chris Mason98509cf2008-09-11 15:51:43 -04005048 if (PageWriteback(page) || PageDirty(page))
5049 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05005050 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005051}
5052
Chris Masona52d9a82007-08-27 16:49:44 -04005053static void btrfs_invalidatepage(struct page *page, unsigned long offset)
5054{
Chris Masond1310b22008-01-24 16:13:08 -05005055 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005056 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005057 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005058 u64 page_start = page_offset(page);
5059 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005060
Chris Mason8b62b722009-09-02 16:53:46 -04005061
5062 /*
5063 * we have the page locked, so new writeback can't start,
5064 * and the dirty bit won't be cleared while we are here.
5065 *
5066 * Wait for IO on this page so that we can safely clear
5067 * the PagePrivate2 bit and do ordered accounting
5068 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005069 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04005070
Chris Masond1310b22008-01-24 16:13:08 -05005071 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005072 if (offset) {
5073 btrfs_releasepage(page, GFP_NOFS);
5074 return;
5075 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00005076 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
5077 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005078 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
5079 page_offset(page));
5080 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04005081 /*
5082 * IO on this page will never be started, so we need
5083 * to account for any ordered extents now
5084 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005085 clear_extent_bit(tree, page_start, page_end,
5086 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04005087 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00005088 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04005089 /*
5090 * whoever cleared the private bit is responsible
5091 * for the finish_ordered_io
5092 */
5093 if (TestClearPagePrivate2(page)) {
5094 btrfs_finish_ordered_io(page->mapping->host,
5095 page_start, page_end);
5096 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005097 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005098 cached_state = NULL;
5099 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
5100 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005101 }
5102 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005103 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00005104 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005105 __btrfs_releasepage(page, GFP_NOFS);
5106
Chris Mason4a096752008-07-21 10:29:44 -04005107 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005108 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04005109 ClearPagePrivate(page);
5110 set_page_private(page, 0);
5111 page_cache_release(page);
5112 }
Chris Mason39279cc2007-06-12 06:35:45 -04005113}
5114
Chris Mason9ebefb182007-06-15 13:50:00 -04005115/*
5116 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
5117 * called from a page fault handler when a page is first dirtied. Hence we must
5118 * be careful to check for EOF conditions here. We set the page up correctly
5119 * for a written page which means we get ENOSPC checking when writing into
5120 * holes and correct delalloc and unwritten extent mapping on filesystems that
5121 * support these features.
5122 *
5123 * We are not allowed to take the i_mutex here so we have to play games to
5124 * protect against truncate races as the page could now be beyond EOF. Because
5125 * vmtruncate() writes the inode size before removing pages, once we have the
5126 * page lock we can determine safely if the page is beyond EOF. If it is not
5127 * beyond EOF, then the page is guaranteed safe against truncation until we
5128 * unlock the page.
5129 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07005130int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04005131{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005132 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05005133 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05005134 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005135 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5136 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005137 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005138 char *kaddr;
5139 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04005140 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05005141 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04005142 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005143 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04005144
Josef Bacik6a632092009-02-20 11:00:09 -05005145 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07005146 if (ret) {
5147 if (ret == -ENOMEM)
5148 ret = VM_FAULT_OOM;
5149 else /* -ENOSPC, -EIO, etc */
5150 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05005151 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07005152 }
Chris Mason1832a6d2007-12-21 16:27:21 -05005153
Josef Bacik9ed74f22009-09-11 16:12:44 -04005154 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
5155 if (ret) {
5156 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5157 ret = VM_FAULT_SIGBUS;
5158 goto out;
5159 }
5160
Nick Piggin56a76f82009-03-31 15:23:23 -07005161 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005162again:
Chris Mason9ebefb182007-06-15 13:50:00 -04005163 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04005164 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005165 page_start = page_offset(page);
5166 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005167
Chris Mason9ebefb182007-06-15 13:50:00 -04005168 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04005169 (page_start >= size)) {
Josef Bacik6a632092009-02-20 11:00:09 -05005170 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Chris Mason9ebefb182007-06-15 13:50:00 -04005171 /* page got truncated out from underneath us */
5172 goto out_unlock;
5173 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005174 wait_on_page_writeback(page);
5175
Josef Bacik2ac55d42010-02-03 19:33:23 +00005176 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
5177 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005178 set_page_extent_mapped(page);
5179
Chris Masoneb84ae02008-07-17 13:53:27 -04005180 /*
5181 * we can't set the delalloc bits if there are pending ordered
5182 * extents. Drop our locks and wait for them to finish
5183 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005184 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5185 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00005186 unlock_extent_cached(io_tree, page_start, page_end,
5187 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005188 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005189 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005190 btrfs_put_ordered_extent(ordered);
5191 goto again;
5192 }
5193
Josef Bacikfbf19082009-10-01 17:10:23 -04005194 /*
5195 * XXX - page_mkwrite gets called every time the page is dirtied, even
5196 * if it was already dirty, so for space accounting reasons we need to
5197 * clear any delalloc bits for the range we are fixing to save. There
5198 * is probably a better way to do this, but for now keep consistent with
5199 * prepare_pages in the normal write path.
5200 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00005201 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005202 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00005203 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04005204
Josef Bacik2ac55d42010-02-03 19:33:23 +00005205 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
5206 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005207 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00005208 unlock_extent_cached(io_tree, page_start, page_end,
5209 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005210 ret = VM_FAULT_SIGBUS;
Josef Bacikfbf19082009-10-01 17:10:23 -04005211 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005212 goto out_unlock;
5213 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005214 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04005215
5216 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04005217 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005218 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04005219 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04005220 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04005221
Chris Masone6dcd2d2008-07-17 12:53:50 -04005222 if (zero_start != PAGE_CACHE_SIZE) {
5223 kaddr = kmap(page);
5224 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5225 flush_dcache_page(page);
5226 kunmap(page);
5227 }
Chris Mason247e7432008-07-17 12:53:51 -04005228 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005229 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04005230 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005231
Chris Mason257c62e2009-10-13 13:21:08 -04005232 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5233 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5234
Josef Bacik2ac55d42010-02-03 19:33:23 +00005235 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04005236
5237out_unlock:
Josef Bacik9ed74f22009-09-11 16:12:44 -04005238 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Chris Mason50a9b212009-09-11 12:33:12 -04005239 if (!ret)
5240 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04005241 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05005242out:
Chris Mason9ebefb182007-06-15 13:50:00 -04005243 return ret;
5244}
5245
Chris Mason39279cc2007-06-12 06:35:45 -04005246static void btrfs_truncate(struct inode *inode)
5247{
5248 struct btrfs_root *root = BTRFS_I(inode)->root;
5249 int ret;
5250 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005251 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04005252 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005253
Yan, Zheng80825102009-11-12 09:35:36 +00005254 if (!S_ISREG(inode->i_mode)) {
5255 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04005256 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005257 }
Chris Mason39279cc2007-06-12 06:35:45 -04005258
Josef Bacik5d5e1032009-10-13 16:46:49 -04005259 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5260 if (ret)
5261 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005262
Chris Mason4a096752008-07-21 10:29:44 -04005263 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00005264 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005265
Chris Mason39279cc2007-06-12 06:35:45 -04005266 trans = btrfs_start_transaction(root, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00005267 btrfs_set_trans_block_group(trans, inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005268
5269 /*
5270 * setattr is responsible for setting the ordered_data_close flag,
5271 * but that is only tested during the last file release. That
5272 * could happen well after the next commit, leaving a great big
5273 * window where new writes may get lost if someone chooses to write
5274 * to this file after truncating to zero
5275 *
5276 * The inode doesn't have any dirty data here, and so if we commit
5277 * this is a noop. If someone immediately starts writing to the inode
5278 * it is very likely we'll catch some of their writes in this
5279 * transaction, and the commit will find this file on the ordered
5280 * data list with good things to send down.
5281 *
5282 * This is a best effort solution, there is still a window where
5283 * using truncate to replace the contents of the file will
5284 * end up with a zero length file after a crash.
5285 */
5286 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5287 btrfs_add_ordered_operation(trans, root, inode);
5288
Yan, Zheng80825102009-11-12 09:35:36 +00005289 while (1) {
5290 ret = btrfs_truncate_inode_items(trans, root, inode,
5291 inode->i_size,
5292 BTRFS_EXTENT_DATA_KEY);
5293 if (ret != -EAGAIN)
5294 break;
Chris Mason39279cc2007-06-12 06:35:45 -04005295
Yan, Zheng80825102009-11-12 09:35:36 +00005296 ret = btrfs_update_inode(trans, root, inode);
5297 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005298
Yan, Zheng80825102009-11-12 09:35:36 +00005299 nr = trans->blocks_used;
5300 btrfs_end_transaction(trans, root);
5301 btrfs_btree_balance_dirty(root, nr);
5302
5303 trans = btrfs_start_transaction(root, 1);
5304 btrfs_set_trans_block_group(trans, inode);
5305 }
5306
5307 if (ret == 0 && inode->i_nlink > 0) {
5308 ret = btrfs_orphan_del(trans, inode);
5309 BUG_ON(ret);
5310 }
5311
5312 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04005313 BUG_ON(ret);
5314
Josef Bacik7b128762008-07-24 12:17:14 -04005315 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04005316 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005317 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005318 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005319}
5320
Sven Wegener3b963622008-06-09 21:57:42 -04005321/*
Chris Masond352ac62008-09-29 15:18:18 -04005322 * create a new subvolume directory/inode (helper for the ioctl).
5323 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05005324int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04005325 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005326 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04005327{
Chris Mason39279cc2007-06-12 06:35:45 -04005328 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005329 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005330 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005331
Josef Bacikaec74772008-07-24 12:12:38 -04005332 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005333 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04005334 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005335 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005336 inode->i_op = &btrfs_dir_inode_operations;
5337 inode->i_fop = &btrfs_dir_file_operations;
5338
Chris Mason39279cc2007-06-12 06:35:45 -04005339 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04005340 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04005341
Yan, Zheng76dda932009-09-21 16:00:26 -04005342 err = btrfs_update_inode(trans, new_root, inode);
5343 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005344
Yan, Zheng76dda932009-09-21 16:00:26 -04005345 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005346 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005347}
5348
Chris Masond352ac62008-09-29 15:18:18 -04005349/* helper function for file defrag and space balancing. This
5350 * forces readahead on a given range of bytes in an inode
5351 */
Chris Masonedbd8d42007-12-21 16:27:24 -05005352unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04005353 struct file_ra_state *ra, struct file *file,
5354 pgoff_t offset, pgoff_t last_index)
5355{
Chris Mason8e7bf942008-04-28 09:02:36 -04005356 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04005357
Chris Mason86479a02007-09-10 19:58:16 -04005358 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5359 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04005360}
5361
Chris Mason39279cc2007-06-12 06:35:45 -04005362struct inode *btrfs_alloc_inode(struct super_block *sb)
5363{
5364 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005365 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005366
5367 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5368 if (!ei)
5369 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005370
5371 ei->root = NULL;
5372 ei->space_info = NULL;
5373 ei->generation = 0;
5374 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04005375 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04005376 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005377 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005378 ei->delalloc_bytes = 0;
5379 ei->reserved_bytes = 0;
5380 ei->disk_i_size = 0;
5381 ei->flags = 0;
5382 ei->index_cnt = (u64)-1;
5383 ei->last_unlink_trans = 0;
5384
5385 spin_lock_init(&ei->accounting_lock);
Josef Bacik32c00af2009-10-08 13:34:05 -04005386 ei->outstanding_extents = 0;
5387 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005388
5389 ei->ordered_data_close = 0;
5390 ei->dummy_inode = 0;
5391 ei->force_compress = 0;
5392
5393 inode = &ei->vfs_inode;
5394 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
5395 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
5396 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
5397 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005398 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04005399 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005400 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005401 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005402 RB_CLEAR_NODE(&ei->rb_node);
5403
5404 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005405}
5406
5407void btrfs_destroy_inode(struct inode *inode)
5408{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005409 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005410 struct btrfs_root *root = BTRFS_I(inode)->root;
5411
Chris Mason39279cc2007-06-12 06:35:45 -04005412 WARN_ON(!list_empty(&inode->i_dentry));
5413 WARN_ON(inode->i_data.nrpages);
5414
Chris Mason5a3f23d2009-03-31 13:27:11 -04005415 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05005416 * This can happen where we create an inode, but somebody else also
5417 * created the same inode and we need to destroy the one we already
5418 * created.
5419 */
5420 if (!root)
5421 goto free;
5422
5423 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005424 * Make sure we're properly removed from the ordered operation
5425 * lists.
5426 */
5427 smp_mb();
5428 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5429 spin_lock(&root->fs_info->ordered_extent_lock);
5430 list_del_init(&BTRFS_I(inode)->ordered_operations);
5431 spin_unlock(&root->fs_info->ordered_extent_lock);
5432 }
5433
5434 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005435 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00005436 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
5437 inode->i_ino);
5438 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04005439 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04005440 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005441
Chris Masond3977122009-01-05 21:25:51 -05005442 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005443 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5444 if (!ordered)
5445 break;
5446 else {
Chris Masond3977122009-01-05 21:25:51 -05005447 printk(KERN_ERR "btrfs found ordered "
5448 "extent %llu %llu on inode cleanup\n",
5449 (unsigned long long)ordered->file_offset,
5450 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005451 btrfs_remove_ordered_extent(inode, ordered);
5452 btrfs_put_ordered_extent(ordered);
5453 btrfs_put_ordered_extent(ordered);
5454 }
5455 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005456 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005457 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05005458free:
Chris Mason39279cc2007-06-12 06:35:45 -04005459 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5460}
5461
Yan, Zheng76dda932009-09-21 16:00:26 -04005462void btrfs_drop_inode(struct inode *inode)
5463{
5464 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005465 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5466 generic_delete_inode(inode);
5467 else
5468 generic_drop_inode(inode);
5469}
5470
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005471static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04005472{
5473 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5474
5475 inode_init_once(&ei->vfs_inode);
5476}
5477
5478void btrfs_destroy_cachep(void)
5479{
5480 if (btrfs_inode_cachep)
5481 kmem_cache_destroy(btrfs_inode_cachep);
5482 if (btrfs_trans_handle_cachep)
5483 kmem_cache_destroy(btrfs_trans_handle_cachep);
5484 if (btrfs_transaction_cachep)
5485 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04005486 if (btrfs_path_cachep)
5487 kmem_cache_destroy(btrfs_path_cachep);
5488}
5489
5490int btrfs_init_cachep(void)
5491{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005492 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5493 sizeof(struct btrfs_inode), 0,
5494 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04005495 if (!btrfs_inode_cachep)
5496 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005497
5498 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5499 sizeof(struct btrfs_trans_handle), 0,
5500 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005501 if (!btrfs_trans_handle_cachep)
5502 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005503
5504 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5505 sizeof(struct btrfs_transaction), 0,
5506 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005507 if (!btrfs_transaction_cachep)
5508 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005509
5510 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5511 sizeof(struct btrfs_path), 0,
5512 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005513 if (!btrfs_path_cachep)
5514 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005515
Chris Mason39279cc2007-06-12 06:35:45 -04005516 return 0;
5517fail:
5518 btrfs_destroy_cachep();
5519 return -ENOMEM;
5520}
5521
5522static int btrfs_getattr(struct vfsmount *mnt,
5523 struct dentry *dentry, struct kstat *stat)
5524{
5525 struct inode *inode = dentry->d_inode;
5526 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05005527 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05005528 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005529 stat->blocks = (inode_get_bytes(inode) +
5530 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04005531 return 0;
5532}
5533
Chris Masond3977122009-01-05 21:25:51 -05005534static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5535 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005536{
5537 struct btrfs_trans_handle *trans;
5538 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005539 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005540 struct inode *new_inode = new_dentry->d_inode;
5541 struct inode *old_inode = old_dentry->d_inode;
5542 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005543 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005544 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005545 int ret;
5546
Yan, Zhengf679a842009-09-24 09:17:31 -04005547 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5548 return -EPERM;
5549
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005550 /* we only allow rename subvolume link between subvolumes */
5551 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05005552 return -EXDEV;
5553
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005554 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5555 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5556 return -ENOTEMPTY;
5557
Chris Mason39279cc2007-06-12 06:35:45 -04005558 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005559 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04005560 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005561 /*
5562 * we're using rename to replace one file with another.
5563 * and the replacement file is large. Start IO on it now so
5564 * we don't add too much work to the end of the transaction
5565 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04005566 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04005567 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5568 filemap_flush(old_inode->i_mapping);
5569
Yan, Zheng76dda932009-09-21 16:00:26 -04005570 /* close the racy window with snapshot create/destroy ioctl */
5571 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5572 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005573 /*
5574 * We want to reserve the absolute worst case amount of items. So if
5575 * both inodes are subvols and we need to unlink them then that would
5576 * require 4 item modifications, but if they are both normal inodes it
5577 * would require 5 item modifications, so we'll assume their normal
5578 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5579 * should cover the worst case number of items we'll modify.
5580 */
5581 trans = btrfs_start_transaction(root, 20);
5582 if (IS_ERR(trans))
5583 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04005584
Yan, Zhenga5719522009-09-24 09:17:31 -04005585 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04005586
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005587 if (dest != root)
5588 btrfs_record_root_in_trans(trans, dest);
5589
Yan, Zhenga5719522009-09-24 09:17:31 -04005590 ret = btrfs_set_inode_index(new_dir, &index);
5591 if (ret)
5592 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005593
Yan, Zhenga5719522009-09-24 09:17:31 -04005594 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005595 /* force full log commit if subvolume involved. */
5596 root->fs_info->last_trans_log_full_commit = trans->transid;
5597 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04005598 ret = btrfs_insert_inode_ref(trans, dest,
5599 new_dentry->d_name.name,
5600 new_dentry->d_name.len,
5601 old_inode->i_ino,
5602 new_dir->i_ino, index);
5603 if (ret)
5604 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005605 /*
5606 * this is an ugly little race, but the rename is required
5607 * to make sure that if we crash, the inode is either at the
5608 * old name or the new one. pinning the log transaction lets
5609 * us make sure we don't allow a log commit to come in after
5610 * we unlink the name but before we add the new name back in.
5611 */
5612 btrfs_pin_log_trans(root);
5613 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005614 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005615 * make sure the inode gets flushed if it is replacing
5616 * something.
5617 */
5618 if (new_inode && new_inode->i_size &&
5619 old_inode && S_ISREG(old_inode->i_mode)) {
5620 btrfs_add_ordered_operation(trans, root, old_inode);
5621 }
5622
Chris Mason39279cc2007-06-12 06:35:45 -04005623 old_dir->i_ctime = old_dir->i_mtime = ctime;
5624 new_dir->i_ctime = new_dir->i_mtime = ctime;
5625 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04005626
Chris Mason12fcfd22009-03-24 10:24:20 -04005627 if (old_dentry->d_parent != new_dentry->d_parent)
5628 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5629
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005630 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5631 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5632 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5633 old_dentry->d_name.name,
5634 old_dentry->d_name.len);
5635 } else {
5636 btrfs_inc_nlink(old_dentry->d_inode);
5637 ret = btrfs_unlink_inode(trans, root, old_dir,
5638 old_dentry->d_inode,
5639 old_dentry->d_name.name,
5640 old_dentry->d_name.len);
5641 }
5642 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005643
5644 if (new_inode) {
5645 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005646 if (unlikely(new_inode->i_ino ==
5647 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5648 root_objectid = BTRFS_I(new_inode)->location.objectid;
5649 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5650 root_objectid,
5651 new_dentry->d_name.name,
5652 new_dentry->d_name.len);
5653 BUG_ON(new_inode->i_nlink == 0);
5654 } else {
5655 ret = btrfs_unlink_inode(trans, dest, new_dir,
5656 new_dentry->d_inode,
5657 new_dentry->d_name.name,
5658 new_dentry->d_name.len);
5659 }
5660 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005661 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04005662 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005663 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005664 }
Chris Mason39279cc2007-06-12 06:35:45 -04005665 }
Josef Bacikaec74772008-07-24 12:12:38 -04005666
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005667 ret = btrfs_add_link(trans, new_dir, old_inode,
5668 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04005669 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005670 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005671
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005672 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5673 btrfs_log_new_name(trans, old_inode, old_dir,
5674 new_dentry->d_parent);
5675 btrfs_end_log_trans(root);
5676 }
Chris Mason39279cc2007-06-12 06:35:45 -04005677out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04005678 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005679
Yan, Zheng76dda932009-09-21 16:00:26 -04005680 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5681 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005682
Chris Mason39279cc2007-06-12 06:35:45 -04005683 return ret;
5684}
5685
Chris Masond352ac62008-09-29 15:18:18 -04005686/*
5687 * some fairly slow code that needs optimization. This walks the list
5688 * of all the inodes with pending delalloc and forces them to disk.
5689 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005690int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04005691{
5692 struct list_head *head = &root->fs_info->delalloc_inodes;
5693 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005694 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04005695
Yan Zhengc146afa2008-11-12 14:34:12 -05005696 if (root->fs_info->sb->s_flags & MS_RDONLY)
5697 return -EROFS;
5698
Chris Mason75eff682008-12-15 15:54:40 -05005699 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05005700 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04005701 binode = list_entry(head->next, struct btrfs_inode,
5702 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005703 inode = igrab(&binode->vfs_inode);
5704 if (!inode)
5705 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05005706 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005707 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005708 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005709 if (delay_iput)
5710 btrfs_add_delayed_iput(inode);
5711 else
5712 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005713 }
5714 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05005715 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04005716 }
Chris Mason75eff682008-12-15 15:54:40 -05005717 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04005718
5719 /* the filemap_flush will queue IO into the worker threads, but
5720 * we have to make sure the IO is actually started and that
5721 * ordered extents get created before we return
5722 */
5723 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05005724 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05005725 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005726 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05005727 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5728 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04005729 }
5730 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04005731 return 0;
5732}
5733
Yan, Zheng5da9d012010-05-16 10:46:25 -04005734int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput)
5735{
5736 struct btrfs_inode *binode;
5737 struct inode *inode = NULL;
5738
5739 spin_lock(&root->fs_info->delalloc_lock);
5740 while (!list_empty(&root->fs_info->delalloc_inodes)) {
5741 binode = list_entry(root->fs_info->delalloc_inodes.next,
5742 struct btrfs_inode, delalloc_inodes);
5743 inode = igrab(&binode->vfs_inode);
5744 if (inode) {
5745 list_move_tail(&binode->delalloc_inodes,
5746 &root->fs_info->delalloc_inodes);
5747 break;
5748 }
5749
5750 list_del_init(&binode->delalloc_inodes);
5751 cond_resched_lock(&root->fs_info->delalloc_lock);
5752 }
5753 spin_unlock(&root->fs_info->delalloc_lock);
5754
5755 if (inode) {
5756 write_inode_now(inode, 0);
5757 if (delay_iput)
5758 btrfs_add_delayed_iput(inode);
5759 else
5760 iput(inode);
5761 return 1;
5762 }
5763 return 0;
5764}
5765
Chris Mason39279cc2007-06-12 06:35:45 -04005766static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5767 const char *symname)
5768{
5769 struct btrfs_trans_handle *trans;
5770 struct btrfs_root *root = BTRFS_I(dir)->root;
5771 struct btrfs_path *path;
5772 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05005773 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005774 int err;
5775 int drop_inode = 0;
5776 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005777 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04005778 int name_len;
5779 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04005780 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005781 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04005782 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05005783 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005784
5785 name_len = strlen(symname) + 1;
5786 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5787 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05005788
Yan, Zhenga22285a2010-05-16 10:48:46 -04005789 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
5790 if (err)
5791 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005792 /*
5793 * 2 items for inode item and ref
5794 * 2 items for dir items
5795 * 1 item for xattr if selinux is on
5796 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005797 trans = btrfs_start_transaction(root, 5);
5798 if (IS_ERR(trans))
5799 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005800
Chris Mason39279cc2007-06-12 06:35:45 -04005801 btrfs_set_trans_block_group(trans, dir);
5802
Josef Bacikaec74772008-07-24 12:12:38 -04005803 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05005804 dentry->d_name.len,
5805 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04005806 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5807 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005808 err = PTR_ERR(inode);
5809 if (IS_ERR(inode))
5810 goto out_unlock;
5811
Yan, Zhengf34f57a2009-11-12 09:35:27 +00005812 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04005813 if (err) {
5814 drop_inode = 1;
5815 goto out_unlock;
5816 }
5817
Chris Mason39279cc2007-06-12 06:35:45 -04005818 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005819 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005820 if (err)
5821 drop_inode = 1;
5822 else {
5823 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005824 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04005825 inode->i_fop = &btrfs_file_operations;
5826 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05005827 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04005828 }
Chris Mason39279cc2007-06-12 06:35:45 -04005829 btrfs_update_inode_block_group(trans, inode);
5830 btrfs_update_inode_block_group(trans, dir);
5831 if (drop_inode)
5832 goto out_unlock;
5833
5834 path = btrfs_alloc_path();
5835 BUG_ON(!path);
5836 key.objectid = inode->i_ino;
5837 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005838 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5839 datasize = btrfs_file_extent_calc_inline_size(name_len);
5840 err = btrfs_insert_empty_item(trans, root, path, &key,
5841 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005842 if (err) {
5843 drop_inode = 1;
5844 goto out_unlock;
5845 }
Chris Mason5f39d392007-10-15 16:14:19 -04005846 leaf = path->nodes[0];
5847 ei = btrfs_item_ptr(leaf, path->slots[0],
5848 struct btrfs_file_extent_item);
5849 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5850 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04005851 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04005852 btrfs_set_file_extent_encryption(leaf, ei, 0);
5853 btrfs_set_file_extent_compression(leaf, ei, 0);
5854 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5855 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5856
Chris Mason39279cc2007-06-12 06:35:45 -04005857 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04005858 write_extent_buffer(leaf, symname, ptr, name_len);
5859 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04005860 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04005861
Chris Mason39279cc2007-06-12 06:35:45 -04005862 inode->i_op = &btrfs_symlink_inode_operations;
5863 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04005864 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04005865 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04005866 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005867 err = btrfs_update_inode(trans, root, inode);
5868 if (err)
5869 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005870
5871out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005872 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04005873 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005874 if (drop_inode) {
5875 inode_dec_link_count(inode);
5876 iput(inode);
5877 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005878 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005879 return err;
5880}
Chris Mason16432982008-04-10 10:23:21 -04005881
Yan, Zheng5a303d52009-11-12 09:34:52 +00005882static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005883 u64 alloc_hint, int mode, loff_t actual_len)
Yan Zhengd899e052008-10-30 14:25:28 -04005884{
Yan, Zheng5a303d52009-11-12 09:34:52 +00005885 struct btrfs_trans_handle *trans;
Yan Zhengd899e052008-10-30 14:25:28 -04005886 struct btrfs_root *root = BTRFS_I(inode)->root;
5887 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04005888 u64 cur_offset = start;
5889 u64 num_bytes = end - start;
5890 int ret = 0;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005891 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04005892
Yan Zhengd899e052008-10-30 14:25:28 -04005893 while (num_bytes > 0) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04005894 trans = btrfs_start_transaction(root, 3);
5895 if (IS_ERR(trans)) {
5896 ret = PTR_ERR(trans);
5897 break;
5898 }
Chris Mason3a1abec2009-12-17 15:47:17 -05005899
Josef Bacik287a0ab2010-03-19 18:07:23 +00005900 ret = btrfs_reserve_extent(trans, root, num_bytes,
Yan Zhengd899e052008-10-30 14:25:28 -04005901 root->sectorsize, 0, alloc_hint,
5902 (u64)-1, &ins, 1);
5903 if (ret) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04005904 btrfs_end_transaction(trans, root);
5905 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005906 }
5907
Yan Zhengd899e052008-10-30 14:25:28 -04005908 ret = insert_reserved_file_extent(trans, inode,
5909 cur_offset, ins.objectid,
5910 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00005911 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04005912 BTRFS_FILE_EXTENT_PREALLOC);
5913 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04005914 btrfs_drop_extent_cache(inode, cur_offset,
5915 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005916
Yan Zhengd899e052008-10-30 14:25:28 -04005917 num_bytes -= ins.offset;
5918 cur_offset += ins.offset;
5919 alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005920
Yan Zhengd899e052008-10-30 14:25:28 -04005921 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005922 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04005923 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Aneesh Kumar K.V23b5c502010-02-04 11:33:03 -05005924 (actual_len > inode->i_size) &&
5925 (cur_offset > inode->i_size)) {
5926
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005927 if (cur_offset > actual_len)
5928 i_size = actual_len;
5929 else
5930 i_size = cur_offset;
5931 i_size_write(inode, i_size);
5932 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005933 }
5934
Yan Zhengd899e052008-10-30 14:25:28 -04005935 ret = btrfs_update_inode(trans, root, inode);
5936 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04005937
Yan, Zheng5a303d52009-11-12 09:34:52 +00005938 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005939 }
Yan Zhengd899e052008-10-30 14:25:28 -04005940 return ret;
5941}
5942
5943static long btrfs_fallocate(struct inode *inode, int mode,
5944 loff_t offset, loff_t len)
5945{
Josef Bacik2ac55d42010-02-03 19:33:23 +00005946 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04005947 u64 cur_offset;
5948 u64 last_byte;
5949 u64 alloc_start;
5950 u64 alloc_end;
5951 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04005952 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04005953 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5954 struct extent_map *em;
5955 int ret;
5956
5957 alloc_start = offset & ~mask;
5958 alloc_end = (offset + len + mask) & ~mask;
5959
Chris Mason546888d2009-04-21 11:53:38 -04005960 /*
5961 * wait for ordered IO before we have any locks. We'll loop again
5962 * below with the locks held.
5963 */
5964 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5965
Yan Zhengd899e052008-10-30 14:25:28 -04005966 mutex_lock(&inode->i_mutex);
5967 if (alloc_start > inode->i_size) {
5968 ret = btrfs_cont_expand(inode, alloc_start);
5969 if (ret)
5970 goto out;
5971 }
5972
Yan, Zheng5a303d52009-11-12 09:34:52 +00005973 ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode,
Josef Bacika970b0a2009-06-27 21:07:34 -04005974 alloc_end - alloc_start);
5975 if (ret)
5976 goto out;
5977
Chris Masone980b502009-04-24 14:39:24 -04005978 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04005979 while (1) {
5980 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04005981
Chris Mason546888d2009-04-21 11:53:38 -04005982 /* the extent lock is ordered inside the running
5983 * transaction
5984 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00005985 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
5986 locked_end, 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04005987 ordered = btrfs_lookup_first_ordered_extent(inode,
5988 alloc_end - 1);
5989 if (ordered &&
5990 ordered->file_offset + ordered->len > alloc_start &&
5991 ordered->file_offset < alloc_end) {
5992 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005993 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
5994 alloc_start, locked_end,
5995 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005996 /*
5997 * we can't wait on the range with the transaction
5998 * running or with the extent lock held
5999 */
Yan Zhengd899e052008-10-30 14:25:28 -04006000 btrfs_wait_ordered_range(inode, alloc_start,
6001 alloc_end - alloc_start);
6002 } else {
6003 if (ordered)
6004 btrfs_put_ordered_extent(ordered);
6005 break;
6006 }
6007 }
6008
6009 cur_offset = alloc_start;
6010 while (1) {
6011 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
6012 alloc_end - cur_offset, 0);
6013 BUG_ON(IS_ERR(em) || !em);
6014 last_byte = min(extent_map_end(em), alloc_end);
6015 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00006016 if (em->block_start == EXTENT_MAP_HOLE ||
6017 (cur_offset >= inode->i_size &&
6018 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6019 ret = prealloc_file_range(inode,
6020 cur_offset, last_byte,
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00006021 alloc_hint, mode, offset+len);
Yan Zhengd899e052008-10-30 14:25:28 -04006022 if (ret < 0) {
6023 free_extent_map(em);
6024 break;
6025 }
6026 }
6027 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
6028 alloc_hint = em->block_start;
6029 free_extent_map(em);
6030
6031 cur_offset = last_byte;
6032 if (cur_offset >= alloc_end) {
6033 ret = 0;
6034 break;
6035 }
6036 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006037 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
6038 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04006039
Yan, Zheng5a303d52009-11-12 09:34:52 +00006040 btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode,
6041 alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04006042out:
6043 mutex_unlock(&inode->i_mutex);
6044 return ret;
6045}
6046
Chris Masone6dcd2d2008-07-17 12:53:50 -04006047static int btrfs_set_page_dirty(struct page *page)
6048{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006049 return __set_page_dirty_nobuffers(page);
6050}
6051
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006052static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05006053{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006054 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05006055 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04006056 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05006057}
Chris Mason39279cc2007-06-12 06:35:45 -04006058
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006059static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05006060 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04006061 .lookup = btrfs_lookup,
6062 .create = btrfs_create,
6063 .unlink = btrfs_unlink,
6064 .link = btrfs_link,
6065 .mkdir = btrfs_mkdir,
6066 .rmdir = btrfs_rmdir,
6067 .rename = btrfs_rename,
6068 .symlink = btrfs_symlink,
6069 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04006070 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006071 .setxattr = btrfs_setxattr,
6072 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05006073 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006074 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05006075 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04006076};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006077static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006078 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05006079 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04006080};
Yan, Zheng76dda932009-09-21 16:00:26 -04006081
Alexey Dobriyan828c0952009-10-01 15:43:56 -07006082static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006083 .llseek = generic_file_llseek,
6084 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01006085 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04006086 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04006087#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04006088 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04006089#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04006090 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04006091 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04006092};
6093
Chris Masond1310b22008-01-24 16:13:08 -05006094static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04006095 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05006096 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04006097 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04006098 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006099 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04006100 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04006101 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05006102 .set_bit_hook = btrfs_set_bit_hook,
6103 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006104 .merge_extent_hook = btrfs_merge_extent_hook,
6105 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04006106};
6107
Chris Mason35054392009-01-21 13:11:13 -05006108/*
6109 * btrfs doesn't support the bmap operation because swapfiles
6110 * use bmap to make a mapping of extents in the file. They assume
6111 * these extents won't change over the life of the file and they
6112 * use the bmap result to do IO directly to the drive.
6113 *
6114 * the btrfs bmap call would return logical addresses that aren't
6115 * suitable for IO and they also will change frequently as COW
6116 * operations happen. So, swapfile + btrfs == corruption.
6117 *
6118 * For now we're avoiding this by dropping bmap.
6119 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006120static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006121 .readpage = btrfs_readpage,
6122 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04006123 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05006124 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04006125 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04006126 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04006127 .invalidatepage = btrfs_invalidatepage,
6128 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006129 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02006130 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04006131};
6132
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006133static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006134 .readpage = btrfs_readpage,
6135 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04006136 .invalidatepage = btrfs_invalidatepage,
6137 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04006138};
6139
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006140static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006141 .truncate = btrfs_truncate,
6142 .getattr = btrfs_getattr,
6143 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006144 .setxattr = btrfs_setxattr,
6145 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05006146 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006147 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05006148 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04006149 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006150 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04006151};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006152static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04006153 .getattr = btrfs_getattr,
6154 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05006155 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006156 .setxattr = btrfs_setxattr,
6157 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04006158 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006159 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04006160};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006161static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006162 .readlink = generic_readlink,
6163 .follow_link = page_follow_link_light,
6164 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05006165 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05006166 .setxattr = btrfs_setxattr,
6167 .getxattr = btrfs_getxattr,
6168 .listxattr = btrfs_listxattr,
6169 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04006170};
Yan, Zheng76dda932009-09-21 16:00:26 -04006171
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04006172const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04006173 .d_delete = btrfs_dentry_delete,
6174};